BlueGit

timfa questward

Defer nature tint baking in the world editor until chunk save.

Skip per-rebuild Perlin fills while painting; run a one-shot bake for saved chunks (and halo neighbors) so the editor stays responsive.

timfa Tim Falken · pushed by timfa

Authored 2026-09-04 · 2 months ago

Hash
4d629f115abbe9489e50a386d2e760acc8034c45
Parent
d57d9ad1
2 files changed +79 −4
client/src/editor/main.js +4 −0
@@ -425,6 +425,7 @@ function saveSelectedChunk ()
425 425 $("apply-status").className = "hint";
426 426 const neighborSaves = [...dirtyNeighborChunks.entries()];
427 427 clearNeighborEditState();
428 const savedChunks = [{ chunkX, chunkY }];
428 429 socket.send({
429 430 type: MessageType.editorSaveChunk,
430 431 chunkX,
@@ -440,6 +441,7 @@ function saveSelectedChunk ()
440 441 for (const [key, data] of neighborSaves)
441 442 {
442 443 const [cx, cy] = key.split(",").map(Number);
444 savedChunks.push({ chunkX: cx, chunkY: cy });
443 445 socket.send({
444 446 type: MessageType.editorSaveChunk,
445 447 chunkX: cx,
@@ -453,6 +455,7 @@ function saveSelectedChunk ()
453 455 }
454 456 });
455 457 }
458 view?.bakeNatureTintForChunks?.(savedChunks);
456 459 requestNeighbors();
457 460 return true;
458 461 }
@@ -1049,6 +1052,7 @@ async function main ()
1049 1052 game.loadScene(scene);
1050 1053 view = new WorldView(game, scene, catalog);
1051 1054 view.followPlayer = false;
1055 view.natureTintEnabled = false;
1052 1056 view.setGraphicsPrefs(loadGraphicsSettings());
1053 1057 syncSeasonControls(view.worldSeason ?? seasonIdAt());
1054 1058 layoutScene();
@@ -425,6 +425,7 @@ function saveSelectedChunk ()
425 $("apply-status").className = "hint";
425 $("apply-status").className = "hint";
426 const neighborSaves = [...dirtyNeighborChunks.entries()];
426 const neighborSaves = [...dirtyNeighborChunks.entries()];
427 clearNeighborEditState();
427 clearNeighborEditState();
428 const savedChunks = [{ chunkX, chunkY }];
428 socket.send({
429 socket.send({
429 type: MessageType.editorSaveChunk,
430 type: MessageType.editorSaveChunk,
430 chunkX,
431 chunkX,
@@ -440,6 +441,7 @@ function saveSelectedChunk ()
440 for (const [key, data] of neighborSaves)
441 for (const [key, data] of neighborSaves)
441 {
442 {
442 const [cx, cy] = key.split(",").map(Number);
443 const [cx, cy] = key.split(",").map(Number);
444 savedChunks.push({ chunkX: cx, chunkY: cy });
443 socket.send({
445 socket.send({
444 type: MessageType.editorSaveChunk,
446 type: MessageType.editorSaveChunk,
445 chunkX: cx,
447 chunkX: cx,
@@ -453,6 +455,7 @@ function saveSelectedChunk ()
453 }
455 }
454 });
456 });
455 }
457 }
458 view?.bakeNatureTintForChunks?.(savedChunks);
456 requestNeighbors();
459 requestNeighbors();
457 return true;
460 return true;
458}
461}
@@ -1049,6 +1052,7 @@ async function main ()
1049 game.loadScene(scene);
1052 game.loadScene(scene);
1050 view = new WorldView(game, scene, catalog);
1053 view = new WorldView(game, scene, catalog);
1051 view.followPlayer = false;
1054 view.followPlayer = false;
1055 view.natureTintEnabled = false;
1052 view.setGraphicsPrefs(loadGraphicsSettings());
1056 view.setGraphicsPrefs(loadGraphicsSettings());
1053 syncSeasonControls(view.worldSeason ?? seasonIdAt());
1057 syncSeasonControls(view.worldSeason ?? seasonIdAt());
1054 layoutScene();
1058 layoutScene();
client/src/render/worldView.js +75 −4
@@ -1082,6 +1082,11 @@ export class WorldView
1082 1082 this.bloomEnabled = true;
1083 1083 /** Soft edge blend for blendable grounds (grass, dirt, …). */
1084 1084 this.softTerrain = false;
1085 /**
1086 * Per-chunk nature Perlin tint. Off in the world editor while painting
1087 * (rebakes are frequent); editor enables a one-shot bake on chunk save.
1088 */
1089 this.natureTintEnabled = true;
1085 1090 this.settings = { ...DEFAULT_WORLD_SETTINGS };
1086 1091 this.worldHour = 12;
1087 1092 /** Calendar season for seasonal ground/block visuals (`spring` / `summer` / `fall` / `winter`). */
@@ -4743,7 +4748,12 @@ export class WorldView
4743 4748 chunkY: snapshot.chunkY
4744 4749 };
4745 4750 baked.groundObject = new GameObject(0, 0, baked.groundImage, LAYER_GROUND);
4746 this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage);
4751 this.#applyAtlasLighting(
4752 baked.groundObject,
4753 baked.lightmapImage,
4754 -1,
4755 this.#groundNatureImage(baked)
4756 );
4747 4757 this.scene.addObject(baked.groundObject);
4748 4758 for (let row = 0; row < CHUNK_SIZE; row++)
4749 4759 {
@@ -4773,7 +4783,12 @@ export class WorldView
4773 4783 baked.groundImage = groundImage;
4774 4784 baked.groundObject.renderer.sprite = groundImage;
4775 4785 }
4776 this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage);
4786 this.#applyAtlasLighting(
4787 baked.groundObject,
4788 baked.lightmapImage,
4789 -1,
4790 this.#groundNatureImage(baked)
4791 );
4777 4792 for (let row = 0; row < CHUNK_SIZE; row++)
4778 4793 {
4779 4794 if (baked.blockCanvases[row] !== atlases.blockRows[row])
@@ -4801,13 +4816,23 @@ export class WorldView
4801 4816 }
4802 4817 baked.chunkX = snapshot.chunkX;
4803 4818 baked.chunkY = snapshot.chunkY;
4804 this.#refreshNatureNoiseAround(snapshot.chunkX, snapshot.chunkY);
4819 if (this.natureTintEnabled)
4820 { this.#refreshNatureNoiseAround(snapshot.chunkX, snapshot.chunkY); }
4805 4821 baked.lightmapCacheKey = null;
4806 4822 this.#invalidateNeighborLightmapCaches(snapshot.chunkX, snapshot.chunkY);
4807 4823 this.#layoutBakedChunk(snapshot);
4808 4824 this.#queueBakedChunkTextures(baked, snapshot.chunkX, snapshot.chunkY);
4809 4825 }
4810 4826
4827 #groundNatureImage (baked)
4828 {
4829 if (!baked?.natureImage)
4830 { return null; }
4831 if (this.natureTintEnabled || baked.natureTintReady)
4832 { return baked.natureImage; }
4833 return null;
4834 }
4835
4811 4836 #releaseBakedAtlasRefs (baked)
4812 4837 {
4813 4838 if (!baked?.atlasCacheRefs)
@@ -4906,9 +4931,53 @@ export class WorldView
4906 4931 };
4907 4932 }
4908 4933
4934 /**
4935 * One-shot nature tint bake (editor chunk-save). Fills noise for each chunk
4936 * and its halo neighbors, and enables tint on the ground atlas.
4937 * @param {Iterable<{ chunkX: number, chunkY: number }>} chunks
4938 */
4939 bakeNatureTintForChunks (chunks)
4940 {
4941 const seen = new Set();
4942 for (const entry of chunks ?? [])
4943 {
4944 const chunkX = entry?.chunkX;
4945 const chunkY = entry?.chunkY;
4946 if (!Number.isFinite(chunkX) || !Number.isFinite(chunkY))
4947 { continue; }
4948 for (let dy = -1; dy <= 1; dy++)
4949 {
4950 for (let dx = -1; dx <= 1; dx++)
4951 {
4952 const key = `${chunkX + dx},${chunkY + dy}`;
4953 if (seen.has(key))
4954 { continue; }
4955 seen.add(key);
4956 const baked = this.bakedChunks.get(key);
4957 const snapshot = this.chunks.get(key);
4958 if (!baked?.natureCanvas || !snapshot)
4959 { continue; }
4960 this.#fillNatureNoise(baked, snapshot);
4961 baked.natureTintReady = true;
4962 if (baked.groundObject)
4963 {
4964 this.#applyAtlasLighting(
4965 baked.groundObject,
4966 baked.lightmapImage,
4967 -1,
4968 baked.natureImage
4969 );
4970 }
4971 }
4972 }
4973 }
4974 }
4975
4909 4976 /** Rebuild nature noise for a chunk and neighbors (halo needs adjacent grounds). */
4910 4977 #refreshNatureNoiseAround (chunkX, chunkY)
4911 4978 {
4979 if (!this.natureTintEnabled)
4980 { return; }
4912 4981 for (let dy = -1; dy <= 1; dy++)
4913 4982 {
4914 4983 for (let dx = -1; dx <= 1; dx++)
@@ -4920,6 +4989,7 @@ export class WorldView
4920 4989 if (!baked?.natureCanvas || !snapshot)
4921 4990 { continue; }
4922 4991 this.#fillNatureNoise(baked, snapshot);
4992 baked.natureTintReady = true;
4923 4993 if (baked.groundObject)
4924 4994 { this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage); }
4925 4995 }
@@ -5532,9 +5602,10 @@ export class WorldView
5532 5602 // Lightmap stores visible light (1 − shade); shader applies darkness = 1 − lit.
5533 5603 if (baked.groundObject?.renderer?.shaderUniforms)
5534 5604 {
5605 const natureOn = this.#groundNatureImage(baked) != null;
5535 5606 Object.assign(baked.groundObject.renderer.shaderUniforms, {
5536 5607 u_ambientDark: 1,
5537 ...natureTintUniforms()
5608 ...(natureOn ? natureTintUniforms() : { u_useNature: 0 })
5538 5609 });
5539 5610 }
5540 5611 for (const object of baked.blockObjects ?? [])
@@ -1082,6 +1082,11 @@ export class WorldView
1082 this.bloomEnabled = true;
1082 this.bloomEnabled = true;
1083 /** Soft edge blend for blendable grounds (grass, dirt, …). */
1083 /** Soft edge blend for blendable grounds (grass, dirt, …). */
1084 this.softTerrain = false;
1084 this.softTerrain = false;
1085 /**
1086 * Per-chunk nature Perlin tint. Off in the world editor while painting
1087 * (rebakes are frequent); editor enables a one-shot bake on chunk save.
1088 */
1089 this.natureTintEnabled = true;
1085 this.settings = { ...DEFAULT_WORLD_SETTINGS };
1090 this.settings = { ...DEFAULT_WORLD_SETTINGS };
1086 this.worldHour = 12;
1091 this.worldHour = 12;
1087 /** Calendar season for seasonal ground/block visuals (`spring` / `summer` / `fall` / `winter`). */
1092 /** Calendar season for seasonal ground/block visuals (`spring` / `summer` / `fall` / `winter`). */
@@ -4743,7 +4748,12 @@ export class WorldView
4743 chunkY: snapshot.chunkY
4748 chunkY: snapshot.chunkY
4744 };
4749 };
4745 baked.groundObject = new GameObject(0, 0, baked.groundImage, LAYER_GROUND);
4750 baked.groundObject = new GameObject(0, 0, baked.groundImage, LAYER_GROUND);
4746 this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage);
4751 this.#applyAtlasLighting(
4752 baked.groundObject,
4753 baked.lightmapImage,
4754 -1,
4755 this.#groundNatureImage(baked)
4756 );
4747 this.scene.addObject(baked.groundObject);
4757 this.scene.addObject(baked.groundObject);
4748 for (let row = 0; row < CHUNK_SIZE; row++)
4758 for (let row = 0; row < CHUNK_SIZE; row++)
4749 {
4759 {
@@ -4773,7 +4783,12 @@ export class WorldView
4773 baked.groundImage = groundImage;
4783 baked.groundImage = groundImage;
4774 baked.groundObject.renderer.sprite = groundImage;
4784 baked.groundObject.renderer.sprite = groundImage;
4775 }
4785 }
4776 this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage);
4786 this.#applyAtlasLighting(
4787 baked.groundObject,
4788 baked.lightmapImage,
4789 -1,
4790 this.#groundNatureImage(baked)
4791 );
4777 for (let row = 0; row < CHUNK_SIZE; row++)
4792 for (let row = 0; row < CHUNK_SIZE; row++)
4778 {
4793 {
4779 if (baked.blockCanvases[row] !== atlases.blockRows[row])
4794 if (baked.blockCanvases[row] !== atlases.blockRows[row])
@@ -4801,13 +4816,23 @@ export class WorldView
4801 }
4816 }
4802 baked.chunkX = snapshot.chunkX;
4817 baked.chunkX = snapshot.chunkX;
4803 baked.chunkY = snapshot.chunkY;
4818 baked.chunkY = snapshot.chunkY;
4804 this.#refreshNatureNoiseAround(snapshot.chunkX, snapshot.chunkY);
4819 if (this.natureTintEnabled)
4820 { this.#refreshNatureNoiseAround(snapshot.chunkX, snapshot.chunkY); }
4805 baked.lightmapCacheKey = null;
4821 baked.lightmapCacheKey = null;
4806 this.#invalidateNeighborLightmapCaches(snapshot.chunkX, snapshot.chunkY);
4822 this.#invalidateNeighborLightmapCaches(snapshot.chunkX, snapshot.chunkY);
4807 this.#layoutBakedChunk(snapshot);
4823 this.#layoutBakedChunk(snapshot);
4808 this.#queueBakedChunkTextures(baked, snapshot.chunkX, snapshot.chunkY);
4824 this.#queueBakedChunkTextures(baked, snapshot.chunkX, snapshot.chunkY);
4809 }
4825 }
4810
4826
4827 #groundNatureImage (baked)
4828 {
4829 if (!baked?.natureImage)
4830 { return null; }
4831 if (this.natureTintEnabled || baked.natureTintReady)
4832 { return baked.natureImage; }
4833 return null;
4834 }
4835
4811 #releaseBakedAtlasRefs (baked)
4836 #releaseBakedAtlasRefs (baked)
4812 {
4837 {
4813 if (!baked?.atlasCacheRefs)
4838 if (!baked?.atlasCacheRefs)
@@ -4906,9 +4931,53 @@ export class WorldView
4906 };
4931 };
4907 }
4932 }
4908
4933
4934 /**
4935 * One-shot nature tint bake (editor chunk-save). Fills noise for each chunk
4936 * and its halo neighbors, and enables tint on the ground atlas.
4937 * @param {Iterable<{ chunkX: number, chunkY: number }>} chunks
4938 */
4939 bakeNatureTintForChunks (chunks)
4940 {
4941 const seen = new Set();
4942 for (const entry of chunks ?? [])
4943 {
4944 const chunkX = entry?.chunkX;
4945 const chunkY = entry?.chunkY;
4946 if (!Number.isFinite(chunkX) || !Number.isFinite(chunkY))
4947 { continue; }
4948 for (let dy = -1; dy <= 1; dy++)
4949 {
4950 for (let dx = -1; dx <= 1; dx++)
4951 {
4952 const key = `${chunkX + dx},${chunkY + dy}`;
4953 if (seen.has(key))
4954 { continue; }
4955 seen.add(key);
4956 const baked = this.bakedChunks.get(key);
4957 const snapshot = this.chunks.get(key);
4958 if (!baked?.natureCanvas || !snapshot)
4959 { continue; }
4960 this.#fillNatureNoise(baked, snapshot);
4961 baked.natureTintReady = true;
4962 if (baked.groundObject)
4963 {
4964 this.#applyAtlasLighting(
4965 baked.groundObject,
4966 baked.lightmapImage,
4967 -1,
4968 baked.natureImage
4969 );
4970 }
4971 }
4972 }
4973 }
4974 }
4975
4909 /** Rebuild nature noise for a chunk and neighbors (halo needs adjacent grounds). */
4976 /** Rebuild nature noise for a chunk and neighbors (halo needs adjacent grounds). */
4910 #refreshNatureNoiseAround (chunkX, chunkY)
4977 #refreshNatureNoiseAround (chunkX, chunkY)
4911 {
4978 {
4979 if (!this.natureTintEnabled)
4980 { return; }
4912 for (let dy = -1; dy <= 1; dy++)
4981 for (let dy = -1; dy <= 1; dy++)
4913 {
4982 {
4914 for (let dx = -1; dx <= 1; dx++)
4983 for (let dx = -1; dx <= 1; dx++)
@@ -4920,6 +4989,7 @@ export class WorldView
4920 if (!baked?.natureCanvas || !snapshot)
4989 if (!baked?.natureCanvas || !snapshot)
4921 { continue; }
4990 { continue; }
4922 this.#fillNatureNoise(baked, snapshot);
4991 this.#fillNatureNoise(baked, snapshot);
4992 baked.natureTintReady = true;
4923 if (baked.groundObject)
4993 if (baked.groundObject)
4924 { this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage); }
4994 { this.#applyAtlasLighting(baked.groundObject, baked.lightmapImage, -1, baked.natureImage); }
4925 }
4995 }
@@ -5532,9 +5602,10 @@ export class WorldView
5532 // Lightmap stores visible light (1 − shade); shader applies darkness = 1 − lit.
5602 // Lightmap stores visible light (1 − shade); shader applies darkness = 1 − lit.
5533 if (baked.groundObject?.renderer?.shaderUniforms)
5603 if (baked.groundObject?.renderer?.shaderUniforms)
5534 {
5604 {
5605 const natureOn = this.#groundNatureImage(baked) != null;
5535 Object.assign(baked.groundObject.renderer.shaderUniforms, {
5606 Object.assign(baked.groundObject.renderer.shaderUniforms, {
5536 u_ambientDark: 1,
5607 u_ambientDark: 1,
5537 ...natureTintUniforms()
5608 ...(natureOn ? natureTintUniforms() : { u_useNature: 0 })
5538 });
5609 });
5539 }
5610 }
5540 for (const object of baked.blockObjects ?? [])
5611 for (const object of baked.blockObjects ?? [])