How do I render two cameras at the same time while preserving depth?

Godot Version

4.5

Question

I’ve been working with post processing and shaders for the first time. Specifically I want to selectively pixelate a layer in my game while keeping everything else non-pixelated.

I’ve managed to hack together a couple of Compositor Shaders (not sure if that’s what they’re called?) which create the desired effect and separated rendering with layers, but I’m now stumped at how to render both my pixelated and non-pixelated views together.

Ideally I’d like to be able to sandwich non-pixelated objects between pixelated objects. This would require rendering based on depth, but I’m not exactly sure how to achieve this.

As of now my scene looks like this:

Pixelation
├── 🎥 Camera
├── ◼️ NormalCube
├── 🌍 WorldEnvironment
├── 💡 DirectionalLight3D
└── 🧩 SubViewportContainer
    └── 🖼️ MapViewport
        ├── 🎥 Camera
        ├── ◼️ Floor
        ├── ◼️ PixelCube
        └── 🌍 WorldEnvironment

The issue is that the second viewport is just rendering on top of the first, hiding it entirely.

Would anyone be able to nudge me in the right direction? Is this even possible?

I’ve pushed up a minimal reproduction here: GitHub - leo-petrucci/selective-pixelation-demo

You’ll need depth info for the viewport render, and then do depth-composite with the main viewport. Compositor shaders have access to the depth buffer so you just need to write the depth from there to a custom buffer and then use it as a texture in your main compositing shader. If you don’t need viewport alpha, you could store it there without the need for an extra buffer.

Haven’t actually tried this but I see no reason for it not to work.

1 Like