How to create a Spatial Mask to show what's behind specific walls or obstacles?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MikeFP

I’m trying to create a see-through effect in 3D to display the player and their surroundings behind big obstacles like walls and such.

So far I’ve setup multiple Viewports rendering 3 different layers on top of each other:

  • Background: whatever isn’t between the camera and the player.
  • Mask: the spatial objects containing the shapes that will mask the foregroung.
  • Foreground: obstacles between the camera and the player with an alpha mask applied.

It was extremely difficult to set this up, in contrast to another method using the Stencil Buffer, unfortunately not supported currently in Godot 3.2, but it works.

However, I just realized that I get no shadows from the obstacle itself when the mask is applied, since I use different layers for the background and the foreground objects.

My question is: Is there a way to do it currently in Godot 3.2? If not, will the use of Stencil Buffer solve it in Godot 4.0?

Update: I was able to get the shadows properly, nothing fancy, I just applied the mask properly over the foreground instead of redrawing the masked background. But now, the problem still remains as I’m not able to sort the rendering order of the obstacles when one of them is getting the mask applied to, and is behind another without the mask applied, like the image below:

Spatial see-through effect in Godot problem

However, I just realized that I get no shadows from the obstacle itself when the mask is applied, since I use different layers for the background and the foreground objects.

Did you try playing around with the light/shadow cull mask in the light nodes? Note that DirectionalLight cull mask doesn’t work in GLES3 yet.

If not, will the use of Stencil Buffer solve it in Godot 4.0?

We don’t know for certain whether Godot 4.0 will have stencil buffer support.

Calinou | 2021-03-16 00:54

Yeah I considered using the Cull Mask, but in my case it wouldn’t work since I use two different Render Layers to render each part of the scene. So what I end up with is the foreground (with the mask applied) only renders one of those two layers, so I get no shadow from the obstacles that were taken to another layer to be drawn separately.

I don’t think I made it clear, but I get the shadows from the obstacle in their own layer, which is great. What I don’t get shadows is when drawing the foreground mask above it, since it has no lighting info about the obstacle, which is in its own layer.

Unless I’m not aware of some lighting techniques, I don’t think I can do it with just lighting.

Thanks for the reply though. And I’d also like to ask if this is a rare use case, since I couldn’t really find any tutorials or questions about it, at least not with Godot.

MikeFP | 2021-03-16 03:00

I got the shadows working. But just like all of my approaches so far, it resulted in this other more complicated problem: sorting the rendering order of some obstacles over others. I edited the question explaining it better.

MikeFP | 2021-03-16 19:36