GrayMesh.shader 2.3 KB

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