Cache per-chunk lightmaps when lighting inputs are unchanged.
Skip raycasts and GPU uploads until lights move, the 3×3 zone shifts, or neighbor chunk baked light updates.
@@ -2648,6 +2648,8 @@ export class WorldView
2648
2648
}
2649
2649
baked.chunkX = snapshot.chunkX;
2650
2650
baked.chunkY = snapshot.chunkY;
2651
baked.lightmapCacheKey = null;
2652
this.#invalidateNeighborLightmapCaches(snapshot.chunkX, snapshot.chunkY);
2651
2653
this.#layoutBakedChunk(snapshot);
2652
2654
this.#queueBakedChunkTextures(baked);
2653
2655
}
@@ -3272,6 +3274,60 @@ export class WorldView
3272
3274
};
3273
3275
}
3274
3276
3277
#hashUint8Array (values)
3278
{
3279
let hash = 2166136261;
3280
for (let i = 0; i < values.length; i++)
3281
{
3282
hash ^= values[i];
3283
hash = Math.imul(hash, 16777619);
3284
}
3285
return hash >>> 0;
3286
}
3287
3288
#lightAffectsChunkBounds (light, chunkX, chunkY)
3289
{
3290
const minX = chunkX * CHUNK_SIZE - 1;
3291
const minY = chunkY * CHUNK_SIZE - 1;
3292
const maxX = chunkX * CHUNK_SIZE + CHUNK_SIZE;
3293
const maxY = chunkY * CHUNK_SIZE + CHUNK_SIZE;
3294
const closestX = Math.max(minX, Math.min(light.tileX, maxX));
3295
const closestY = Math.max(minY, Math.min(light.tileY, maxY));
3296
return Math.hypot(light.tileX - closestX, light.tileY - closestY) <= light.range;
3297
}
3298
3299
#chunkLightmapCacheKey (baked, activeLights)
3300
{
3301
const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
3302
if (!snapshot?.light)
3303
{ return null; }
3304
const bakedHash = this.#hashUint8Array(snapshot.light);
3305
if (!this.#isActiveLightingChunk(baked.chunkX, baked.chunkY))
3306
{ return `i:${bakedHash}`; }
3307
const mode = this.lightingMode === "simple" ? "s" : "f";
3308
let lightsKey = "";
3309
for (const light of activeLights)
3310
{
3311
if (!this.#lightAffectsChunkBounds(light, baked.chunkX, baked.chunkY))
3312
{ continue; }
3313
lightsKey += `${light.tileX},${light.tileY},${light.range},${light.intensity ?? 1};`;
3314
}
3315
return `a:${mode}:${bakedHash}:${lightsKey}`;
3316
}
3317
3318
#invalidateNeighborLightmapCaches (chunkX, chunkY)
3319
{
3320
for (let dy = -1; dy <= 1; dy++)
3321
{
3322
for (let dx = -1; dx <= 1; dx++)
3323
{
3324
const baked = this.bakedChunks.get(`${chunkX + dx},${chunkY + dy}`);
3325
if (baked)
3326
{ baked.lightmapCacheKey = null; }
3327
}
3328
}
3329
}
3330
3275
3331
/**
3276
3332
* Per-chunk lightmap for atlas ground/blocks. Open tiles blend into open neighbours;
3277
3333
* occluding tiles stay flat and contribute 0 into others so light does not bleed
@@ -3282,6 +3338,9 @@ export class WorldView
3282
3338
const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);
3283
3339
if (!snapshot)
3284
3340
{ return; }
3341
const cacheKey = this.#chunkLightmapCacheKey(baked, activeLights);
3342
if (cacheKey && baked.lightmapCacheKey === cacheKey)
3343
{ return; }
3285
3344
const atlasPx = CHUNK_SIZE * SPRITE_SIZE_PX;
3286
3345
const pixelCount = atlasPx * atlasPx * 4;
3287
3346
if (!baked.lightmapPixels || baked.lightmapPixels.length !== pixelCount)
@@ -3342,6 +3401,7 @@ export class WorldView
3342
3401
const ctx = baked.lightmapCanvas.getContext("2d");
3343
3402
ctx.putImageData(new ImageData(pixels, atlasPx, atlasPx), 0, 0);
3344
3403
invalidateCanvasTexture(this.scene, baked.lightmapCanvas);
3404
baked.lightmapCacheKey = cacheKey;
3345
3405
}
3346
3406
3347
3407
#updateSwipes (now)
@@ -2648,6 +2648,8 @@ export class WorldView2648
}2648
}2649
baked.chunkX = snapshot.chunkX;2649
baked.chunkX = snapshot.chunkX;2650
baked.chunkY = snapshot.chunkY;2650
baked.chunkY = snapshot.chunkY;2651
baked.lightmapCacheKey = null;2652
this.#invalidateNeighborLightmapCaches(snapshot.chunkX, snapshot.chunkY);2651
this.#layoutBakedChunk(snapshot);2653
this.#layoutBakedChunk(snapshot);2652
this.#queueBakedChunkTextures(baked);2654
this.#queueBakedChunkTextures(baked);2653
}2655
}@@ -3272,6 +3274,60 @@ export class WorldView3272
};3274
};3273
}3275
}3274
3276
3277
#hashUint8Array (values)3278
{3279
let hash = 2166136261;3280
for (let i = 0; i < values.length; i++)3281
{3282
hash ^= values[i];3283
hash = Math.imul(hash, 16777619);3284
}3285
return hash >>> 0;3286
}3287
3288
#lightAffectsChunkBounds (light, chunkX, chunkY)3289
{3290
const minX = chunkX * CHUNK_SIZE - 1;3291
const minY = chunkY * CHUNK_SIZE - 1;3292
const maxX = chunkX * CHUNK_SIZE + CHUNK_SIZE;3293
const maxY = chunkY * CHUNK_SIZE + CHUNK_SIZE;3294
const closestX = Math.max(minX, Math.min(light.tileX, maxX));3295
const closestY = Math.max(minY, Math.min(light.tileY, maxY));3296
return Math.hypot(light.tileX - closestX, light.tileY - closestY) <= light.range;3297
}3298
3299
#chunkLightmapCacheKey (baked, activeLights)3300
{3301
const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);3302
if (!snapshot?.light)3303
{ return null; }3304
const bakedHash = this.#hashUint8Array(snapshot.light);3305
if (!this.#isActiveLightingChunk(baked.chunkX, baked.chunkY))3306
{ return `i:${bakedHash}`; }3307
const mode = this.lightingMode === "simple" ? "s" : "f";3308
let lightsKey = "";3309
for (const light of activeLights)3310
{3311
if (!this.#lightAffectsChunkBounds(light, baked.chunkX, baked.chunkY))3312
{ continue; }3313
lightsKey += `${light.tileX},${light.tileY},${light.range},${light.intensity ?? 1};`;3314
}3315
return `a:${mode}:${bakedHash}:${lightsKey}`;3316
}3317
3318
#invalidateNeighborLightmapCaches (chunkX, chunkY)3319
{3320
for (let dy = -1; dy <= 1; dy++)3321
{3322
for (let dx = -1; dx <= 1; dx++)3323
{3324
const baked = this.bakedChunks.get(`${chunkX + dx},${chunkY + dy}`);3325
if (baked)3326
{ baked.lightmapCacheKey = null; }3327
}3328
}3329
}3330
3275
/**3331
/**3276
* Per-chunk lightmap for atlas ground/blocks. Open tiles blend into open neighbours;3332
* Per-chunk lightmap for atlas ground/blocks. Open tiles blend into open neighbours;3277
* occluding tiles stay flat and contribute 0 into others so light does not bleed3333
* occluding tiles stay flat and contribute 0 into others so light does not bleed@@ -3282,6 +3338,9 @@ export class WorldView3282
const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);3338
const snapshot = this.chunks.get(`${baked.chunkX},${baked.chunkY}`);3283
if (!snapshot)3339
if (!snapshot)3284
{ return; }3340
{ return; }3341
const cacheKey = this.#chunkLightmapCacheKey(baked, activeLights);3342
if (cacheKey && baked.lightmapCacheKey === cacheKey)3343
{ return; }3285
const atlasPx = CHUNK_SIZE * SPRITE_SIZE_PX;3344
const atlasPx = CHUNK_SIZE * SPRITE_SIZE_PX;3286
const pixelCount = atlasPx * atlasPx * 4;3345
const pixelCount = atlasPx * atlasPx * 4;3287
if (!baked.lightmapPixels || baked.lightmapPixels.length !== pixelCount)3346
if (!baked.lightmapPixels || baked.lightmapPixels.length !== pixelCount)@@ -3342,6 +3401,7 @@ export class WorldView3342
const ctx = baked.lightmapCanvas.getContext("2d");3401
const ctx = baked.lightmapCanvas.getContext("2d");3343
ctx.putImageData(new ImageData(pixels, atlasPx, atlasPx), 0, 0);3402
ctx.putImageData(new ImageData(pixels, atlasPx, atlasPx), 0, 0);3344
invalidateCanvasTexture(this.scene, baked.lightmapCanvas);3403
invalidateCanvasTexture(this.scene, baked.lightmapCanvas);3404
baked.lightmapCacheKey = cacheKey;3345
}3405
}3346
3406
3347
#updateSwipes (now)3407
#updateSwipes (now)