Camera画面边缘羽化
用到的shader
Shader "My/EdgeFade" { Properties { _EdgeFade("EdgeFade", Range(0, 1)) = 0.8 } SubShader { Tags { "Queue" = "Overlay" "IgnoreProjector" = "True" "RenderType" = "Transparent" } Pass { Blend Zero One, One Zero Cull Off ZWrite Off ZTest Off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma target 2.0 #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float2 texcoord : TEXCOORD0; }; struct v2f { float4 vertex : SV_POSITION; half2 texcoord : TEXCOORD0; }; half _EdgeFade; v2f vert(appdata_t v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.vertex.xyzw = o.vertex.xyzw / o.vertex.w; o.vertex.xy = v.texcoord.xy * 2.0 - 1.0; //[0, 1]范围变成[-1, 1] o.texcoord = v.texcoord; return o; } fixed4 frag(v2f i) : SV_Target { half2 uv = abs(i.texcoord.xy * 2.0 - 1.0); half2 dis = clamp(uv - _EdgeFade, half2(0, 0), half2(1.0, 1.0)); //uv值<_EdgeFade时dis总是为0; uv值越靠近1, dis值越大 half factor = 1.001 / (1.001 - _EdgeFade); half2 a = clamp(half2(1.0, 1.0) - dis * factor, half2(0, 0), half2(1.0, 1.0)); //uv值越靠近1, 透明度越接近0; 越靠近0, 越接近1; 透明度线性变化, t值越大斜率越大; fixed4 col = fixed4(0, 0, 0, a.x * a.y); //x固定时, 是线性方程; y固定时, 也是线性方程; return col; } ENDCG } } }
假设_EdgeFade=0.8, dis的方程图像,x<=0.8时dis总是为0
factor的方程图像,EdgeFade对最终alpha的影响是非线性的
不考虑factor时alpha的方程图像