一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - UnityShader使用速度映射圖實現運動模糊

UnityShader使用速度映射圖實現運動模糊

2022-03-11 13:06啦啦啦小聰聰 C#

這篇文章主要為大家詳細介紹了UnityShader使用速度映射圖實現運動模糊,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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
}

UnityShader使用速度映射圖實現運動模糊

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/weixin_37994402/article/details/79578751

延伸 · 閱讀

精彩推薦
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
主站蜘蛛池模板: 久久精品一区二区免费看 | 波多野结衣作品在线观看 | 国产激情一区二区三区成人91 | 激情男人天堂 | 亚洲 国产精品 日韩 | 日本搜子同屋的日子2国语 日本爽p大片免费观看 | 欧美精品三区 | 亚洲免费大全 | 美女天天色 | 日日免费视频 | 午夜久久久久久亚洲国产精品 | 亚洲成人国产精品 | 国产欧美精品 | 日本乱中文字幕系列在线观看 | 亚洲国产精品综合久久网络 | 久久这里有精品 | 久久精品亚洲国产AV涩情 | 99 久久99久久精品免观看 | 亚洲成av人片天堂网 | 国产成年人 | 美女林柏欣21p人体之仓之梦 | 欧美男女交配 | 黑人video粗暴日本 | 双性np玩烂了np欲之国的太子 | 精品无人区乱码1区2区3区免费 | 久久er99热精品一区二区 | 女仆掀起蕾丝裙被打屁股作文 | 性做久久久久久久 | 亚洲一区二区三区91 | 狠狠撸在线播放 | 欧美精品99久久久久久人 | 亚洲国产精久久久久久久 | 无罩看奶禁18 | 动漫美女被羞羞产奶 | 91交换论坛 | 沟厕okn系列在线播放 | 欧美日韩中文字幕一区二区高清 | 任我鲁精品视频精品 | 九九九国产视频 | 12一14性xxxxx国外 | luan小说|