本篇文章為大家展示了怎么在UnityShader中利用速度映射圖實現(xiàn)運動模糊,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
原理:
像素的當前幀的NDC坐標(x,y值由uv映射而來,z值由深度值映射而來)——(使用_CurrentViewProjectionInverseMartix變換,并除以w分量)—— 像素的世界坐標 ——(使用_PreviousViewProjectionMatrix變換,并除以w分量)—— 像素的前一幀的NDC坐標 —— (當前幀NDC-前一幀NDC)—— 速度
1.此代碼掛在攝像機上,使攝像機運動起來
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 MotionBlurWithDepthTexture : PostEffectsBase { public Shader MotionBlurShader; private Material _motionBlurMaterial = null; public Material Material { get { _motionBlurMaterial = CheckShaderAndCreateMaterial(MotionBlurShader, _motionBlurMaterial); return _motionBlurMaterial; } } //定義運動模糊時模糊圖像使用的大小 [Range(0.0f, 1.0f)] public float BlurSize = 0.5f; //定義一個Camera變量,獲取該腳本所在的攝像機組建,得到攝像機的視角和投影矩陣 private Camera _myCamera; public Camera Camera { get { if (_myCamera == null) { _myCamera = GetComponent<Camera>(); } return _myCamera; } } //定義一個變量保存 上一幀攝像機的視角 * 投影矩陣 private Matrix4x4 _previousViewProjectionMatrix; //在OnEnable中設置攝像機的狀態(tài),以獲得深度紋理 void OnEnable() { Camera.depthTextureMode = DepthTextureMode.Depth; } void OnRenderImage(RenderTexture src, RenderTexture dest) { if (Material != null) { //將模糊大小傳給Shader Material.SetFloat("_BlurSize", BlurSize); //使用 視角 * 投影矩陣 對NDC(歸一化的設備坐標)下的頂點坐標進行變換,得到該像素在世界空間下的位置 //計算前一幀與當前幀的位置差,生成該像素的速度 //將 前一幀視角 * 投影矩陣 傳給Shader Material.SetMatrix("_PreviousViewProjectionMatrix", _previousViewProjectionMatrix); //計算 當前幀視角 * 投影矩陣 //Camera.projectionMatrix獲得當前攝像機投影矩陣 //Camera.worldToCameraMatrix獲得當前攝像機視角矩陣 Matrix4x4 currentViewProjectionMartix = Camera.projectionMatrix * Camera.worldToCameraMatrix; //計算 當前幀視角 * 投影矩陣 的逆矩陣 Matrix4x4 currentViewProjectionInverseMartix = currentViewProjectionMartix.inverse; //將當前幀視角 * 投影矩陣 的逆矩陣 傳遞給Shader Material.SetMatrix("_CurrentViewProjectionInverseMartix", currentViewProjectionInverseMartix); //將 當前幀視角 * 投影矩陣 保存為 前一幀視角 * 投影矩陣 _previousViewProjectionMatrix = currentViewProjectionMartix; Graphics.Blit(src, dest, Material); } else { Graphics.Blit(src, dest); } } }
3.此Shader賦值給代碼2
Shader "Unity Shaders Book/Chapter 13/MotionBlurWithDepthTexture" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} //模糊圖像時使用的參數(shù) _BlurSize ("Blur Size", Float) = 1.0 //這里并沒有聲明_PreviousViewProjectionMatrix和_CurrentViewProjectionInverseMartix //是因為Unity并沒有提供矩陣類型的屬性,但仍然可以在CG代碼塊中定義這些矩陣,并從腳本中設置它們 } SubShader { CGINCLUDE #include "UnityCG.cginc" sampler2D _MainTex; //使用_MainTex_TexelSize變量來對深度紋理的采樣坐標進行平臺化差異處理 half4 _MainTex_TexelSize; //Unity傳遞來的深度紋理 sampler2D _CameraDepthTexture; //聲明_PreviousViewProjectionMatrix和_CurrentViewProjectionInverseMartix float4x4 _PreviousViewProjectionMatrix; float4x4 _CurrentViewProjectionInverseMartix; half _BlurSize; //定義頂點著色器 struct v2f { float4 pos : SV_POSITION; half2 uv : TEXCOORD0; //添加了用于深度紋理采樣的紋理坐標變量 half2 uv_depth : TEXCOORD1; }; v2f vert(appdata_img v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; o.uv_depth = v.texcoord; //平臺差異化處理 #if UNITY_UV_STARTS_AT_TOP if (_MainTex_TexelSize.y < 0) { o.uv_depth.y = 1 - o.uv_depth.y; } #endif return o; } //定義片元著色器 fixed4 frag(v2f i) : SV_Target{ //使用宏和紋理坐標對深度紋理進行采樣,得到深度值 float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth); //構建當前像素的NDC坐標,xy坐標由像素的紋理坐標映射而來,z坐標由深度值d映射而來 float4 H = float4(i.uv.x * 2 - 1, i.uv.y * 2 - 1, d * 2 - 1, 1); //使用 當前幀的視角 * 投影矩陣 的逆矩陣 對H進行變換 float4 D = mul(_CurrentViewProjectionInverseMartix, H); //把結果除以它的w分量,得到該像素世界空間下的坐標 float4 worldPos = D / D.w; //像素當前幀NDC坐標 float4 currentPos = H; //使用 前一幀視角 * 投影矩陣 變換世界空間的的坐標worldPos,并除以它的w分量,得到前一幀的NDC坐標 float4 previousPos = mul(_PreviousViewProjectionMatrix, worldPos); previousPos /= previousPos.w; //計算當前幀與前一幀在屏幕空間下的位置差,得到該像素的速度 float2 velocity = (currentPos.xy - previousPos.xy) / 2.0f; //使用速度值對鄰域像素進行采樣,相加后取平均值得到一個模糊效果,使用_BlurSize控制采樣距離 float2 uv = i.uv; float4 c = tex2D(_MainTex, uv); uv += velocity * _BlurSize; for (int it = 1; it < 3; it++, uv += velocity * _BlurSize) { float4 currentColor = tex2D(_MainTex, uv); c += currentColor; } c /= 3; return fixed4(c.rgb, 1.0); } ENDCG Pass { ZTest Always Cull Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag ENDCG } } Fallback Off }
上述內容就是怎么在UnityShader中利用速度映射圖實現(xiàn)運動模糊,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)成都網站設計公司行業(yè)資訊頻道。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網頁名稱:怎么在UnityShader中利用速度映射圖實現(xiàn)運動模糊-創(chuàng)新互聯(lián)
分享地址:http://jinyejixie.com/article2/dpcooc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站設計公司、移動網站建設、定制網站、小程序開發(fā)、企業(yè)網站制作、域名注冊
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)