Why can't overlay cameras use viewport rect?

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?

4 Likes

bump - looking into this as well and can’t make it work, but works fine in built in renderer

Same, looking to add an overlay camera in between other overlay cameras, but not have it render to the whole screen, only a viewport.

Yep same here!

Another here. Is there really no solution here?

here my plan

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

Yep same issue here! Still no solution?

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.

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.

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.

Project Zip

4 Likes

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.


Unity seems to just draw one camera or the other, and then completely go bonkas for the rest of the screen.

I regret switching to the URP. I thought it was more complete than this.

Unity! are you going to fix this thing or not?

Wow…No fix???
How can one make a game map?

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

FYI this does not work in Unity 2022 anymore sadly

Hey guys, I figured this one out finally, initially inspired with the help of this guy’s post: Overlay camera not rendering plane with custom shader - Unity Engine - Unity Discussions

The meat of the solution is based around this tutorial: Impossible Geometry with Stencils in Unity URP

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

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:

Then apply the following shader to the quad:

Shader "Custom/Mask"
{
   Properties
   {
      [IntRange] _StencilID ("Stencil ID", Range(0, 255)) = 0
      [Enum(Depth Only, 0, Show Color, 15)] _ColorMask ("Color Mask", Float) = 0
   }

   SubShader
   {

      Tags
      {
         "RenderType" = "Opaque"
         "RenderPipeline" = "UniversalPipeline"
         "Queue" = "Geometry"
      }

      Pass
      {
         ZWrite Off

         Stencil
         {
            Ref [_StencilID]
            Comp Always
            Pass Replace
            Fail Keep
         }
   
         ColorMask [_ColorMask]
      }
   }
}

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.

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!
8904879--1218738--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.

1 Like

Note I don’t think hack is necessary anymore.

Previously we had to do this because render textures were really bad: Terrible Render Texture URP Performance (2022.2)

But now the performance problems seem to have been fixed Unity Issue Tracker - UI is drawn twice in the URP project

So I recommend using render textures instead. You can still the stencil trick to only render what you want in your minimap

1 Like

Hi, are there any news on this feature?
Is this on the roadmap or was it considered and dropped?

Being able to position and resize the overlay camera like we used to in the Built-In renderer would be very nice. My concrete use case is transparent materials. In render textures those always result in pre-multiplied alpha and thus do not blend well in UI (with default shaders). Using overlays this could be handled without the need for extra shaders that take pre-multiplied alpha into account.

1 Like