這篇文章給大家介紹怎么在UnityShader中利用圖像疊加實現運動模糊,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

網站建設哪家好,找創新互聯!專注于網頁設計、網站建設、微信開發、成都微信小程序、集團企業網站建設等服務項目。為回饋新老客戶創新互聯還提供了邱縣免費建站歡迎大家使用!
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.此代碼掛在攝像機上
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;
}
}
//定義運動模糊在混合圖像時使用的模糊參數,值越大,拖尾越明顯,但過大會完全替代當前幀的渲染結果,所以最大為0.9
[Range(0.0f, 0.9f)] public float BlurAmount = 0.5f;
//定義一個RenderTexture類型的變量,保存之前圖像疊加的結果
private RenderTexture _accumulationTexture;
//當腳本不運行時,立即銷毀,這樣在下一次開始運行時會重新疊加圖像
void OnDisable()
{
DestroyImmediate(_accumulationTexture);
}
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (Material != null)
{
//判斷用于混合圖像的_accumulationTexture是否為空,或者是否與當前屏幕分辨率相等
//如果不滿足,需要重新創建
if (_accumulationTexture == null || _accumulationTexture.width != src.width ||
_accumulationTexture.height != src.height)
{
//立即銷毀當前_accumulationTexture
DestroyImmediate(_accumulationTexture);
//創建一個與當前屏幕分辨率相等的
_accumulationTexture = new RenderTexture(src.width, src.height, 0);
//由于我們自己控制這個變量的銷毀,所以不需要他顯示在Hierarchy中,也不需要保存在場景中
_accumulationTexture.hideFlags = HideFlags.HideAndDontSave;
//使用當前的幀圖像初始化_accumulationTexture
Graphics.Blit(src, _accumulationTexture);
}
//對渲染紋理進行恢復操作,發生在渲染的紋理而該紋理又沒有被提前清空或銷毀的情況下
//運動模糊每次調用OnRenderImage函數,都需要把當前紋理與_accumulationTexture中的圖像混合,所以_accumulationTexture不需要被提前清空
_accumulationTexture.MarkRestoreExpected();
//將參數傳給材質
Material.SetFloat("_BlurAmount", 1.0f - BlurAmount);
//把當前的屏幕圖像疊加到_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" {}
//混合系數
_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通道的值設為_BlurAmount,這樣在后面混合時可直接使用透明通道進行混合
return fixed4(tex2D(_MainTex, i.uv).rgb, _BlurAmount);
}
//定義第二個片元著色器,用于更新渲染紋理的A通道部分
half4 fragA(v2f i) : SV_Target{
//直接返回采樣結果,為了維護渲染紋理的透明通道值,不讓其受到混合時使用的透明度值的影響
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
}關于怎么在UnityShader中利用圖像疊加實現運動模糊就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網站名稱:怎么在UnityShader中利用圖像疊加實現運動模糊
本文URL:http://www.yijiale78.com/article36/pchosg.html
成都網站建設公司_創新互聯,為您提供響應式網站、靜態網站、品牌網站設計、網站內鏈、網站設計、網站收錄
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯