Shader "Unlit/MirrorAlpha" { Properties { _MainTex("Texture", 2D) = "white" {} _MirrorU("水平镜像翻转",float) = 0 _MirrorV("竖直镜像翻转",float) = 0 _AlphaLX("RangeAlphaLX",float) = 0 _AlphaRX("RangeAlphaRX",float) = 1 _AlphaTY("RangeAlphaTY",float) = 1 _AlphaBY("RangeAlphaBY",float) = 0 _AlphaPower("Power",float) = 0 //透明度变化范围 _AlphaStart("Start",float) = 0 } SubShader { Tags {"RenderType" = "Transparent" "Queue" = "Transparent"} Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag sampler2D _MainTex; float _MirrorU; float _MirrorV; float _AlphaPower; sampler2D _AlphaTex; float _AlphaLX; float _AlphaRX; float _AlphaTY; float _AlphaBY; float _AlphaStart; struct a2v { float4 vertex : POSITION; float3 texcoord : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; if (_MirrorU > 0) { o.uv.x = 1 - o.uv.x;//Flip the coordinates to get the image sampling data } if (_MirrorV > 0) { o.uv.y = 1 - o.uv.y;//Flip the coordinates to get the image sampling data } return o; } //此方法取自Unity默认Sprite的Shader fixed4 SampleSpriteTexture(float2 uv) { fixed4 color = tex2D(_MainTex, uv); #if ETC1_EXTERNAL_ALPHA // get the color from an external texture (usecase: Alpha support for ETC1 on android) color.a = tex2D(_AlphaTex, uv).r; #endif //ETC1_EXTERNAL_ALPHA return color; } fixed4 frag(v2f i) : SV_Target{ // sample the texture fixed4 col = SampleSpriteTexture(i.uv); //利用透明度阈值和uv坐标的差来计算透明的程度和是否控制其半透 //四个方向只是对坐标的取值和正反方向不同,原理一致 fixed alphalx = col.a * lerp(_AlphaStart, _AlphaPower, (_AlphaLX - i.uv.x)); col.a = saturate(lerp(alphalx, col.a, step(_AlphaLX, i.uv.x))); fixed alpharx = col.a * lerp(_AlphaStart, _AlphaPower, (i.uv.x - _AlphaRX)); col.a = saturate(lerp(col.a, alpharx, step(_AlphaRX, i.uv.x))); fixed alphaby = col.a * lerp(_AlphaStart, _AlphaPower, (_AlphaBY - i.uv.y)); col.a = saturate(lerp(alphaby, col.a, step(_AlphaBY, i.uv.y))); fixed alphaty = col.a * lerp(_AlphaStart, _AlphaPower, (i.uv.y - _AlphaTY)); col.a = saturate(lerp(col.a, alphaty, step(_AlphaTY, i.uv.y))); return col; //return tex2D(_MainTex,i.uv); } ENDCG } } FallBack off }