Search Issue Tracker

By Design

Votes

3

Found in [Package]

7.5.1

Issue ID

1321988

Regression

No

[URP] Overlay Stack Camera causes all stencil tests to pass when custom post effects are used

Package: Scriptable Render Pipeline Universal

-

Reproduction steps:
1. Open the attached project ("stencilPostEffectTest.zip")
2. Open the "SampleScene" scene
3. Select StackCamera Game object and enable Camera component
4. Open the Game window

Expected result: Only the left cube is red
Actual result: Everything in the game window is red

Reproducible with: URP 7.5.1, 12.0.0 (2019.4.24f1, 2020.3.4f1, 2021.1.3f1, 2021.2.0a12)
Could not test with: 2018.4.33f1 (URP not available)

  1. Resolution Note:

    First off, blit should not be used with the same target for both source and destination. But the actual issue here is depth not getting bound properly. This happens because using an overlay camera causes URP to create a depth texture rather than using one that is implicitly created with the backbuffer, and that makes it incompatible with Blit. This is because Blit has no way to specify a separate depth texture, but will only use one if it's implicitly created with the source texture.

    In this case, the supplied StencilTestShader doesn't read from the source, and so it does not have to use Blit. Instead, you can draw a full-screen triangle like this:
    var camera = renderingData.cameraData.camera;
    cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
    cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, stencilTestMaterial);
    cmd.SetViewProjectionMatrices(camera.worldToCameraMatrix, camera.projectionMatrix);
    This doesn't touch the currently bound render targets, so the stencil buffer stays bound, and makes the post-process effect work correctly.

Comments (2)

  1. Jar_Coding

    Aug 07, 2023 06:29

    Can you please add an working example here or to the documentation. Because noone but the author has access to the stencilPostEffectTest.zip and therefore its hard to follow.

    We currently have a similar problem and the issue here is almost the same.

  2. MasayaTakahashiDayo

    Jun 29, 2021 06:25

    Thank you for telling me the detailed solution.
    I appreciate it very much.

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.