Issue with stacked SubViewports and screen space Spatial shaders

Godot Version

v4.2.1.stable.official [b09f793f5]

Question

I need to comp together multiple camera views. In a test scene, I have used two SubViewportContainers and two SubViewports, the top one of which has transparent background enabled. I am also using two cameras each with their own quad MeshInstance3D that use a spatial shader for applying an outline that needs access to the depth buffer, as documented here: Advanced post-processing — Godot Engine (latest) documentation in English.

This seems to cause a lot of issues…
From what I can tell, the spatial shaders are drawn after whatever comps the viewports together and it means that the shaders get messed with. Only one camera view’s depth buffer gets used, despite the transparent background however on top of this the background of the upper viewport is drawn black when the shader is enabled despite this not being the case when transparent bg is disabled or when the shader is disabled.

Weirdly I’ve now also tied the visibility of the top viewport to whether or not its shader is enabled, even after an editor restart which I can’t really comprehend…

If anyone could shed some light on the situation it would be much appreciated :slight_smile:

An update

I have been experimenting and realised some things I thought about SubViewports were wrong. I was treating each one as an individual scene that can only interact within its own scope or higher than its own scope but that is wrong in some cases, like my issue with the shader.

The issue I was getting with the black background was that my shader takes the hint_screen_texture sampler and modifies some of the colours. When transparent bg is enabled, this is just black as it’s a vec3. I have been looking for a workaround for this, thought I could maybe use a ViewportTexture and get the alpha channel from that but the ouput of the SubViewport is already operated on by the shader, so has the black bacground.

Does anyone know a way to expose the alpha channel here, or of any other workaround to this issue?

If I were comping together 2 screen space effects, I would use two slightly transparent full-screen quads and layer them on top of each other.

I’m not sure how this would play out with your project, but SubViewports do have this cool “Transparent Bg” property. You probably have those checked, since otherwise you would be seeing a skybox in the wrong place as opposed to a black background… I think.

Do you have any more details about the structure of your scene and shaders?