Custom sprite shader with both greyscale and masking
I need a sprite which I can smoothly desaturate, but which ALSO works with masks. There are numerous answers online for how to either make sprite shaders which work with masks, or for shaders which have a greyscale slider, but for some reason these seem to be mutually exclusive. Adding the lines which make masks work, breaks the greyscale.
I'm a programmer but I know basically nothing about shaders. Here's what I've made from cobbling together different answers I've found. (It's probably far too long.)
Note that the lines which "toggle" the mask/greyscale functionality are those which refer to "_StencilComp."
Shader "Custom/GreyScaleSpriteShader"
{
Properties
{
_GrayScale("Saturation", Range(0.0, 1.0)) = 0.0
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
[HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
[PerRendererData] _AlphaTex("External Alpha", 2D) = "white" {}
[PerRendererData] _EnableExternalAlpha("Enable External Alpha", Float) = 0
// required for UI.Mask
[HideInInspector] _StencilComp("Stencil Comparison", Float) = 8
[HideInInspector] _Stencil("Stencil ID", Float) = 0
[HideInInspector] _StencilOp("Stencil Operation", Float) = 0
[HideInInspector] _StencilWriteMask("Stencil Write Mask", Float) = 255
[HideInInspector] _StencilReadMask("Stencil Read Mask", Float) = 255
[HideInInspector] _ColorMask("Color Mask", Float) = 15
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
// required for UI.Mask
Stencil
{
Ref[_Stencil]
Comp[_StencilComp]
Pass[_StencilOp]
ReadMask[_StencilReadMask]
WriteMask[_StencilWriteMask]
}
ColorMask[_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ GRAYSCALE_ON GRAYSCALE_OFF
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
float _GrayScale;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap(OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
c.rgb *= c.a;
if (IN.texcoord.y > _GrayScale)
{
fixed avg = (c.r + c.g + c.b) / 3;
c.rgb = avg;
}
return c;
}
ENDCG
}
}
}
Answer by akb165 · Jul 06, 2018 at 05:31 AM
Not sure if this is the way you are going but this is what i used for my 3d game. I am sure it can be modified to work in 2d.
https://www.youtube.com/watch?v=Ws4ukvCgTOU∈dex=2&list=PL3POsQzaCw50pR7Wc6NKw9NgVJ9Y629Z7
Answer by Draikz · Apr 16, 2019 at 01:03 PM
Are we having the same issue?
https://answers.unity.com/questions/1620941/image-showing-out-of-mask-when-applying-shaderimag.html