- Home /
Strange issue with stencil buffer and draw order
Im drawing two quads, A which is opac black and B which is transparent black. They are using the same shader with a stencilbuffer and an alpha-parameter. I want B to not draw over A, which it is as you can see in the example image. I use the stencilbuffer to avoid drawing internally overlapping triangles. My real case is more complex than this example image but this is basically the problem I get. It doesnt matter what order Im drawing the quads in or which z-order they are in. B is always drawing over A. This seem almost impossible to me but apparently not. Anybody understand whats wrong here?
Shader "My/GuideLine" {
Properties {
_Opacity ("Opacity", Range (0.0, 1.0)) = 1.0
_MainTex ("Texture", 2D) = "white" {}
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Stencil {
Ref 10
ReadMask 10
Comp NotEqual
Pass Replace
}
Blend SrcAlpha OneMinusSrcAlpha
Lighting Off
ZWrite Off
Cull Off
SetTexture [_MainTex]
{
constantColor (1, 1, 1, [_Opacity])
combine texture * constant
}
}
}
}
Graphics.DrawMesh(A.Mesh, _matrix, A.Material, 0, gameObject.camera, 0);
Graphics.DrawMesh(B.Mesh, _matrix, B.Material, 0, gameObject.camera, 0);
UPDATE: The problem has been solved but for the record I didn't delete this post. It could be helpful for others.
The solution: It doesnt matter in what order the DrawMesh is called BUT in what order I have created my mesh-geometries! This was news to me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613184235im_/https://answers.unity.com/themes/thub/images/avi.jpg)