Achieving Light-Only Reveal Effect in Godot 4 for 3D Objects

Achieving Light-Only Reveal Effect in Godot 4 for 3D Objects

Hey there, I’ve been working on an awesome visual effect in Godot Engine 4 where I want objects to only be visible in the parts that are illuminated by light sources. For my 2D scenes, I nailed it with the following shader code:

shader_type canvas_item;

render_mode light_only;

void light() {
LIGHT = mix(
vec4(0.0),
vec4(LIGHT_COLOR. rgb * COLOR. rgb * LIGHT_ENERGY, LIGHT_COLOR.a),
step(LIGHT_COLOR.r,0.0)
);
}

This has worked beautifully. However, when 3D scenes, I hit a wall. I’m quite new to shaders and particularly to 3D shader logic.

If anyone could provide guidance, share some sample code, or point me towards resources or tutorials that could help, I’d be immensely grateful. I’m eager to learn and get this technique under my belt! Best of luck, and I hope someone can chime in with some advice!

Can someone help me😭

Would this work with just lights in 3D? What do you have so far in 3D? You might have to set the environment to have no ambient light.

Not really sure what I’m looking at in 2D from your example.

1 Like


I tried to change the alpha in light(), but it got weird jagged


I gave it a shot and tweaked things a bit, and it looks pretty cool, but when I start spinning the view around, I’m still getting some weird jagged edges that just don’t look right.

Is your material set to alpha enabled, alpha scissor or alpha hashed?

1 Like

I used ShaderMaterial

— from spatial shader docs

Ah, try discarding instead of setting alpha. or set ALPHA_SCISSOR_THRESHOLD to 0.5

void light() {
    DIFFUSE_LIGHT = vec3(1.0);
    if (LIGHT_COLOR.r * ATTENUATION < 0.5) {
        discard;
    }
}

This could help with transparency sorting on the same mesh; this is usually an issue when rotating around a transparent object that overlaps itself. It is a good idea to avoid “the transparent pipeline” where possible

1 Like

I found that I should add attenuation to the transparency, and it worked really well

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.