HighlightingTransparent.shader 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. Shader "Hidden/Highlighted/Transparent"
  2. {
  3. Properties
  4. {
  5. [HideInInspector] _MainTex ("", 2D) = "" {}
  6. [HideInInspector] _HighlightingColor ("", Color) = (1, 1, 1, 1)
  7. [HideInInspector] _Cutoff ("", Float) = 0.5
  8. [HideInInspector] _HighlightingCull ("", Int) = 2 // UnityEngine.Rendering.CullMode.Back
  9. }
  10. SubShader
  11. {
  12. Lighting Off
  13. Fog { Mode Off }
  14. ZWrite Off // Manual depth test
  15. ZTest Always // Manual depth test
  16. Cull [_HighlightingCull] // For rendering both sides of the Sprite
  17. Pass
  18. {
  19. Stencil
  20. {
  21. Ref 1
  22. Comp Always
  23. Pass Replace
  24. ZFail Keep
  25. }
  26. CGPROGRAM
  27. #pragma vertex vert
  28. #pragma fragment frag
  29. #pragma fragmentoption ARB_precision_hint_fastest
  30. #pragma target 2.0
  31. #pragma multi_compile __ HIGHLIGHTING_OVERLAY
  32. #include "UnityCG.cginc"
  33. uniform fixed4 _HighlightingColor;
  34. uniform sampler2D _MainTex;
  35. uniform float4 _MainTex_ST;
  36. uniform fixed _Cutoff;
  37. #ifndef HIGHLIGHTING_OVERLAY
  38. UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
  39. #endif
  40. struct vs_input
  41. {
  42. float4 vertex : POSITION;
  43. float2 texcoord : TEXCOORD0;
  44. fixed4 color : COLOR;
  45. UNITY_VERTEX_INPUT_INSTANCE_ID
  46. };
  47. struct ps_input
  48. {
  49. float4 pos : SV_POSITION;
  50. float2 texcoord : TEXCOORD0;
  51. fixed alpha : TEXCOORD1;
  52. #ifndef HIGHLIGHTING_OVERLAY
  53. float4 screen : TEXCOORD2;
  54. #endif
  55. };
  56. ps_input vert(vs_input v)
  57. {
  58. ps_input o;
  59. UNITY_SETUP_INSTANCE_ID(v);
  60. o.pos = UnityObjectToClipPos(v.vertex);
  61. #ifndef HIGHLIGHTING_OVERLAY
  62. o.screen = ComputeScreenPos(o.pos);
  63. COMPUTE_EYEDEPTH(o.screen.z);
  64. #endif
  65. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  66. o.alpha = v.color.a;
  67. return o;
  68. }
  69. fixed4 frag(ps_input i) : SV_Target
  70. {
  71. fixed a = tex2D(_MainTex, i.texcoord).a;
  72. clip(a - _Cutoff);
  73. #ifndef HIGHLIGHTING_OVERLAY
  74. float z = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.screen));
  75. float perspZ = LinearEyeDepth(z); // LinearEyeDepth automatically handles UNITY_REVERSED_Z case
  76. #if defined(UNITY_REVERSED_Z)
  77. z = 1 - z;
  78. #endif
  79. float orthoZ = _ProjectionParams.y + z * (_ProjectionParams.z - _ProjectionParams.y); // near + z * (far - near)
  80. float sceneZ = lerp(perspZ, orthoZ, unity_OrthoParams.w);
  81. clip(sceneZ - i.screen.z + 0.01);
  82. #endif
  83. fixed4 c = _HighlightingColor;
  84. c.a *= i.alpha;
  85. return c;
  86. }
  87. ENDCG
  88. }
  89. }
  90. Fallback Off
  91. }