Apply shader to Sprite3D? (SOLVED)

Godot Version

4.2.2

Question

I have a Sprite3D in billboard mode which I’m trying to affect using a shader.

I want to overlay the sprite’s opaque pixels with a colour, generating a silhouette of its image.

So far I’ve tried to apply a shader like this:

shader_type spatial;

uniform bool effect_enabled;
uniform sampler2D albedo_texture : source_color;

void fragment() {
	if (effect_enabled) {
		ALBEDO = vec3(1.0, 1.0, 1.0); //White colour
	} else {
		ALBEDO = texture(albedo_texture, UV).rgb;
	}
}

I applied it on the Material Override.

While this seems to work in the preview of the material in the Inspector, my Sprite3D itself remains invisible.

Is it not possible to affect a Sprite3D, or am I missing something?

Through my own experimentation, it seems that custom shaders (except visual shaders) do not contain the default capabilities of the BaseMaterial3D. As such, the billboarding effect is disabled once your custom shader material is applied. You have to implement your own billboarding.

Working code for your use case is seen below.
NOTE: I just took the code generated by VisualShaderNodeBillboard.

shader_type spatial;

uniform bool use_billboard;
void vertex() {
	if (use_billboard){
        MODELVIEW_MATRIX = VIEW_MATRIX * mat4(INV_VIEW_MATRIX[0], INV_VIEW_MATRIX[1], INV_VIEW_MATRIX[2], MODEL_MATRIX[3]);
	}
}

uniform bool effect_enabled;
uniform sampler2D albedo_texture : source_color;

void fragment() {
	if (effect_enabled) {
		ALBEDO = vec3(1.0, 1.0, 1.0); //White colour
	} else {
		ALBEDO = texture(albedo_texture, UV).rgb;
	}
	ALPHA = texture(albedo_texture, UV).a;
}
2 Likes

Works like a charm, thanks a lot!

1 Like

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