BlueGit

timfa questward

Speed up dynamic light rays without changing falloff.

Skip Bresenham when a tile is outside light range, and walk LOS without allocating a tile array on the hot path.

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
765343f2605f739b1e50d2654fd65b209b3424e8
Parent
f0f560db
2 files changed +63 −5
server/tests/lighting.test.js +29 −0
@@ -62,6 +62,35 @@ test("dynamic lights use the same wall occlusion as baked light", () =>
62 62 assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0);
63 63 });
64 64
65 test("dynamic lights skip rays beyond range before occlusion work", () =>
66 {
67 let occlusionCalls = 0;
68 const occlusion = () =>
69 {
70 occlusionCalls += 1;
71 return LightOcclusion.clear;
72 };
73 const sources = [{ tileX: 0, tileY: 0, range: 2, intensity: 1 }];
74 assert.equal(dynamicLightAtTile(5, 0, sources, occlusion), 0);
75 assert.equal(occlusionCalls, 0, "out-of-range tiles must not run LOS");
76 // Two steps away → one intermediate tile, so occlusion is consulted.
77 assert.ok(dynamicLightAtTile(2, 0, sources, occlusion) > 0);
78 assert.ok(occlusionCalls > 0);
79 });
80
81 test("evaluateLightRay matches Bresenham intermediates without allocating the path", () =>
82 {
83 const blocked = new Set(["1,0"]);
84 const occlusion = (x, y) => (blocked.has(`${x},${y}`)
85 ? LightOcclusion.solid
86 : LightOcclusion.clear);
87 assert.equal(evaluateLightRay(0, 0, 2, 0, occlusion).reaches, false);
88 assert.equal(evaluateLightRay(0, 0, 3, 0, () => LightOcclusion.clear).reaches, true);
89 assert.equal(evaluateLightRay(0, 0, 3, 0, () => LightOcclusion.clear).extraDistance, 0);
90 const partial = (x, y) => (x === 1 && y === 0 ? LightOcclusion.partial : LightOcclusion.clear);
91 assert.equal(evaluateLightRay(0, 0, 2, 0, partial).extraDistance, 1);
92 });
93
65 94 test("tables and marketplace counters do not occlude light rays", () =>
66 95 {
67 96 const origin = createEmptyChunkData();
@@ -62,6 +62,35 @@ test("dynamic lights use the same wall occlusion as baked light", () =>
62 assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0);
62 assert.ok(dynamicLightAtTile(2, 0, sources, () => true) > 0);
63});
63});
64
64
65test("dynamic lights skip rays beyond range before occlusion work", () =>
66{
67 let occlusionCalls = 0;
68 const occlusion = () =>
69 {
70 occlusionCalls += 1;
71 return LightOcclusion.clear;
72 };
73 const sources = [{ tileX: 0, tileY: 0, range: 2, intensity: 1 }];
74 assert.equal(dynamicLightAtTile(5, 0, sources, occlusion), 0);
75 assert.equal(occlusionCalls, 0, "out-of-range tiles must not run LOS");
76 // Two steps away → one intermediate tile, so occlusion is consulted.
77 assert.ok(dynamicLightAtTile(2, 0, sources, occlusion) > 0);
78 assert.ok(occlusionCalls > 0);
79});
80
81test("evaluateLightRay matches Bresenham intermediates without allocating the path", () =>
82{
83 const blocked = new Set(["1,0"]);
84 const occlusion = (x, y) => (blocked.has(`${x},${y}`)
85 ? LightOcclusion.solid
86 : LightOcclusion.clear);
87 assert.equal(evaluateLightRay(0, 0, 2, 0, occlusion).reaches, false);
88 assert.equal(evaluateLightRay(0, 0, 3, 0, () => LightOcclusion.clear).reaches, true);
89 assert.equal(evaluateLightRay(0, 0, 3, 0, () => LightOcclusion.clear).extraDistance, 0);
90 const partial = (x, y) => (x === 1 && y === 0 ? LightOcclusion.partial : LightOcclusion.clear);
91 assert.equal(evaluateLightRay(0, 0, 2, 0, partial).extraDistance, 1);
92});
93
65test("tables and marketplace counters do not occlude light rays", () =>
94test("tables and marketplace counters do not occlude light rays", () =>
66{
95{
67 const origin = createEmptyChunkData();
96 const origin = createEmptyChunkData();
shared/lighting/raycast.js +34 −5
@@ -47,6 +47,7 @@ export function rayReaches (fromX, fromY, toX, toY, isClear)
47 47
48 48 /**
49 49 * Light transmission along a Bresenham ray.
50 * Walks the line without allocating a tile array (hot path for dynamic lights).
50 51 * @param {(x: number, y: number) => number} occlusionAt
51 52 * {@link LightOcclusion.clear|partial|solid}
52 53 * @returns {{ reaches: boolean, extraDistance: number }}
@@ -63,12 +64,32 @@ export function evaluateLightRay (fromX, fromY, toX, toY, occlusionAt)
63 64 };
64 65 }
65 66
66 const tiles = bresenhamTiles(fromX, fromY, toX, toY);
67 let x = fromX;
68 let y = fromY;
69 const dx = Math.abs(toX - fromX);
70 const dy = Math.abs(toY - fromY);
71 const sx = fromX < toX ? 1 : -1;
72 const sy = fromY < toY ? 1 : -1;
73 let err = dx - dy;
67 74 let extraDistance = 0;
75
68 76 // Only tiles strictly between source and target can occlude / add penalty.
69 for (let i = 1; i < tiles.length - 1; i++)
77 while (x !== toX || y !== toY)
70 78 {
71 const level = occlusionAt(tiles[i].x, tiles[i].y) | 0;
79 const e2 = 2 * err;
80 if (e2 > -dy)
81 {
82 err -= dy;
83 x += sx;
84 }
85 if (e2 < dx)
86 {
87 err += dx;
88 y += sy;
89 }
90 if (x === toX && y === toY)
91 { break; }
92 const level = occlusionAt(x, y) | 0;
72 93 if (level >= LightOcclusion.solid)
73 94 { return { reaches: false, extraDistance: 0 }; }
74 95 if (level === LightOcclusion.partial)
@@ -108,18 +129,26 @@ export function lightIntensity (encoded)
108 129 */
109 130 export function dynamicLightAtTile (tileX, tileY, sources, occlusionAt)
110 131 {
132 if (!sources?.length)
133 { return 0; }
111 134 const occlude = normalizeOcclusionFn(occlusionAt);
112 135 let best = 0;
113 136 for (const source of sources)
114 137 {
138 const range = source.range;
139 if (!(range > 0))
140 { continue; }
115 141 const geo = Math.hypot(tileX - source.tileX, tileY - source.tileY);
142 // Partial occlusion only *increases* distance — never shortens it.
143 if (geo > range)
144 { continue; }
116 145 const ray = evaluateLightRay(source.tileX, source.tileY, tileX, tileY, occlude);
117 146 if (!ray.reaches)
118 147 { continue; }
119 148 const dist = geo + ray.extraDistance;
120 if (dist > source.range)
149 if (dist > range)
121 150 { continue; }
122 best = Math.max(best, falloffAtDistance(dist, source.range) * (source.intensity ?? 1));
151 best = Math.max(best, falloffAtDistance(dist, range) * (source.intensity ?? 1));
123 152 }
124 153 return best;
125 154 }
@@ -47,6 +47,7 @@ export function rayReaches (fromX, fromY, toX, toY, isClear)
47
47
48/**
48/**
49 * Light transmission along a Bresenham ray.
49 * Light transmission along a Bresenham ray.
50 * Walks the line without allocating a tile array (hot path for dynamic lights).
50 * @param {(x: number, y: number) => number} occlusionAt
51 * @param {(x: number, y: number) => number} occlusionAt
51 * {@link LightOcclusion.clear|partial|solid}
52 * {@link LightOcclusion.clear|partial|solid}
52 * @returns {{ reaches: boolean, extraDistance: number }}
53 * @returns {{ reaches: boolean, extraDistance: number }}
@@ -63,12 +64,32 @@ export function evaluateLightRay (fromX, fromY, toX, toY, occlusionAt)
63 };
64 };
64 }
65 }
65
66
66 const tiles = bresenhamTiles(fromX, fromY, toX, toY);
67 let x = fromX;
68 let y = fromY;
69 const dx = Math.abs(toX - fromX);
70 const dy = Math.abs(toY - fromY);
71 const sx = fromX < toX ? 1 : -1;
72 const sy = fromY < toY ? 1 : -1;
73 let err = dx - dy;
67 let extraDistance = 0;
74 let extraDistance = 0;
75
68 // Only tiles strictly between source and target can occlude / add penalty.
76 // Only tiles strictly between source and target can occlude / add penalty.
69 for (let i = 1; i < tiles.length - 1; i++)
77 while (x !== toX || y !== toY)
70 {
78 {
71 const level = occlusionAt(tiles[i].x, tiles[i].y) | 0;
79 const e2 = 2 * err;
80 if (e2 > -dy)
81 {
82 err -= dy;
83 x += sx;
84 }
85 if (e2 < dx)
86 {
87 err += dx;
88 y += sy;
89 }
90 if (x === toX && y === toY)
91 { break; }
92 const level = occlusionAt(x, y) | 0;
72 if (level >= LightOcclusion.solid)
93 if (level >= LightOcclusion.solid)
73 { return { reaches: false, extraDistance: 0 }; }
94 { return { reaches: false, extraDistance: 0 }; }
74 if (level === LightOcclusion.partial)
95 if (level === LightOcclusion.partial)
@@ -108,18 +129,26 @@ export function lightIntensity (encoded)
108 */
129 */
109export function dynamicLightAtTile (tileX, tileY, sources, occlusionAt)
130export function dynamicLightAtTile (tileX, tileY, sources, occlusionAt)
110{
131{
132 if (!sources?.length)
133 { return 0; }
111 const occlude = normalizeOcclusionFn(occlusionAt);
134 const occlude = normalizeOcclusionFn(occlusionAt);
112 let best = 0;
135 let best = 0;
113 for (const source of sources)
136 for (const source of sources)
114 {
137 {
138 const range = source.range;
139 if (!(range > 0))
140 { continue; }
115 const geo = Math.hypot(tileX - source.tileX, tileY - source.tileY);
141 const geo = Math.hypot(tileX - source.tileX, tileY - source.tileY);
142 // Partial occlusion only *increases* distance — never shortens it.
143 if (geo > range)
144 { continue; }
116 const ray = evaluateLightRay(source.tileX, source.tileY, tileX, tileY, occlude);
145 const ray = evaluateLightRay(source.tileX, source.tileY, tileX, tileY, occlude);
117 if (!ray.reaches)
146 if (!ray.reaches)
118 { continue; }
147 { continue; }
119 const dist = geo + ray.extraDistance;
148 const dist = geo + ray.extraDistance;
120 if (dist > source.range)
149 if (dist > range)
121 { continue; }
150 { continue; }
122 best = Math.max(best, falloffAtDistance(dist, source.range) * (source.intensity ?? 1));
151 best = Math.max(best, falloffAtDistance(dist, range) * (source.intensity ?? 1));
123 }
152 }
124 return best;
153 return best;
125}
154}