
离线 TA的专栏
- 打卡等级:热心大叔
- 打卡总天数:203
- 打卡月天数:0
- 打卡总奖励:3146
- 最近打卡:2023-08-27 07:10:01
|
本文实例为大家分享了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
- }
复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持晓枫资讯。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:  进行删除处理。
4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
|