Grayscale.shader 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. Shader "BetterUI/Grayscale"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6. _StencilComp("Stencil Comparison", Float) = 8
  7. _Stencil("Stencil ID", Float) = 0
  8. _StencilOp("Stencil Operation", Float) = 0
  9. _StencilWriteMask("Stencil Write Mask", Float) = 255
  10. _StencilReadMask("Stencil Read Mask", Float) = 255
  11. _ColorMask("Color Mask", Float) = 15[Toggle(UNITY_UI_ALPHACLIP)]
  12. _UseUIAlphaClip("Use Alpha Clip", Float) = 0
  13. SrcBlendMode("SrcBlendMode", Float) = 5
  14. DstBlendMode("DstBlendMode", Float) = 10
  15. [Toggle(COMBINE_ALPHA)] CombineAlpha("Combine Alpha", Float) = 0
  16. [Toggle(FORCE_CLIP)] ForceClip("Force Clip", Float) = 0
  17. ClipThreshold("Alpha Clip Threshold", Float) = 0.5
  18. }
  19. SubShader
  20. {
  21. Tags
  22. {
  23. "Queue" = "Transparent"
  24. "IgnoreProjector" = "True"
  25. "RenderType" = "Transparent"
  26. "PreviewType" = "Plane"
  27. "CanUseSpriteAtlas" = "True"
  28. }
  29. Stencil
  30. {
  31. Ref[_Stencil]
  32. Comp[_StencilComp]
  33. Pass[_StencilOp]
  34. ReadMask[_StencilReadMask]
  35. WriteMask[_StencilWriteMask]
  36. }
  37. Cull Off
  38. Lighting Off
  39. ZWrite Off
  40. ZTest[unity_GUIZTestMode]
  41. Blend[SrcBlendMode][DstBlendMode]
  42. ColorMask[_ColorMask]
  43. Pass
  44. {
  45. Name "Grayscale"
  46. CGPROGRAM
  47. #pragma vertex vert
  48. #pragma fragment frag
  49. #pragma target 2.0
  50. #include "UnityCG.cginc"
  51. #include "UnityUI.cginc"
  52. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  53. #pragma multi_compile __ COMBINE_ALPHA
  54. #pragma multi_compile __ FORCE_CLIP
  55. struct appdata_t
  56. {
  57. float4 vertex : POSITION;
  58. float4 color : COLOR;
  59. float2 texcoord : TEXCOORD0;
  60. float2 uv1 : TEXCOORD1;
  61. #if UNITY_VERSION >= 550
  62. UNITY_VERTEX_INPUT_INSTANCE_ID
  63. #endif
  64. };
  65. struct v2f
  66. {
  67. float4 vertex : SV_POSITION;
  68. fixed4 color : COLOR;
  69. float2 texcoord : TEXCOORD0;
  70. float4 worldPosition : TEXCOORD1;
  71. float2 amount : TEXCOORD2;
  72. #if UNITY_VERSION >= 550
  73. UNITY_VERTEX_OUTPUT_STEREO
  74. #endif
  75. };
  76. fixed4 _TextureSampleAdd;
  77. float4 _ClipRect;
  78. #if UNITY_VERSION < 550
  79. bool _UseClipRect;
  80. bool _UseAlphaClip;
  81. bool CombineAlpha;
  82. bool ForceClip;
  83. #endif
  84. // VERTEX SHADER
  85. v2f vert(appdata_t IN)
  86. {
  87. v2f OUT;
  88. #if UNITY_VERSION >= 550
  89. UNITY_SETUP_INSTANCE_ID(IN);
  90. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  91. #endif
  92. OUT.worldPosition = IN.vertex;
  93. #if UNITY_VERSION >= 550
  94. OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  95. #else
  96. OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  97. #ifdef UNITY_HALF_TEXEL_OFFSET
  98. OUT.vertex.xy += (_ScreenParams.zw - 1.0) * float2(-1, 1);
  99. #endif
  100. #endif
  101. OUT.texcoord = IN.texcoord;
  102. OUT.color = IN.color;
  103. OUT.amount = IN.uv1;
  104. return OUT;
  105. }
  106. sampler2D _MainTex;
  107. float ClipThreshold;
  108. // FRAGMENT SHADER
  109. fixed4 frag(v2f IN) : SV_Target
  110. {
  111. half4 origColor = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
  112. half4 greyColor = dot(origColor.rgb, float3(0.3, 0.59, 0.11));
  113. half4 color = lerp(origColor, greyColor, IN.amount.x);
  114. color.a = origColor.a;
  115. #if UNITY_VERSION >= 550
  116. #ifdef COMBINE_ALPHA
  117. color.rgb *= color.a;
  118. #endif
  119. #ifdef FORCE_CLIP
  120. clip(color.a - ClipThreshold);
  121. #endif
  122. #else
  123. if (CombineAlpha)
  124. color.rgb *= color.a;
  125. if (ForceClip)
  126. clip(color.a - ClipThreshold);
  127. #endif
  128. #if UNITY_VERSION >= 550
  129. color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  130. #ifdef UNITY_UI_ALPHACLIP
  131. clip(color.a - 0.001);
  132. #endif
  133. #else
  134. if (_UseClipRect)
  135. color *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); if (_UseAlphaClip)
  136. clip(color.a - 0.001);
  137. #endif
  138. return color;
  139. }
  140. ENDCG
  141. }
  142. }
  143. }