Is it possible to make Shader Material "Light only" like CanvasItemMaterial?

Godot Version

4.4

Question

Hi community, I am currently developing a 2d game. I was making a 'Fog of War' effect by adding a CanvasItemMaterial to ally units which is "light only", and any unit which is lighted up by player's light source will appear, others will hide. This works perfect for my game.

However, when I start making enemy units, I was trying to replace the theme color of default (ally) peasant with some other color, e.g. I replaced red color with blue to show that this unit is from another team. To do this ,I have to use a shader material. Since I am not really good at Shader coding, I used this simple code to do the work:

shader_type canvas_item;

uniform vec4 red_dark   : source_color;
uniform vec4 red_light  : source_color;
uniform vec4 blue_dark  : source_color;
uniform vec4 blue_light : source_color;

uniform float threshold = 0.04;

void fragment() {
vec4 tex = texture(TEXTURE, UV);

if (distance(tex.rgb, red_dark.rgb) < threshold) {
    COLOR = vec4(blue_dark.rgb, tex.a);
}

else if (distance(tex.rgb, red_light.rgb) < threshold) {
    COLOR = vec4(blue_light.rgb, tex.a);
}

else {
    COLOR = tex;
}

}

So my problem is, I can only add one material to the Sprte2D (or AnimatedSprite2D), so my previous fog of war material is overrided. Is there any way that I can set my shader to “Light Only” too?

Add render mode line at the top of your shader code:

shader_type canvas_item;
render_mode light_only;

# the rest of it
3 Likes

What @normalized says. In general you can right-click on the material in the inspector and convert it to a shader, to see the corresponding shader-code

2 Likes

This works perfectly! Thank you for the solution!

1 Like

I see, thanks for the additional details!