Search Unity

Question A mirror with right depth for post processing

Discussion in 'Shaders' started by uotamic, Sep 5, 2022.

  1. uotamic

    uotamic

    Joined:
    Sep 30, 2020
    Posts:
    1
    I'm working on a scene using some post processing effects like depth of field. Now, I have to add a mirror into this scene, and I find that post processing effect relies on the camera depth texture. So, I need to make the reflected object have a right depth.

    I try to implement this by making a copy of object to be reflected, puting it with with right global positon and rotation according to the mirror plane, and use the stencil buffer to mask the objects. (Just like the example in https://docs.unity3d.com/Manual/SL-Stencil.html, one for writing stencil and one for read)

    The result is like this.

    屏幕截图 2022-09-05 125440.png

    And in the game view:

    屏幕截图 2022-09-05 125513.png

    Clearly there are artifacts. The reason for this is that stencil buffer doesn't work on the camera depth texture.

    屏幕截图 2022-09-05 125550.png

    This shows the camera depth texture after some transiformations at the post processing stage.

    I find the only way writing the camera depth texture is in the shadow caster pass. I can add stencil buffer there, but there's no way for my to make sure that the shadow caster pass of mirror object is the first to run.

    So, my questions are:
    Is it possible to make the shadow caster pass of mirror object run first? Or is there a properer way to implement such mirror?

    Thanks ahead of time.