BlueGit

timfa questward

Fade torch light tile by tile using baked intensity.

Co-authored-by: Cursor <cursoragent@cursor.com>

timfa Tim Falken · pushed by timfa

Authored 2026-08-24 · 2 months ago

Hash
9a190375a93546b555638db70fb266b5549ea314
Parent
2b7e9e50
5 files changed +34 −8
client/src/render/worldView.js +2 −2
@@ -261,8 +261,8 @@ export class WorldView
261 261 {
262 262 if (!objects.shade)
263 263 { continue; }
264 const lit = (objects.light ?? 0) > 0;
265 objects.shade.renderer.alpha = lit ? ambientDark * 0.12 : ambientDark;
264 const intensity = Math.min(1, Math.max(0, (objects.light ?? 0) / 255));
265 objects.shade.renderer.alpha = ambientDark * (1 - intensity);
266 266 }
267 267 }
268 268 }
@@ -261,8 +261,8 @@ export class WorldView
261 {
261 {
262 if (!objects.shade)
262 if (!objects.shade)
263 { continue; }
263 { continue; }
264 const lit = (objects.light ?? 0) > 0;
264 const intensity = Math.min(1, Math.max(0, (objects.light ?? 0) / 255));
265 objects.shade.renderer.alpha = lit ? ambientDark * 0.12 : ambientDark;
265 objects.shade.renderer.alpha = ambientDark * (1 - intensity);
266 }
266 }
267 }
267 }
268}
268}
docs/FEATURES.md +2 −2
@@ -58,7 +58,7 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
58 58 ## Baked lighting
59 59
60 60 **Status:** full
61 **Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 reach mask (encoded intensity). Torch range is 8 tiles.
61 **Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 intensity map (0–255, smoothstep falloff by tile distance). Torch range is 8 tiles.
62 62 **Code:** `server/src/lighting/baker.js`, tests in `server/tests/lighting.test.js`
63 63
64 64 ## World editor (server)
@@ -136,7 +136,7 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
136 136 ## Night shading
137 137
138 138 **Status:** full
139 **Spec:** World time is server time minus 1200 years. At night, tiles the baked light map cannot reach are darkened; lit tiles stay brighter.
139 **Spec:** World time is server time minus 1200 years. At night, unlit tiles are darkened and reachable tiles use the baked 0–255 intensity so a torch fades tile-by-tile instead of a hard circle.
140 140 **Code:** `client/src/render/worldView.js` (`nightFactor`, `#updateShade`), `server/src/simulation/simulation.js` (`worldTime`)
141 141
142 142 ## FrameworkV3 client bootstrap
@@ -58,7 +58,7 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
58## Baked lighting
58## Baked lighting
59
59
60**Status:** full
60**Status:** full
61**Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 reach mask (encoded intensity). Torch range is 8 tiles.
61**Spec:** On chunk save, raycast light from blocks with `lightRangeTiles` through passable tiles, including across chunk borders. Store a 16×16 intensity map (0–255, smoothstep falloff by tile distance). Torch range is 8 tiles.
62**Code:** `server/src/lighting/baker.js`, tests in `server/tests/lighting.test.js`
62**Code:** `server/src/lighting/baker.js`, tests in `server/tests/lighting.test.js`
63
63
64## World editor (server)
64## World editor (server)
@@ -136,7 +136,7 @@ Each entry is a short note on behaviour and where the code lives. Update this fi
136## Night shading
136## Night shading
137
137
138**Status:** full
138**Status:** full
139**Spec:** World time is server time minus 1200 years. At night, tiles the baked light map cannot reach are darkened; lit tiles stay brighter.
139**Spec:** World time is server time minus 1200 years. At night, unlit tiles are darkened and reachable tiles use the baked 0–255 intensity so a torch fades tile-by-tile instead of a hard circle.
140**Code:** `client/src/render/worldView.js` (`nightFactor`, `#updateShade`), `server/src/simulation/simulation.js` (`worldTime`)
140**Code:** `client/src/render/worldView.js` (`nightFactor`, `#updateShade`), `server/src/simulation/simulation.js` (`worldTime`)
141
141
142## FrameworkV3 client bootstrap
142## FrameworkV3 client bootstrap
server/src/lighting/baker.js +24 −2
@@ -51,6 +51,29 @@ export function rayReaches (fromX, fromY, toX, toY, isPassable)
51 51 return true;
52 52 }
53 53
54 export function falloffAtDistance (dist, range)
55 {
56 if (range <= 0 || dist > range)
57 { return 0; }
58
59 const t = 1 - dist / range;
60 return t * t * (3 - 2 * t);
61 }
62
63 export function encodeLight (dist, range)
64 {
65 const falloff = falloffAtDistance(dist, range);
66 if (falloff <= 0)
67 { return 0; }
68
69 return Math.max(1, Math.round(255 * falloff));
70 }
71
72 export function lightIntensity (encoded)
73 {
74 return Math.min(1, Math.max(0, (encoded ?? 0) / 255));
75 }
76
54 77 export function maxDefinitionLightRange (blockDefs)
55 78 {
56 79 let max = 0;
@@ -106,8 +129,7 @@ export function bakeLightMaps (centerChunkX, centerChunkY, loadChunk, maxRange)
106 129 { continue; }
107 130 if (rayReaches(source.tileX, source.tileY, tileX, tileY, isPassable))
108 131 {
109 const encoded = Math.max(1, Math.round(255 * (1 - dist / (source.range + 1))));
110 best = Math.max(best, encoded);
132 best = Math.max(best, encodeLight(dist, source.range));
111 133 }
112 134 }
113 135 }
@@ -51,6 +51,29 @@ export function rayReaches (fromX, fromY, toX, toY, isPassable)
51 return true;
51 return true;
52}
52}
53
53
54export function falloffAtDistance (dist, range)
55{
56 if (range <= 0 || dist > range)
57 { return 0; }
58
59 const t = 1 - dist / range;
60 return t * t * (3 - 2 * t);
61}
62
63export function encodeLight (dist, range)
64{
65 const falloff = falloffAtDistance(dist, range);
66 if (falloff <= 0)
67 { return 0; }
68
69 return Math.max(1, Math.round(255 * falloff));
70}
71
72export function lightIntensity (encoded)
73{
74 return Math.min(1, Math.max(0, (encoded ?? 0) / 255));
75}
76
54export function maxDefinitionLightRange (blockDefs)
77export function maxDefinitionLightRange (blockDefs)
55{
78{
56 let max = 0;
79 let max = 0;
@@ -106,8 +129,7 @@ export function bakeLightMaps (centerChunkX, centerChunkY, loadChunk, maxRange)
106 { continue; }
129 { continue; }
107 if (rayReaches(source.tileX, source.tileY, tileX, tileY, isPassable))
130 if (rayReaches(source.tileX, source.tileY, tileX, tileY, isPassable))
108 {
131 {
109 const encoded = Math.max(1, Math.round(255 * (1 - dist / (source.range + 1))));
132 best = Math.max(best, encodeLight(dist, source.range));
110 best = Math.max(best, encoded);
111 }
133 }
112 }
134 }
113 }
135 }
server/tests/lighting.test.js +2 −0
@@ -36,6 +36,8 @@ test("a torch lights passable tiles and not tiles behind walls", () =>
36 36 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
37 37 assert.ok(self.light[0] > 0, "torch tile is lit");
38 38 assert.ok(self.light[1] > 0, "tile beside torch is lit");
39 assert.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour");
40 assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south");
39 41 assert.equal(self.light[2], 0, "wall is unlit");
40 42 assert.equal(self.light[3], 0, "tile behind wall is unlit");
41 43 assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE);
@@ -36,6 +36,8 @@ test("a torch lights passable tiles and not tiles behind walls", () =>
36 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
36 const self = baked.find((chunk) => chunk.chunkX === 0 && chunk.chunkY === 0).data;
37 assert.ok(self.light[0] > 0, "torch tile is lit");
37 assert.ok(self.light[0] > 0, "torch tile is lit");
38 assert.ok(self.light[1] > 0, "tile beside torch is lit");
38 assert.ok(self.light[1] > 0, "tile beside torch is lit");
39 assert.ok(self.light[0] > self.light[1], "torch tile is brighter than its neighbour");
40 assert.ok(self.light[1] > self.light[32], "one tile away is brighter than two tiles south");
39 assert.equal(self.light[2], 0, "wall is unlit");
41 assert.equal(self.light[2], 0, "wall is unlit");
40 assert.equal(self.light[3], 0, "tile behind wall is unlit");
42 assert.equal(self.light[3], 0, "tile behind wall is unlit");
41 assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE);
43 assert.equal(self.light.length, CHUNK_SIZE * CHUNK_SIZE);
tile_mmo_server_technical_design_v0.6.md +4 −2
@@ -573,8 +573,10 @@ Neighbor rebakes use light sources and walls from the pending revision,
573 573 including the chunk just saved. Those neighbor light maps are written
574 574 into the same pending revision so play never mixes old and new lighting.
575 575
576 The light map is stored in the chunk `data_blob` (a 16×16 reachable-or-
577 not mask, optionally with distance to the nearest reaching source). It is
576 The light map is stored in the chunk `data_blob` as a 16×16 intensity
577 (0 = unreachable, 1–255 = falloff by distance from the strongest
578 reaching source). The client darkens tiles by that intensity at night,
579 so a torch fades tile-by-tile rather than lighting a hard disc. It is
578 580 replicated to clients in `CHUNK_SNAPSHOT` / `CHUNK_DELTA`.
579 581
580 582 Runtime door open/close does not rebake static light unless a later
@@ -573,8 +573,10 @@ Neighbor rebakes use light sources and walls from the pending revision,
573including the chunk just saved. Those neighbor light maps are written
573including the chunk just saved. Those neighbor light maps are written
574into the same pending revision so play never mixes old and new lighting.
574into the same pending revision so play never mixes old and new lighting.
575
575
576The light map is stored in the chunk `data_blob` (a 16×16 reachable-or-
576The light map is stored in the chunk `data_blob` as a 16×16 intensity
577not mask, optionally with distance to the nearest reaching source). It is
577(0 = unreachable, 1–255 = falloff by distance from the strongest
578reaching source). The client darkens tiles by that intensity at night,
579so a torch fades tile-by-tile rather than lighting a hard disc. It is
578replicated to clients in `CHUNK_SNAPSHOT` / `CHUNK_DELTA`.
580replicated to clients in `CHUNK_SNAPSHOT` / `CHUNK_DELTA`.
579
581
580Runtime door open/close does not rebake static light unless a later
582Runtime door open/close does not rebake static light unless a later