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

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

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實現運動模糊的具體代碼,供大家參考,具體內容如下

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
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

?
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
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使用圖像疊加實現運動模糊

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

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

延伸 · 閱讀

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

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

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

    GhostRider9502022-01-21
  • 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#WPF 自定義雷達圖開發實例教程

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

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

    WinterFish13112021-12-06
  • C#C#通過KD樹進行距離最近點的查找

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

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

    帆帆帆6112022-01-22
  • C#Unity3D實現虛擬按鈕控制人物移動效果

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

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

    shenqingyu060520232410972022-03-11
  • C#深入解析C#中的交錯數組與隱式類型的數組

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

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

    C#教程網6172021-11-09
主站蜘蛛池模板: 被肉日常np高h | 久久精品国产亚洲AV麻豆欧美玲 | 12345国产精品高清在线 | 国产一区精品视频 | 国产伦久视频免费观看视频 | 91精品国产91久久久久久 | 国产一级持黄大片99久久 | 十大网站免费货源 | 国产精品理论片 | 九九热视频 这里有精品 | 海派甜心完整版在线观看 | 免费看日本 | 免费看男人使劲躁女人小说 | 精品国产美女福利在线 | 青青国产精品 | 毛片视频网站 | 精品国产免费久久久久久婷婷 | 日韩在线一区二区三区 | 久久re热在线视频精99 | 极品虎白女在线观看一线天 | 婷婷色在线观看 | 亚洲国产精品久久人人爱 | japanesexxxx在线播放 | 欧美最猛性xxxxx短视频 | 亚洲成年网站在线观看 | 麻豆亚洲一区 | 久久理论片 | 国产一区二区三区福利 | 亚洲+国产+图片 | 日韩一区二区三区四区五区 | 国产亚洲小视频 | 国产91精品久久久久久 | 男人疯狂擦进女人下面 | tk白丝丨vk| 日韩黄色录像 | 草莓绿巨人香蕉茄子芭乐 | 9420高清视频在线观看网百度 | 91在线老师啪国自产 | 精品欧美一区二区三区四区 | 日产精品一二三四区国产 | 亚洲精品中文 |