Transparent Shader with Stencil 'Comp NotEqual' sorting order no longer works?
Hello, I am fairly new to Unity so bare with me please. I am creating randomly generated tilemap in 2d, I am trying to add 'shadows' (semi transparent texture) to my mountains. I have created each layer of mountains as MeshRenderers generating a large mesh in seperate layers. And a seperate MeshRenderer shadow for each on the layer below. I have the Sorting Order of these MeshRenderers exposed in the Editor and was using that to set their order. Using the shader I was originally using (Unlit/Transparent) the shadows overlapping each other become darker. (stacking semi transparent sprites). So I read into creating a custom shader using a Stencil, with:
Stencil {
Ref 1
Comp NotEqual
Pass Replace
}
in place, its almost working, the shadows all join together rather than 'overlapping', but my sorting order for the seperate MeshRenderers are all on the bottom level. So the shadows are all entirely under my first level of mountains. See images! Hope this makes sense.
With default 'Unlit/Transparent' shader:
With my custom shader & stencil:
Thanks for any assistance in advanced!
Answer by pepsipwns92 · Dec 28, 2021 at 06:41 AM
I have almost got it I think through pure fluke. I have added a Stencil to the 'mountain' shader as follows:
Stencil {
Ref 2
Comp NotEqual
Pass IncrSat
}
And my shadow shader like:
Stencil {
Ref 1
Comp NotEqual
Pass Replace
}
And it has given me almost the result I want! Where the shadows are combined into one & on the correct layers. However now the transparent parts are not cutting out with the stencil, so the shadows from aboves 'edges' are showing lines where the transparency should be. See image:
[Solved] For anyone wondering: By modifying my 'Pass' section in the Subshader:
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.texcoord);
**clip(col.a - 1.0 / 255.0);
col.rgb *= col.a;**
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
Your answer
Follow this Question
Related Questions
Add distance fade to his shader 1 Answer
Problems with stencil / spawn objects @ stencil position 0 Answers
Hide parts of a mesh that are behind itself 0 Answers
Problem with transparency of an object 0 Answers
Circular Bar Transparent gradient 0 Answers