BlueGit

timfa questward

Pre-bake chunk light occlusion and limit dynamic lighting to 3×3 chunks.

Store per-tile occlusion on editor save for fast raycasts; only run torch/entity lighting in the nine chunks around the player while distant chunks keep baked torch light and ambient darkness.

timfa Tim Falken · pushed by timfa

Authored 2026-08-28 · 2 months ago

Hash
cc8e77f1d80f1e625a28ac6b54afbcf97f8700eb
Parent
9a3f48d2
6 files changed +120 −16
client/src/render/worldView.js +40 −3
@@ -1,10 +1,11 @@
1 1 import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT, DEFAULT_WORLD_SETTINGS, PIVOT_FROM_BOTTOM_PX, ENTITY_STACK_SPREAD_DIAMETER_PX } from "/shared/protocol/constants.js";
2 2 import { dynamicLightAtTile, lightIntensity } from "/shared/lighting/raycast.js";
3 3 import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";
4 import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";
4 5 import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";
5 6 import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
6 7 import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
7 import { normalizeGround, isWadeGroundInSeason, isWaterGround, isLiquidWaterGround, isGroundPassable, isBlockOccludingLight } from "/shared/definitions/tiles.js";
8 import { normalizeGround, isWadeGroundInSeason, isWaterGround, isLiquidWaterGround, isGroundPassable, isBlockOccludingLight, LightOcclusion } from "/shared/definitions/tiles.js";
8 9 import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js";
9 10 import { bakeChunkAtlases } from "./chunkBake.js";
10 11 import
@@ -536,6 +537,10 @@ export class WorldView
536 537 if (normalizeDimensionId(this.dimensionId) !== snapDim)
537 538 { return; }
538 539 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
540 if (!Array.isArray(snapshot.occlusion) || snapshot.occlusion.length < CHUNK_SIZE * CHUNK_SIZE)
541 {
542 snapshot.occlusion = bakeChunkOcclusion(snapshot);
543 }
539 544 this.chunks.set(key, snapshot);
540 545 this.#scheduleChunkRebuild(snapshot);
541 546 this.#redrawPreview();
@@ -3011,6 +3016,34 @@ export class WorldView
3011 3016 this.scene._sortDirty = true;
3012 3017 }
3013 3018
3019 /** Player chunk for dynamic lighting; null in editor / before spawn → no 3×3 limit. */
3020 #activeLightingCenterChunk ()
3021 {
3022 const local = this.entities.get(this.localEntityId);
3023 if (!local)
3024 { return null; }
3025 return {
3026 chunkX: Math.floor(local.tileX / CHUNK_SIZE),
3027 chunkY: Math.floor(local.tileY / CHUNK_SIZE)
3028 };
3029 }
3030
3031 #isActiveLightingChunk (chunkX, chunkY)
3032 {
3033 const center = this.#activeLightingCenterChunk();
3034 if (!center)
3035 { return true; }
3036 return Math.abs(chunkX - center.chunkX) <= 1 && Math.abs(chunkY - center.chunkY) <= 1;
3037 }
3038
3039 #isActiveLightingTile (tileX, tileY)
3040 {
3041 return this.#isActiveLightingChunk(
3042 Math.floor(tileX / CHUNK_SIZE),
3043 Math.floor(tileY / CHUNK_SIZE)
3044 );
3045 }
3046
3014 3047 #updateShade ()
3015 3048 {
3016 3049 const ambientDark = this.#ambientDarkness();
@@ -3057,7 +3090,8 @@ export class WorldView
3057 3090 {
3058 3091 if (objects._inViewport === false)
3059 3092 { continue; }
3060 const intensity = this.#tileLightIntensity(objects, activeLights, lightOcclusion);
3093 const lights = this.#isActiveLightingTile(objects.tileX, objects.tileY) ? activeLights : [];
3094 const intensity = this.#tileLightIntensity(objects, lights, lightOcclusion);
3061 3095 objects.shadeAlpha = ambientDark * (1 - intensity);
3062 3096 }
3063 3097
@@ -3065,7 +3099,8 @@ export class WorldView
3065 3099 {
3066 3100 if (baked._inViewport === false)
3067 3101 { continue; }
3068 this.#writeChunkLightmap(baked, activeLights, lightOcclusion);
3102 const lights = this.#isActiveLightingChunk(baked.chunkX, baked.chunkY) ? activeLights : [];
3103 this.#writeChunkLightmap(baked, lights, lightOcclusion);
3069 3104 if (baked.groundObject?.renderer?.shaderUniforms)
3070 3105 { baked.groundObject.renderer.shaderUniforms.u_ambientDark = ambientDark; }
3071 3106 for (const object of baked.blockObjects ?? [])
@@ -3142,6 +3177,8 @@ export class WorldView
3142 3177 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
3143 3178 { return false; }
3144 3179 const index = localY * CHUNK_SIZE + localX;
3180 if (Array.isArray(snapshot.occlusion) && snapshot.occlusion.length > index)
3181 { return (snapshot.occlusion[index] | 0) >= LightOcclusion.solid; }
3145 3182 const ground = normalizeGround(snapshot.grounds?.[index]);
3146 3183 if (!isGroundPassable(ground.typeId))
3147 3184 { return true; }
@@ -1,10 +1,11 @@
1import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT, DEFAULT_WORLD_SETTINGS, PIVOT_FROM_BOTTOM_PX, ENTITY_STACK_SPREAD_DIAMETER_PX } from "/shared/protocol/constants.js";
1import { CHUNK_SIZE, SPRITE_SIZE_PX, GAIT, DEFAULT_WORLD_SETTINGS, PIVOT_FROM_BOTTOM_PX, ENTITY_STACK_SPREAD_DIAMETER_PX } from "/shared/protocol/constants.js";
2import { dynamicLightAtTile, lightIntensity } from "/shared/lighting/raycast.js";
2import { dynamicLightAtTile, lightIntensity } from "/shared/lighting/raycast.js";
3import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";
3import { makeSnapshotLightOcclusion } from "/shared/lighting/tilePassable.js";
4import { bakeChunkOcclusion } from "/shared/lighting/chunkOcclusion.js";
4import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";
5import { bloomCompanionName, spriteImageName } from "/shared/definitions/tileSprites.js";
5import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
6import { EQUIPMENT_SLOT, itemEquipSprite, itemSpriteName, itemDefinition } from "/shared/definitions/items.js";
6import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
7import { resolveItemTint, resolveEquipTint, resolveItemDualTint, normalizeRgb } from "/shared/render/tint.js";
7import { normalizeGround, isWadeGroundInSeason, isWaterGround, isLiquidWaterGround, isGroundPassable, isBlockOccludingLight } from "/shared/definitions/tiles.js";
8import { normalizeGround, isWadeGroundInSeason, isWaterGround, isLiquidWaterGround, isGroundPassable, isBlockOccludingLight, LightOcclusion } from "/shared/definitions/tiles.js";
8import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js";
9import { spriteCenterFromPivot, spriteDrawSize, tilePivot, ySortLayer } from "/shared/render/pivot.js";
9import { bakeChunkAtlases } from "./chunkBake.js";
10import { bakeChunkAtlases } from "./chunkBake.js";
10import
11import
@@ -536,6 +537,10 @@ export class WorldView
536 if (normalizeDimensionId(this.dimensionId) !== snapDim)
537 if (normalizeDimensionId(this.dimensionId) !== snapDim)
537 { return; }
538 { return; }
538 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
539 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
540 if (!Array.isArray(snapshot.occlusion) || snapshot.occlusion.length < CHUNK_SIZE * CHUNK_SIZE)
541 {
542 snapshot.occlusion = bakeChunkOcclusion(snapshot);
543 }
539 this.chunks.set(key, snapshot);
544 this.chunks.set(key, snapshot);
540 this.#scheduleChunkRebuild(snapshot);
545 this.#scheduleChunkRebuild(snapshot);
541 this.#redrawPreview();
546 this.#redrawPreview();
@@ -3011,6 +3016,34 @@ export class WorldView
3011 this.scene._sortDirty = true;
3016 this.scene._sortDirty = true;
3012 }
3017 }
3013
3018
3019 /** Player chunk for dynamic lighting; null in editor / before spawn → no 3×3 limit. */
3020 #activeLightingCenterChunk ()
3021 {
3022 const local = this.entities.get(this.localEntityId);
3023 if (!local)
3024 { return null; }
3025 return {
3026 chunkX: Math.floor(local.tileX / CHUNK_SIZE),
3027 chunkY: Math.floor(local.tileY / CHUNK_SIZE)
3028 };
3029 }
3030
3031 #isActiveLightingChunk (chunkX, chunkY)
3032 {
3033 const center = this.#activeLightingCenterChunk();
3034 if (!center)
3035 { return true; }
3036 return Math.abs(chunkX - center.chunkX) <= 1 && Math.abs(chunkY - center.chunkY) <= 1;
3037 }
3038
3039 #isActiveLightingTile (tileX, tileY)
3040 {
3041 return this.#isActiveLightingChunk(
3042 Math.floor(tileX / CHUNK_SIZE),
3043 Math.floor(tileY / CHUNK_SIZE)
3044 );
3045 }
3046
3014 #updateShade ()
3047 #updateShade ()
3015 {
3048 {
3016 const ambientDark = this.#ambientDarkness();
3049 const ambientDark = this.#ambientDarkness();
@@ -3057,7 +3090,8 @@ export class WorldView
3057 {
3090 {
3058 if (objects._inViewport === false)
3091 if (objects._inViewport === false)
3059 { continue; }
3092 { continue; }
3060 const intensity = this.#tileLightIntensity(objects, activeLights, lightOcclusion);
3093 const lights = this.#isActiveLightingTile(objects.tileX, objects.tileY) ? activeLights : [];
3094 const intensity = this.#tileLightIntensity(objects, lights, lightOcclusion);
3061 objects.shadeAlpha = ambientDark * (1 - intensity);
3095 objects.shadeAlpha = ambientDark * (1 - intensity);
3062 }
3096 }
3063
3097
@@ -3065,7 +3099,8 @@ export class WorldView
3065 {
3099 {
3066 if (baked._inViewport === false)
3100 if (baked._inViewport === false)
3067 { continue; }
3101 { continue; }
3068 this.#writeChunkLightmap(baked, activeLights, lightOcclusion);
3102 const lights = this.#isActiveLightingChunk(baked.chunkX, baked.chunkY) ? activeLights : [];
3103 this.#writeChunkLightmap(baked, lights, lightOcclusion);
3069 if (baked.groundObject?.renderer?.shaderUniforms)
3104 if (baked.groundObject?.renderer?.shaderUniforms)
3070 { baked.groundObject.renderer.shaderUniforms.u_ambientDark = ambientDark; }
3105 { baked.groundObject.renderer.shaderUniforms.u_ambientDark = ambientDark; }
3071 for (const object of baked.blockObjects ?? [])
3106 for (const object of baked.blockObjects ?? [])
@@ -3142,6 +3177,8 @@ export class WorldView
3142 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
3177 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
3143 { return false; }
3178 { return false; }
3144 const index = localY * CHUNK_SIZE + localX;
3179 const index = localY * CHUNK_SIZE + localX;
3180 if (Array.isArray(snapshot.occlusion) && snapshot.occlusion.length > index)
3181 { return (snapshot.occlusion[index] | 0) >= LightOcclusion.solid; }
3145 const ground = normalizeGround(snapshot.grounds?.[index]);
3182 const ground = normalizeGround(snapshot.grounds?.[index]);
3146 if (!isGroundPassable(ground.typeId))
3183 if (!isGroundPassable(ground.typeId))
3147 { return true; }
3184 { return true; }
server/src/simulation/workerWorld.js +2 −0
@@ -1821,6 +1821,7 @@ export class WorkerWorld
1821 1821 grounds: data.grounds,
1822 1822 blocks: data.blocks,
1823 1823 light: data.light,
1824 occlusion: data.occlusion,
1824 1825 entities: [],
1825 1826 groundItems: []
1826 1827 };
@@ -1844,6 +1845,7 @@ export class WorkerWorld
1844 1845 grounds: data.grounds,
1845 1846 blocks: data.blocks,
1846 1847 light: data.light,
1848 occlusion: data.occlusion,
1847 1849 entities,
1848 1850 groundItems: groundItemsInChunk(this, chunkX, chunkY)
1849 1851 };
@@ -1821,6 +1821,7 @@ export class WorkerWorld
1821 grounds: data.grounds,
1821 grounds: data.grounds,
1822 blocks: data.blocks,
1822 blocks: data.blocks,
1823 light: data.light,
1823 light: data.light,
1824 occlusion: data.occlusion,
1824 entities: [],
1825 entities: [],
1825 groundItems: []
1826 groundItems: []
1826 };
1827 };
@@ -1844,6 +1845,7 @@ export class WorkerWorld
1844 grounds: data.grounds,
1845 grounds: data.grounds,
1845 blocks: data.blocks,
1846 blocks: data.blocks,
1846 light: data.light,
1847 light: data.light,
1848 occlusion: data.occlusion,
1847 entities,
1849 entities,
1848 groundItems: groundItemsInChunk(this, chunkX, chunkY)
1850 groundItems: groundItemsInChunk(this, chunkX, chunkY)
1849 };
1851 };
server/src/world/chunkData.js +15 −11
@@ -1,16 +1,17 @@
1 1 import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
2 2 import {
3 GroundType,
4 3 BlockType,
4 GroundType,
5 5 isBlockPassable,
6 blockBlocksRangedAttacks,
7 6 blockLightOcclusionForBlock,
8 7 LightOcclusion,
9 8 isGroundPassable,
9 blockBlocksRangedAttacks,
10 10 blockLightRange,
11 11 normalizeGround,
12 12 normalizeBlock
13 13 } from "../../../shared/definitions/tiles.js";
14 import { tileLightOcclusionAt, bakeChunkOcclusion, ensureChunkOcclusion } from "../../../shared/lighting/chunkOcclusion.js";
14 15 import {
15 16 flowerBlockState,
16 17 flowerColorIdFromBlockState,
@@ -41,19 +42,22 @@ export function cloneChunkData (data)
41 42 grounds: data.grounds.map((ground) => normalizeGround(ground)),
42 43 blocks: data.blocks.map((block) => structuredClone(normalizeBlock(block))),
43 44 light: data.light.slice(),
45 occlusion: ensureChunkOcclusion(data).slice(),
44 46 spawners: (data.spawners ?? []).map((entry) => normalizeSpawner(entry)).filter(Boolean)
45 47 };
46 48 }
47 49
48 50 export function encodeChunkBlob (data)
49 51 {
50 return JSON.stringify({
52 const normalized = {
51 53 open: !!data.open,
52 54 grounds: data.grounds.map((ground) => normalizeGround(ground)),
53 55 blocks: data.blocks.map((block) => normalizeBlock(block)),
54 56 light: data.light,
55 57 spawners: (data.spawners ?? []).map((entry) => normalizeSpawner(entry)).filter(Boolean)
56 });
58 };
59 normalized.occlusion = bakeChunkOcclusion(normalized);
60 return JSON.stringify(normalized);
57 61 }
58 62
59 63 export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId, chunkX = 0, chunkY = 0)
@@ -67,6 +71,7 @@ export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.def
67 71 const grounds = Array.isArray(parsed.grounds) ? parsed.grounds.slice(0, tileCount) : fallback.grounds;
68 72 const blocks = Array.isArray(parsed.blocks) ? parsed.blocks.slice(0, tileCount) : fallback.blocks.slice();
69 73 const light = Array.isArray(parsed.light) ? parsed.light.slice(0, tileCount) : fallback.light;
74 const occlusionRaw = Array.isArray(parsed.occlusion) ? parsed.occlusion.slice(0, tileCount) : null;
70 75
71 76 while (grounds.length < tileCount)
72 77 { grounds.push(normalizeGround(groundTypeId, groundTypeId)); }
@@ -79,7 +84,7 @@ export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.def
79 84 const originX = (chunkX | 0) * CHUNK_SIZE;
80 85 const originY = (chunkY | 0) * CHUNK_SIZE;
81 86
82 return {
87 const decoded = {
83 88 open: !!parsed.open,
84 89 grounds: grounds.map((ground) => normalizeGround(ground, groundTypeId)),
85 90 blocks: blocks.map((block, index) =>
@@ -100,6 +105,10 @@ export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.def
100 105 light,
101 106 spawners
102 107 };
108 decoded.occlusion = occlusionRaw?.length === tileCount
109 ? occlusionRaw.map((value) => value | 0)
110 : bakeChunkOcclusion(decoded);
111 return decoded;
103 112 }
104 113
105 114 export function normalizeSpawner (value)
@@ -178,12 +187,7 @@ export function isChunkTilePassable (data, localX, localY)
178 187 /** Light occlusion level for a chunk tile (ground impassable → solid). */
179 188 export function chunkTileLightOcclusion (data, localX, localY)
180 189 {
181 const index = tileIndex(localX, localY);
182 const ground = normalizeGround(data.grounds[index]);
183 if (!isGroundPassable(ground.typeId))
184 { return LightOcclusion.solid; }
185 const block = data.blocks[index];
186 return blockLightOcclusionForBlock(block);
190 return tileLightOcclusionAt(data.grounds, data.blocks, localX, localY);
187 191 }
188 192
189 193 /** True when light rays are not solid-stopped (partial canopy still clears). */
@@ -1,16 +1,17 @@
1import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
1import { CHUNK_SIZE, DEFAULT_WORLD_SETTINGS } from "../../../shared/protocol/constants.js";
2import {
2import {
3 GroundType,
4 BlockType,
3 BlockType,
4 GroundType,
5 isBlockPassable,
5 isBlockPassable,
6 blockBlocksRangedAttacks,
7 blockLightOcclusionForBlock,
6 blockLightOcclusionForBlock,
8 LightOcclusion,
7 LightOcclusion,
9 isGroundPassable,
8 isGroundPassable,
9 blockBlocksRangedAttacks,
10 blockLightRange,
10 blockLightRange,
11 normalizeGround,
11 normalizeGround,
12 normalizeBlock
12 normalizeBlock
13} from "../../../shared/definitions/tiles.js";
13} from "../../../shared/definitions/tiles.js";
14import { tileLightOcclusionAt, bakeChunkOcclusion, ensureChunkOcclusion } from "../../../shared/lighting/chunkOcclusion.js";
14import {
15import {
15 flowerBlockState,
16 flowerBlockState,
16 flowerColorIdFromBlockState,
17 flowerColorIdFromBlockState,
@@ -41,19 +42,22 @@ export function cloneChunkData (data)
41 grounds: data.grounds.map((ground) => normalizeGround(ground)),
42 grounds: data.grounds.map((ground) => normalizeGround(ground)),
42 blocks: data.blocks.map((block) => structuredClone(normalizeBlock(block))),
43 blocks: data.blocks.map((block) => structuredClone(normalizeBlock(block))),
43 light: data.light.slice(),
44 light: data.light.slice(),
45 occlusion: ensureChunkOcclusion(data).slice(),
44 spawners: (data.spawners ?? []).map((entry) => normalizeSpawner(entry)).filter(Boolean)
46 spawners: (data.spawners ?? []).map((entry) => normalizeSpawner(entry)).filter(Boolean)
45 };
47 };
46}
48}
47
49
48export function encodeChunkBlob (data)
50export function encodeChunkBlob (data)
49{
51{
50 return JSON.stringify({
52 const normalized = {
51 open: !!data.open,
53 open: !!data.open,
52 grounds: data.grounds.map((ground) => normalizeGround(ground)),
54 grounds: data.grounds.map((ground) => normalizeGround(ground)),
53 blocks: data.blocks.map((block) => normalizeBlock(block)),
55 blocks: data.blocks.map((block) => normalizeBlock(block)),
54 light: data.light,
56 light: data.light,
55 spawners: (data.spawners ?? []).map((entry) => normalizeSpawner(entry)).filter(Boolean)
57 spawners: (data.spawners ?? []).map((entry) => normalizeSpawner(entry)).filter(Boolean)
56 });
58 };
59 normalized.occlusion = bakeChunkOcclusion(normalized);
60 return JSON.stringify(normalized);
57}
61}
58
62
59export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId, chunkX = 0, chunkY = 0)
63export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.defaultGroundTypeId, chunkX = 0, chunkY = 0)
@@ -67,6 +71,7 @@ export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.def
67 const grounds = Array.isArray(parsed.grounds) ? parsed.grounds.slice(0, tileCount) : fallback.grounds;
71 const grounds = Array.isArray(parsed.grounds) ? parsed.grounds.slice(0, tileCount) : fallback.grounds;
68 const blocks = Array.isArray(parsed.blocks) ? parsed.blocks.slice(0, tileCount) : fallback.blocks.slice();
72 const blocks = Array.isArray(parsed.blocks) ? parsed.blocks.slice(0, tileCount) : fallback.blocks.slice();
69 const light = Array.isArray(parsed.light) ? parsed.light.slice(0, tileCount) : fallback.light;
73 const light = Array.isArray(parsed.light) ? parsed.light.slice(0, tileCount) : fallback.light;
74 const occlusionRaw = Array.isArray(parsed.occlusion) ? parsed.occlusion.slice(0, tileCount) : null;
70
75
71 while (grounds.length < tileCount)
76 while (grounds.length < tileCount)
72 { grounds.push(normalizeGround(groundTypeId, groundTypeId)); }
77 { grounds.push(normalizeGround(groundTypeId, groundTypeId)); }
@@ -79,7 +84,7 @@ export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.def
79 const originX = (chunkX | 0) * CHUNK_SIZE;
84 const originX = (chunkX | 0) * CHUNK_SIZE;
80 const originY = (chunkY | 0) * CHUNK_SIZE;
85 const originY = (chunkY | 0) * CHUNK_SIZE;
81
86
82 return {
87 const decoded = {
83 open: !!parsed.open,
88 open: !!parsed.open,
84 grounds: grounds.map((ground) => normalizeGround(ground, groundTypeId)),
89 grounds: grounds.map((ground) => normalizeGround(ground, groundTypeId)),
85 blocks: blocks.map((block, index) =>
90 blocks: blocks.map((block, index) =>
@@ -100,6 +105,10 @@ export function decodeChunkBlob (blob, groundTypeId = DEFAULT_WORLD_SETTINGS.def
100 light,
105 light,
101 spawners
106 spawners
102 };
107 };
108 decoded.occlusion = occlusionRaw?.length === tileCount
109 ? occlusionRaw.map((value) => value | 0)
110 : bakeChunkOcclusion(decoded);
111 return decoded;
103}
112}
104
113
105export function normalizeSpawner (value)
114export function normalizeSpawner (value)
@@ -178,12 +187,7 @@ export function isChunkTilePassable (data, localX, localY)
178/** Light occlusion level for a chunk tile (ground impassable → solid). */
187/** Light occlusion level for a chunk tile (ground impassable → solid). */
179export function chunkTileLightOcclusion (data, localX, localY)
188export function chunkTileLightOcclusion (data, localX, localY)
180{
189{
181 const index = tileIndex(localX, localY);
190 return tileLightOcclusionAt(data.grounds, data.blocks, localX, localY);
182 const ground = normalizeGround(data.grounds[index]);
183 if (!isGroundPassable(ground.typeId))
184 { return LightOcclusion.solid; }
185 const block = data.blocks[index];
186 return blockLightOcclusionForBlock(block);
187}
191}
188
192
189/** True when light rays are not solid-stopped (partial canopy still clears). */
193/** True when light rays are not solid-stopped (partial canopy still clears). */
server/tests/lighting.test.js +13 −0
@@ -167,3 +167,16 @@ test("tree stumps do not occlude light rays", () =>
167 167 const pastTree = dynamicLightAtTile(2, 0, sources, tree);
168 168 assert.ok(pastStump > pastTree, "stump clears canopy penalty vs standing tree");
169 169 });
170
171 test("encodeChunkBlob stores pre-baked occlusion aligned with chunkTileLightOcclusion", async () =>
172 {
173 const { encodeChunkBlob, decodeChunkBlob, chunkTileLightOcclusion } = await import("../src/world/chunkData.js");
174 const data = createEmptyChunkData();
175 data.open = true;
176 data.blocks[0] = { typeId: BlockType.stoneWall, config: null };
177 const blob = encodeChunkBlob(data);
178 const decoded = decodeChunkBlob(blob);
179 assert.equal(decoded.occlusion.length, CHUNK_SIZE * CHUNK_SIZE);
180 assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0));
181 assert.equal(decoded.occlusion[1], LightOcclusion.clear);
182 });
@@ -167,3 +167,16 @@ test("tree stumps do not occlude light rays", () =>
167 const pastTree = dynamicLightAtTile(2, 0, sources, tree);
167 const pastTree = dynamicLightAtTile(2, 0, sources, tree);
168 assert.ok(pastStump > pastTree, "stump clears canopy penalty vs standing tree");
168 assert.ok(pastStump > pastTree, "stump clears canopy penalty vs standing tree");
169});
169});
170
171test("encodeChunkBlob stores pre-baked occlusion aligned with chunkTileLightOcclusion", async () =>
172{
173 const { encodeChunkBlob, decodeChunkBlob, chunkTileLightOcclusion } = await import("../src/world/chunkData.js");
174 const data = createEmptyChunkData();
175 data.open = true;
176 data.blocks[0] = { typeId: BlockType.stoneWall, config: null };
177 const blob = encodeChunkBlob(data);
178 const decoded = decodeChunkBlob(blob);
179 assert.equal(decoded.occlusion.length, CHUNK_SIZE * CHUNK_SIZE);
180 assert.equal(decoded.occlusion[0], chunkTileLightOcclusion(decoded, 0, 0));
181 assert.equal(decoded.occlusion[1], LightOcclusion.clear);
182});
shared/lighting/chunkOcclusion.js added +44 −0
@@ -0,0 +1,44 @@
1 import { CHUNK_SIZE } from "../protocol/constants.js";
2 import {
3 blockLightOcclusionForBlock,
4 isGroundPassable,
5 LightOcclusion,
6 normalizeGround
7 } from "../definitions/tiles.js";
8
9 /** Light occlusion for one tile inside a chunk snapshot or editor chunk data. */
10 export function tileLightOcclusionAt (grounds, blocks, localX, localY)
11 {
12 const index = localY * CHUNK_SIZE + localX;
13 const ground = normalizeGround(grounds?.[index]);
14 if (!isGroundPassable(ground.typeId))
15 { return LightOcclusion.solid; }
16 return blockLightOcclusionForBlock(blocks?.[index]);
17 }
18
19 /**
20 * Pre-bake per-tile light occlusion (0=clear, 1=partial, 2=solid) for a chunk.
21 * Stored on save so runtime raycasts avoid re-parsing grounds/blocks each step.
22 */
23 export function bakeChunkOcclusion (data)
24 {
25 const grounds = data?.grounds;
26 const blocks = data?.blocks;
27 const out = new Array(CHUNK_SIZE * CHUNK_SIZE);
28 for (let localY = 0; localY < CHUNK_SIZE; localY++)
29 {
30 for (let localX = 0; localX < CHUNK_SIZE; localX++)
31 {
32 const index = localY * CHUNK_SIZE + localX;
33 out[index] = tileLightOcclusionAt(grounds, blocks, localX, localY);
34 }
35 }
36 return out;
37 }
38
39 export function ensureChunkOcclusion (data)
40 {
41 if (Array.isArray(data?.occlusion) && data.occlusion.length >= CHUNK_SIZE * CHUNK_SIZE)
42 { return data.occlusion; }
43 return bakeChunkOcclusion(data);
44 }
@@ -0,0 +1,44 @@
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import {
3 blockLightOcclusionForBlock,
4 isGroundPassable,
5 LightOcclusion,
6 normalizeGround
7} from "../definitions/tiles.js";
8
9/** Light occlusion for one tile inside a chunk snapshot or editor chunk data. */
10export function tileLightOcclusionAt (grounds, blocks, localX, localY)
11{
12 const index = localY * CHUNK_SIZE + localX;
13 const ground = normalizeGround(grounds?.[index]);
14 if (!isGroundPassable(ground.typeId))
15 { return LightOcclusion.solid; }
16 return blockLightOcclusionForBlock(blocks?.[index]);
17}
18
19/**
20 * Pre-bake per-tile light occlusion (0=clear, 1=partial, 2=solid) for a chunk.
21 * Stored on save so runtime raycasts avoid re-parsing grounds/blocks each step.
22 */
23export function bakeChunkOcclusion (data)
24{
25 const grounds = data?.grounds;
26 const blocks = data?.blocks;
27 const out = new Array(CHUNK_SIZE * CHUNK_SIZE);
28 for (let localY = 0; localY < CHUNK_SIZE; localY++)
29 {
30 for (let localX = 0; localX < CHUNK_SIZE; localX++)
31 {
32 const index = localY * CHUNK_SIZE + localX;
33 out[index] = tileLightOcclusionAt(grounds, blocks, localX, localY);
34 }
35 }
36 return out;
37}
38
39export function ensureChunkOcclusion (data)
40{
41 if (Array.isArray(data?.occlusion) && data.occlusion.length >= CHUNK_SIZE * CHUNK_SIZE)
42 { return data.occlusion; }
43 return bakeChunkOcclusion(data);
44}
shared/lighting/tilePassable.js +6 −2
@@ -9,6 +9,7 @@ import {
9 9 /**
10 10 * Light occlusion lookup from a chunk snapshot map keyed by "chunkX,chunkY".
11 11 * Returns {@link LightOcclusion} levels (not movement passability).
12 * Uses pre-baked {@link snapshot.occlusion} when present (see editor save / decodeChunkBlob).
12 13 */
13 14 export function makeSnapshotLightOcclusion (chunks)
14 15 {
@@ -21,12 +22,15 @@ export function makeSnapshotLightOcclusion (chunks)
21 22 { return LightOcclusion.clear; }
22 23 const localX = tileX - chunkX * CHUNK_SIZE;
23 24 const localY = tileY - chunkY * CHUNK_SIZE;
25 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
26 { return LightOcclusion.clear; }
24 27 const index = localY * CHUNK_SIZE + localX;
28 if (Array.isArray(snapshot.occlusion) && snapshot.occlusion.length > index)
29 { return snapshot.occlusion[index] | 0; }
25 30 const ground = normalizeGround(snapshot.grounds?.[index]);
26 31 if (!isGroundPassable(ground.typeId))
27 32 { return LightOcclusion.solid; }
28 const block = snapshot.blocks?.[index];
29 return blockLightOcclusionForBlock(block);
33 return blockLightOcclusionForBlock(snapshot.blocks?.[index]);
30 34 };
31 35 }
32 36
@@ -9,6 +9,7 @@ import {
9/**
9/**
10 * Light occlusion lookup from a chunk snapshot map keyed by "chunkX,chunkY".
10 * Light occlusion lookup from a chunk snapshot map keyed by "chunkX,chunkY".
11 * Returns {@link LightOcclusion} levels (not movement passability).
11 * Returns {@link LightOcclusion} levels (not movement passability).
12 * Uses pre-baked {@link snapshot.occlusion} when present (see editor save / decodeChunkBlob).
12 */
13 */
13export function makeSnapshotLightOcclusion (chunks)
14export function makeSnapshotLightOcclusion (chunks)
14{
15{
@@ -21,12 +22,15 @@ export function makeSnapshotLightOcclusion (chunks)
21 { return LightOcclusion.clear; }
22 { return LightOcclusion.clear; }
22 const localX = tileX - chunkX * CHUNK_SIZE;
23 const localX = tileX - chunkX * CHUNK_SIZE;
23 const localY = tileY - chunkY * CHUNK_SIZE;
24 const localY = tileY - chunkY * CHUNK_SIZE;
25 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
26 { return LightOcclusion.clear; }
24 const index = localY * CHUNK_SIZE + localX;
27 const index = localY * CHUNK_SIZE + localX;
28 if (Array.isArray(snapshot.occlusion) && snapshot.occlusion.length > index)
29 { return snapshot.occlusion[index] | 0; }
25 const ground = normalizeGround(snapshot.grounds?.[index]);
30 const ground = normalizeGround(snapshot.grounds?.[index]);
26 if (!isGroundPassable(ground.typeId))
31 if (!isGroundPassable(ground.typeId))
27 { return LightOcclusion.solid; }
32 { return LightOcclusion.solid; }
28 const block = snapshot.blocks?.[index];
33 return blockLightOcclusionForBlock(snapshot.blocks?.[index]);
29 return blockLightOcclusionForBlock(block);
30 };
34 };
31}
35}
32
36