// 摄像机深度模糊 Shader "Zhanyou/Mobile/CameraDepthBlur" { Properties { [NoScaleOffset]_MainTex ("Texture", 2D) = "white" {} _Offsets("Offset", Vector) = (0.1,0.1,0,0) } SubShader { Tags { "IgnoreProjector"="True" "Queue"="Overlay" // 防止Shader Replace渲染这个东西 "RenderType"="NoReplace" } Pass { Lighting Off ZWrite Off CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform sampler2D _CameraDepthTexture; uniform float4 _Offsets; struct appdata { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; half4 blur(sampler2D mainTex, float2 uv, float blurFactor) { float4 uv01 = uv.xyxy + blurFactor * _Offsets.xyxy * half4(1,1, -1,-1); float4 uv23 = uv.xyxy + blurFactor * _Offsets.xyxy * half4(1,1, -1,-1) * 2.0; float4 uv45 = uv.xyxy + blurFactor * _Offsets.xyxy * half4(1,1, -1,-1) * 3.0; float4 uv67 = uv.xyxy + blurFactor * _Offsets.xyxy * half4(1,1, -1,-1) * 4.0; half4 color = half4(0, 0, 0, 0); color += 0.225 * tex2D (mainTex, uv); color += 0.150 * tex2D (mainTex, uv01.xy); color += 0.150 * tex2D (mainTex, uv01.zw); color += 0.110 * tex2D (mainTex, uv23.xy); color += 0.110 * tex2D (mainTex, uv23.zw); color += 0.075 * tex2D (mainTex, uv45.xy); color += 0.075 * tex2D (mainTex, uv45.zw); color += 0.0525 * tex2D (mainTex, uv67.xy); color += 0.0525 * tex2D (mainTex, uv67.zw); return color; } v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; return o; } fixed4 frag (v2f i) : SV_Target { //return tex2D(_MainTex, i.uv); float depth = UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, i.uv)); depth = Linear01Depth(depth); depth = clamp((depth - _Offsets.z) / (1 - _Offsets.z) * _Offsets.w, 0, 1); return blur(_MainTex, i.uv, depth); } ENDCG } } }