How to "Flash" a Sprite3D?

Godot Version

Godot v.4.1.1

Question

I want to make a Sprite3D “flash” white, i.e. transition from its normal texture colour towards pure white and back. I’ve done this with Sprite2Ds before using Modulate, but it appears Modulate doesn’t allow values above 1.0 for Sprite3Ds (according to this).

I tried making a shader as suggested in the above thread, but I’ve been unable to get any shaders to work on my Sprite3D. Adding any material_override immediately removes the texture (which makes sense at first, but it still doesn’t display anything if I add the texture to the shader parameters), and I can’t seem to get a material_overlay to do anything at all to the sprite. This thread (from Godot 3.5 I think) suggests Material Overlays are the way to go, but I can’t make sense of the picture guide there - the interface I have looks completely different.

The closest I’ve got is to set the sprite’s Material_Overlay to a ShaderMaterial with this shader, which I adapted from a thread (lost the link, unfortunately) that purported to share the “basic” shader used for Sprite3Ds:

shader_type spatial;

uniform sampler2D sprite_texture : source_color, filter_nearest;
uniform float exposure = 4.0;

void vertex() {
	
}

void fragment() {
	vec4 color = texture(sprite_texture, UV);
	ALBEDO *= color.rgb * exposure;
	ALPHA = color.a;
}

Still, using this shader the Sprite3D always has the base texture colour, and never modulates no matter what I set exposure to, even if I set it somewhere between 0 and 1.

So how should I go about “flashing” a Sprite3D?

1 Like

Just create a ShaderMaterial with a simple Shader as the GeometryInstance3D.material_override that does that:

shader_type spatial;
render_mode unshaded;

uniform sampler2D tex:source_color,filter_linear_mipmap,repeat_disable;
uniform vec3 flash_color:source_color = vec3(1.0);
uniform float intensity:hint_range(0.0, 1.0, 0.1) = 0.0;

void fragment() {
	vec4 tex_color = texture(tex, UV);
	ALBEDO = mix(tex_color.rgb, flash_color, intensity);
	ALPHA = tex_color.a;
}
1 Like

The way your post is written, it seems like you want the sprite to flash white using modulate.
But you are mentioning overbrightening (values above 1) too, which is necessary if you also want it to glow.
If you actually only want it to flash white, you can set modulate to Color.WHITE or Color(1, 1, 1, 1).

Thank you, this works brilliantly.

I’m embarrassed to say I spent a long time wondering why applying the material was making my texture invisible, only to eventually realize that I had configured my sprite with its normal pointing “down” instead of “up” (using an X rotation of 90 instead of -90), so I just wasn’t seeing it from the camera angle I was using!

I guess by default Sprite3D textures are rendered in both directions, but switching to the material only rendered it on one side. Good thing to know for the future.

You can disable culling by adding cull_disabled in the render_mode list. More info Spatial shaders — Godot Engine (stable) documentation in English

1 Like