介绍
本篇文章为大家展示了怎么在UnityShader中利用速度映射图实现运动模糊,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
<强>原理:强>
像素的当前帧的NDC坐标(x, y值由紫外线映射而来,z值由深度值映射而来),(使用_CurrentViewProjectionInverseMartix变换,并除以w分量)——像素的世界坐标——(使用_PreviousViewProjectionMatrix变换,并除以w分量)——像素的前一帧的NDC坐标——(当前帧NDC——前一帧NDC)——速度
1。此代码挂在摄像机上,使摄像机运动起来
using UnityEngine;, using System.Collections, , 时间:public class  Translating MonoBehaviour {, , ,public float speed =, 10.0 f,, ,public Vector3 startPoint =, Vector3.zero;, ,public Vector3 endPoint =, Vector3.zero;, ,public Vector3 lookAt =, Vector3.zero;, ,public bool pingpong =,真的,, , ,private Vector3 curEndPoint =, Vector3.zero;, , ,//Use 却;能够for initialization ,void Start (), {,=,transform.position 曾经繁荣,,=,curEndPoint 端点,, }大敌; , ,//Update is  nbsp; once per frame ,void Update (), {,=,,transform.position  Vector3.Slerp (transform.position, curEndPoint,, Time.deltaTime *,速度),, ,transform.LookAt(之外),, ,if (乒乓球),{, if 才能;(Vector3.Distance (transform.position, curEndPoint), & lt;, 0.001度),{, curEndPoint 才能=,Vector3.Distance (curEndPoint,端点),& lt;, Vector3.Distance (curEndPoint,曾经繁荣),?,startPoint :,端点,, ,,}, }大敌; }大敌; }
2。此代码挂在摄像机上
using System.Collections; using System.Collections.Generic; using UnityEngine; , public class MotionBlurWithDepthTexture : PostEffectsBase { ,public Shader  MotionBlurShader; ,private Material _motionBlurMaterial =,空; , public Material 材料 ,{ ,得到 ,{ 时间=_motionBlurMaterial 才能;CheckShaderAndCreateMaterial (_motionBlurMaterial MotionBlurShader也); return 才能;_motionBlurMaterial; ,} ,} , ,//定义运动模糊时模糊图像使用的大小 ,(范围(0.0 f, 1.0 f)], public float BlurSize =, 0.5 f; ,//定义一个相机变量,获取该脚本所在的摄像机组建,得到摄像机的视角和投影矩阵 ,private Camera  _myCamera; , ,public Camera 相机 ,{ ,得到 ,{ if 才能;(_myCamera ==, null) {才能 时间=_myCamera 才能;GetComponent(); ,,} return 才能;_myCamera; ,} ,} , ,//定义一个变量保存,上一帧摄像机的视角,*,投影矩阵 ,private Matrix4x4  _previousViewProjectionMatrix; , ,//在OnEnable中设置摄像机的状态,以获得深度纹理 ,void OnEnable () ,{=,,Camera.depthTextureMode  DepthTextureMode.Depth; ,} , ,void OnRenderImage (RenderTexture src, RenderTexture dest) ,{ ,if (Material !=, null) ,{//将才能模糊大小传给材质 Material.SetFloat才能(“_BlurSize",, BlurSize); ,//使才能用,视角,*,投影矩阵,对国防委员会(归一化的设备坐标)下的顶点坐标进行变换,得到该像素在世界空间下的位置//计才能算前一帧与当前帧的位置差,生成该像素的速度 ,//将才能,前一帧视角,*,投影矩阵,传给材质 Material.SetMatrix才能(“_PreviousViewProjectionMatrix",, _previousViewProjectionMatrix);//计才能算,当前帧视角,*,投影矩阵//Camera.projectionMatrix才能获得当前摄像机投影矩阵//Camera.worldToCameraMatrix才能获得当前摄像机视角矩阵 Matrix4x4 才能;currentViewProjectionMartix =, Camera.projectionMatrix *, Camera.worldToCameraMatrix;//计才能算,当前帧视角,*,投影矩阵,的逆矩阵 Matrix4x4 才能;currentViewProjectionInverseMartix =, currentViewProjectionMartix.inverse;//将才能当前帧视角,*,投影矩阵,的逆矩阵,传递给材质 Material.SetMatrix才能(“_CurrentViewProjectionInverseMartix",, currentViewProjectionInverseMartix);//将才能,当前帧视角,*,投影矩阵,保存为,前一帧视角,*,投影矩阵 时间=_previousViewProjectionMatrix 才能;currentViewProjectionMartix; , Graphics.Blit才能(src,,,不在座位上,材料); ,} 其他的, ,{ Graphics.Blit才能(src,桌子); ,} ,} } 怎么在UnityShader中利用速度映射图实现运动模糊