Laser and Lightning Beam
One shader for both a straight laser and a jagged lightning bolt. jag_amount 0 gives a laser with a white core and a soft glow. Raise it and the beam turns into a zigzag that redraws itself a few times a second, with a short flash each time.
Put it on a long, thin QuadMesh (for example 8 x 1) as Material Override. With face_camera on, the quad turns around its long axis to face the camera, so it looks like a round beam from any side.
appear grows the beam from start to end, from 0 to 1, and vanish lets the tail catch up so it disappears. core_width, glow_falloff and the two colors set the look. jag_frequency is how many corners the bolt has and jag_rate is how often it redraws. I tried smooth noise for the bolt first and it just looked like a wave. Straight segments between random points look much more like electricity.
It’s unshaded and additive. No textures are used, and the hashes avoid sin(), so it looks the same in Forward+, Mobile and Compatibility. I tested it on Godot 4.4 and 4.7.
The code was written with the help of an AI coding assistant.
This is a cut-down version of the beams in my Lightning & Beam VFX pack for Godot 4, which adds sky strikes, chain lightning and ground effects. The free version of that pack is the first link and the full pack is the second.
https://gennai.itch.io/arcflux-lite?utm_source=godotshaders&utm_medium=referral&utm_campaign=beam
Shader code
// Lightning / Laser Beam for Godot 4
// MIT License. Copyright (c) 2026 gennai (https://gennai.itch.io)
//
// A glowing beam on one thin strip. Put this on a MeshInstance3D with a long, thin
// QuadMesh (for example size 8 x 0.6) as Geometry > Material Override. UV.x runs
// along the beam, UV.y across it. With face_camera on, the strip turns around its
// long axis to face the camera, so it never shows edge-on.
//
// Animate appear from 0 to 1 to shoot the beam out, then vanish from 0 to 1 to
// pull it back in. Set jag_amount to 0 for a straight laser, and raise it for a jagged lightning bolt.
// No textures: the noise is computed in the shader and looks the same on every renderer.
shader_type spatial;
render_mode unshaded, blend_add, depth_draw_never, cull_disabled, skip_vertex_transform, shadows_disabled, fog_disabled;
group_uniforms timing;
// 0 = no beam, 1 = beam reaches the far end. Animate this to fire it.
uniform float appear : hint_range(0.0, 1.0) = 1.0;
// 0 = beam intact, 1 = the tail has caught up and the beam is gone.
uniform float vanish : hint_range(0.0, 1.0) = 0.0;
// Fade length at both ends, as a fraction of the beam length.
uniform float end_fade : hint_range(0.0, 0.5) = 0.08;
group_uniforms shape;
// Turn the strip around its long axis to face the camera.
uniform bool face_camera = true;
// Thickness of the bright core, as a fraction of the strip width.
uniform float core_width : hint_range(0.02, 1.0) = 0.14;
// How quickly the outer glow fades out. Higher = tighter glow.
uniform float glow_falloff : hint_range(0.5, 8.0) = 2.0;
// How far the path swings sideways, as a fraction of the strip half-width. 0 = straight laser.
uniform float jag_amount : hint_range(0.0, 1.0) = 0.0;
// Number of coarse kinks along the beam. Finer kinks are layered on top.
uniform float jag_frequency : hint_range(2.0, 30.0) = 8.0;
// How many times per second the whole path is re-rolled.
uniform float jag_rate : hint_range(0.0, 30.0) = 10.0;
// Brightness flash each time the path re-rolls. 0 = none.
uniform float flash : hint_range(0.0, 3.0) = 1.0;
group_uniforms flow;
// Size of the noise along the beam. Higher = finer.
uniform float noise_scale : hint_range(0.2, 12.0) = 3.0;
// How fast the noise runs along the beam.
uniform float flow_speed : hint_range(-10.0, 10.0) = 2.0;
// How much noise roughens the edge of the glow.
uniform float edge_noise : hint_range(0.0, 1.0) = 0.35;
// How much noise varies the brightness along the beam.
uniform float body_noise : hint_range(0.0, 1.0) = 0.4;
// Strength of the bright bands that travel along the beam. 0 = none.
uniform float band_strength : hint_range(0.0, 4.0) = 0.6;
// Distance between bands, as a fraction of the beam length.
uniform float band_spacing : hint_range(0.02, 1.0) = 0.15;
// Changes the pattern so two beams do not match.
uniform float seed : hint_range(0.0, 100.0) = 0.0;
group_uniforms colors;
// Colour of the bright centre.
uniform vec4 core_color : source_color = vec4(0.86, 0.93, 1.0, 1.0);
// Colour of the outer glow.
uniform vec4 glow_color : source_color = vec4(0.24, 0.52, 1.0, 1.0);
uniform float core_energy : hint_range(0.0, 10.0) = 5.0;
uniform float glow_energy : hint_range(0.0, 10.0) = 2.2;
// Overall brightness.
uniform float intensity : hint_range(0.0, 4.0) = 1.0;
// Hashes without sin(), so every GPU gives the same pattern.
float bm_hash(vec2 p) {
vec3 p3 = fract(vec3(p.x, p.y, p.x) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
float bm_noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = bm_hash(i);
float b = bm_hash(i + vec2(1.0, 0.0));
float c = bm_hash(i + vec2(0.0, 1.0));
float d = bm_hash(i + vec2(1.0, 1.0));
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
float bm_fbm(vec2 p) {
float v = 0.0;
float amp = 0.55;
for (int i = 0; i < 3; i++) {
v += bm_noise(p) * amp;
p = p * 2.03 + vec2(13.1, 7.7);
amp *= 0.5;
}
return v / 0.9625;
}
// Random value in -0.5..0.5 at each whole number, joined by straight lines.
float bm_poly(float x, float roll) {
float i = floor(x);
float a = bm_hash(vec2(i, roll)) - 0.5;
float b = bm_hash(vec2(i + 1.0, roll)) - 0.5;
return mix(a, b, fract(x));
}
void vertex() {
vec3 p = (MODELVIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
if (face_camera) {
// Keep the strip on its long (local X) axis and turn its width toward the camera.
vec3 centre = (MODELVIEW_MATRIX * vec4(VERTEX.x, 0.0, 0.0, 1.0)).xyz;
vec3 t = (MODELVIEW_MATRIX * vec4(1.0, 0.0, 0.0, 0.0)).xyz;
bool ortho = PROJECTION_MATRIX[3][3] > 0.5;
vec3 view_dir = ortho ? vec3(0.0, 0.0, -1.0) : normalize(centre);
vec3 side = cross(t, view_dir);
float sl = length(side);
side = sl > 1e-5 ? side / sl : vec3(0.0, 1.0, 0.0);
p = centre + side * VERTEX.y * length(MODEL_MATRIX[1].xyz);
}
VERTEX = p;
NORMAL = vec3(0.0, 0.0, 1.0);
}
void fragment() {
float x = UV.x;
float v = UV.y * 2.0 - 1.0;
// Lightning: random points at even spacing, joined by straight lines (no smoothing),
// with finer kinks layered on top. The whole path is re-rolled jag_rate times a second.
float tick = floor(TIME * jag_rate);
float roll = mod(tick, 997.0) * 1.37 + seed * 5.0;
float jag = bm_poly(x * jag_frequency, roll) * 0.6
+ bm_poly(x * jag_frequency * 3.3, roll + 41.0) * 0.28
+ bm_poly(x * jag_frequency * 8.1, roll + 83.0) * 0.12;
float centre_x = clamp(jag * jag_amount * 2.0, -0.8, 0.8);
// The ends stay on the middle line so the beam leaves and arrives cleanly.
centre_x *= smoothstep(0.0, 0.06, x) * smoothstep(1.0, 0.94, x);
float pulse = 1.0 + flash * exp(-fract(TIME * jag_rate) * 9.0);
float d = abs(v - centre_x);
float n = bm_fbm(vec2(x * noise_scale * 4.0 - TIME * flow_speed, v * 1.3 + seed));
float d_eff = d / max(1.0 - edge_noise * (n - 0.2), 0.05);
float core = 1.0 - smoothstep(core_width * 0.45, core_width, d_eff);
float halo = pow(clamp(1.0 - d_eff, 0.0, 1.0), glow_falloff);
float body = mix(1.0, 0.45 + 1.1 * n, body_noise);
float bands = 1.0;
if (band_strength > 0.0) {
float b = 0.5 + 0.5 * cos((x / band_spacing - TIME * flow_speed * 0.5) * TAU);
bands = 1.0 + band_strength * pow(b, 6.0);
}
// Soft ends, the head reaching out as appear rises, the tail following as vanish rises.
float fade = max(end_fade, 1e-4);
float head = appear * (1.0 + fade);
float tail = vanish * (1.0 + fade) - fade;
float ends = smoothstep(0.0, fade, x) * smoothstep(1.0, 1.0 - fade, x);
ends *= smoothstep(head, head - fade, x) * smoothstep(tail, tail + fade, x);
// Keep the glow from being cut off by the strip edge.
float edge_mask = 1.0 - smoothstep(0.8, 1.0, abs(v));
vec3 col = core_color.rgb * (core_energy * core) + glow_color.rgb * (glow_energy * halo);
ALBEDO = col * (intensity * pulse * body * bands * ends * edge_mask);
}



Comments 0
No comments yet.