UnityShader使用速度映射圖實(shí)現(xiàn)運(yùn)動(dòng)模糊
本文實(shí)例為大家分享了UnityShader實(shí)現(xiàn)運(yùn)動(dòng)模糊的具體代碼,供大家參考,具體內(nèi)容如下
原理:
像素的當(dāng)前幀的NDC坐標(biāo)(x,y值由uv映射而來(lái),z值由深度值映射而來(lái))——(使用_CurrentViewProjectionInverseMartix變換,并除以w分量)—— 像素的世界坐標(biāo) ——(使用_PreviousViewProjectionMatrix變換,并除以w分量)—— 像素的前一幀的NDC坐標(biāo) —— (當(dāng)前幀NDC-前一幀NDC)—— 速度
1.此代碼掛在攝像機(jī)上,使攝像機(jī)運(yùn)動(dòng)起來(lái)
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 MotionBlurWithDepthTexture : PostEffectsBase
{
public Shader MotionBlurShader;
private Material _motionBlurMaterial = null;
public Material Material
{
get
{
_motionBlurMaterial = CheckShaderAndCreateMaterial(MotionBlurShader, _motionBlurMaterial);
return _motionBlurMaterial;
}
}
//定義運(yùn)動(dòng)模糊時(shí)模糊圖像使用的大小
[Range(0.0f, 1.0f)] public float BlurSize = 0.5f;
//定義一個(gè)Camera變量,獲取該腳本所在的攝像機(jī)組建,得到攝像機(jī)的視角和投影矩陣
private Camera _myCamera;
public Camera Camera
{
get
{
if (_myCamera == null)
{
_myCamera = GetComponent<Camera>();
}
return _myCamera;
}
}
//定義一個(gè)變量保存 上一幀攝像機(jī)的視角 * 投影矩陣
private Matrix4x4 _previousViewProjectionMatrix;
//在OnEnable中設(shè)置攝像機(jī)的狀態(tài),以獲得深度紋理
void OnEnable()
{
Camera.depthTextureMode = DepthTextureMode.Depth;
}
void OnRenderImage(RenderTexture src, RenderTexture dest)
{
if (Material != null)
{
//將模糊大小傳給Shader
Material.SetFloat("_BlurSize", BlurSize);
//使用 視角 * 投影矩陣 對(duì)NDC(歸一化的設(shè)備坐標(biāo))下的頂點(diǎn)坐標(biāo)進(jìn)行變換,得到該像素在世界空間下的位置
//計(jì)算前一幀與當(dāng)前幀的位置差,生成該像素的速度
//將 前一幀視角 * 投影矩陣 傳給Shader
Material.SetMatrix("_PreviousViewProjectionMatrix", _previousViewProjectionMatrix);
//計(jì)算 當(dāng)前幀視角 * 投影矩陣
//Camera.projectionMatrix獲得當(dāng)前攝像機(jī)投影矩陣
//Camera.worldToCameraMatrix獲得當(dāng)前攝像機(jī)視角矩陣
Matrix4x4 currentViewProjectionMartix = Camera.projectionMatrix * Camera.worldToCameraMatrix;
//計(jì)算 當(dāng)前幀視角 * 投影矩陣 的逆矩陣
Matrix4x4 currentViewProjectionInverseMartix = currentViewProjectionMartix.inverse;
//將當(dāng)前幀視角 * 投影矩陣 的逆矩陣 傳遞給Shader
Material.SetMatrix("_CurrentViewProjectionInverseMartix", currentViewProjectionInverseMartix);
//將 當(dāng)前幀視角 * 投影矩陣 保存為 前一幀視角 * 投影矩陣
_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í)使用的參數(shù)
_BlurSize ("Blur Size", Float) = 1.0
//這里并沒(méi)有聲明_PreviousViewProjectionMatrix和_CurrentViewProjectionInverseMartix
//是因?yàn)閁nity并沒(méi)有提供矩陣類型的屬性,但仍然可以在CG代碼塊中定義這些矩陣,并從腳本中設(shè)置它們
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
//使用_MainTex_TexelSize變量來(lái)對(duì)深度紋理的采樣坐標(biāo)進(jìn)行平臺(tái)化差異處理
half4 _MainTex_TexelSize;
//Unity傳遞來(lái)的深度紋理
sampler2D _CameraDepthTexture;
//聲明_PreviousViewProjectionMatrix和_CurrentViewProjectionInverseMartix
float4x4 _PreviousViewProjectionMatrix;
float4x4 _CurrentViewProjectionInverseMartix;
half _BlurSize;
//定義頂點(diǎn)著色器
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
//添加了用于深度紋理采樣的紋理坐標(biāo)變量
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;
//平臺(tái)差異化處理
#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{
//使用宏和紋理坐標(biāo)對(duì)深度紋理進(jìn)行采樣,得到深度值
float d = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth);
//構(gòu)建當(dāng)前像素的NDC坐標(biāo),xy坐標(biāo)由像素的紋理坐標(biāo)映射而來(lái),z坐標(biāo)由深度值d映射而來(lái)
float4 H = float4(i.uv.x * 2 - 1, i.uv.y * 2 - 1, d * 2 - 1, 1);
//使用 當(dāng)前幀的視角 * 投影矩陣 的逆矩陣 對(duì)H進(jìn)行變換
float4 D = mul(_CurrentViewProjectionInverseMartix, H);
//把結(jié)果除以它的w分量,得到該像素世界空間下的坐標(biāo)
float4 worldPos = D / D.w;
//像素當(dāng)前幀NDC坐標(biāo)
float4 currentPos = H;
//使用 前一幀視角 * 投影矩陣 變換世界空間的的坐標(biāo)worldPos,并除以它的w分量,得到前一幀的NDC坐標(biāo)
float4 previousPos = mul(_PreviousViewProjectionMatrix, worldPos);
previousPos /= previousPos.w;
//計(jì)算當(dāng)前幀與前一幀在屏幕空間下的位置差,得到該像素的速度
float2 velocity = (currentPos.xy - previousPos.xy) / 2.0f;
//使用速度值對(duì)鄰域像素進(jìn)行采樣,相加后取平均值得到一個(gè)模糊效果,使用_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
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Unity5.6大規(guī)模地形資源創(chuàng)建方法
欄 目:C#教程
下一篇:Unity實(shí)現(xiàn)3D循環(huán)滾動(dòng)效果
本文標(biāo)題:UnityShader使用速度映射圖實(shí)現(xiàn)運(yùn)動(dòng)模糊
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4877.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類寫(xiě)Windows事件日志的方法
- 01-10C#使用windows服務(wù)開(kāi)啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來(lái)訪問(wèn)Access數(shù)據(jù)庫(kù)的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中yield用法使用說(shuō)明
- 01-10C#編程和Visual Studio使用技巧(下)
- 01-10C#編程和Visual Studio使用技巧(上)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


