DEV Community

Cover image for How to Generate Crisp Procedural Pixel-Art Explosions, Sword Slashes & Godot 4 SpriteFrames (.tres) in Pure JS
Edward Lorian
Edward Lorian

Posted on Edited on

How to Generate Crisp Procedural Pixel-Art Explosions, Sword Slashes & Godot 4 SpriteFrames (.tres) in Pure JS

Most browser-based particle generators have the same giveaway: blurry, semi-transparent soft circles that immediately clash with hand-drawn 2D pixel art.

While building PixelBurst 2D v1.5 (a zero-dependency browser studio for procedural pixel explosions, sword slashes, laser beams, bouncing debris, and lightning sprite sheets), I found that getting authentic Metal Slug / Dead Cells / Celeste-style pixel VFX comes down to three surprisingly compact rendering techniques — plus eliminating the tedious manual slicing step in Godot 4.

Here is the exact pipeline, complete with copy-pasteable code.


1. Ditch Alpha Blending — Render to a Scalar Heat/Density Field + Bayer 4×4 Dither

Instead of drawing anti-aliased radial gradients directly to a 2D <canvas>, simulate particles into two integer-resolution Float32Array buffers (64×64):

  • density[x + y * W]: accumulated particle / shockwave / slash mass
  • heat[x + y * W]: maximum temperature (0.0 cool smoke → 1.0 white-hot core)

When quantizing those buffers into RGBA pixels, apply a hard alpha threshold (every pixel is strictly alpha = 0 or alpha = 255) combined with a 4×4 Bayer ordered dithering matrix mapped onto a curated 5-stop color ramp:

const BAYER4 = [
   0/16 - 0.5,  8/16 - 0.5,  2/16 - 0.5, 10/16 - 0.5,
  12/16 - 0.5,  4/16 - 0.5, 14/16 - 0.5,  6/16 - 0.5,
   3/16 - 0.5, 11/16 - 0.5,  1/16 - 0.5,  9/16 - 0.5,
  15/16 - 0.5,  7/16 - 0.5, 13/16 - 0.5,  5/16 - 0.5
];

// Quantize density & heat into crisp 5-color pixel art
for (let y = 0; y < h; y++) {
  const bayerRow = (y & 3) * 4;
  for (let x = 0; x < w; x++) {
    const idx = y * w + x;
    const bayer = BAYER4[bayerRow + (x & 3)];
    const cut = threshold + bayer * ditherAmount * 0.22;
    if (density[idx] < cut) continue; // 100% transparent

    mask[idx] = 1; // opaque silhouette
    const hVal = Math.max(0, Math.min(0.999, heat[idx] * 0.85 + bayer * ditherAmount * 0.24));
    const stopIdx = Math.floor((1 - hVal) * paletteStops.length);
    const [r, g, b] = paletteStops[stopIdx];
    const p4 = idx * 4;
    rgba[p4] = r; rgba[p4 + 1] = g; rgba[p4 + 2] = b; rgba[p4 + 3] = 255;
  }
}
Enter fullscreen mode Exit fullscreen mode

Adding a subtle coherent 2D value-noise wobble to density[idx] right before thresholding turns round blobs into organic billowing pixel fireballs and smoke clouds.


2. The 1px Pixel-Art Outline Pass (Instant Studio Polish)

Once mask[] holds your crisp binary silhouette, a single 4-connected neighbor pass wraps every explosion puff, sword slash crescent, and flying spark in a 1px dark (or bright rim) pixel outline:

for (let y = 0; y < h; y++) {
  for (let x = 0; x < w; x++) {
    const idx = y * w + x;
    if (mask[idx] !== 0) continue;
    const touchesSprite =
      (y > 0 && mask[idx - w] === 1) ||
      (y < h - 1 && mask[idx + w] === 1) ||
      (x > 0 && mask[idx - 1] === 1) ||
      (x < w - 1 && mask[idx + 1] === 1);

    if (touchesSprite) {
      const p4 = idx * 4;
      rgba[p4] = outlineRgb[0];
      rgba[p4 + 1] = outlineRgb[1];
      rgba[p4 + 2] = outlineRgb[2];
      rgba[p4 + 3] = 255;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Also watch out for Frame 0 input lag: for one-shot combat VFX (hit sparks, sword slashes, muzzle flashes), sample normalized lifetime at (frameIndex + 0.45) / (frameCount + 0.15) instead of frameIndex / (frameCount - 1) so Frame 1 immediately pops with an impact flash instead of sitting blank for 66ms!


3. Auto-Generating Godot 4 SpriteFrames (.tres) Directly in the Browser

Exporting a horizontal PNG strip is only half the workflow — manually creating AnimatedSprite2D frames in Godot takes time when you have dozens of hit sparks and spells.

Because Godot 4 .tres files are clean plain-text resources, you can emit a ready-to-drop SpriteFrames right alongside your baked PNG sheet:

[gd_resource type="SpriteFrames" load_steps=16 format=3]

[ext_resource type="Texture2D" path="res://classic_fireball_sheet.png" id="1_sheet"]

[sub_resource type="AtlasTexture" id="AtlasTexture_000"]
atlas = ExtResource("1_sheet")
region = Rect2(0, 0, 64, 64)

[sub_resource type="AtlasTexture" id="AtlasTexture_001"]
atlas = ExtResource("1_sheet")
region = Rect2(64, 0, 64, 64)

[resource]
animations = [{
"frames": [{
"duration": 1.0,
"texture": SubResource("AtlasTexture_000")
}, {
"duration": 1.0,
"texture": SubResource("AtlasTexture_001")
}],
"loop": false,
"name": &"default",
"speed": 15.0
}]
Enter fullscreen mode Exit fullscreen mode

Drop both classic_fireball_sheet.png and classic_fireball_SpriteFrames.tres into res://, assign the .tres to any AnimatedSprite2D, and it plays immediately at exact FPS with crisp nearest-neighbor bounds.


Try the Live Browser Studio (Free Web Demo v1.5)

I packaged this entire engine — expanded in v1.5 to 30 Studio Presets, 9 stackable procedural layer types (Explosions, Shockwave Rings, Sword Slashes, Impact Stars, Vortex/Plumes, Pixel Lightning, Laser Beams, Bouncing Shards/Debris, and Directional Cone Fire Breath), 18 palettes, 2-Way / 4-Way Mirror Symmetry, Onion Skinning, and JSON Recipe Save/Load — into PixelBurst 2D.

PixelBurst 2D Live Pixel VFX Showcase

New in v1.5.1 — Interactive RPG Target Preview: You can now preview all 30 procedural VFX live over 8 built-in RPG sprites (Slime, Knight, Boss Skull, Goblin, Chest, Wood Crate, Sword, Potion) or drag-and-drop your own PNG — complete with Hit-Flash, Recoil, Front/Back depth layering & Crate Shatter (studio preview only — exported PNG/GIF/Godot/Unity files remain 100% clean transparent VFX).

What kind of procedural 2D VFX (boss telegraphs, rain/weather loops, RPG buff runes) takes up the most time in your own games? Drop a comment below — I'm collecting ideas for v1.6!

Top comments (0)