I’m using Unity 2021.1.2f1 and HDRP version 11.0.0
I’m attempting to use a Custom Pass and CustomPassUtils.RenderFromCamera() to render a bunch of objects from a separate top-down orthographic camera to a render texture.
The objects I need to render in this top-down camera are NOT visible to the Main Camera, they’re in a layer that’s excluded from the Culling Mask of the Main Camera, and only the top down camera should be able to render.
My Custom Pass is pretty simple. I have a second incactive camera I’d like to render from (topDownCamera), a render texture to render to (topDownRenderTexture), and a culling mask containing the objects to render (topDownLayerMask).
Then the Execute function is just
protected override void Execute(CustomPassContext ctx)
{
CoreUtils.SetRenderTarget(ctx.cmd, topDownRenderTexture, ClearFlag.All);
CustomPassUtils.RenderFromCamera(ctx, topDownCamera, topDownLayerMask);
}
RenderFromCamera() uses the correct position and orthographic projection of my top down camera, but it does not use the correct aspect ratio, which is set to 1 on my top down as I’m rendering to a 512x512 square texture.
Instead it uses the aspect ratio of the Main Camera (or scene camera). That’s annoying, but it’s not a huge problem as I can compensate for an incorrect aspect ratio.
The big problem is that it won’t render anything on the topDownLayerMask if the main camera isn’t also rendering it. The objects to render get culled, either by the layer mask or by frustum culling of the main camera.
Now at first I thought it was working fine, but it was actually only working in the editor because I have a Scene window open, and the scene camera was allowing the objects to avoid culling.
With the scene window closed, or with
if (ctx.hdCamera.camera.cameraType == CameraType.SceneView)
return;
included at the start of the Execute function, then nothing renders.
Is there a way I can force the objects in the topDownLayerMask to render duing RenderFromCamera, even though they’re not visible in the main camera?
I assumed because RenderFromCamera takes a LayerMask that this would be a non-issue, but it’s not working for me.
Is there something obvious I’m missing? Is there a better way to achieve this?
More Info:
Custom Passes is my third attempt at this problem.
Previously I had it working simply using a second camera, but in HDRP this adds >1.5ms of additional render time, even if all the Custom Frame Settings > Rendering flags are set to false, which I assume is meant to streamline the camera.
Then I had it (almost) working by using Command Buffers and drawing the objects with commandBuffer.DrawRenderer(). But in HDRP, commandBuffer.SetViewProjectionMatrices() doesn’t work, so I can’t render from the viewpoint of my top-down camera.
If either of these previous methods can work with a slight tweak, (either streamlining the use of a second camera, or a working version of commandBuffer.SetViewProjectionMatrices) please let me know, as they’d be preferable to using Custom Passes.