DirectionalWipeTransparent.shader 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. Shader "Custom/DirectionalWipeTransparent"
  2. {
  3. Properties
  4. {
  5. _MainTex("Texture", 2D) = "white" {}
  6. _Color("Color Tint", Color) = (1,1,1,1)
  7. _Threshold("Wipe Threshold", Range(-1, 1)) = -1
  8. _EdgeSmooth("Edge Smoothness", Range(0.001, 1)) = 0.1
  9. _WipeDir("Wipe Direction Vector", Vector) = (1, 0, 0, 0) // 默认沿X轴
  10. }
  11. SubShader
  12. {
  13. Tags { "Queue" = "Transparent" "RenderType" = "Transparent" }
  14. LOD 200
  15. Blend SrcAlpha OneMinusSrcAlpha
  16. ZWrite Off
  17. Cull Off
  18. Pass
  19. {
  20. CGPROGRAM
  21. #pragma vertex vert
  22. #pragma fragment frag
  23. #include "UnityCG.cginc"
  24. sampler2D _MainTex;
  25. float4 _Color;
  26. float _Threshold;
  27. float _EdgeSmooth;
  28. float4 _WipeDir; // 方向向量,xyz有效
  29. struct appdata
  30. {
  31. float4 vertex : POSITION;
  32. float2 uv : TEXCOORD0;
  33. };
  34. struct v2f
  35. {
  36. float2 uv : TEXCOORD0;
  37. float4 vertex : SV_POSITION;
  38. float3 localPos : TEXCOORD1;
  39. };
  40. v2f vert(appdata v)
  41. {
  42. v2f o;
  43. o.vertex = UnityObjectToClipPos(v.vertex);
  44. o.uv = v.uv;
  45. o.localPos = v.vertex.xyz;
  46. return o;
  47. }
  48. fixed4 frag(v2f i) : SV_Target
  49. {
  50. // 方向向量点乘位置向量,得到在该方向上的投影值
  51. float wipeCoord = dot(i.localPos, _WipeDir.xyz);
  52. // 使用 smoothstep 实现边缘渐变
  53. float alphaMask = smoothstep(_Threshold - _EdgeSmooth, _Threshold + _EdgeSmooth, wipeCoord);
  54. fixed4 col = tex2D(_MainTex, i.uv) * _Color;
  55. col.a *= alphaMask;
  56. return col;
  57. }
  58. ENDCG
  59. }
  60. }
  61. }