Guest User

SpriteColorMask.shader

a guest
Jul 17th, 2020
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Sprites/Masked"
  2. {
  3.     Properties
  4.     {
  5.         [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  6.        
  7.         _ColorPrim("Primary Color (R)", Color) = (1,1,1,1)
  8.         _ColorSec("Secondary Color (G)", Color) = (1,1,1,1)
  9.         _ColorTert("Tertiary Color (B)", Color) = (1,1,1,1)
  10.         [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
  11.         [MaterialToggle] KEEPBLACK("Keep Black Pixels", Float) = 0
  12.  
  13.         // stencil for UI integration
  14.         [HideInInspector] _StencilComp("Stencil Comparison", Float) = 8
  15.         [HideInInspector] _Stencil("Stencil ID", Float) = 0
  16.         [HideInInspector] _StencilOp("Stencil Operation", Float) = 0
  17.         [HideInInspector] _StencilWriteMask("Stencil Write Mask", Float) = 255
  18.         [HideInInspector] _StencilReadMask("Stencil Read Mask", Float) = 255
  19.  
  20.         [HideInInspector] _ColorMask("Color Mask", Float) = 15
  21.     }
  22.  
  23.         SubShader
  24. {
  25.     Tags
  26.     {
  27.         "Queue" = "Transparent"
  28.         "IgnoreProjector" = "True"
  29.         "RenderType" = "Transparent"
  30.         "PreviewType" = "Plane"
  31.         "CanUseSpriteAtlas" = "True"
  32.     }
  33.  
  34.     Cull Off
  35.     Lighting Off
  36.     ZWrite Off
  37.     Blend One OneMinusSrcAlpha
  38.  
  39.     Pass
  40.     {
  41.              Stencil
  42.             {
  43.                 Ref[_Stencil]
  44.                 Comp[_StencilComp]
  45.                 Pass[_StencilOp]
  46.                 ReadMask[_StencilReadMask]
  47.                 WriteMask[_StencilWriteMask]
  48.             }
  49.     CGPROGRAM
  50.         #pragma vertex vert
  51.         #pragma fragment frag
  52.         #pragma multi_compile _ PIXELSNAP_ON
  53.         #pragma multi_compile _ KEEPBLACK_ON
  54.         #include "UnityCG.cginc"
  55.  
  56.         struct appdata_t
  57.         {
  58.             float4 vertex   : POSITION;
  59.             float4 color    : COLOR;
  60.             float2 texcoord : TEXCOORD0;
  61.         };
  62.  
  63.         struct v2f
  64.         {
  65.             float4 vertex   : SV_POSITION;
  66.             fixed4 color : COLOR;
  67.             float2 texcoord  : TEXCOORD0;
  68.         };
  69.  
  70.         fixed4 _Color;
  71.  
  72.         v2f vert(appdata_t IN)
  73.         {
  74.             v2f OUT;
  75.             OUT.vertex = UnityObjectToClipPos(IN.vertex);
  76.             OUT.texcoord = IN.texcoord;
  77.             OUT.color = IN.color * _Color;
  78.             #ifdef PIXELSNAP_ON
  79.             OUT.vertex = UnityPixelSnap(OUT.vertex);
  80.             #endif
  81.  
  82.             return OUT;
  83.         }
  84.  
  85.         sampler2D _MainTex;
  86.         sampler2D _AlphaTex;
  87.         float _AlphaSplitEnabled;
  88.         float4 _ColorPrim, _ColorSec, _ColorTert;
  89.        
  90.         fixed4 SampleSpriteTexture(float2 uv)
  91.         {
  92.             fixed4 color = tex2D(_MainTex, uv);
  93.  
  94.  
  95. #if UNITY_TEXTURE_ALPHASPLIT_ALLOWED
  96.                 if (_AlphaSplitEnabled)
  97.                     color.a = tex2D(_AlphaTex, uv).r;
  98. #endif //UNITY_TEXTURE_ALPHASPLIT_ALLOWED
  99.  
  100.                 return color;
  101.             }
  102.  
  103.             fixed4 frag(v2f IN) : SV_Target
  104.             {              
  105.                 // sprite texture
  106.                 fixed4 c = SampleSpriteTexture(IN.texcoord);
  107.                
  108.             // all color channels multiplied with a new color
  109.                 float4 colorPrim = c.r * _ColorPrim;
  110.                 float4 colorSec = c.g * _ColorSec;
  111.                 float4 colorTert = c.b * _ColorTert;
  112.                 // added together
  113.                 float4 colorResult = colorPrim + colorSec + colorTert;
  114.                 // keep black color for outlines
  115. #ifdef KEEPBLACK_ON
  116.                 colorResult += c * (1 - (c.r + c.g + c.b));
  117. #endif
  118.                 // multiply with alpha
  119.                 colorResult *= c.a;
  120.                 return colorResult;
  121.             }
  122.         ENDCG
  123.         }
  124. }
  125. }
Add Comment
Please, Sign In to add comment