BlueGit

timfa questward

Sample tile shade from lightmap intensity grids.

Memoize per-chunk light lists for a shade pass and reuse the same grids the atlas lightmap already builds, so viewport tiles avoid a second full raycast sweep.

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
6f2c9841dd7fe7effaef82944941d92d5b021009
Parent
765343f2
1 file changed +99 −20
client/src/render/worldView.js +99 −20
@@ -3187,11 +3187,7 @@ export class WorldView
3187 3187 this.#setRendererVisible(objects.groundBloom, visible && this.bloomEnabled);
3188 3188 this.#setRendererVisible(objects.block, visible);
3189 3189 this.#setRendererVisible(objects.blockBloom, visible && this.bloomEnabled);
3190 if (objects.waterEdges)
3191 {
3192 for (const edge of Object.values(objects.waterEdges))
3193 { this.#setRendererVisible(edge, visible); }
3194 }
3190 this.#setRendererVisible(objects.lavaBloom, visible && this.bloomEnabled);
3195 3191 }
3196 3192 }
3197 3193
@@ -5568,6 +5564,45 @@ export class WorldView
5568 5564 return sources;
5569 5565 }
5570 5566
5567 /**
5568 * Lights for one chunk, memoized for a single shade pass.
5569 * Halo tiles in {@link #fillChunkLightGrids} often share the same chunk key.
5570 */
5571 #cachedLightsForChunk (chunkX, chunkY, mobileLights, blockLights, cache)
5572 {
5573 const key = `${chunkX},${chunkY}`;
5574 let lights = cache.get(key);
5575 if (!lights)
5576 {
5577 lights = this.#dynamicLightsForChunk(chunkX, chunkY, mobileLights, blockLights);
5578 cache.set(key, lights);
5579 }
5580 return lights;
5581 }
5582
5583 /**
5584 * Shade 0–1 from the chunk intensity grid (1 − lit). Same quantity
5585 * {@link #intensityAtTile} writes during lightmap fills — avoids a second
5586 * per-tile raycast pass after the grids are current.
5587 * @returns {number|null}
5588 */
5589 #shadeAlphaFromLightGrid (tileX, tileY)
5590 {
5591 const chunkX = Math.floor(tileX / CHUNK_SIZE);
5592 const chunkY = Math.floor(tileY / CHUNK_SIZE);
5593 const baked = this.bakedChunks.get(`${chunkX},${chunkY}`);
5594 const grid = baked?.lightIntensityGrid;
5595 if (!grid?.length)
5596 { return null; }
5597 const localX = tileX - chunkX * CHUNK_SIZE;
5598 const localY = tileY - chunkY * CHUNK_SIZE;
5599 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
5600 { return null; }
5601 const gridW = CHUNK_SIZE + 2;
5602 const lit = grid[(localY + 1) * gridW + (localX + 1)] ?? 0;
5603 return Math.min(1, Math.max(0, 1 - lit));
5604 }
5605
5571 5606 #updateShade ()
5572 5607 {
5573 5608 const ambientDark = this.#ambientDarkness();
@@ -5640,23 +5675,30 @@ export class WorldView
5640 5675 });
5641 5676 }
5642 5677 const blockLights = this.#collectBlockLightsForShading();
5643
5644 5678 const lightOcclusion = makeSnapshotLightOcclusion(this.chunks);
5679 const lightsByChunk = new Map();
5645 5680
5646 for (const objects of this.tiles.values())
5647 {
5648 if (objects._inViewport === false)
5649 { continue; }
5650 const lights = this.#dynamicLightsForTile(objects.tileX, objects.tileY, mobileLights, blockLights);
5651 objects.shadeAlpha = this.#tileShadeAlpha(objects, lights, lightOcclusion, ambientDark);
5652 }
5653
5681 // Lightmaps first so intensity grids are filled (or kept on cache hit).
5654 5682 for (const baked of this.bakedChunks.values())
5655 5683 {
5656 5684 if (baked._inViewport === false)
5657 5685 { continue; }
5658 const lights = this.#dynamicLightsForChunk(baked.chunkX, baked.chunkY, mobileLights, blockLights);
5659 this.#writeChunkLightmap(baked, lights, mobileLights, blockLights, lightOcclusion, ambientDark);
5686 const lights = this.#cachedLightsForChunk(
5687 baked.chunkX,
5688 baked.chunkY,
5689 mobileLights,
5690 blockLights,
5691 lightsByChunk
5692 );
5693 this.#writeChunkLightmap(
5694 baked,
5695 lights,
5696 mobileLights,
5697 blockLights,
5698 lightOcclusion,
5699 ambientDark,
5700 lightsByChunk
5701 );
5660 5702 // Lightmap stores visible light (1 − shade); shader applies darkness = 1 − lit.
5661 5703 if (baked.groundObject?.renderer?.shaderUniforms)
5662 5704 {
@@ -5673,6 +5715,27 @@ export class WorldView
5673 5715 }
5674 5716 }
5675 5717
5718 // Per-tile shade from the same grids the lightmap used — no second raycast sweep.
5719 for (const objects of this.tiles.values())
5720 {
5721 if (objects._inViewport === false)
5722 { continue; }
5723 const fromGrid = this.#shadeAlphaFromLightGrid(objects.tileX, objects.tileY);
5724 if (fromGrid != null)
5725 { objects.shadeAlpha = fromGrid; }
5726 else
5727 {
5728 const lights = this.#cachedLightsForChunk(
5729 objects.chunkX,
5730 objects.chunkY,
5731 mobileLights,
5732 blockLights,
5733 lightsByChunk
5734 );
5735 objects.shadeAlpha = this.#tileShadeAlpha(objects, lights, lightOcclusion, ambientDark);
5736 }
5737 }
5738
5676 5739 const timeSec = now / 1000;
5677 5740 for (const objects of this.tiles.values())
5678 5741 {
@@ -5790,7 +5853,7 @@ export class WorldView
5790 5853 return blockLightOcclusionForBlock(snapshot.blocks?.[index]) === LightOcclusion.solid;
5791 5854 }
5792 5855
5793 #fillChunkLightGrids (baked, mobileLights, blockLights, lightOcclusion, ambientDark)
5856 #fillChunkLightGrids (baked, mobileLights, blockLights, lightOcclusion, ambientDark, lightsByChunk = null)
5794 5857 {
5795 5858 const gridW = CHUNK_SIZE + 2;
5796 5859 const gridSize = gridW * gridW;
@@ -5800,6 +5863,7 @@ export class WorldView
5800 5863 { baked.lightOccludeGrid = new Uint8Array(gridSize); }
5801 5864 const grid = baked.lightIntensityGrid;
5802 5865 const occlude = baked.lightOccludeGrid;
5866 const cache = lightsByChunk ?? new Map();
5803 5867 for (let ly = -1; ly <= CHUNK_SIZE; ly++)
5804 5868 {
5805 5869 for (let lx = -1; lx <= CHUNK_SIZE; lx++)
@@ -5809,7 +5873,13 @@ export class WorldView
5809 5873 const gi = (ly + 1) * gridW + (lx + 1);
5810 5874 // Halo tiles may sit in the other lighting region (3×3 vs outer). Resolve
5811 5875 // sources per tile so corner blends match the neighbour chunk's shading.
5812 const lights = this.#dynamicLightsForTile(tileX, tileY, mobileLights, blockLights);
5876 const lights = this.#cachedLightsForChunk(
5877 Math.floor(tileX / CHUNK_SIZE),
5878 Math.floor(tileY / CHUNK_SIZE),
5879 mobileLights,
5880 blockLights,
5881 cache
5882 );
5813 5883 grid[gi] = this.#intensityAtTile(tileX, tileY, lights, lightOcclusion, ambientDark);
5814 5884 occlude[gi] = this.#tileOccludesLight(tileX, tileY) ? 1 : 0;
5815 5885 }
@@ -5941,6 +6011,7 @@ export class WorldView
5941 6011 * through a wall to the far side. Diagonals only when both cardinals are clear.
5942 6012 * @param chunkLights Sources that affect this chunk (cache key + in-chunk samples).
5943 6013 * @param mobileLights / blockLights Full lists so the ±1 halo can sample across the 3×3 boundary.
6014 * @param lightsByChunk Optional per-pass memo of chunk → light list.
5944 6015 */
5945 6016 #writeChunkLightmap (
5946 6017 baked,
@@ -5948,7 +6019,8 @@ export class WorldView
5948 6019 mobileLights,
5949 6020 blockLights,
5950 6021 lightOcclusion,
5951 ambientDark = this.#ambientDarkness()
6022 ambientDark = this.#ambientDarkness(),
6023 lightsByChunk = null
5952 6024 )
5953 6025 {
5954 6026 const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
@@ -5963,7 +6035,14 @@ export class WorldView
5963 6035 { baked.lightmapPixels = new Uint8ClampedArray(pixelCount); }
5964 6036 const pixels = baked.lightmapPixels;
5965 6037
5966 this.#fillChunkLightGrids(baked, mobileLights, blockLights, lightOcclusion, ambientDark);
6038 this.#fillChunkLightGrids(
6039 baked,
6040 mobileLights,
6041 blockLights,
6042 lightOcclusion,
6043 ambientDark,
6044 lightsByChunk
6045 );
5967 6046 const simple = this.lightingMode === "simple";
5968 6047
5969 6048 for (let localY = 0; localY < CHUNK_SIZE; localY++)
@@ -3187,11 +3187,7 @@ export class WorldView
3187 this.#setRendererVisible(objects.groundBloom, visible && this.bloomEnabled);
3187 this.#setRendererVisible(objects.groundBloom, visible && this.bloomEnabled);
3188 this.#setRendererVisible(objects.block, visible);
3188 this.#setRendererVisible(objects.block, visible);
3189 this.#setRendererVisible(objects.blockBloom, visible && this.bloomEnabled);
3189 this.#setRendererVisible(objects.blockBloom, visible && this.bloomEnabled);
3190 if (objects.waterEdges)
3190 this.#setRendererVisible(objects.lavaBloom, visible && this.bloomEnabled);
3191 {
3192 for (const edge of Object.values(objects.waterEdges))
3193 { this.#setRendererVisible(edge, visible); }
3194 }
3195 }
3191 }
3196 }
3192 }
3197
3193
@@ -5568,6 +5564,45 @@ export class WorldView
5568 return sources;
5564 return sources;
5569 }
5565 }
5570
5566
5567 /**
5568 * Lights for one chunk, memoized for a single shade pass.
5569 * Halo tiles in {@link #fillChunkLightGrids} often share the same chunk key.
5570 */
5571 #cachedLightsForChunk (chunkX, chunkY, mobileLights, blockLights, cache)
5572 {
5573 const key = `${chunkX},${chunkY}`;
5574 let lights = cache.get(key);
5575 if (!lights)
5576 {
5577 lights = this.#dynamicLightsForChunk(chunkX, chunkY, mobileLights, blockLights);
5578 cache.set(key, lights);
5579 }
5580 return lights;
5581 }
5582
5583 /**
5584 * Shade 0–1 from the chunk intensity grid (1 − lit). Same quantity
5585 * {@link #intensityAtTile} writes during lightmap fills — avoids a second
5586 * per-tile raycast pass after the grids are current.
5587 * @returns {number|null}
5588 */
5589 #shadeAlphaFromLightGrid (tileX, tileY)
5590 {
5591 const chunkX = Math.floor(tileX / CHUNK_SIZE);
5592 const chunkY = Math.floor(tileY / CHUNK_SIZE);
5593 const baked = this.bakedChunks.get(`${chunkX},${chunkY}`);
5594 const grid = baked?.lightIntensityGrid;
5595 if (!grid?.length)
5596 { return null; }
5597 const localX = tileX - chunkX * CHUNK_SIZE;
5598 const localY = tileY - chunkY * CHUNK_SIZE;
5599 if (localX < 0 || localY < 0 || localX >= CHUNK_SIZE || localY >= CHUNK_SIZE)
5600 { return null; }
5601 const gridW = CHUNK_SIZE + 2;
5602 const lit = grid[(localY + 1) * gridW + (localX + 1)] ?? 0;
5603 return Math.min(1, Math.max(0, 1 - lit));
5604 }
5605
5571 #updateShade ()
5606 #updateShade ()
5572 {
5607 {
5573 const ambientDark = this.#ambientDarkness();
5608 const ambientDark = this.#ambientDarkness();
@@ -5640,23 +5675,30 @@ export class WorldView
5640 });
5675 });
5641 }
5676 }
5642 const blockLights = this.#collectBlockLightsForShading();
5677 const blockLights = this.#collectBlockLightsForShading();
5643
5644 const lightOcclusion = makeSnapshotLightOcclusion(this.chunks);
5678 const lightOcclusion = makeSnapshotLightOcclusion(this.chunks);
5679 const lightsByChunk = new Map();
5645
5680
5646 for (const objects of this.tiles.values())
5681 // Lightmaps first so intensity grids are filled (or kept on cache hit).
5647 {
5648 if (objects._inViewport === false)
5649 { continue; }
5650 const lights = this.#dynamicLightsForTile(objects.tileX, objects.tileY, mobileLights, blockLights);
5651 objects.shadeAlpha = this.#tileShadeAlpha(objects, lights, lightOcclusion, ambientDark);
5652 }
5653
5654 for (const baked of this.bakedChunks.values())
5682 for (const baked of this.bakedChunks.values())
5655 {
5683 {
5656 if (baked._inViewport === false)
5684 if (baked._inViewport === false)
5657 { continue; }
5685 { continue; }
5658 const lights = this.#dynamicLightsForChunk(baked.chunkX, baked.chunkY, mobileLights, blockLights);
5686 const lights = this.#cachedLightsForChunk(
5659 this.#writeChunkLightmap(baked, lights, mobileLights, blockLights, lightOcclusion, ambientDark);
5687 baked.chunkX,
5688 baked.chunkY,
5689 mobileLights,
5690 blockLights,
5691 lightsByChunk
5692 );
5693 this.#writeChunkLightmap(
5694 baked,
5695 lights,
5696 mobileLights,
5697 blockLights,
5698 lightOcclusion,
5699 ambientDark,
5700 lightsByChunk
5701 );
5660 // Lightmap stores visible light (1 − shade); shader applies darkness = 1 − lit.
5702 // Lightmap stores visible light (1 − shade); shader applies darkness = 1 − lit.
5661 if (baked.groundObject?.renderer?.shaderUniforms)
5703 if (baked.groundObject?.renderer?.shaderUniforms)
5662 {
5704 {
@@ -5673,6 +5715,27 @@ export class WorldView
5673 }
5715 }
5674 }
5716 }
5675
5717
5718 // Per-tile shade from the same grids the lightmap used — no second raycast sweep.
5719 for (const objects of this.tiles.values())
5720 {
5721 if (objects._inViewport === false)
5722 { continue; }
5723 const fromGrid = this.#shadeAlphaFromLightGrid(objects.tileX, objects.tileY);
5724 if (fromGrid != null)
5725 { objects.shadeAlpha = fromGrid; }
5726 else
5727 {
5728 const lights = this.#cachedLightsForChunk(
5729 objects.chunkX,
5730 objects.chunkY,
5731 mobileLights,
5732 blockLights,
5733 lightsByChunk
5734 );
5735 objects.shadeAlpha = this.#tileShadeAlpha(objects, lights, lightOcclusion, ambientDark);
5736 }
5737 }
5738
5676 const timeSec = now / 1000;
5739 const timeSec = now / 1000;
5677 for (const objects of this.tiles.values())
5740 for (const objects of this.tiles.values())
5678 {
5741 {
@@ -5790,7 +5853,7 @@ export class WorldView
5790 return blockLightOcclusionForBlock(snapshot.blocks?.[index]) === LightOcclusion.solid;
5853 return blockLightOcclusionForBlock(snapshot.blocks?.[index]) === LightOcclusion.solid;
5791 }
5854 }
5792
5855
5793 #fillChunkLightGrids (baked, mobileLights, blockLights, lightOcclusion, ambientDark)
5856 #fillChunkLightGrids (baked, mobileLights, blockLights, lightOcclusion, ambientDark, lightsByChunk = null)
5794 {
5857 {
5795 const gridW = CHUNK_SIZE + 2;
5858 const gridW = CHUNK_SIZE + 2;
5796 const gridSize = gridW * gridW;
5859 const gridSize = gridW * gridW;
@@ -5800,6 +5863,7 @@ export class WorldView
5800 { baked.lightOccludeGrid = new Uint8Array(gridSize); }
5863 { baked.lightOccludeGrid = new Uint8Array(gridSize); }
5801 const grid = baked.lightIntensityGrid;
5864 const grid = baked.lightIntensityGrid;
5802 const occlude = baked.lightOccludeGrid;
5865 const occlude = baked.lightOccludeGrid;
5866 const cache = lightsByChunk ?? new Map();
5803 for (let ly = -1; ly <= CHUNK_SIZE; ly++)
5867 for (let ly = -1; ly <= CHUNK_SIZE; ly++)
5804 {
5868 {
5805 for (let lx = -1; lx <= CHUNK_SIZE; lx++)
5869 for (let lx = -1; lx <= CHUNK_SIZE; lx++)
@@ -5809,7 +5873,13 @@ export class WorldView
5809 const gi = (ly + 1) * gridW + (lx + 1);
5873 const gi = (ly + 1) * gridW + (lx + 1);
5810 // Halo tiles may sit in the other lighting region (3×3 vs outer). Resolve
5874 // Halo tiles may sit in the other lighting region (3×3 vs outer). Resolve
5811 // sources per tile so corner blends match the neighbour chunk's shading.
5875 // sources per tile so corner blends match the neighbour chunk's shading.
5812 const lights = this.#dynamicLightsForTile(tileX, tileY, mobileLights, blockLights);
5876 const lights = this.#cachedLightsForChunk(
5877 Math.floor(tileX / CHUNK_SIZE),
5878 Math.floor(tileY / CHUNK_SIZE),
5879 mobileLights,
5880 blockLights,
5881 cache
5882 );
5813 grid[gi] = this.#intensityAtTile(tileX, tileY, lights, lightOcclusion, ambientDark);
5883 grid[gi] = this.#intensityAtTile(tileX, tileY, lights, lightOcclusion, ambientDark);
5814 occlude[gi] = this.#tileOccludesLight(tileX, tileY) ? 1 : 0;
5884 occlude[gi] = this.#tileOccludesLight(tileX, tileY) ? 1 : 0;
5815 }
5885 }
@@ -5941,6 +6011,7 @@ export class WorldView
5941 * through a wall to the far side. Diagonals only when both cardinals are clear.
6011 * through a wall to the far side. Diagonals only when both cardinals are clear.
5942 * @param chunkLights Sources that affect this chunk (cache key + in-chunk samples).
6012 * @param chunkLights Sources that affect this chunk (cache key + in-chunk samples).
5943 * @param mobileLights / blockLights Full lists so the ±1 halo can sample across the 3×3 boundary.
6013 * @param mobileLights / blockLights Full lists so the ±1 halo can sample across the 3×3 boundary.
6014 * @param lightsByChunk Optional per-pass memo of chunk → light list.
5944 */
6015 */
5945 #writeChunkLightmap (
6016 #writeChunkLightmap (
5946 baked,
6017 baked,
@@ -5948,7 +6019,8 @@ export class WorldView
5948 mobileLights,
6019 mobileLights,
5949 blockLights,
6020 blockLights,
5950 lightOcclusion,
6021 lightOcclusion,
5951 ambientDark = this.#ambientDarkness()
6022 ambientDark = this.#ambientDarkness(),
6023 lightsByChunk = null
5952 )
6024 )
5953 {
6025 {
5954 const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
6026 const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
@@ -5963,7 +6035,14 @@ export class WorldView
5963 { baked.lightmapPixels = new Uint8ClampedArray(pixelCount); }
6035 { baked.lightmapPixels = new Uint8ClampedArray(pixelCount); }
5964 const pixels = baked.lightmapPixels;
6036 const pixels = baked.lightmapPixels;
5965
6037
5966 this.#fillChunkLightGrids(baked, mobileLights, blockLights, lightOcclusion, ambientDark);
6038 this.#fillChunkLightGrids(
6039 baked,
6040 mobileLights,
6041 blockLights,
6042 lightOcclusion,
6043 ambientDark,
6044 lightsByChunk
6045 );
5967 const simple = this.lightingMode === "simple";
6046 const simple = this.lightingMode === "simple";
5968
6047
5969 for (let localY = 0; localY < CHUNK_SIZE; localY++)
6048 for (let localY = 0; localY < CHUNK_SIZE; localY++)