Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Camera stacking, post-processing and 2D lights

Discussion in 'Universal Render Pipeline' started by SMT5015, Jan 9, 2023.

  1. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    I'm making a 2D game and found that blurring backgrounds trough global depth of field effect doesn't work as expected: it just blurs everything pretty much equally. Then I turned to overlay cameras (by the way, can URP's post-processing work not on all cameras except through local volume with a mask?), but now my 2D lights broke. The ones which are rendered by the overlay camera appear flipped vertically and follow the player in Y direction, like this:



    Is there a way to make it all match?

    Is it possible to stop the overlay camera from misrendering 2D lights? I don't think I'm ready to sacrifice them.
    If I should return to a single camera, how can I apply certain effects to all background objects without too much manual work (like beyond assigning all of them certain layer and sorting layer)?
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    I don't have any good news. :( And all of it requires substantial work.
    Despite the option on the 2D Renderer Data asset, the depth texture is specifically broken. And even if you were to fix that, adding depth would require forking the 2D Renderer and many more changes.
    The best option for this at the moment is materials/shaders for the sprites, but that can only go so far.

    Another readily available option is multiple cameras with Renderer Features:
    With this approach, you output your background objects with a 2nd camera to a RenderTexture by targeting a Layer (not Sorting Layer... a Layer), then blur that texture (giving you pseudo-depth of field), then display that RenderTexture in scene on some full-screen sprite that is correctly ordered behind your foreground sprites.

    The deeper problem is with the 2D Renderer itself. It shoots itself in the foot, preventing more expressive Renderer Features, which (again, requiring fork) could be used to avoid the second camera, saving substantial culling costs from the 2nd camera.
     
  3. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Then materials it is. Or they are? Doesn't matter. Since I'm going to lean on copypastable background objects anyway, guess prefabs will do.

    Hmm. I tried some KawaseBlur thing, but that version still required camera stacking. So separate camera with texture but not overlay should not make lights derp?
     
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    It should be a Base camera output to a Render Texture. Sample that RT in a shader / plug it into a Material. Then use a full-screen mesh/sprite. The issue with the Overlay camera may be a Unity bug.
     
  5. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    It worked. Kinda. Since camera has a clipping plane, the texture has to be placed at some distance before it, which causes additional parallax effect and makes backgrounds harder to place correctly. Also the trick with duplicated lights for different distance backgrounds doesn't work too, but now in different way: the lights on the texture are displaced due to this additional parallax. Now materials are the only way, unless I find someone to modify the renderer.
     
  6. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Is your full-screen sprite childed to main camera (thus no parallax relative to camera), and has sorting layer/order sending it to the back?

    Lights: you have the choice between targeting the full-screen blurred sprite versus targeting background sprites as they're rendered before the blur.
     
  7. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    ...okay, turned out fileds of wiev were somehow mismatched, despite the camera being used as an overlay before. Now it seems to work. There are two questions, last for now, I hope.

    1) How much does this workaround hurt performance? Is it better than shaders or worse?
    2) Should I change my RenderTexture's resolution manually to match the screen resolution?... Ok, this is not supported, then what should I do when the resolution changes?
     
    Last edited: Jan 11, 2023
  8. SMT5015

    SMT5015

    Joined:
    Apr 5, 2021
    Posts:
    53
    Update!

    First, the method with base camera rendering background on a texture has it own problems. The background becomes distorted depending on resolution. How do I properly scale render textures to avoid it?

    Second, it seems that the initial light issue is, at least partly, caused by Cinemachine. I have a light source on my player character, positioned 2 units above it. Cinemachine virtual camera is positioned 15 units above the character, and its child overlay camera with local position of (0, 0, 0) displays the light also 15 units above (it comes to normal when moved to (0, -13, 0)). Weird.
     
  9. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Have you tried using the render texture API to rescale this render texture to match or be half/quarter-rez the camera? I forget what the rules are for Render Textures that are also assets.