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

Why can't overlay cameras use viewport rect?

Discussion in 'Universal Render Pipeline' started by Matkins, Jul 3, 2020.

  1. Matkins

    Matkins

    Joined:
    Aug 24, 2009
    Posts:
    152
    I'm really struggling to achieve what I want with the camera stack system. But it wouldn't be difficult at all if only I could set the viewport rect of an overlay camera, rather than it always rendering fullscreen. Why can't I do that?

    I can't just make that camera a 2nd base camera either because it's not the topmost camera, it nestles in between overlays.

    I'm beginning to think that my only option is to use a render texture, but I really don't want to do that because it would introduce all sorts of other issues. Are there any other options available to me?
     
  2. Kujo87

    Kujo87

    Joined:
    Sep 16, 2013
    Posts:
    160
    bump - looking into this as well and can't make it work, but works fine in built in renderer
     
  3. RemiOuterminds

    RemiOuterminds

    Joined:
    Mar 23, 2017
    Posts:
    10
    Same, looking to add an overlay camera in between other overlay cameras, but not have it render to the whole screen, only a viewport.
     
  4. HugeWill

    HugeWill

    Joined:
    Nov 4, 2016
    Posts:
    27
    Yep same here!
     
  5. silentslack

    silentslack

    Joined:
    Apr 5, 2013
    Posts:
    379
    Another here. Is there really no solution here?
     
  6. dowon951

    dowon951

    Joined:
    Jun 24, 2019
    Posts:
    12
  7. Sevdanski

    Sevdanski

    Joined:
    Nov 14, 2013
    Posts:
    5
    I've hit the same issue. I was resizing the viewport rect of the scene view whenever a menu slid in from the side - resizing it like this kept the scene centered.
    Now the code is adjusting the size of the camera (and culling elements) but it does not respect the x/y position of the rect, which means it is always centered, and objects are getting culled.
    So far, all combinations I have tried have failed
     
  8. HugeWill

    HugeWill

    Joined:
    Nov 4, 2016
    Posts:
    27
    Yep same issue here! Still no solution?
     
  9. weiping-toh

    weiping-toh

    Joined:
    Sep 8, 2015
    Posts:
    186
    There is no out-of-the box solution for this.
    In fact, the ScriptableRenderer hard-coded the clearing of the renderTarget for the cameras, so by changing the overlay viewport would expose the area that was not rendered and it being cleared of contents.
     
  10. Repixel8

    Repixel8

    Joined:
    Sep 29, 2016
    Posts:
    1
    Same problem here too. After moving our project to URP, this is our last remaining issue. Tried using render textures as a workaround, but this seems to introduce a whole new range of issues. Really don't want to have to switch back to the built-in pipeline after the time invested in updating.
     
  11. Andre_Mcgrail

    Andre_Mcgrail

    Unity Technologies

    Joined:
    Dec 9, 2016
    Posts:
    244
    Hey all,

    We are looking into adding this to the Camera Stacking in URP, just as with everything it needs special consideration for how it will work across the range of devices/APIs that we support, UX, feature compatibility, as well as other work taking priority, but it is on our radar for sure, you're not the only ones asking for this feature.

    In the meantime you can achieve a similar result with RenderTextures but as some of you have noted that can introduce other issues to solve, and is not a silver bullet(nothing generally is). Another way to get there now is using the stencil buffer, essentially making a panel where you want the viewport to be and setting it to a value that the overlay camer will only render to using the stencil buffer as a masking system.

    I'va attatched a small sample(made very quickly so there are probably cases that are not tested) that does this, it has a custom renderer assigned to the middle overlay, that renders a quad before anything and sets the setencil value, then everything later will only render where that panel was rendered on screen. This approach is not ideal if you are doing large crops as you are still culling the original fullscreen, but it also has the upside of having some interesting crop shapes as it is just rendering an object, this could be a circle, a logo, or anything really, another side effect is that Depth is cleared, this may be avoided but I didnt get time to see if this was a possibility.

    Screenshot_494.png

    Project Zip
     
    silentslack, GAAC_Unity and UnityMaru like this.
  12. deus0

    deus0

    Joined:
    May 12, 2015
    Posts:
    256
    Are there any performance hits when using render textures, compared to say using camera's rect? Having similar issues with default camera rects not really working at all.
    upload_2021-3-3_3-0-39.png
    Unity seems to just draw one camera or the other, and then completely go bonkas for the rest of the screen.
     
  13. LexGear

    LexGear

    Joined:
    Sep 24, 2012
    Posts:
    11
    I regret switching to the URP. I thought it was more complete than this.
     
  14. Aliosa

    Aliosa

    Joined:
    Dec 20, 2013
    Posts:
    8
    Unity! are you going to fix this thing or not?
     
  15. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Wow....No fix!!??
    How can one make a game map?
     
  16. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
    Damn, really need this feature, it's quite painful not having it. I've been fumbling around with a workaround of using a base camera render to a texture, but URP seems to have a bug where performance is horrendous for rendering two cameras, even with proper culling masks and ultra-low-quality renderer on the overhead camera
     
  17. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
    FYI this does not work in Unity 2022 anymore sadly
     
  18. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
    Hey guys, I figured this one out finally, initially inspired with the help of this guy's post: https://forum.unity.com/threads/ove...lane-with-custom-shader.1088242/#post-7022965

    The meat of the solution is based around this tutorial: https://danielilett.com/2022-01-05-tut5-22-impossible-geom-stencils/

    For example, let's say we have a minimap as shown below

    upload_2023-3-26_13-42-52.png

    A bunch of stuff is flowing outside of it that we want to crop out. So what I do is I create a huge quad and place it in the sky over my entire game map so it covers the overhead camera's view precisely where I would want it cropped to:
    upload_2023-3-26_13-46-32.png

    upload_2023-3-26_13-46-48.png


    Then apply the following shader to the quad:

    Code (CSharp):
    1.  
    2. Shader "Custom/Mask"
    3. {
    4.    Properties
    5.    {
    6.       [IntRange] _StencilID ("Stencil ID", Range(0, 255)) = 0
    7.       [Enum(Depth Only, 0, Show Color, 15)] _ColorMask ("Color Mask", Float) = 0
    8.    }
    9.  
    10.    SubShader
    11.    {
    12.  
    13.       Tags
    14.       {
    15.          "RenderType" = "Opaque"
    16.          "RenderPipeline" = "UniversalPipeline"
    17.          "Queue" = "Geometry"
    18.       }
    19.  
    20.       Pass
    21.       {
    22.          ZWrite Off
    23.  
    24.          Stencil
    25.          {
    26.             Ref [_StencilID]
    27.             Comp Always
    28.             Pass Replace
    29.             Fail Keep
    30.          }
    31.    
    32.          ColorMask [_ColorMask]
    33.       }
    34.    }
    35. }
    36.  
    Set its stencil ID to some value that isn't 0, for example 1

    Put the quad on its own Layer, for example a new layer called "StencilMask"

    Next, go to your URP Asset and add a new item to the "Renderer List", for example called "MinimapRenderer"

    Now carefully configure it so the Layer Mask is only your Stencil, and then create a new "Render Objects"
    feature that renders anything with a stencil value of 1.

    upload_2023-3-26_13-59-25.png

    Go to your overhead camera and set the "Renderer" to this new MinimapRenderer. You're all done and you have a perfectly cropped overlay camera!
    upload_2023-3-26_14-1-29.png

    This works because the Stencil Value for everything is 0 by default. You are telling the renderer "Only show things with a Stencil Value of 1" which makes everything disappear. Then the shader on your Quad says "Set the stencil value of this entire region to 1", so your renderer is able to see everything in the line of sight of that shader quad.

    NOTE:
    If you are rendering sprites that you also want to be affected by this, you need to duplicate this "Render Objects" feature and set the Filters -> Queue to "Transparent" because it would seem sprite renderers are not considered part of the Opaque queue

    -- FYI
    If you're actually using this for a minimap like I am, then you will want to add further optimizations to improve the performance of your overlay camera, such as only rendering certain minimap-specific layers (e.g. some sprite layer and lower resolution map) rather than rendering everything. But that is unrelated to this discussion here.
     
    Last edited: Mar 26, 2023
    Hozgen90 likes this.
  19. whyatme

    whyatme

    Joined:
    Apr 4, 2021
    Posts:
    20
    Accky likes this.