Muzzle Flash Shader (Random Shape per Shot)

A star-shaped muzzle flash that looks different on every shot. Give seed a new random number each time you fire and run progress from 0 to 1 in about a tenth of a second. The seed changes which spikes are long, where they point and how big the flash is.

Use two QuadMeshes of about 2.8 x 2.8 at the muzzle, one rotated 90 degrees around the barrel (the quad’s X axis), both with this material, and the flash reads as round from any side. The spikes are longer toward the front, forward_bias sets how much.

It’s additive, so it needs a darkish background. No textures, and it looked the same on Forward+, Mobile and Compatibility in 4.4 and 4.7.

The code was written with the help of an AI coding assistant.

I wrote this as a small take on the muzzle flashes in my Gun & Shooting VFX pack for Godot 4, which also has tracers, sparks and bullet impacts. The free version is the first link and the full pack is the second.
https://gennai.itch.io/free-gun-vfx-godot-4?utm_source=godotshaders&utm_medium=referral&utm_campaign=muzzle
https://gennai.itch.io/gun-shooting-vfx-godot-4?utm_source=godotshaders&utm_medium=referral&utm_campaign=muzzle

Shader code
// Muzzle Flash with a Random Shape per Shot for Godot 4
// MIT License. Copyright (c) 2026 gennai (https://gennai.itch.io)
//
// A star-shaped muzzle flash drawn on a flat quad. Put TWO QuadMeshes (about 2.8 x 2.8)
// at the muzzle, one rotated 90 degrees around the barrel axis (the quad's X axis), so
// the flash looks round from every side. Both use this shader as Material Override. The
// barrel points along the quad's +X axis, so rotate the pair to match your gun.
//
// For every shot, set seed to a new random number and animate progress from 0 to 1 over
// about 0.05 to 0.1 seconds. The seed changes how many spikes are long, where they point
// and how big the flash is, so no two shots look the same. The flash snaps open and then
// shrinks and fades fast. It is additive, so it needs a dark enough background.
// No textures: the pattern is computed in the shader with sin-free hashes, so it looks
// the same on every renderer.
shader_type spatial;
render_mode unshaded, blend_add, depth_draw_never, cull_disabled, shadows_disabled, fog_disabled;

group_uniforms shot;
// 0 = the shot starts, 1 = the flash is gone. Animate this on every shot.
uniform float progress : hint_range(0.0, 1.0) = 0.15;
// Pick a new value for every shot. Changes the shape of the flash.
uniform float seed : hint_range(0.0, 1000.0) = 1.0;

group_uniforms shape;
// Number of spikes around the muzzle.
uniform int spike_count : hint_range(5, 20) = 11;
// 0 = all spikes have the same length, 1 = very different lengths.
uniform float length_variation : hint_range(0.0, 1.0) = 0.8;
// 0 = round, 1 = the flash is mostly in front of the muzzle.
uniform float forward_bias : hint_range(0.0, 1.0) = 0.85;
// Thickness of the spikes at the base.
uniform float spike_width : hint_range(0.1, 1.0) = 0.55;
// Size of the bright round core.
uniform float core_size : hint_range(0.0, 1.0) = 0.22;

group_uniforms look;
// Colour of the outer part of the spikes.
uniform vec4 color_outer : source_color = vec4(1.0, 0.45, 0.08, 1.0);
// Colour of the core and the middle of the spikes.
uniform vec4 color_core : source_color = vec4(1.0, 0.95, 0.7, 1.0);
// Brightness. Values above 1 bloom when glow is on.
uniform float energy : hint_range(0.0, 8.0) = 2.5;

float mf_hash(float p) {
	p = fract(p * 0.1031);
	p *= p + 33.33;
	p *= p + p;
	return fract(p);
}

void fragment() {
	vec2 q = (UV - 0.5) * 2.0;
	float r = length(q);
	float a = atan(q.y, q.x);
	float life = 1.0 - progress;
	// The flash opens quickly, then shrinks and fades.
	float open = smoothstep(0.0, 0.12, progress);
	float size = open * life * life * (0.75 + 0.5 * mf_hash(seed * 7.7));
	float n = float(spike_count);
	float sect = (a / TAU + 0.5 + mf_hash(seed * 3.3)) * n;
	float id = floor(sect);
	float f = abs(fract(sect) - 0.5) * 2.0;
	// Every spike gets its own length. Spikes in front of the muzzle are longer.
	float rl = mix(1.0, mf_hash(id + seed * 13.1), length_variation);
	float fwd = 0.5 + 0.5 * cos(a);
	float len = size * mix(0.3, 1.0, rl) * mix(1.0, 0.25 + 0.75 * fwd, forward_bias);
	// A spike gets narrower along its length.
	float wd = clamp(1.0 - r / max(len, 0.001), 0.0, 1.0);
	float spike = (1.0 - smoothstep(0.0, wd * spike_width + 0.001, f)) * step(r, len);
	spike *= wd;
	float core = exp(-r * r / max(core_size * core_size * size * 1.2 + 0.002, 0.002)) * open;
	float v = clamp(spike * 1.6 + core, 0.0, 1.0);
	vec3 col = mix(color_outer.rgb, color_core.rgb, clamp(v * v * 1.3, 0.0, 1.0));
	float edge = smoothstep(1.0, 0.8, r);
	ALBEDO = col * energy * v * edge;
	ALPHA = v * edge;
}
Live Preview
Tags
3d, firearm, flash, FPS, gun, muzzle flash, random, shooting, vfx, weapon
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from gennai

Related shaders

Comments 0

No comments yet.