0
\$\begingroup\$

I have a pre-rendered 3D scene which I am rendering as a background image. Over this, I am rendering an invisible version of this scene only into the depth buffer, so that any other 3D object will be not be rendered if they pass behind an object in the 3D scene. As long as the camera remains static, the player will not notice that the scene is pre-rendered. I believe this method is called 'Depth Compositing', or something similar. It has been used in older games.

The problem I am having is that the pre-rendered background image is not showing through the invisible 3D scene being rendered to the depth buffer, like so:

Current

It should look similar to this:

Should

Here is a screenshot of the scene view to help visualize how this is setup:

Scene

The cube is rendered with this shader:

Shader "Custom/Depth"
{
    SubShader {
        Tags { "Queue" = "Geometry-10" }

        ColorMask 0
        ZWrite On

        Pass {}
    }
}

The background image is rendered in panel in a UI canvas, set to Screen Space - Camera. It is rendered in its own camera with a lower depth than the 3D object camera.

My assumption is this is to do with render ordering somehow, as perhaps the UI is being rendered after the 3D objects?

\$\endgroup\$
  • 1
    \$\begingroup\$ You're right, the UI is usually rendered after the 3D scene - particularly in screenspace modes. Have you tried placing your image on a quad instead of in a UI panel? \$\endgroup\$ – DMGregory Jun 16 '18 at 17:26
0
\$\begingroup\$

The issue was to do with render order - using the Canvas was causing the problem. Thanks to DMGregory's suggestion, using a Plane fixed the issue.

The final method I used is like this:

  1. Create two cameras: 'Background Camera', '3D Camera'
  2. Set both cameras is position 0,0,0 and rotation 0,0,0
  3. Set the depth of 'Background Camera' to 0 and '3D Camera' to 1
  4. Create two layers: 'Background', '3D'
  5. Set 'Background Camera' to only render the 'Background' layer; '3D Camera' to render the '3D' layer (and the other general layers)
  6. Create a Plane. Add the plane to the 'Background' layer
  7. Set the plane to position 0,0,1 and rotation 90,90,-90 and scale to 0.25,1,0.13
  8. Add the prerendered background to the plane's material
  9. Add the invisible scene to the '3D' layer

To visualize how this looked for me, here is a screenshot of my editor:

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.