Bash.tv → GitHub → itch.io

Dynamic lighting and shadows for 2D and 3D web games. 2D Canvas lightmaps with soft colored radial lights, flicker, ambient darkness, day and night cycles, line-of-sight shadows from tile walls with visibility polygons, a GLSL 2D point-light shader, and 3D lighting in three.js (hemisphere + directional, shadow maps tuned for performance, point and spot lights, baked lighting, light probes and e...

Download skill (.zip) Files on GitHub

Game lighting

Drop-in: assets/lighting.js (tested in a GPU browser: the lightmap lights the area around the torch (187 vs 9 in the dark), the wall casts a shadow, and the GLSL light shader compiles).

2D (Canvas)

import { LightMap, visibility, segmentsFromTiles, dayNight } from "./core/lighting.js";
const lights = new LightMap(W, H);                                   // half size is fine and faster: new LightMap(W/2, H/2)
const walls = segmentsFromTiles((c, r) => map.isSolid(c, r), map.width, map.height, TILE);   // once per level
// render, after drawing the world:
const { ambient, tint } = dayNight(clock.hour);
lights.begin(ambient, tint);
const p = cam.toScreen(player.x, player.y);                          // the lightmap is in screen space
const poly = visibility(player.x, player.y, walls).map((q) => cam.toScreen(q.x, q.y));   // shadows: world → screen
lights.light(p.x, p.y, 160, "#ffd9a0", 1, { polygon: poly });
for (const t of torches) { const s = cam.toScreen(t.x, t.y); lights.light(s.x, s.y, 90, "#ff9944", 0.9, { flicker: 0.15, time }); }
lights.end(ctx);                                                      // multiply: darkness everywhere except lights
drawHUD();                                                            // HUD stays unlit

GPU alternative: LIGHTS_FRAG (up to 16 point lights) as a fullscreen pass (webgl-rendering), or Pixi / Phaser light plugins.

3D (three.js)

Need Use
Outdoor base HemisphereLight (sky / ground) + one DirectionalLight sun
Shadows Sun only: castShadow, map 1024–2048, a tight shadow.camera following the player, shadow.bias −0.0005
Torches, lamps PointLight (no shadows; few), or fake glow sprites
Flashlight SpotLight with a soft penumbra, maybe with a shadow (expensive)
Static scenes Bake lighting into textures or vertex colors; shadowMap.autoUpdate = false
Shiny materials An environment map (PMREMGenerator) instead of many lights

Phones: 1 shadowed light, ≤ 3 small point lights, Lambert materials where possible (webgl-performance).

Design