Shader that makes geometry invisible but still has them write to depth buffer as if they were opaque?

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

I recall figuring out how to do this in Unity with almost zero effort, but I’m struggling with this in Godot.

I’m making a game with 2d pre-rendered backgrounds and 3d characters and objects, ala Resident Evil or Final Fantasy VII. I’ve already figured out the part that involves overlaying the 3d characters over the 2d backdrops, but I’d like to hide objects that are supposed to be behind walls and such.

For that, I’ve thought of a solution to use some kind of shader for the collision geometry that renders it invisible so the backdrop can be seen – but still has it conceal any characters as if they were opaque 3d objects.

I’m sure there’s some simple depth buffer trickery I can pull to do this, but it’s beyond me. I know next to nothing about writing shaders. I’ve tried messing with the Spatial Material settings and can’t seem to get what I want.

:bust_in_silhouette: Reply From: LukeH

This might apply to your situation. Here is code for a really simple ShaderMaterial that only writes to the depth buffer.

shader_type spatial;
render_mode blend_mix,depth_draw_always,cull_back,diffuse_burley,specular_schlick_ggx,unshaded;

void fragment() {
	DEPTH = 1.0;
}

This is how I use it. I have two viewports, one over the other (using ViewportContainers), the top one has transparent background checked. Any object in that top viewport to which I give this material, it will punch a hole in objects behind it, so you can see the contents of the underlying viewport instead. I haven’t tested it much though. So depending on the settings of the materials on the objects behind it (or post-processing), its possible it wont punch through everything.

You sir are a Genius. Dang i want to give you an award but this isn’t reddit. So this’ll have todo

Wakatta | 2021-02-28 20:55