Search Issue Tracker
By Design
Votes
0
Found in
2022.3.16f1
Issue ID
UUM-59308
Regression
No
Stencil buffer doesn't work in Build when using ECS
How to reproduce:
1. Open the attached project "Unity.zip"
2. Build And Run the project with "StencilBufferTest.unity" Scene
Expected results: Stencil buffer works in the Player
Actual results: Stencil buffer doesn't work in the Player
Reproducible with: 2022.3.16f1 (1.0.16, 1.2.0-pre.6)
Reproducible on: Windows 11 22H2
Not reproducible on: No other environment tested
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- [URP] Invalid update of shader stripping settings
- [HDRP] Errors on Shader stripper that do not stripp shaders.
- Usage of Adaptive Probe Volumes throws a lot of IndexOutOfRangeException and Assertion Failed errors
- Prefab overrides don't recover missing managed references
- Trail Renderer visual doesn't match the positions of vertices when they have been offset
Resolution Note:
There are 2 objects in the scene each with it's own material and shader, both being drawn in the opaque pass.
The "mask" draws to the stencil buffer and to the depth buffer while the "space" object checks the stencil and always writes to the depth buffer without checking it (ztest always).
Due to the nature of the depth buffer settings, there is no preferred order drawing since front to back is not going to be valid. Given there's no preferred order of drawing, the SRP batcher optimizes this for the least amount of state changes which orders by material id and shader id. Since these ids are not guaranteed to be the same between editor and player the order cannot and should not be guaranteed to stay the same. The same issue can be reproduced in the editor by duplicating the shaders and materials and finding a combination in which the order of drawing is reversed, regardless of the objects being in a subcene or not.
One easy way to fix this is to make use of the queue order in the material. By default it is set to 2000 but can be edited to force a specific ordering.
Another thing that is worth mentioning is that having the "space" shader use ztest always and zwrite yes is peculiar and depending on the exact use case could also be a mistake