本文實例為大家分享了unityshader實現運動模糊的具體代碼,供大家參考,具體內容如下
原理:
像素的當前幀的ndc坐標(x,y值由uv映射而來,z值由深度值映射而來)——(使用_currentviewprojectioninversemartix變換,并除以w分量)—— 像素的世界坐標 ——(使用_previousviewprojectionmatrix變換,并除以w分量)—— 像素的前一幀的ndc坐標 —— (當前幀ndc-前一幀ndc)—— 速度
1.此代碼掛在攝像機上,使攝像機運動起來
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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.此代碼掛在攝像機上
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
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中設置攝像機的狀態,以獲得深度紋理 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
shader "unity shaders book/chapter 13/motionblurwithdepthtexture" { properties { _maintex ( "base (rgb)" , 2d) = "white" {} //模糊圖像時使用的參數 _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 } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_37994402/article/details/79578751