- Home /
Is it possible to have multiple stencil reference values in one shader?
I'm fairly new to Unity and C#, let alone tampering with shaders. At the moment I have the result I want, but I think it can be done more neatly/optimized.
I have multiple identical shaders with each its own stencil reference value. Is there a way to combine them, as in having multiple reference values in one shader or another way of optimizing my shaders?
I'm not experienced enough to create my own shader, so I just tried editing an existing shader (Sprite-Diffuse) and applied that to my sprites.
What I have are multiple layers of sprites (e.g. body, hair, clothing, etc) all forming one character. When the character collides with another sprite (e.g. blood), I want the second sprite to stick onto the character on the right layer and get cut off at the edges of the sprite.
I now have a mask shader with a reference value in the stencil buffer for each layer (body, hair, clothing) and use the same reference value in the shader of the other sprite
In the mask shader I have:
Stencil
{
Ref 1
Comp always
Pass replace
}
and in the void surf of that shader:
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
if (c.a<0.1) discard;
in the shader of the particles I have the same reference value in the stencil buffer, like this:
Stencil
{
Ref 1
Comp Equal
}
Now I have multiple shaders with each its own reference value in the stencil buffer, but with the rest of the script completely identical. All works great, but I don't like to have identical scripts working together. Is there a way to have multiple reference values in the stencil buffer? Or is there an other way I could optimize my script?
Thank you for reading
I'd really love to know the answer to this. If you figure it out on your own, please update here!
Your answer
![](https://koobas.hobune.stream/wayback/20220613201512im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Deferred Light Rendering: Prevent additional lighting passes on pixels in overlapping point lights 0 Answers
Are shaders more efficient than manual pixel replacement? 0 Answers
My shader doesn't work if I use the same material for multiple objects with the same texture. 1 Answer