服务器之家:专注于服务器技术及软件下载分享
分类导航

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
656