Object rendered by overlay camera no shadow from other objects on other camera

I had an object rendered by an overlay camera, and then be overlaid on top of the base camera. This worked perfectly, until I noticed the object rendered by the overlay camera receives no shadows from other objects (rendered by the base camera). It only receives self shadows.

The intended effect (with exaggerated light intensity)
The current effect
I did it like this to deal with object clipping.
The wanted effect with overlay and base camera
The result with having the base camera render both cannon and the scene
Is there another way to do it? Perhaps have the cannon have a different shader that is very much like the URP Lit shader, except that it always renders on top of the camera’s resulting image? (this to prevent it from rendering on top of the helmet geometry, which is rendered by another overlay camera)

1 Like

bump, i would like to know the anwser too, i dont know how to solve this problem

1 Like

I think I accidentaly stumbled acrossed the solution while messing with urp. Instead of telling one camera to cull everything but the gun, create a secondary urp renderer that renders only the gun layer and tell the camera to use that.

4 Likes

Could you please explain this setup in more detail? My current setup has one base camera and an overlay camera, where the base camera renders everything but the gun, and the overlay camera renders only the gun. Then there’s also a third overlay camera rendering only the helmet.

How would this differ from your setup? Currently not seeing the difference

If you take a look in the Frame Debugger you’ll see that each of the cameras in the camera stack has its own render process. This means that each camera has their own MainLightShadow pass, which means that any object only rendered by one camera will not be present in the shadowmap of the other.
My advice would be to ditch camera stacking (at least for the gun camera) and instead draw the gun using the RenderObjects renderer feature. This will make sure that the gun is drawn with the shadowmap that the environment is drawn into. Make sure to draw the gun at a late pipeline event (like BeforeDrawingPostProcessing) and make sure that the depth test is set to Always to avoid clipping with the environment.
Hopefully that solves it for you :slight_smile:

1 Like

One thing to be aware of with the above solution;
You will get shadows on the gun when it is inside the environment geo, even though it’s forced to not clip.

2 Likes

Will have a look into using RenderObjects, thank you! Should that give worse results, it isn’t the worst thing in the world. Just curious if it was possible

Want to change fov for the gun when aiming for example throw script in real time how can I do that?

@ahmadgml32
You can modify the renderer (UniversalRendererData) in use by assigning it to script:

public UniversalRendererData rendererData;

void Start()
{
    RenderObjects ro = rendererData.rendererFeatures.OfType<RenderObjects>().FirstOrDefault();

    if(ro != null)
    {
        ro.settings.cameraSettings.cameraFieldOfView = 120;
    }

    rendererData.SetDirty();
}

Currently there is no API for getting a reference to the current Universal Renderer completely from script. You need to assign it in the editor

This is the only solution after hours of digging that really works. I wonder if there is overhead because the weapon-only rendering camera still has Everything in its culling mask, but its renderer doesn’t include those layers (this way shadows work).
Thank you dawg. I needed a camera stack solution to have a different FOV for my weapons. (the render objects FOV solution was being weird for me and didn’t work with the SSAO renderer feature)

1 Like

really is the only solution that works. the render object renderer feature with depth test at always just make my gun model looks like an xray image, the cylinders and cubes that it was made up of gets drawn over the actual mesh. Kind of a bummer that this method (extra renderer) makes the scene view camera to not draw the gun. I am also curious if having extra renderers impacts performance though.

Thanks! It works for me, and I learned some cool effect during learning it.

For someone do not understand what he is talking about, you can try this example below and you are ready to go.

Example: How to create a custom rendering effect using the Render Objects Renderer Feature | Universal RP | 13.1.9 (unity3d.com)

1 Like

So, thought I would put my two cents in here and help reaffirm some stuff about all this. After much tweaking and trying different approaches heres my thoughts:
-The render objects technique: Probably the most efficient since it is all done on one cameras rendering pipeline but has the major drawback of having to put the depth test to always which include the objects on that render objects layer so if you just have one object its fine but if you have mutlitple the draw order is all wack
-the camera stack(as described in OP): on the surface seems to be the intended solution for this whole thing but what I’ve learned is that each camera in the camera stack goes through the entire pipeline (including post processing) then they get added on top, so not to useful in my opinion
-finally the best solution but worst for performance is the solution where you have a base camera with culling mask of everything but using the render pass without the player layer, then have an overlay camera also with culling mask of everything with a render pass thats just the player layer. People were discussing in earlier threads about performance and yeah as I understand it it means that everything is being rendered twice and only taking the layers in each render pass, Im not sure though since it would make sense that it “culls” based on the render passes layers

Overall I would say this is all insanely over-complicated and seems like URP has way too many options that all do basically the same thing but with slight differences instead of one unified approach thats flexible enough, Im no expert though. Hope this helps anyone since this took me a bit to figure out, good luck!
0