0 votes

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.

in Engine by (25 points)

1 Answer

+1 vote

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.

by (16 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.