|
| 1 | +export type FalloffMode = 'linear' | 'quadratic' | 'smoothstep'; |
| 2 | + |
| 3 | +export interface PointLight { |
| 4 | + x: number; |
| 5 | + y: number; |
| 6 | + radius: number; |
| 7 | + intensity?: number; |
| 8 | + falloff?: FalloffMode; |
| 9 | + color?: [number, number, number]; |
| 10 | +} |
| 11 | + |
| 12 | +export interface LightingGridOptions { |
| 13 | + width: number; |
| 14 | + height: number; |
| 15 | + tileSize: number; |
| 16 | + ambient?: number; |
| 17 | + lights: ReadonlyArray<PointLight>; |
| 18 | + obstacles?: (x: number, y: number) => boolean; |
| 19 | +} |
| 20 | + |
| 21 | +export interface LightingCell { |
| 22 | + light: number; |
| 23 | + color: [number, number, number]; |
| 24 | +} |
| 25 | + |
| 26 | +export interface LightingGridResult { |
| 27 | + width: number; |
| 28 | + height: number; |
| 29 | + cells: LightingCell[]; |
| 30 | +} |
| 31 | + |
| 32 | +function assertPositive(value: number, label: string): void { |
| 33 | + if (typeof value !== 'number' || Number.isNaN(value) || !Number.isFinite(value) || value <= 0) { |
| 34 | + throw new Error(`${label} must be a positive finite number.`); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function clamp(value: number, min: number, max: number): number { |
| 39 | + if (value < min) return min; |
| 40 | + if (value > max) return max; |
| 41 | + return value; |
| 42 | +} |
| 43 | + |
| 44 | +function falloff(distance: number, radius: number, mode: FalloffMode): number { |
| 45 | + const t = clamp(distance / radius, 0, 1); |
| 46 | + switch (mode) { |
| 47 | + case 'linear': |
| 48 | + return 1 - t; |
| 49 | + case 'quadratic': |
| 50 | + return 1 - t * t; |
| 51 | + case 'smoothstep': |
| 52 | + return 1 - (t * t * (3 - 2 * t)); |
| 53 | + default: |
| 54 | + return 1 - t; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function blendColor(base: [number, number, number], add: [number, number, number], weight: number): [number, number, number] { |
| 59 | + return [ |
| 60 | + clamp(base[0] + add[0] * weight, 0, 1), |
| 61 | + clamp(base[1] + add[1] * weight, 0, 1), |
| 62 | + clamp(base[2] + add[2] * weight, 0, 1), |
| 63 | + ]; |
| 64 | +} |
| 65 | + |
| 66 | +function defaultColor(): [number, number, number] { |
| 67 | + return [0, 0, 0]; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Calculates a lighting grid for 2D tile maps with simple falloff. |
| 72 | + * Useful for: top-down games, roguelike rendering, and fog-of-war overlays. |
| 73 | + */ |
| 74 | +export function computeLightingGrid(options: LightingGridOptions): LightingGridResult { |
| 75 | + if (!Array.isArray(options.lights) || options.lights.length === 0) { |
| 76 | + throw new Error('lights must contain at least one point light.'); |
| 77 | + } |
| 78 | + assertPositive(options.width, 'width'); |
| 79 | + assertPositive(options.height, 'height'); |
| 80 | + assertPositive(options.tileSize, 'tileSize'); |
| 81 | + |
| 82 | + const ambient = clamp(options.ambient ?? 0.1, 0, 1); |
| 83 | + const obstacles = options.obstacles ?? (() => false); |
| 84 | + |
| 85 | + const cells: LightingCell[] = []; |
| 86 | + |
| 87 | + const lights = options.lights as ReadonlyArray<PointLight>; |
| 88 | + |
| 89 | + for (let y = 0; y < options.height; y += 1) { |
| 90 | + for (let x = 0; x < options.width; x += 1) { |
| 91 | + let light = ambient; |
| 92 | + let color: [number, number, number] = defaultColor(); |
| 93 | + color = blendColor(color, [ambient, ambient, ambient], 1); |
| 94 | + |
| 95 | + if (obstacles(x, y)) { |
| 96 | + cells.push({ light, color }); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + const worldX = (x + 0.5) * options.tileSize; |
| 101 | + const worldY = (y + 0.5) * options.tileSize; |
| 102 | + |
| 103 | + for (const point of lights) { |
| 104 | + assertPositive(point.radius, 'light.radius'); |
| 105 | + const dx = worldX - point.x; |
| 106 | + const dy = worldY - point.y; |
| 107 | + const distance = Math.sqrt(dx * dx + dy * dy); |
| 108 | + if (distance > point.radius) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + const intensity = clamp(point.intensity ?? 1, 0, 10); |
| 112 | + const mode = point.falloff ?? 'smoothstep'; |
| 113 | + const percent = falloff(distance, point.radius, mode) * intensity; |
| 114 | + light = clamp(light + percent, 0, 1); |
| 115 | + color = blendColor(color, point.color ?? [1, 0.95, 0.8], percent); |
| 116 | + } |
| 117 | + |
| 118 | + cells.push({ light, color }); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return { |
| 123 | + width: options.width, |
| 124 | + height: options.height, |
| 125 | + cells, |
| 126 | + }; |
| 127 | +} |
0 commit comments