温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在UnityShader中利用速度映射图实现运动模糊

发布时间:2021-05-17 17:43:43 来源:亿速云 阅读:202 作者:Leah 栏目:编程语言

本篇文章为大家展示了怎么在UnityShader中利用速度映射图实现运动模糊,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

原理:

像素的当前帧的NDC坐标(x,y值由uv映射而来,z值由深度值映射而来)——(使用_CurrentViewProjectionInverseMartix变换,并除以w分量)—— 像素的世界坐标 ——(使用_PreviousViewProjectionMatrix变换,并除以w分量)—— 像素的前一帧的NDC坐标 —— (当前帧NDC-前一帧NDC)—— 速度

1.此代码挂在摄像机上,使摄像机运动起来

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.此代码挂在摄像机上

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

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中利用速度映射图实现运动模糊,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI