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
- [Android] [iOS] [UnityWebRequest] Requests with "UnityWebRequest" are open for SSL Proxying
- SetWindowsHookEx does not prevent Windows key input when Editor or Player Window is focused and Active Input Handling is set to “Input System Package (New)” or “Both”
- Crash on GetEffectiveBc7TextureCompressor() when loading and unloading all Assets in the Project
- Terrain Masks do not ignore the mipmap limit
- "Build And Run" the project for Web is allowed even though the directory for the non-default browser is not selected
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