UnityShader使用圖像疊加實現(xiàn)運動模糊
本文實例為大家分享了UnityShader實現(xiàn)運動模糊的具體代碼,供大家參考,具體內(nèi)容如下
1.此代碼掛在攝像機(jī)上,使攝像機(jī)運動起來
using UnityEngine;
using System.Collections;
public class Translating : MonoBehaviour {
public float speed = 10.0f;
public Vector3 startPoint = Vector3.zero;
public Vector3 endPoint = Vector3.zero;
public Vector3 lookAt = Vector3.zero;
public bool pingpong = true;
private Vector3 curEndPoint = Vector3.zero;
// Use this for initialization
void Start () {
transform.position = startPoint;
curEndPoint = endPoint;
}
// Update is called once per frame
void Update () {
transform.position = Vector3.Slerp(transform.position, curEndPoint, Time.deltaTime * speed);
transform.LookAt(lookAt);
if (pingpong) {
if (Vector3.Distance(transform.position, curEndPoint) < 0.001f) {
curEndPoint = Vector3.Distance(curEndPoint, endPoint) < Vector3.Distance(curEndPoint, startPoint) ? startPoint : endPoint;
}
}
}
}
2.此代碼掛在攝像機(jī)上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MotionBlur : PostEffectsBase
{
public Shader MotionBlurShader;
private Material _motionBlurMaterial = null;
public Material Material
{
get
{
_motionBlurMaterial = CheckShaderAndCreateMaterial(MotionBlurShader, _motionBlurMaterial);
return _motionBlurMaterial;
}
}
//定義運動模糊在混合圖像時使用的模糊參數(shù),值越大,拖尾越明顯,但過大會完全替代當(dāng)前幀的渲染結(jié)果,所以最大為0.9
[Range(0.0f, 0.9f)] public float BlurAmount = 0.5f;
//定義一個RenderTexture類型的變量,保存之前圖像疊加的結(jié)果
private RenderTexture _accumulationTexture;
//當(dāng)腳本不運行時,立即銷毀,這樣在下一次開始運行時會重新疊加圖像
void OnDisable()
{
DestroyImmediate(_accumulationTexture);
}
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (Material != null)
{
//判斷用于混合圖像的_accumulationTexture是否為空,或者是否與當(dāng)前屏幕分辨率相等
//如果不滿足,需要重新創(chuàng)建
if (_accumulationTexture == null || _accumulationTexture.width != src.width ||
_accumulationTexture.height != src.height)
{
//立即銷毀當(dāng)前_accumulationTexture
DestroyImmediate(_accumulationTexture);
//創(chuàng)建一個與當(dāng)前屏幕分辨率相等的
_accumulationTexture = new RenderTexture(src.width, src.height, 0);
//由于我們自己控制這個變量的銷毀,所以不需要他顯示在Hierarchy中,也不需要保存在場景中
_accumulationTexture.hideFlags = HideFlags.HideAndDontSave;
//使用當(dāng)前的幀圖像初始化_accumulationTexture
Graphics.Blit(src, _accumulationTexture);
}
//對渲染紋理進(jìn)行恢復(fù)操作,發(fā)生在渲染的紋理而該紋理又沒有被提前清空或銷毀的情況下
//運動模糊每次調(diào)用OnRenderImage函數(shù),都需要把當(dāng)前紋理與_accumulationTexture中的圖像混合,所以_accumulationTexture不需要被提前清空
_accumulationTexture.MarkRestoreExpected();
//將參數(shù)傳給材質(zhì)
Material.SetFloat("_BlurAmount", 1.0f - BlurAmount);
//把當(dāng)前的屏幕圖像疊加到_accumulationTexture
Graphics.Blit(src, _accumulationTexture, Material);
//把疊加后的圖像輸出到屏幕
Graphics.Blit(_accumulationTexture, dest);
}
else
{
Graphics.Blit(src, dest);
}
}
}
3.此Shader賦值給代碼2
Shader "Unity Shaders Book/Chapter 12/MotionBlur"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
//混合系數(shù)
_BlurAmount("Blur Amount", Float) = 1.0
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
fixed _BlurAmount;
//定義頂點著色器
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
};
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
//定義第一個片元著色器,用于更新渲染紋理的RGB通道部分
fixed4 fragRGB(v2f i) : SV_Target{
//將A通道的值設(shè)為_BlurAmount,這樣在后面混合時可直接使用透明通道進(jìn)行混合
return fixed4(tex2D(_MainTex, i.uv).rgb, _BlurAmount);
}
//定義第二個片元著色器,用于更新渲染紋理的A通道部分
half4 fragA(v2f i) : SV_Target{
//直接返回采樣結(jié)果,為了維護(hù)渲染紋理的透明通道值,不讓其受到混合時使用的透明度值的影響
return tex2D(_MainTex, i.uv);
}
ENDCG
ZTest Always Cull Off Zwrite Off
//第一個Pass,用于更新渲染紋理的RGB通道
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB
CGPROGRAM
#pragma vertex vert
#pragma fragment fragRGB
ENDCG
}
//第二個Pass,用于更新渲染紋理的A通道
Pass
{
Blend One Zero
ColorMask A
CGPROGRAM
#pragma vertex vert
#pragma fragment fragA
ENDCG
}
}
Fallback Off
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:Unity3D使用Shader實現(xiàn)腐蝕消失
本文標(biāo)題:UnityShader使用圖像疊加實現(xiàn)運動模糊
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4859.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實現(xiàn)手動對資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡單實現(xiàn)程序單實例運行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中yield用法使用說明
- 01-10C#編程和Visual Studio使用技巧(下)
- 01-10C#編程和Visual Studio使用技巧(上)


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


