GraySprite.shader 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Shader "DashGame/GraySprite"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6. _Color("Tint", Color) = (1,1,1,1)
  7. [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
  8. [PerRendererData] _GrayLerp("GrayLerp", range(0,1)) = 1
  9. [PerRendererData] _GrayScaleX("GrayScaleX", range(0,1)) = 0.299
  10. [PerRendererData] _GrayScaleY("GrayScaleY", range(0,1)) = 0.587
  11. [PerRendererData] _GrayScaleZ("GrayScaleZ", range(0,1)) = 0.114
  12. }
  13. SubShader
  14. {
  15. Tags
  16. {
  17. "Queue" = "Transparent"
  18. "IgnoreProjector" = "True"
  19. "RenderType" = "Transparent"
  20. "PreviewType" = "Plane"
  21. "CanUseSpriteAtlas" = "True"
  22. }
  23. Cull Off
  24. Lighting Off
  25. ZWrite Off
  26. Blend One OneMinusSrcAlpha
  27. Pass
  28. {
  29. CGPROGRAM
  30. #pragma vertex vert
  31. #pragma fragment frag
  32. #pragma target 2.0
  33. #pragma multi_compile _ PIXELSNAP_ON
  34. #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
  35. #include "UnityCG.cginc"
  36. struct appdata_t
  37. {
  38. float4 vertex : POSITION;
  39. float4 color : COLOR;
  40. float2 texcoord : TEXCOORD0;
  41. UNITY_VERTEX_INPUT_INSTANCE_ID
  42. };
  43. struct v2f
  44. {
  45. float4 vertex : SV_POSITION;
  46. fixed4 color : COLOR;
  47. float2 texcoord : TEXCOORD0;
  48. UNITY_VERTEX_OUTPUT_STEREO
  49. };
  50. fixed4 _Color;
  51. v2f vert(appdata_t IN)
  52. {
  53. v2f OUT;
  54. UNITY_SETUP_INSTANCE_ID(IN);
  55. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  56. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  57. OUT.texcoord = IN.texcoord;
  58. OUT.color = IN.color * _Color;
  59. #ifdef PIXELSNAP_ON
  60. OUT.vertex = UnityPixelSnap(OUT.vertex);
  61. #endif
  62. return OUT;
  63. }
  64. float _GrayLerp;
  65. float _GrayScaleX;
  66. float _GrayScaleY;
  67. float _GrayScaleZ;
  68. sampler2D _MainTex;
  69. sampler2D _AlphaTex;
  70. fixed4 SampleSpriteTexture(float2 uv)
  71. {
  72. fixed4 color = tex2D(_MainTex, uv);
  73. #if ETC1_EXTERNAL_ALPHA
  74. // get the color from an external texture (usecase: Alpha support for ETC1 on android)
  75. color.a = tex2D(_AlphaTex, uv).r;
  76. #endif //ETC1_EXTERNAL_ALPHA
  77. return color;
  78. }
  79. fixed4 frag(v2f IN) : SV_Target
  80. {
  81. fixed4 c = SampleSpriteTexture(IN.texcoord) * IN.color;
  82. c.rgb *= c.a;
  83. fixed gray = dot(c.rgb, fixed3(_GrayScaleX, _GrayScaleY, _GrayScaleZ));
  84. fixed newR = lerp(c.r, gray, _GrayLerp);
  85. fixed newG = lerp(c.g, gray, _GrayLerp);
  86. fixed newB = lerp(c.b, gray, _GrayLerp);
  87. c.rgb = fixed3(newR, newG, newB);
  88. return c;
  89. }
  90. ENDCG
  91. }
  92. }
  93. }