BlueGit

timfa questward

Smooth chunk-boundary hitching from bake, nature, and lightmaps.

Defer soft-neighbor atlas rebakes across frames, skip nature Perlin refill when grounds are unchanged, and only bust lightmap caches for chunks entering or leaving the active 3×3 (plus a one-chunk halo).

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
75f28b1146dc4238fc95a3a35ef48c883214bcde
Parent
1af0a6fd
1 file changed +156 −37
client/src/render/worldView.js +156 −37
@@ -1038,6 +1038,8 @@ function getTightFitLitSpriteShader ()
1038 1038 export class WorldView
1039 1039 {
1040 1040 #chunkRebuildKeys = new Set();
1041 /** Soft-blend / wet-sand neighbor rebakes — deferred so they do not nest inside a primary bake. */
1042 #chunkSoftNeighborKeys = new Set();
1041 1043 #chunkRebuildRaf = 0;
1042 1044 #edgesDirty = false;
1043 1045 #CHUNK_REBUILD_BATCH = 2;
@@ -1213,6 +1215,7 @@ export class WorldView
1213 1215 {
1214 1216 this.#cancelIdlePrefetch();
1215 1217 this.#chunkRebuildKeys.clear();
1218 this.#chunkSoftNeighborKeys.clear();
1216 1219 this.#playerChunkX = null;
1217 1220 this.#playerChunkY = null;
1218 1221 this.#playerMoveChunkDx = 0;
@@ -4463,7 +4466,22 @@ export class WorldView
4463 4466 {
4464 4467 if (!snapshot)
4465 4468 { return; }
4466 this.#chunkRebuildKeys.add(`${snapshot.chunkX},${snapshot.chunkY}`);
4469 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
4470 this.#chunkRebuildKeys.add(key);
4471 this.#chunkSoftNeighborKeys.delete(key);
4472 this.#cancelIdlePrefetch();
4473 this.#ensureChunkFlushScheduled();
4474 }
4475
4476 /** Queue a neighbor atlas rebake without cascading further soft-neighbor work. */
4477 #scheduleSoftNeighborChunkRebuild (snapshot)
4478 {
4479 if (!snapshot)
4480 { return; }
4481 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
4482 if (this.#chunkRebuildKeys.has(key))
4483 { return; }
4484 this.#chunkSoftNeighborKeys.add(key);
4467 4485 this.#cancelIdlePrefetch();
4468 4486 this.#ensureChunkFlushScheduled();
4469 4487 }
@@ -4475,6 +4493,11 @@ export class WorldView
4475 4493 this.#ensureChunkFlushScheduled();
4476 4494 }
4477 4495
4496 #pendingChunkRebuildCount ()
4497 {
4498 return this.#chunkRebuildKeys.size + this.#chunkSoftNeighborKeys.size;
4499 }
4500
4478 4501 #ensureChunkFlushScheduled ()
4479 4502 {
4480 4503 if (this.#chunkRebuildRaf)
@@ -4507,8 +4530,7 @@ export class WorldView
4507 4530 /** When the 3×3 lighting center moves: refresh door sprites (resting vs live) and lightmaps. */
4508 4531 #onActiveLightingCenterChanged (prevChunkX, prevChunkY, chunkX, chunkY)
4509 4532 {
4510 this.#invalidateNeighborLightmapCaches(prevChunkX, prevChunkY);
4511 this.#invalidateNeighborLightmapCaches(chunkX, chunkY);
4533 this.#invalidateActiveLightingMoveCaches(prevChunkX, prevChunkY, chunkX, chunkY);
4512 4534 const doorChunks = new Set();
4513 4535 for (const [cx, cy] of [[prevChunkX, prevChunkY], [chunkX, chunkY]])
4514 4536 {
@@ -4525,6 +4547,51 @@ export class WorldView
4525 4547 }
4526 4548 }
4527 4549
4550 /**
4551 * Only bust lightmaps for chunks whose active-3×3 membership (or ±1 halo) changed.
4552 * Cache keys already track light positions; a full ±2 wipe around both centers was redundant.
4553 */
4554 #invalidateActiveLightingMoveCaches (prevChunkX, prevChunkY, nextChunkX, nextChunkY)
4555 {
4556 const prev = new Set();
4557 const next = new Set();
4558 for (let dy = -1; dy <= 1; dy++)
4559 {
4560 for (let dx = -1; dx <= 1; dx++)
4561 {
4562 prev.add(`${prevChunkX + dx},${prevChunkY + dy}`);
4563 next.add(`${nextChunkX + dx},${nextChunkY + dy}`);
4564 }
4565 }
4566 const changed = [];
4567 for (const key of prev)
4568 {
4569 if (!next.has(key))
4570 { changed.push(key); }
4571 }
4572 for (const key of next)
4573 {
4574 if (!prev.has(key))
4575 { changed.push(key); }
4576 }
4577 const bust = new Set();
4578 for (const key of changed)
4579 {
4580 const [cx, cy] = key.split(",").map(Number);
4581 for (let dy = -1; dy <= 1; dy++)
4582 {
4583 for (let dx = -1; dx <= 1; dx++)
4584 { bust.add(`${cx + dx},${cy + dy}`); }
4585 }
4586 }
4587 for (const key of bust)
4588 {
4589 const baked = this.bakedChunks.get(key);
4590 if (baked)
4591 { baked.lightmapCacheKey = null; }
4592 }
4593 }
4594
4528 4595 #redrawDoorTilesInChunk (chunkX, chunkY)
4529 4596 {
4530 4597 const snapshot = this.chunks.get(`${chunkX},${chunkY}`);
@@ -4579,9 +4646,9 @@ export class WorldView
4579 4646 return dist * 10 - Math.max(0, ahead) * 3;
4580 4647 }
4581 4648
4582 #sortedChunkRebuildKeys (mode = "urgent")
4649 #sortedChunkRebuildKeys (keySet, mode = "urgent")
4583 4650 {
4584 return [...this.#chunkRebuildKeys].sort((a, b) =>
4651 return [...keySet].sort((a, b) =>
4585 4652 this.#chunkRebuildScore(a, mode) - this.#chunkRebuildScore(b, mode));
4586 4653 }
4587 4654
@@ -4611,7 +4678,9 @@ export class WorldView
4611 4678 for (const snapshot of this.chunks.values())
4612 4679 {
4613 4680 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
4614 if (this.bakedChunks.has(key) || this.#chunkRebuildKeys.has(key))
4681 if (this.bakedChunks.has(key)
4682 || this.#chunkRebuildKeys.has(key)
4683 || this.#chunkSoftNeighborKeys.has(key))
4615 4684 { continue; }
4616 4685 const score = this.#chunkRebuildScore(key, "idle");
4617 4686 if (score < bestScore)
@@ -4632,7 +4701,7 @@ export class WorldView
4632 4701
4633 4702 #scheduleIdleChunkPrefetch ()
4634 4703 {
4635 if (this.#idlePrefetchId || this.#chunkRebuildRaf || this.#chunkRebuildKeys.size)
4704 if (this.#idlePrefetchId || this.#chunkRebuildRaf || this.#pendingChunkRebuildCount())
4636 4705 { return; }
4637 4706 if (typeof requestIdleCallback !== "function")
4638 4707 { return; }
@@ -4647,10 +4716,10 @@ export class WorldView
4647 4716 #runIdleChunkPrefetch (deadline)
4648 4717 {
4649 4718 this.#idlePrefetchId = 0;
4650 if (this.#chunkRebuildRaf || this.#chunkRebuildKeys.size)
4719 if (this.#chunkRebuildRaf || this.#pendingChunkRebuildCount())
4651 4720 { return; }
4652 4721 this.#discoverUnbakedChunks();
4653 if (this.#chunkRebuildKeys.size)
4722 if (this.#pendingChunkRebuildCount())
4654 4723 {
4655 4724 this.#ensureChunkFlushScheduled();
4656 4725 return;
@@ -4671,19 +4740,32 @@ export class WorldView
4671 4740 #flushChunkWork ()
4672 4741 {
4673 4742 this.#chunkRebuildRaf = 0;
4674 const keys = this.#sortedChunkRebuildKeys("urgent");
4675 const batch = keys.slice(0, this.#CHUNK_REBUILD_BATCH);
4676 const rest = keys.slice(this.#CHUNK_REBUILD_BATCH);
4677 this.#chunkRebuildKeys.clear();
4678 for (const key of rest)
4679 { this.#chunkRebuildKeys.add(key); }
4680 for (const key of batch)
4743 const primary = this.#sortedChunkRebuildKeys(this.#chunkRebuildKeys, "urgent");
4744 const soft = this.#sortedChunkRebuildKeys(this.#chunkSoftNeighborKeys, "urgent");
4745 let budget = this.#CHUNK_REBUILD_BATCH;
4746 const primaryBatch = primary.slice(0, budget);
4747 budget -= primaryBatch.length;
4748 const softBatch = soft.slice(0, Math.max(0, budget));
4749
4750 for (const key of primaryBatch)
4751 { this.#chunkRebuildKeys.delete(key); }
4752 for (const key of softBatch)
4753 { this.#chunkSoftNeighborKeys.delete(key); }
4754
4755 for (const key of primaryBatch)
4681 4756 {
4682 4757 const snap = this.chunks.get(key);
4683 4758 if (snap)
4684 4759 { this.#rebuildChunk(snap, { refreshSoftNeighbors: true }); }
4685 4760 }
4686 if (this.#chunkRebuildKeys.size)
4761 for (const key of softBatch)
4762 {
4763 const snap = this.chunks.get(key);
4764 if (snap)
4765 { this.#rebuildChunk(snap, { refreshSoftNeighbors: false }); }
4766 }
4767
4768 if (this.#pendingChunkRebuildCount())
4687 4769 {
4688 4770 this.#ensureChunkFlushScheduled();
4689 4771 return;
@@ -4723,7 +4805,7 @@ export class WorldView
4723 4805 { continue; }
4724 4806 const neighbor = this.chunks.get(`${snapshot.chunkX + dx},${snapshot.chunkY + dy}`);
4725 4807 if (neighbor)
4726 { this.#rebuildChunk(neighbor, { refreshSoftNeighbors: false }); }
4808 { this.#scheduleSoftNeighborChunkRebuild(neighbor); }
4727 4809 }
4728 4810 }
4729 4811 }
@@ -4993,16 +5075,17 @@ export class WorldView
4993 5075 const snapshot = this.chunks.get(key);
4994 5076 if (!baked?.natureCanvas || !snapshot)
4995 5077 { continue; }
4996 this.#fillNatureNoise(baked, snapshot);
4997 baked.natureTintReady = true;
4998 if (baked.groundObject)
5078 if (this.#fillNatureNoise(baked, snapshot) || baked.natureTintReady)
4999 5079 {
5000 this.#applyAtlasLighting(
5001 baked.groundObject,
5002 baked.lightmapImage,
5003 -1,
5004 baked.natureImage
5005 );
5080 if (baked.groundObject)
5081 {
5082 this.#applyAtlasLighting(
5083 baked.groundObject,
5084 baked.lightmapImage,
5085 -1,
5086 baked.natureImage
5087 );
5088 }
5006 5089 }
5007 5090 }
5008 5091 }
@@ -5024,18 +5107,46 @@ export class WorldView
5024 5107 const snapshot = this.chunks.get(`${cx},${cy}`);
5025 5108 if (!baked?.natureCanvas || !snapshot)
5026 5109 { continue; }
5027 this.#fillNatureNoise(baked, snapshot);
5028 baked.natureTintReady = true;
5029 if (baked.groundObject)
5110 if (this.#fillNatureNoise(baked, snapshot) && baked.groundObject)
5030 5111 { this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage); }
5031 5112 }
5032 5113 }
5033 5114 }
5034 5115
5116 /**
5117 * Hash of grounds sampled by the nature canvas (chunk + pad halo).
5118 * Used to skip Perlin refill when nothing under the noise map changed.
5119 */
5120 #natureSourceHash (snapshot)
5121 {
5122 let hash = 2166136261;
5123 const baseX = snapshot.chunkX * CHUNK_SIZE;
5124 const baseY = snapshot.chunkY * CHUNK_SIZE;
5125 const pad = NATURE_NOISE_PAD;
5126 for (let ly = -pad; ly < CHUNK_SIZE + pad; ly++)
5127 {
5128 for (let lx = -pad; lx < CHUNK_SIZE + pad; lx++)
5129 {
5130 const ground = this.#groundAtTile(baseX + lx, baseY + ly);
5131 const typeId = ground == null ? -1 : (normalizeGround(ground).typeId ?? -1);
5132 hash ^= (typeId + 1) >>> 0;
5133 hash = Math.imul(hash, 16777619);
5134 }
5135 }
5136 return hash >>> 0;
5137 }
5138
5139 /**
5140 * @returns {boolean} true when the nature canvas was rewritten
5141 */
5035 5142 #fillNatureNoise (baked, snapshot)
5036 5143 {
5037 5144 if (!baked?.natureCanvas || !snapshot)
5038 { return; }
5145 { return false; }
5146 const sourceHash = this.#natureSourceHash(snapshot);
5147 if (baked.natureSourceHash === sourceHash && baked.natureTintReady && baked.natureImage)
5148 { return false; }
5149
5039 5150 const canvas = baked.natureCanvas;
5040 5151 if (canvas.width !== NATURE_NOISE_SIZE || canvas.height !== NATURE_NOISE_SIZE)
5041 5152 {
@@ -5044,7 +5155,12 @@ export class WorldView
5044 5155 }
5045 5156 const scale = NATURE_TINT.scale;
5046 5157 const ctx = canvas.getContext("2d");
5047 const image = ctx.createImageData(NATURE_NOISE_SIZE, NATURE_NOISE_SIZE);
5158 const pixelCount = NATURE_NOISE_SIZE * NATURE_NOISE_SIZE * 4;
5159 if (!baked.naturePixels || baked.naturePixels.length !== pixelCount)
5160 { baked.naturePixels = new Uint8ClampedArray(pixelCount); }
5161 const pixels = baked.naturePixels;
5162 if (!baked.natureImageData || baked.natureImageData.data !== pixels)
5163 { baked.natureImageData = new ImageData(pixels, NATURE_NOISE_SIZE, NATURE_NOISE_SIZE); }
5048 5164 const baseX = snapshot.chunkX * CHUNK_SIZE;
5049 5165 const baseY = snapshot.chunkY * CHUNK_SIZE;
5050 5166 const pad = NATURE_NOISE_PAD;
@@ -5062,17 +5178,20 @@ export class WorldView
5062 5178 const n = natureNoise01(worldX, worldY, scale);
5063 5179 const i = (py * NATURE_NOISE_SIZE + px) * 4;
5064 5180 const v = Math.round(n * 255);
5065 image.data[i] = v;
5066 image.data[i + 1] = v;
5067 image.data[i + 2] = v;
5068 image.data[i + 3] = blendable ? 255 : 0;
5181 pixels[i] = v;
5182 pixels[i + 1] = v;
5183 pixels[i + 2] = v;
5184 pixels[i + 3] = blendable ? 255 : 0;
5069 5185 }
5070 5186 }
5071 ctx.putImageData(image, 0, 0);
5187 ctx.putImageData(baked.natureImageData, 0, 0);
5072 5188 invalidateCanvasTexture(this.scene, baked.natureCanvas);
5073 5189 this.#queueCanvasTextureWarm(baked.natureCanvas, snapshot.chunkX, snapshot.chunkY);
5074 5190 if (!baked.natureImage)
5075 5191 { baked.natureImage = frameworkImageFromCanvas(baked.natureCanvas); }
5192 baked.natureSourceHash = sourceHash;
5193 baked.natureTintReady = true;
5194 return true;
5076 5195 }
5077 5196
5078 5197 #syncCliffGroundTopSprite (object, darkness, groundImage, localX, localY)
@@ -1038,6 +1038,8 @@ function getTightFitLitSpriteShader ()
1038export class WorldView
1038export class WorldView
1039{
1039{
1040 #chunkRebuildKeys = new Set();
1040 #chunkRebuildKeys = new Set();
1041 /** Soft-blend / wet-sand neighbor rebakes — deferred so they do not nest inside a primary bake. */
1042 #chunkSoftNeighborKeys = new Set();
1041 #chunkRebuildRaf = 0;
1043 #chunkRebuildRaf = 0;
1042 #edgesDirty = false;
1044 #edgesDirty = false;
1043 #CHUNK_REBUILD_BATCH = 2;
1045 #CHUNK_REBUILD_BATCH = 2;
@@ -1213,6 +1215,7 @@ export class WorldView
1213 {
1215 {
1214 this.#cancelIdlePrefetch();
1216 this.#cancelIdlePrefetch();
1215 this.#chunkRebuildKeys.clear();
1217 this.#chunkRebuildKeys.clear();
1218 this.#chunkSoftNeighborKeys.clear();
1216 this.#playerChunkX = null;
1219 this.#playerChunkX = null;
1217 this.#playerChunkY = null;
1220 this.#playerChunkY = null;
1218 this.#playerMoveChunkDx = 0;
1221 this.#playerMoveChunkDx = 0;
@@ -4463,7 +4466,22 @@ export class WorldView
4463 {
4466 {
4464 if (!snapshot)
4467 if (!snapshot)
4465 { return; }
4468 { return; }
4466 this.#chunkRebuildKeys.add(`${snapshot.chunkX},${snapshot.chunkY}`);
4469 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
4470 this.#chunkRebuildKeys.add(key);
4471 this.#chunkSoftNeighborKeys.delete(key);
4472 this.#cancelIdlePrefetch();
4473 this.#ensureChunkFlushScheduled();
4474 }
4475
4476 /** Queue a neighbor atlas rebake without cascading further soft-neighbor work. */
4477 #scheduleSoftNeighborChunkRebuild (snapshot)
4478 {
4479 if (!snapshot)
4480 { return; }
4481 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
4482 if (this.#chunkRebuildKeys.has(key))
4483 { return; }
4484 this.#chunkSoftNeighborKeys.add(key);
4467 this.#cancelIdlePrefetch();
4485 this.#cancelIdlePrefetch();
4468 this.#ensureChunkFlushScheduled();
4486 this.#ensureChunkFlushScheduled();
4469 }
4487 }
@@ -4475,6 +4493,11 @@ export class WorldView
4475 this.#ensureChunkFlushScheduled();
4493 this.#ensureChunkFlushScheduled();
4476 }
4494 }
4477
4495
4496 #pendingChunkRebuildCount ()
4497 {
4498 return this.#chunkRebuildKeys.size + this.#chunkSoftNeighborKeys.size;
4499 }
4500
4478 #ensureChunkFlushScheduled ()
4501 #ensureChunkFlushScheduled ()
4479 {
4502 {
4480 if (this.#chunkRebuildRaf)
4503 if (this.#chunkRebuildRaf)
@@ -4507,8 +4530,7 @@ export class WorldView
4507 /** When the 3×3 lighting center moves: refresh door sprites (resting vs live) and lightmaps. */
4530 /** When the 3×3 lighting center moves: refresh door sprites (resting vs live) and lightmaps. */
4508 #onActiveLightingCenterChanged (prevChunkX, prevChunkY, chunkX, chunkY)
4531 #onActiveLightingCenterChanged (prevChunkX, prevChunkY, chunkX, chunkY)
4509 {
4532 {
4510 this.#invalidateNeighborLightmapCaches(prevChunkX, prevChunkY);
4533 this.#invalidateActiveLightingMoveCaches(prevChunkX, prevChunkY, chunkX, chunkY);
4511 this.#invalidateNeighborLightmapCaches(chunkX, chunkY);
4512 const doorChunks = new Set();
4534 const doorChunks = new Set();
4513 for (const [cx, cy] of [[prevChunkX, prevChunkY], [chunkX, chunkY]])
4535 for (const [cx, cy] of [[prevChunkX, prevChunkY], [chunkX, chunkY]])
4514 {
4536 {
@@ -4525,6 +4547,51 @@ export class WorldView
4525 }
4547 }
4526 }
4548 }
4527
4549
4550 /**
4551 * Only bust lightmaps for chunks whose active-3×3 membership (or ±1 halo) changed.
4552 * Cache keys already track light positions; a full ±2 wipe around both centers was redundant.
4553 */
4554 #invalidateActiveLightingMoveCaches (prevChunkX, prevChunkY, nextChunkX, nextChunkY)
4555 {
4556 const prev = new Set();
4557 const next = new Set();
4558 for (let dy = -1; dy <= 1; dy++)
4559 {
4560 for (let dx = -1; dx <= 1; dx++)
4561 {
4562 prev.add(`${prevChunkX + dx},${prevChunkY + dy}`);
4563 next.add(`${nextChunkX + dx},${nextChunkY + dy}`);
4564 }
4565 }
4566 const changed = [];
4567 for (const key of prev)
4568 {
4569 if (!next.has(key))
4570 { changed.push(key); }
4571 }
4572 for (const key of next)
4573 {
4574 if (!prev.has(key))
4575 { changed.push(key); }
4576 }
4577 const bust = new Set();
4578 for (const key of changed)
4579 {
4580 const [cx, cy] = key.split(",").map(Number);
4581 for (let dy = -1; dy <= 1; dy++)
4582 {
4583 for (let dx = -1; dx <= 1; dx++)
4584 { bust.add(`${cx + dx},${cy + dy}`); }
4585 }
4586 }
4587 for (const key of bust)
4588 {
4589 const baked = this.bakedChunks.get(key);
4590 if (baked)
4591 { baked.lightmapCacheKey = null; }
4592 }
4593 }
4594
4528 #redrawDoorTilesInChunk (chunkX, chunkY)
4595 #redrawDoorTilesInChunk (chunkX, chunkY)
4529 {
4596 {
4530 const snapshot = this.chunks.get(`${chunkX},${chunkY}`);
4597 const snapshot = this.chunks.get(`${chunkX},${chunkY}`);
@@ -4579,9 +4646,9 @@ export class WorldView
4579 return dist * 10 - Math.max(0, ahead) * 3;
4646 return dist * 10 - Math.max(0, ahead) * 3;
4580 }
4647 }
4581
4648
4582 #sortedChunkRebuildKeys (mode = "urgent")
4649 #sortedChunkRebuildKeys (keySet, mode = "urgent")
4583 {
4650 {
4584 return [...this.#chunkRebuildKeys].sort((a, b) =>
4651 return [...keySet].sort((a, b) =>
4585 this.#chunkRebuildScore(a, mode) - this.#chunkRebuildScore(b, mode));
4652 this.#chunkRebuildScore(a, mode) - this.#chunkRebuildScore(b, mode));
4586 }
4653 }
4587
4654
@@ -4611,7 +4678,9 @@ export class WorldView
4611 for (const snapshot of this.chunks.values())
4678 for (const snapshot of this.chunks.values())
4612 {
4679 {
4613 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
4680 const key = `${snapshot.chunkX},${snapshot.chunkY}`;
4614 if (this.bakedChunks.has(key) || this.#chunkRebuildKeys.has(key))
4681 if (this.bakedChunks.has(key)
4682 || this.#chunkRebuildKeys.has(key)
4683 || this.#chunkSoftNeighborKeys.has(key))
4615 { continue; }
4684 { continue; }
4616 const score = this.#chunkRebuildScore(key, "idle");
4685 const score = this.#chunkRebuildScore(key, "idle");
4617 if (score < bestScore)
4686 if (score < bestScore)
@@ -4632,7 +4701,7 @@ export class WorldView
4632
4701
4633 #scheduleIdleChunkPrefetch ()
4702 #scheduleIdleChunkPrefetch ()
4634 {
4703 {
4635 if (this.#idlePrefetchId || this.#chunkRebuildRaf || this.#chunkRebuildKeys.size)
4704 if (this.#idlePrefetchId || this.#chunkRebuildRaf || this.#pendingChunkRebuildCount())
4636 { return; }
4705 { return; }
4637 if (typeof requestIdleCallback !== "function")
4706 if (typeof requestIdleCallback !== "function")
4638 { return; }
4707 { return; }
@@ -4647,10 +4716,10 @@ export class WorldView
4647 #runIdleChunkPrefetch (deadline)
4716 #runIdleChunkPrefetch (deadline)
4648 {
4717 {
4649 this.#idlePrefetchId = 0;
4718 this.#idlePrefetchId = 0;
4650 if (this.#chunkRebuildRaf || this.#chunkRebuildKeys.size)
4719 if (this.#chunkRebuildRaf || this.#pendingChunkRebuildCount())
4651 { return; }
4720 { return; }
4652 this.#discoverUnbakedChunks();
4721 this.#discoverUnbakedChunks();
4653 if (this.#chunkRebuildKeys.size)
4722 if (this.#pendingChunkRebuildCount())
4654 {
4723 {
4655 this.#ensureChunkFlushScheduled();
4724 this.#ensureChunkFlushScheduled();
4656 return;
4725 return;
@@ -4671,19 +4740,32 @@ export class WorldView
4671 #flushChunkWork ()
4740 #flushChunkWork ()
4672 {
4741 {
4673 this.#chunkRebuildRaf = 0;
4742 this.#chunkRebuildRaf = 0;
4674 const keys = this.#sortedChunkRebuildKeys("urgent");
4743 const primary = this.#sortedChunkRebuildKeys(this.#chunkRebuildKeys, "urgent");
4675 const batch = keys.slice(0, this.#CHUNK_REBUILD_BATCH);
4744 const soft = this.#sortedChunkRebuildKeys(this.#chunkSoftNeighborKeys, "urgent");
4676 const rest = keys.slice(this.#CHUNK_REBUILD_BATCH);
4745 let budget = this.#CHUNK_REBUILD_BATCH;
4677 this.#chunkRebuildKeys.clear();
4746 const primaryBatch = primary.slice(0, budget);
4678 for (const key of rest)
4747 budget -= primaryBatch.length;
4679 { this.#chunkRebuildKeys.add(key); }
4748 const softBatch = soft.slice(0, Math.max(0, budget));
4680 for (const key of batch)
4749
4750 for (const key of primaryBatch)
4751 { this.#chunkRebuildKeys.delete(key); }
4752 for (const key of softBatch)
4753 { this.#chunkSoftNeighborKeys.delete(key); }
4754
4755 for (const key of primaryBatch)
4681 {
4756 {
4682 const snap = this.chunks.get(key);
4757 const snap = this.chunks.get(key);
4683 if (snap)
4758 if (snap)
4684 { this.#rebuildChunk(snap, { refreshSoftNeighbors: true }); }
4759 { this.#rebuildChunk(snap, { refreshSoftNeighbors: true }); }
4685 }
4760 }
4686 if (this.#chunkRebuildKeys.size)
4761 for (const key of softBatch)
4762 {
4763 const snap = this.chunks.get(key);
4764 if (snap)
4765 { this.#rebuildChunk(snap, { refreshSoftNeighbors: false }); }
4766 }
4767
4768 if (this.#pendingChunkRebuildCount())
4687 {
4769 {
4688 this.#ensureChunkFlushScheduled();
4770 this.#ensureChunkFlushScheduled();
4689 return;
4771 return;
@@ -4723,7 +4805,7 @@ export class WorldView
4723 { continue; }
4805 { continue; }
4724 const neighbor = this.chunks.get(`${snapshot.chunkX + dx},${snapshot.chunkY + dy}`);
4806 const neighbor = this.chunks.get(`${snapshot.chunkX + dx},${snapshot.chunkY + dy}`);
4725 if (neighbor)
4807 if (neighbor)
4726 { this.#rebuildChunk(neighbor, { refreshSoftNeighbors: false }); }
4808 { this.#scheduleSoftNeighborChunkRebuild(neighbor); }
4727 }
4809 }
4728 }
4810 }
4729 }
4811 }
@@ -4993,16 +5075,17 @@ export class WorldView
4993 const snapshot = this.chunks.get(key);
5075 const snapshot = this.chunks.get(key);
4994 if (!baked?.natureCanvas || !snapshot)
5076 if (!baked?.natureCanvas || !snapshot)
4995 { continue; }
5077 { continue; }
4996 this.#fillNatureNoise(baked, snapshot);
5078 if (this.#fillNatureNoise(baked, snapshot) || baked.natureTintReady)
4997 baked.natureTintReady = true;
4998 if (baked.groundObject)
4999 {
5079 {
5000 this.#applyAtlasLighting(
5080 if (baked.groundObject)
5001 baked.groundObject,
5081 {
5002 baked.lightmapImage,
5082 this.#applyAtlasLighting(
5003 -1,
5083 baked.groundObject,
5004 baked.natureImage
5084 baked.lightmapImage,
5005 );
5085 -1,
5086 baked.natureImage
5087 );
5088 }
5006 }
5089 }
5007 }
5090 }
5008 }
5091 }
@@ -5024,18 +5107,46 @@ export class WorldView
5024 const snapshot = this.chunks.get(`${cx},${cy}`);
5107 const snapshot = this.chunks.get(`${cx},${cy}`);
5025 if (!baked?.natureCanvas || !snapshot)
5108 if (!baked?.natureCanvas || !snapshot)
5026 { continue; }
5109 { continue; }
5027 this.#fillNatureNoise(baked, snapshot);
5110 if (this.#fillNatureNoise(baked, snapshot) && baked.groundObject)
5028 baked.natureTintReady = true;
5029 if (baked.groundObject)
5030 { this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage); }
5111 { this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage); }
5031 }
5112 }
5032 }
5113 }
5033 }
5114 }
5034
5115
5116 /**
5117 * Hash of grounds sampled by the nature canvas (chunk + pad halo).
5118 * Used to skip Perlin refill when nothing under the noise map changed.
5119 */
5120 #natureSourceHash (snapshot)
5121 {
5122 let hash = 2166136261;
5123 const baseX = snapshot.chunkX * CHUNK_SIZE;
5124 const baseY = snapshot.chunkY * CHUNK_SIZE;
5125 const pad = NATURE_NOISE_PAD;
5126 for (let ly = -pad; ly < CHUNK_SIZE + pad; ly++)
5127 {
5128 for (let lx = -pad; lx < CHUNK_SIZE + pad; lx++)
5129 {
5130 const ground = this.#groundAtTile(baseX + lx, baseY + ly);
5131 const typeId = ground == null ? -1 : (normalizeGround(ground).typeId ?? -1);
5132 hash ^= (typeId + 1) >>> 0;
5133 hash = Math.imul(hash, 16777619);
5134 }
5135 }
5136 return hash >>> 0;
5137 }
5138
5139 /**
5140 * @returns {boolean} true when the nature canvas was rewritten
5141 */
5035 #fillNatureNoise (baked, snapshot)
5142 #fillNatureNoise (baked, snapshot)
5036 {
5143 {
5037 if (!baked?.natureCanvas || !snapshot)
5144 if (!baked?.natureCanvas || !snapshot)
5038 { return; }
5145 { return false; }
5146 const sourceHash = this.#natureSourceHash(snapshot);
5147 if (baked.natureSourceHash === sourceHash && baked.natureTintReady && baked.natureImage)
5148 { return false; }
5149
5039 const canvas = baked.natureCanvas;
5150 const canvas = baked.natureCanvas;
5040 if (canvas.width !== NATURE_NOISE_SIZE || canvas.height !== NATURE_NOISE_SIZE)
5151 if (canvas.width !== NATURE_NOISE_SIZE || canvas.height !== NATURE_NOISE_SIZE)
5041 {
5152 {
@@ -5044,7 +5155,12 @@ export class WorldView
5044 }
5155 }
5045 const scale = NATURE_TINT.scale;
5156 const scale = NATURE_TINT.scale;
5046 const ctx = canvas.getContext("2d");
5157 const ctx = canvas.getContext("2d");
5047 const image = ctx.createImageData(NATURE_NOISE_SIZE, NATURE_NOISE_SIZE);
5158 const pixelCount = NATURE_NOISE_SIZE * NATURE_NOISE_SIZE * 4;
5159 if (!baked.naturePixels || baked.naturePixels.length !== pixelCount)
5160 { baked.naturePixels = new Uint8ClampedArray(pixelCount); }
5161 const pixels = baked.naturePixels;
5162 if (!baked.natureImageData || baked.natureImageData.data !== pixels)
5163 { baked.natureImageData = new ImageData(pixels, NATURE_NOISE_SIZE, NATURE_NOISE_SIZE); }
5048 const baseX = snapshot.chunkX * CHUNK_SIZE;
5164 const baseX = snapshot.chunkX * CHUNK_SIZE;
5049 const baseY = snapshot.chunkY * CHUNK_SIZE;
5165 const baseY = snapshot.chunkY * CHUNK_SIZE;
5050 const pad = NATURE_NOISE_PAD;
5166 const pad = NATURE_NOISE_PAD;
@@ -5062,17 +5178,20 @@ export class WorldView
5062 const n = natureNoise01(worldX, worldY, scale);
5178 const n = natureNoise01(worldX, worldY, scale);
5063 const i = (py * NATURE_NOISE_SIZE + px) * 4;
5179 const i = (py * NATURE_NOISE_SIZE + px) * 4;
5064 const v = Math.round(n * 255);
5180 const v = Math.round(n * 255);
5065 image.data[i] = v;
5181 pixels[i] = v;
5066 image.data[i + 1] = v;
5182 pixels[i + 1] = v;
5067 image.data[i + 2] = v;
5183 pixels[i + 2] = v;
5068 image.data[i + 3] = blendable ? 255 : 0;
5184 pixels[i + 3] = blendable ? 255 : 0;
5069 }
5185 }
5070 }
5186 }
5071 ctx.putImageData(image, 0, 0);
5187 ctx.putImageData(baked.natureImageData, 0, 0);
5072 invalidateCanvasTexture(this.scene, baked.natureCanvas);
5188 invalidateCanvasTexture(this.scene, baked.natureCanvas);
5073 this.#queueCanvasTextureWarm(baked.natureCanvas, snapshot.chunkX, snapshot.chunkY);
5189 this.#queueCanvasTextureWarm(baked.natureCanvas, snapshot.chunkX, snapshot.chunkY);
5074 if (!baked.natureImage)
5190 if (!baked.natureImage)
5075 { baked.natureImage = frameworkImageFromCanvas(baked.natureCanvas); }
5191 { baked.natureImage = frameworkImageFromCanvas(baked.natureCanvas); }
5192 baked.natureSourceHash = sourceHash;
5193 baked.natureTintReady = true;
5194 return true;
5076 }
5195 }
5077
5196
5078 #syncCliffGroundTopSprite (object, darkness, groundImage, localX, localY)
5197 #syncCliffGroundTopSprite (object, darkness, groundImage, localX, localY)