Pixel Art Laser Beam Shader (2D)

A horizontal laser in chunky pixels: white core, two color bands, a dithered glow, a flare at the muzzle and another where it hits. Bright bits run along it, sparks jump off, and it flickers in steps like hand-drawn frames.

Put it on a ColorRect (or any CanvasItem without a texture) and set size_px to the node’s size. pixel_size is how many screen pixels one art pixel covers, keep it the same as your sprites. Animate fire from 0 to 1 to shoot the beam out to the right and rotate the node to aim. beam_width is in art pixels. The four palette colors are plain uniforms, so a red or green laser is just new colors.

My pixel laser pack on itch is sprite sheets. This shader is a separate small version I wrote to match its look. 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.

The sprite sheet pack is ARCBIT, 16 pixel laser and lightning effects that work in any engine. The free version is the first link and the full pack is the second.
https://gennai.itch.io/arcbit-lite?utm_source=godotshaders&utm_medium=referral&utm_campaign=pixellaser
https://gennai.itch.io/arcbit-pixel-beams-lightning?utm_source=godotshaders&utm_medium=referral&utm_campaign=pixellaser

Shader code
// Pixel Art Laser Beam (2D) for Godot 4
// MIT License. Copyright (c) 2026 gennai (https://gennai.itch.io)
//
// A horizontal laser beam drawn in chunky pixels: a white core, two bands of colour, a
// dithered glow, a flare at the muzzle on the left and an impact flare at the far end.
// Everything snaps to a grid of art pixels, so it matches pixel-art sprites. Put it on a
// ColorRect (or any CanvasItem that has no texture) and set the shader's size_px to the
// same size as the node. Rotate the node to aim the beam.
//
// pixel_size is how many screen pixels one art pixel covers (4 = each art pixel is 4 x 4
// screen pixels). Keep your game's other sprites at the same scale. Animate fire from 0 to 1 to
// shoot the beam out to the right, then back to 0 to switch it off. beam_width is the
// thickness in art pixels. The beam flickers at frame_rate steps per second, like hand-drawn
// frames, and a few bright pieces run along it.
// No textures: the pattern is computed in the shader with sin-free hashes, so it looks the
// same on every renderer.
shader_type canvas_item;

group_uniforms setup;
// Size of the node in screen pixels. Set this to the node's size.
uniform vec2 size_px = vec2(640.0, 96.0);
// Screen pixels per art pixel. Use a whole number.
uniform float pixel_size : hint_range(1.0, 16.0, 1.0) = 4.0;

group_uniforms beam;
// 0 = off, 1 = the beam reaches the right edge. Animate this.
uniform float fire : hint_range(0.0, 1.0) = 1.0;
// Thickness of the beam in art pixels.
uniform float beam_width : hint_range(2.0, 24.0) = 8.0;
// Number of flicker steps per second.
uniform float frame_rate : hint_range(1.0, 30.0) = 12.0;
// How fast the bright pieces run along the beam, in art pixels per second.
uniform float run_speed : hint_range(0.0, 200.0) = 60.0;
// Number of sparks around the beam.
uniform float sparks : hint_range(0.0, 1.0) = 0.5;
// Size of the flares at both ends, in art pixels.
uniform float flare_size : hint_range(0.0, 16.0) = 7.0;
// Changes the flicker.
uniform float seed : hint_range(0.0, 100.0) = 0.0;

group_uniforms palette;
uniform vec4 color_core : source_color = vec4(1.0, 1.0, 0.92, 1.0);
uniform vec4 color_mid : source_color = vec4(1.0, 0.86, 0.3, 1.0);
uniform vec4 color_edge : source_color = vec4(1.0, 0.5, 0.1, 1.0);
uniform vec4 color_glow : source_color = vec4(0.75, 0.22, 0.12, 1.0);

const float PL_BAYER[16] = float[16](0.0, 8.0, 2.0, 10.0, 12.0, 4.0, 14.0, 6.0, 3.0, 11.0, 1.0, 9.0, 15.0, 7.0, 13.0, 5.0);

float pl_hash(vec2 p) {
	vec3 p3 = fract(vec3(p.xyx) * 0.1031);
	p3 += dot(p3, p3.yzx + 33.33);
	return fract((p3.x + p3.y) * p3.z);
}

float pl_bayer(vec2 p) {
	ivec2 q = ivec2(mod(p, 4.0));
	return (PL_BAYER[q.x + q.y * 4] + 0.5) / 16.0;
}

void fragment() {
	vec2 dim = floor(size_px / pixel_size);
	vec2 art = floor(UV * size_px / pixel_size);
	float tq = floor(TIME * frame_rate) + seed * 3.0;
	float cy = dim.y * 0.5;
	float dy = abs(art.y + 0.5 - cy);
	float x = art.x;
	float muzzle_x = flare_size * 0.5 + 1.0;
	float len = muzzle_x + (dim.x - muzzle_x) * fire;

	// the thickness changes a little along the beam and from frame to frame
	float wob = floor((pl_hash(vec2(floor(x / 5.0), tq)) - 0.5) * 3.0 + 0.5) * 0.5;
	float w = max(beam_width * 0.5 + wob, 1.0);
	// the beam narrows into a point right before it ends, and swells a little at the muzzle
	float endk = clamp((len - x) / 6.0, 0.0, 1.0);
	float startk = 1.0 + 0.25 * (1.0 - clamp((x - muzzle_x) / 8.0, 0.0, 1.0));
	w *= mix(0.6, 1.0, endk) * startk;

	vec4 col = vec4(0.0);
	float on = step(muzzle_x * 0.5, x) * step(x, len) * step(0.01, fire);
	float d = dy / max(w, 0.5);
	// bright pieces running along the beam
	float seg = fract((x - TIME * run_speed) / 22.0);
	float piece = step(seg, 0.18);
	float core_w = 0.28 + 0.14 * piece;
	if (on > 0.5) {
		if (d < core_w) {
			col = color_core;
		} else if (d < 0.62 + 0.1 * piece) {
			col = color_mid;
		} else if (d < 1.0) {
			col = color_edge;
		} else if (d < 1.45) {
			// dithered glow around the beam
			float k = 1.0 - (d - 1.0) / 0.45;
			if (k > pl_bayer(art)) {
				col = color_glow;
			}
		}
	}

	// sparks that jump off the beam
	float sh = pl_hash(art + vec2(tq * 7.7, seed));
	if (col.a < 0.5 && on > 0.5 && sh > 1.0 - 0.03 * sparks && d < 3.2 && d > 1.0) {
		col = color_mid;
	}

	// flare at the muzzle and at the far end: a round glow with a cross of spikes
	for (int k = 0; k < 2; k++) {
		float fx = k == 0 ? muzzle_x : len;
		float fsz = flare_size * (k == 0 ? 1.0 : 0.85) * mix(0.0, 1.0, clamp(fire * 6.0, 0.0, 1.0));
		float jit = floor(pl_hash(vec2(tq, float(k) + 3.0)) * 3.0) - 1.0;
		vec2 c = vec2(x + 0.5 - fx, art.y + 0.5 - cy);
		float rr = length(c);
		float R = fsz * 0.5 + jit * 0.5 * step(0.5, fsz);
		float spike = step(min(abs(c.x), abs(c.y)), 0.5) * step(rr, R * 1.9);
		if (R > 0.5 && (rr < R * 0.5 || (spike > 0.5 && rr < R * 0.6 + 2.0))) {
			col = color_core;
		} else if (R > 0.5 && rr < R * 0.85) {
			col = color_mid;
		} else if (R > 0.5 && (rr < R * 1.1 || spike > 0.5)) {
			col = color_edge;
		} else if (R > 0.5 && rr < R * 1.5 && (1.0 - (rr - R * 1.1) / (R * 0.4)) > pl_bayer(art)) {
			col = color_glow;
		}
	}
	COLOR = col;
}
Live Preview
Tags
2d, beam, canvas item, laser, pixel-art, retro, sci-fi, shader, shmup, vfx
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.