@threenative/terrain 0.1.9 → 0.1.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -59
- package/THIRD_PARTY_NOTICES.md +2 -1
- package/dist/bake-DJ5cxg0C.d.ts +82 -0
- package/dist/editor/index.d.ts +4 -4
- package/dist/editor/index.js +130 -128
- package/dist/editor/server.d.ts +2 -2
- package/dist/editor/server.js +38 -35
- package/dist/editor/worker.d.ts +1 -1
- package/dist/editor/worker.js +101 -101
- package/dist/export.d.ts +1 -1
- package/dist/index.d.ts +156 -273
- package/dist/index.js +562 -378
- package/dist/kit-world.d.ts +107 -0
- package/dist/kit-world.js +5134 -0
- package/dist/kit.d.ts +21 -4
- package/dist/kit.js +337 -223
- package/dist/mcp/cook-child.js +1963 -1543
- package/dist/mcp/server.js +716 -429
- package/dist/prop-package-Dwz0EWoB.d.ts +141 -0
- package/dist/{server-B5RdvEAC.d.ts → server-LnFwY8sr.d.ts} +1 -1
- package/dist/starter.d.ts +1142 -7
- package/dist/starter.js +2102 -120
- package/dist/{terrain-CAEaW0mQ.d.ts → terrain-DYCyAOCJ.d.ts} +1 -1
- package/dist/three.d.ts +1 -1
- package/dist/three.js +1 -1
- package/dist/{types-BpH00qsx.d.ts → types-kpI0C4hW.d.ts} +1 -1
- package/dist/{world-package-B8QgHjAQ.d.ts → world-package-DqJC3-Nn.d.ts} +2 -2
- package/docs/authoring/editor.md +73 -0
- package/docs/authoring/new-environment.md +97 -0
- package/docs/authoring/recipes.md +97 -0
- package/docs/authoring/world-export.md +66 -0
- package/package.json +24 -8
- package/starter/alpine/README.md +22 -12
- package/starter/alpine/bake.mjs +14 -1
- package/starter/alpine/biome.data.json +187 -25
- package/starter/alpine/heightmap.r16 +0 -0
- package/starter/alpine/playerRig.ts +89 -0
- package/starter/alpine/recipe.json +169 -15
- package/starter/alpine/render/bakedFile.ts +17 -0
- package/starter/alpine/render/benchmark.ts +77 -0
- package/starter/alpine/render/biomeData.ts +14 -0
- package/starter/alpine/render/biomes.ts +687 -0
- package/starter/alpine/render/ground/cover.ts +565 -0
- package/starter/alpine/render/ground/horizon.ts +219 -0
- package/starter/alpine/render/ground/palette.d.ts +1 -0
- package/starter/alpine/render/ground/palette.js +11 -0
- package/starter/alpine/render/ground/scatter.ts +104 -0
- package/starter/alpine/render/ground/terrain.ts +1860 -0
- package/starter/alpine/render/kitAssets.ts +28 -0
- package/starter/alpine/render/loading.ts +354 -0
- package/starter/alpine/render/lookKnobs.ts +37 -0
- package/starter/alpine/render/orbitCamera.ts +155 -0
- package/starter/alpine/render/props/farForest.ts +94 -0
- package/starter/alpine/render/props/pack.ts +1114 -0
- package/starter/alpine/render/props/prepared.ts +225 -0
- package/starter/alpine/render/props/propMaterials.ts +1239 -0
- package/starter/alpine/render/props/propProgress.ts +2 -0
- package/starter/alpine/render/props/propWorld.ts +105 -0
- package/starter/alpine/render/props/props.ts +633 -0
- package/starter/alpine/render/props/spruce.ts +440 -0
- package/starter/alpine/render/sky/atmosphere.ts +329 -0
- package/starter/alpine/render/sky/sky.ts +382 -0
- package/starter/alpine/render/texturePreparation.ts +135 -0
- package/starter/alpine/render/water/river.ts +1132 -0
- package/starter/alpine/render/worldNames.ts +6 -0
- package/starter/alpine/scene.ts +715 -0
- package/starter/alpine/sceneProps.ts +561 -0
- package/starter/alpine/sceneState.ts +33 -0
- package/starter/alpine/world/terrainAssets.ts +145 -0
- package/starter/alpine/world/walkBand.ts +47 -0
- package/starter/alpine/world/water.ts +74 -0
- package/starter/alpine/worlds.ts +13 -0
- package/starter/arbor/wind.ts +84 -0
- package/starter/coastal/README.md +26 -25
- package/starter/coastal/assets.json +10 -0
- package/starter/coastal/bake.mjs +14 -1
- package/starter/coastal/biome.data.json +18 -3
- package/starter/coastal/playerRig.ts +89 -0
- package/starter/coastal/recipe.json +220 -18
- package/starter/coastal/render/bakedFile.ts +17 -0
- package/starter/coastal/render/benchmark.ts +81 -0
- package/starter/coastal/render/biomeData.ts +14 -0
- package/starter/coastal/render/biomes.ts +678 -0
- package/starter/coastal/render/ground/cover.ts +565 -0
- package/starter/coastal/render/ground/horizon.ts +219 -0
- package/starter/coastal/render/ground/palette.d.ts +1 -0
- package/starter/coastal/render/ground/palette.js +11 -0
- package/starter/coastal/render/ground/scatter.ts +104 -0
- package/starter/coastal/render/ground/terrain.ts +1829 -0
- package/starter/coastal/render/kitAssets.ts +28 -0
- package/starter/coastal/render/loading.ts +354 -0
- package/starter/coastal/render/lookKnobs.ts +37 -0
- package/starter/coastal/render/orbitCamera.ts +155 -0
- package/starter/coastal/render/props/farForest.ts +94 -0
- package/starter/coastal/render/props/pack.ts +1109 -0
- package/starter/coastal/render/props/prepared.ts +225 -0
- package/starter/coastal/render/props/propMaterials.ts +1239 -0
- package/starter/coastal/render/props/propProgress.ts +2 -0
- package/starter/coastal/render/props/propWorld.ts +105 -0
- package/starter/coastal/render/props/props.ts +633 -0
- package/starter/coastal/render/props/spruce.ts +440 -0
- package/starter/coastal/render/sky/atmosphere.ts +329 -0
- package/starter/coastal/render/sky/sky.ts +382 -0
- package/starter/coastal/render/texturePreparation.ts +135 -0
- package/starter/coastal/render/water/ocean.ts +284 -0
- package/starter/coastal/render/water/river.ts +1132 -0
- package/starter/coastal/render/worldNames.ts +6 -0
- package/starter/coastal/scene.ts +740 -0
- package/starter/coastal/sceneProps.ts +559 -0
- package/starter/coastal/sceneState.ts +33 -0
- package/starter/coastal/world/terrainAssets.ts +151 -0
- package/starter/coastal/world/walkBand.ts +47 -0
- package/starter/coastal/world/water.ts +74 -0
- package/starter/coastal/worlds.ts +13 -0
- package/starter/desert/README.md +51 -23
- package/starter/desert/bake-flora.mjs +142 -0
- package/starter/desert/bake-rocks.mjs +291 -0
- package/starter/desert/bake.mjs +14 -1
- package/starter/desert/biome.data.json +24 -12
- package/starter/desert/playerRig.ts +89 -0
- package/starter/desert/recipe.json +63 -16
- package/starter/desert/render/bakedFile.ts +17 -0
- package/starter/desert/render/benchmark.ts +79 -0
- package/starter/desert/render/biomeData.ts +14 -0
- package/starter/desert/render/biomes.ts +660 -0
- package/starter/desert/render/ground/cover.ts +565 -0
- package/starter/desert/render/ground/debris.ts +136 -0
- package/starter/desert/render/ground/horizon.ts +219 -0
- package/starter/desert/render/ground/palette.d.ts +1 -0
- package/starter/desert/render/ground/palette.js +11 -0
- package/starter/desert/render/ground/scatter.ts +104 -0
- package/starter/desert/render/ground/terrain.ts +1854 -0
- package/starter/desert/render/kitAssets.ts +28 -0
- package/starter/desert/render/loading.ts +354 -0
- package/starter/desert/render/lookKnobs.ts +37 -0
- package/starter/desert/render/orbitCamera.ts +155 -0
- package/starter/desert/render/props/farForest.ts +96 -0
- package/starter/desert/render/props/formations.ts +630 -0
- package/starter/desert/render/props/pack.ts +1195 -0
- package/starter/desert/render/props/prepared.ts +225 -0
- package/starter/desert/render/props/propMaterials.ts +1255 -0
- package/starter/desert/render/props/propProgress.ts +2 -0
- package/starter/desert/render/props/propWorld.ts +105 -0
- package/starter/desert/render/props/props.ts +624 -0
- package/starter/desert/render/props/spruce.ts +440 -0
- package/starter/desert/render/sand.ts +581 -0
- package/starter/desert/render/sky/atmosphere.ts +329 -0
- package/starter/desert/render/sky/sky.ts +382 -0
- package/starter/desert/render/texturePreparation.ts +135 -0
- package/starter/desert/render/water/river.ts +1132 -0
- package/starter/desert/render/worldNames.ts +6 -0
- package/starter/desert/scene.ts +748 -0
- package/starter/desert/sceneProps.ts +562 -0
- package/starter/desert/sceneState.ts +33 -0
- package/starter/desert/world/terrainAssets.ts +162 -0
- package/starter/desert/world/walkBand.ts +47 -0
- package/starter/desert/world/water.ts +74 -0
- package/starter/desert/worlds.ts +13 -0
- package/starter/forest/README.md +59 -116
- package/starter/forest/assets.json +14 -14
- package/starter/forest/bake.mjs +26 -4
- package/starter/forest/playerRig.ts +89 -0
- package/starter/forest/recipe.json +233 -43
- package/starter/forest/render/bakedFile.ts +17 -0
- package/starter/forest/render/benchmark.ts +77 -0
- package/starter/forest/render/biomeData.ts +14 -0
- package/starter/forest/render/biomes.ts +637 -0
- package/starter/forest/render/ground/cover.ts +565 -0
- package/starter/forest/render/ground/horizon.ts +219 -0
- package/starter/forest/render/ground/palette.d.ts +1 -0
- package/starter/forest/render/ground/palette.js +11 -0
- package/starter/forest/render/ground/scatter.ts +104 -0
- package/starter/forest/render/ground/terrain.ts +1796 -0
- package/starter/forest/render/kitAssets.ts +28 -0
- package/starter/forest/render/loading.ts +354 -0
- package/starter/forest/render/lookKnobs.ts +37 -0
- package/starter/forest/render/orbitCamera.ts +155 -0
- package/starter/forest/render/props/farForest.ts +94 -0
- package/starter/forest/render/props/pack.ts +1078 -0
- package/starter/forest/render/props/prepared.ts +226 -0
- package/starter/forest/render/props/propMaterials.ts +1247 -0
- package/starter/forest/render/props/propProgress.ts +2 -0
- package/starter/forest/render/props/propWorld.ts +105 -0
- package/starter/forest/render/props/props.ts +624 -0
- package/starter/forest/render/props/spruce.ts +440 -0
- package/starter/forest/render/sky/atmosphere.ts +329 -0
- package/starter/forest/render/sky/sky.ts +382 -0
- package/starter/forest/render/texturePreparation.ts +135 -0
- package/starter/forest/render/water/river.ts +1132 -0
- package/starter/forest/render/worldNames.ts +6 -0
- package/starter/forest/scene.ts +715 -0
- package/starter/forest/sceneProps.ts +559 -0
- package/starter/forest/sceneState.ts +33 -0
- package/starter/forest/surface.json +2 -2
- package/starter/forest/world/terrainAssets.ts +158 -0
- package/starter/forest/world/walkBand.ts +47 -0
- package/starter/forest/world/water.ts +74 -0
- package/starter/forest/worlds.ts +13 -0
- package/starter/tundra/README.md +35 -14
- package/starter/tundra/bake.mjs +14 -1
- package/starter/tundra/biome.data.json +6 -6
- package/starter/tundra/playerRig.ts +89 -0
- package/starter/tundra/recipe.json +111 -110
- package/starter/tundra/render/bakedFile.ts +17 -0
- package/starter/tundra/render/benchmark.ts +79 -0
- package/starter/tundra/render/biomeData.ts +14 -0
- package/starter/tundra/render/biomes.ts +714 -0
- package/starter/tundra/render/ground/cover.ts +565 -0
- package/starter/tundra/render/ground/horizon.ts +219 -0
- package/starter/tundra/render/ground/palette.d.ts +1 -0
- package/starter/tundra/render/ground/palette.js +11 -0
- package/starter/tundra/render/ground/scatter.ts +104 -0
- package/starter/tundra/render/ground/terrain.ts +1874 -0
- package/starter/tundra/render/kitAssets.ts +28 -0
- package/starter/tundra/render/loading.ts +354 -0
- package/starter/tundra/render/lookKnobs.ts +37 -0
- package/starter/tundra/render/orbitCamera.ts +155 -0
- package/starter/tundra/render/props/farForest.ts +96 -0
- package/starter/tundra/render/props/pack.ts +1088 -0
- package/starter/tundra/render/props/prepared.ts +225 -0
- package/starter/tundra/render/props/propMaterials.ts +1247 -0
- package/starter/tundra/render/props/propProgress.ts +2 -0
- package/starter/tundra/render/props/propWorld.ts +105 -0
- package/starter/tundra/render/props/props.ts +624 -0
- package/starter/tundra/render/props/spruce.ts +440 -0
- package/starter/tundra/render/sky/atmosphere.ts +329 -0
- package/starter/tundra/render/sky/sky.ts +382 -0
- package/starter/tundra/render/texturePreparation.ts +135 -0
- package/starter/tundra/render/water/river.ts +1132 -0
- package/starter/tundra/render/worldNames.ts +6 -0
- package/starter/tundra/scene.ts +715 -0
- package/starter/tundra/sceneProps.ts +559 -0
- package/starter/tundra/sceneState.ts +33 -0
- package/starter/tundra/world/terrainAssets.ts +159 -0
- package/starter/tundra/world/walkBand.ts +47 -0
- package/starter/tundra/world/water.ts +74 -0
- package/starter/tundra/worlds.ts +13 -0
- package/starter-assets.json +4 -4
- package/starter-credits.json +901 -15
- package/AGENT_GUIDE.md +0 -209
- package/starter/forest/sky.ts +0 -38
- package/starter/forest/water.ts +0 -257
- package/starter/forest/world.ts +0 -182
package/dist/kit.js
CHANGED
|
@@ -1,115 +1,12 @@
|
|
|
1
1
|
import { readFile, mkdir, mkdtemp, rename, rm, writeFile, copyFile, lstat } from 'fs/promises';
|
|
2
|
+
import { homedir, tmpdir } from 'os';
|
|
2
3
|
import { dirname, join, resolve } from 'path';
|
|
3
|
-
import { fileURLToPath } from 'url';
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
5
|
+
import { existsSync, statSync, readdirSync, readFileSync } from 'fs';
|
|
4
6
|
import { createHash } from 'crypto';
|
|
5
|
-
import { homedir } from 'os';
|
|
6
7
|
import { gunzipSync } from 'zlib';
|
|
7
8
|
|
|
8
9
|
// src/kit.ts
|
|
9
|
-
function fail(reason) {
|
|
10
|
-
throw new Error(`TN_TERRAIN_ASSETS_${reason}`);
|
|
11
|
-
}
|
|
12
|
-
async function isDirectory(path) {
|
|
13
|
-
return (await lstat(path).catch((error) => {
|
|
14
|
-
if (error.code === "ENOENT") return void 0;
|
|
15
|
-
throw error;
|
|
16
|
-
}))?.isDirectory() === true;
|
|
17
|
-
}
|
|
18
|
-
async function extract(archive, target) {
|
|
19
|
-
const tar = gunzipSync(archive, { maxOutputLength: 128 * 1024 * 1024 });
|
|
20
|
-
const seen = /* @__PURE__ */ new Set();
|
|
21
|
-
for (let at = 0; at + 512 <= tar.length; ) {
|
|
22
|
-
const header = tar.subarray(at, at + 512);
|
|
23
|
-
if (header.every((byte) => byte === 0)) {
|
|
24
|
-
if (tar.length < at + 1024 || !tar.subarray(at).every((byte) => byte === 0) || seen.size === 0)
|
|
25
|
-
fail("END");
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
const text = (from, to) => header.subarray(from, to).toString("utf8").split("\0")[0] ?? "";
|
|
29
|
-
const octal = (from, to) => {
|
|
30
|
-
const value = text(from, to).trim();
|
|
31
|
-
if (!/^[0-7]+$/u.test(value)) fail("HEADER");
|
|
32
|
-
return Number.parseInt(value, 8);
|
|
33
|
-
};
|
|
34
|
-
const checksum = header.reduce(
|
|
35
|
-
(sum, byte, index) => sum + (index >= 148 && index < 156 ? 32 : byte),
|
|
36
|
-
0
|
|
37
|
-
);
|
|
38
|
-
if (text(257, 263) !== "ustar" || text(263, 265) !== "00" || octal(148, 156) !== checksum)
|
|
39
|
-
fail("HEADER");
|
|
40
|
-
const prefix = text(345, 500);
|
|
41
|
-
const name = `${prefix ? `${prefix}/` : ""}${text(0, 100)}`.replace(/\/$/u, "");
|
|
42
|
-
if (!name.startsWith("starter-assets/") && name !== "starter-assets") fail("PATH");
|
|
43
|
-
if (name.includes("\\") || name.split("/").some((part) => part === ".." || part === "." || part === "" || part.includes(":")))
|
|
44
|
-
fail("PATH");
|
|
45
|
-
if (seen.has(name)) fail("DUPLICATE");
|
|
46
|
-
seen.add(name);
|
|
47
|
-
const size = octal(124, 136);
|
|
48
|
-
const type = header[156];
|
|
49
|
-
if (type !== 48 && type !== 53) fail("TYPE");
|
|
50
|
-
if (!Number.isSafeInteger(size) || size < 0 || type === 53 && size !== 0 || name === "starter-assets" && type !== 53)
|
|
51
|
-
fail("SIZE");
|
|
52
|
-
const next = at + 512 + Math.ceil(size / 512) * 512;
|
|
53
|
-
if (next > tar.length) fail("TRUNCATED");
|
|
54
|
-
const path = join(target, ...name.split("/").slice(1));
|
|
55
|
-
if (type === 53) await mkdir(path, { recursive: true, mode: 493 });
|
|
56
|
-
else {
|
|
57
|
-
await mkdir(dirname(path), { recursive: true, mode: 493 });
|
|
58
|
-
await writeFile(path, tar.subarray(at + 512, at + 512 + size), { flag: "wx", mode: 420 });
|
|
59
|
-
}
|
|
60
|
-
at = next;
|
|
61
|
-
}
|
|
62
|
-
fail("END");
|
|
63
|
-
}
|
|
64
|
-
async function starterAssetsDir() {
|
|
65
|
-
const root = dirname(fileURLToPath(import.meta.resolve("@threenative/terrain/package.json")));
|
|
66
|
-
const local = join(root, "starter-assets");
|
|
67
|
-
if (await isDirectory(local)) return local;
|
|
68
|
-
const manifest = JSON.parse(await readFile(join(root, "starter-assets.json"), "utf8"));
|
|
69
|
-
if (!/^[a-f0-9]{64}$/u.test(manifest.sha256) || !Number.isSafeInteger(manifest.bytes) || manifest.bytes <= 0 || manifest.bytes > 128 * 1024 * 1024)
|
|
70
|
-
fail("MANIFEST");
|
|
71
|
-
const cache = join(
|
|
72
|
-
process.env.XDG_CACHE_HOME || join(homedir(), ".cache"),
|
|
73
|
-
"threenative",
|
|
74
|
-
"terrain-starter-assets",
|
|
75
|
-
manifest.sha256
|
|
76
|
-
);
|
|
77
|
-
if (await isDirectory(cache)) return cache;
|
|
78
|
-
const url = new URL(process.env.THREENATIVE_TERRAIN_ASSETS_URL || manifest.url);
|
|
79
|
-
let archive;
|
|
80
|
-
if (url.protocol === "file:") archive = await readFile(url);
|
|
81
|
-
else {
|
|
82
|
-
if (url.protocol !== "https:" && url.protocol !== "http:") fail("URL");
|
|
83
|
-
const response = await fetch(url);
|
|
84
|
-
if (!response.ok) fail(`HTTP: ${response.status} ${url.href}`);
|
|
85
|
-
const declared = Number(response.headers.get("content-length") ?? manifest.bytes);
|
|
86
|
-
if (declared !== manifest.bytes || !response.body) fail("SIZE");
|
|
87
|
-
const chunks = [];
|
|
88
|
-
let total = 0;
|
|
89
|
-
for await (const chunk of response.body) {
|
|
90
|
-
total += chunk.length;
|
|
91
|
-
if (total > manifest.bytes) fail("SIZE");
|
|
92
|
-
chunks.push(chunk);
|
|
93
|
-
}
|
|
94
|
-
archive = Buffer.concat(chunks);
|
|
95
|
-
}
|
|
96
|
-
if (archive.length !== manifest.bytes) fail("SIZE");
|
|
97
|
-
if (createHash("sha256").update(archive).digest("hex") !== manifest.sha256) fail("SHA256");
|
|
98
|
-
await mkdir(dirname(cache), { recursive: true });
|
|
99
|
-
const tmp = await mkdtemp(`${cache}.tmp-`);
|
|
100
|
-
try {
|
|
101
|
-
await extract(archive, tmp);
|
|
102
|
-
try {
|
|
103
|
-
await rename(tmp, cache);
|
|
104
|
-
} catch (error) {
|
|
105
|
-
if (!await isDirectory(cache) || !["EEXIST", "ENOTEMPTY"].includes(error.code ?? ""))
|
|
106
|
-
throw error;
|
|
107
|
-
}
|
|
108
|
-
} finally {
|
|
109
|
-
await rm(tmp, { recursive: true, force: true });
|
|
110
|
-
}
|
|
111
|
-
return cache;
|
|
112
|
-
}
|
|
113
10
|
|
|
114
11
|
// src/core/math.ts
|
|
115
12
|
var clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));
|
|
@@ -265,6 +162,304 @@ function splinePoints(points, step = 3, smooth = true) {
|
|
|
265
162
|
out.push([last[0], last[1], last[2]]);
|
|
266
163
|
return out;
|
|
267
164
|
}
|
|
165
|
+
var round = (value) => Math.round(value * 1e3) / 1e3;
|
|
166
|
+
var ENTRIES = ["index.js", "kit.js", "mcp/cook-child.js", "mcp/server.js"];
|
|
167
|
+
function sourceFiles(directory, found = []) {
|
|
168
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
169
|
+
const absolute = join(directory, entry.name);
|
|
170
|
+
if (entry.isDirectory()) sourceFiles(absolute, found);
|
|
171
|
+
else if (entry.name.endsWith(".ts")) found.push(absolute);
|
|
172
|
+
}
|
|
173
|
+
return found;
|
|
174
|
+
}
|
|
175
|
+
var written = (file) => statSync(file).mtimeMs;
|
|
176
|
+
function assertDistFresh(root) {
|
|
177
|
+
const packageRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
178
|
+
const source = join(packageRoot, "src");
|
|
179
|
+
if (!existsSync(source)) return;
|
|
180
|
+
const built = ENTRIES.map((entry) => join(packageRoot, "dist", entry));
|
|
181
|
+
if (!built.every((file) => existsSync(file))) return;
|
|
182
|
+
const oldestBuild = Math.min(...built.map(written));
|
|
183
|
+
const newestSource = Math.max(...sourceFiles(source).map(written));
|
|
184
|
+
if (newestSource <= oldestBuild) return;
|
|
185
|
+
throw new Error(
|
|
186
|
+
"terrain: the built package is older than packages/terrain/src, so this bake would cook the previous world. Run: pnpm --filter @threenative/terrain build"
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
function fail(reason) {
|
|
190
|
+
throw new Error(`TN_TERRAIN_ASSETS_${reason}`);
|
|
191
|
+
}
|
|
192
|
+
async function isDirectory(path) {
|
|
193
|
+
return (await lstat(path).catch((error) => {
|
|
194
|
+
if (error.code === "ENOENT") return void 0;
|
|
195
|
+
throw error;
|
|
196
|
+
}))?.isDirectory() === true;
|
|
197
|
+
}
|
|
198
|
+
async function extract(archive, target) {
|
|
199
|
+
const tar = gunzipSync(archive, { maxOutputLength: 128 * 1024 * 1024 });
|
|
200
|
+
const seen = /* @__PURE__ */ new Set();
|
|
201
|
+
for (let at = 0; at + 512 <= tar.length; ) {
|
|
202
|
+
const header = tar.subarray(at, at + 512);
|
|
203
|
+
if (header.every((byte) => byte === 0)) {
|
|
204
|
+
if (tar.length < at + 1024 || !tar.subarray(at).every((byte) => byte === 0) || seen.size === 0)
|
|
205
|
+
fail("END");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const text = (from, to) => header.subarray(from, to).toString("utf8").split("\0")[0] ?? "";
|
|
209
|
+
const octal = (from, to) => {
|
|
210
|
+
const value = text(from, to).trim();
|
|
211
|
+
if (!/^[0-7]+$/u.test(value)) fail("HEADER");
|
|
212
|
+
return Number.parseInt(value, 8);
|
|
213
|
+
};
|
|
214
|
+
const checksum = header.reduce(
|
|
215
|
+
(sum, byte, index) => sum + (index >= 148 && index < 156 ? 32 : byte),
|
|
216
|
+
0
|
|
217
|
+
);
|
|
218
|
+
if (text(257, 263) !== "ustar" || text(263, 265) !== "00" || octal(148, 156) !== checksum)
|
|
219
|
+
fail("HEADER");
|
|
220
|
+
const prefix = text(345, 500);
|
|
221
|
+
const name = `${prefix ? `${prefix}/` : ""}${text(0, 100)}`.replace(/\/$/u, "");
|
|
222
|
+
if (!name.startsWith("starter-assets/") && name !== "starter-assets") fail("PATH");
|
|
223
|
+
if (name.includes("\\") || name.split("/").some((part) => part === ".." || part === "." || part === "" || part.includes(":")))
|
|
224
|
+
fail("PATH");
|
|
225
|
+
if (seen.has(name)) fail("DUPLICATE");
|
|
226
|
+
seen.add(name);
|
|
227
|
+
const size = octal(124, 136);
|
|
228
|
+
const type = header[156];
|
|
229
|
+
if (type !== 48 && type !== 53) fail("TYPE");
|
|
230
|
+
if (!Number.isSafeInteger(size) || size < 0 || type === 53 && size !== 0 || name === "starter-assets" && type !== 53)
|
|
231
|
+
fail("SIZE");
|
|
232
|
+
const next = at + 512 + Math.ceil(size / 512) * 512;
|
|
233
|
+
if (next > tar.length) fail("TRUNCATED");
|
|
234
|
+
const path = join(target, ...name.split("/").slice(1));
|
|
235
|
+
if (type === 53) await mkdir(path, { recursive: true, mode: 493 });
|
|
236
|
+
else {
|
|
237
|
+
await mkdir(dirname(path), { recursive: true, mode: 493 });
|
|
238
|
+
await writeFile(path, tar.subarray(at + 512, at + 512 + size), { flag: "wx", mode: 420 });
|
|
239
|
+
}
|
|
240
|
+
at = next;
|
|
241
|
+
}
|
|
242
|
+
fail("END");
|
|
243
|
+
}
|
|
244
|
+
var DEFAULT_SKY_HDRI = "hdri/kloofendal_48d_partly_cloudy_1k.hdr";
|
|
245
|
+
async function starterAssetsDir() {
|
|
246
|
+
const root = dirname(fileURLToPath(import.meta.resolve("@threenative/terrain/package.json")));
|
|
247
|
+
const local = join(root, "starter-assets");
|
|
248
|
+
if (await isDirectory(local)) return local;
|
|
249
|
+
const manifest = JSON.parse(await readFile(join(root, "starter-assets.json"), "utf8"));
|
|
250
|
+
if (!/^[a-f0-9]{64}$/u.test(manifest.sha256) || !Number.isSafeInteger(manifest.bytes) || manifest.bytes <= 0 || manifest.bytes > 128 * 1024 * 1024)
|
|
251
|
+
fail("MANIFEST");
|
|
252
|
+
const cache = join(
|
|
253
|
+
process.env.XDG_CACHE_HOME || join(homedir(), ".cache"),
|
|
254
|
+
"threenative",
|
|
255
|
+
"terrain-starter-assets",
|
|
256
|
+
manifest.sha256
|
|
257
|
+
);
|
|
258
|
+
if (await isDirectory(cache)) return cache;
|
|
259
|
+
const url = new URL(process.env.THREENATIVE_TERRAIN_ASSETS_URL || manifest.url);
|
|
260
|
+
let archive;
|
|
261
|
+
if (url.protocol === "file:") archive = await readFile(url);
|
|
262
|
+
else {
|
|
263
|
+
if (url.protocol !== "https:" && url.protocol !== "http:") fail("URL");
|
|
264
|
+
const response = await fetch(url);
|
|
265
|
+
if (!response.ok) fail(`HTTP: ${response.status} ${url.href}`);
|
|
266
|
+
const declared = Number(response.headers.get("content-length") ?? manifest.bytes);
|
|
267
|
+
if (declared !== manifest.bytes || !response.body) fail("SIZE");
|
|
268
|
+
const chunks = [];
|
|
269
|
+
let total = 0;
|
|
270
|
+
for await (const chunk of response.body) {
|
|
271
|
+
total += chunk.length;
|
|
272
|
+
if (total > manifest.bytes) fail("SIZE");
|
|
273
|
+
chunks.push(chunk);
|
|
274
|
+
}
|
|
275
|
+
archive = Buffer.concat(chunks);
|
|
276
|
+
}
|
|
277
|
+
if (archive.length !== manifest.bytes) fail("SIZE");
|
|
278
|
+
if (createHash("sha256").update(archive).digest("hex") !== manifest.sha256) fail("SHA256");
|
|
279
|
+
await mkdir(dirname(cache), { recursive: true });
|
|
280
|
+
const tmp = await mkdtemp(`${cache}.tmp-`);
|
|
281
|
+
try {
|
|
282
|
+
await extract(archive, tmp);
|
|
283
|
+
try {
|
|
284
|
+
await rename(tmp, cache);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
if (!await isDirectory(cache) || !["EEXIST", "ENOTEMPTY"].includes(error.code ?? ""))
|
|
287
|
+
throw error;
|
|
288
|
+
}
|
|
289
|
+
} finally {
|
|
290
|
+
await rm(tmp, { recursive: true, force: true });
|
|
291
|
+
}
|
|
292
|
+
return cache;
|
|
293
|
+
}
|
|
294
|
+
var PACKAGE = "@threenative/procedural-environment";
|
|
295
|
+
var SPECIES = /^arbor:([a-z0-9]+)(?:@(\d{1,9}))?$/u;
|
|
296
|
+
var LODS = [1, 2];
|
|
297
|
+
function parseArborSpecies(species) {
|
|
298
|
+
if (!species.startsWith("arbor:")) return void 0;
|
|
299
|
+
const match = SPECIES.exec(species);
|
|
300
|
+
if (match === null)
|
|
301
|
+
throw new Error(
|
|
302
|
+
`cook: species '${species}' must read arbor:<preset> or arbor:<preset>@<seed>, for example arbor:oak@7.`
|
|
303
|
+
);
|
|
304
|
+
return { preset: match[1], seed: match[2] === void 0 ? 1 : Number(match[2]) };
|
|
305
|
+
}
|
|
306
|
+
var idOf = (preset, seed) => `arbor-${preset}-${seed}`;
|
|
307
|
+
function installedEntry(projectRoot) {
|
|
308
|
+
let directory = resolve(projectRoot);
|
|
309
|
+
while (true) {
|
|
310
|
+
const manifestFile = join(directory, "node_modules", PACKAGE, "package.json");
|
|
311
|
+
if (existsSync(manifestFile)) {
|
|
312
|
+
const manifest = JSON.parse(readFileSync(manifestFile, "utf8"));
|
|
313
|
+
const root = manifest.exports?.["."];
|
|
314
|
+
const file = (typeof root === "string" ? root : root?.import) ?? manifest.main;
|
|
315
|
+
return file === void 0 ? void 0 : resolve(dirname(manifestFile), file);
|
|
316
|
+
}
|
|
317
|
+
const parent = dirname(directory);
|
|
318
|
+
if (parent === directory) return void 0;
|
|
319
|
+
directory = parent;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
async function loadVegetation(projectRoot, wanted) {
|
|
323
|
+
const entry = installedEntry(projectRoot);
|
|
324
|
+
if (entry === void 0 || !existsSync(entry))
|
|
325
|
+
throw new Error(`cook: species '${wanted}' needs the Arbor bake: npm i -D ${PACKAGE}`);
|
|
326
|
+
return await import(pathToFileURL(entry).href);
|
|
327
|
+
}
|
|
328
|
+
async function growArborAsset(projectRoot, scratch, species, seedOffset = 0, patch = {}) {
|
|
329
|
+
const parsed = parseArborSpecies(species);
|
|
330
|
+
if (parsed === void 0) throw new Error(`cook: species '${species}' must start with arbor:.`);
|
|
331
|
+
const seed = parsed.seed + seedOffset;
|
|
332
|
+
const id = idOf(parsed.preset, seed);
|
|
333
|
+
const vegetation = await loadVegetation(projectRoot, species);
|
|
334
|
+
if (!vegetation.vegetationPresets().some((p) => p.id === parsed.preset))
|
|
335
|
+
throw new Error(
|
|
336
|
+
`cook: species '${species}' names no Arbor preset. Call vegetationPresets() from ${PACKAGE} for the ids.`
|
|
337
|
+
);
|
|
338
|
+
const files = await vegetation.bakeVegetation({
|
|
339
|
+
lods: LODS,
|
|
340
|
+
out: join(scratch, id),
|
|
341
|
+
preset: parsed.preset,
|
|
342
|
+
seed,
|
|
343
|
+
...patch
|
|
344
|
+
});
|
|
345
|
+
const [near, mid] = files;
|
|
346
|
+
return { bounds: near.bounds, maxDistance: 600, mid: mid.path, midDistance: 45, near: near.path };
|
|
347
|
+
}
|
|
348
|
+
async function growArborRows(table, projectRoot, scratch, assetsDir = projectRoot) {
|
|
349
|
+
let grew = false;
|
|
350
|
+
for (const [id, spec] of Object.entries(table)) {
|
|
351
|
+
const { arbor, form, leafImage, ...row } = spec;
|
|
352
|
+
if (arbor === void 0) continue;
|
|
353
|
+
const patch = {
|
|
354
|
+
...form === void 0 ? {} : { form },
|
|
355
|
+
...leafImage === void 0 ? {} : { leafImage: resolve(assetsDir, leafImage) }
|
|
356
|
+
};
|
|
357
|
+
table[id] = {
|
|
358
|
+
...await growArborAsset(projectRoot, scratch, `arbor:${arbor}`, 0, patch),
|
|
359
|
+
...row
|
|
360
|
+
};
|
|
361
|
+
grew = true;
|
|
362
|
+
}
|
|
363
|
+
return grew;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// src/core/masks.ts
|
|
367
|
+
var MATERIAL_IDS = Object.freeze([
|
|
368
|
+
"grass",
|
|
369
|
+
"dirt",
|
|
370
|
+
"rock",
|
|
371
|
+
"snow",
|
|
372
|
+
"sand",
|
|
373
|
+
"mud",
|
|
374
|
+
"road",
|
|
375
|
+
"moss"
|
|
376
|
+
]);
|
|
377
|
+
var range = (v, min, max, fade = 0) => fade > 0 ? smoothstep(min - fade, min, v) * (1 - smoothstep(max, max + fade, v)) : Number(v >= min && v <= max);
|
|
378
|
+
function compileMask(mask, s) {
|
|
379
|
+
if (!mask || mask.type === "all") return () => 1;
|
|
380
|
+
switch (mask.type) {
|
|
381
|
+
case "none":
|
|
382
|
+
return () => 0;
|
|
383
|
+
case "circle": {
|
|
384
|
+
const centre = mask.at;
|
|
385
|
+
return (i, x, z) => falloff(
|
|
386
|
+
Math.hypot(x - centre[0], z - centre[1]),
|
|
387
|
+
mask.radius,
|
|
388
|
+
mask.falloff ?? 0.6
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
case "rectangle": {
|
|
392
|
+
const centre = mask.at;
|
|
393
|
+
const rad = (mask.rotation ?? 0) * Math.PI / 180;
|
|
394
|
+
const c = Math.cos(rad);
|
|
395
|
+
const sn = Math.sin(rad);
|
|
396
|
+
const size = Array.isArray(mask.size) ? mask.size : [mask.size, mask.size];
|
|
397
|
+
return (i, x, z) => {
|
|
398
|
+
const dx = x - centre[0];
|
|
399
|
+
const dz = z - centre[1];
|
|
400
|
+
return falloff(
|
|
401
|
+
Math.max(
|
|
402
|
+
Math.abs(dx * c + dz * sn) / (size[0] / 2),
|
|
403
|
+
Math.abs(-dx * sn + dz * c) / (size[1] / 2)
|
|
404
|
+
),
|
|
405
|
+
1,
|
|
406
|
+
mask.falloff ?? 0.2
|
|
407
|
+
);
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
case "height":
|
|
411
|
+
return (i) => range(s.height[i], mask.min ?? -1e9, mask.max ?? 1e9, mask.fade);
|
|
412
|
+
case "slope":
|
|
413
|
+
return (i) => range(slopeAtIndex(s, i), mask.min ?? 0, mask.max ?? 90, mask.fade);
|
|
414
|
+
case "noise":
|
|
415
|
+
return (i, x, z) => smoothstep(
|
|
416
|
+
(mask.threshold ?? 0.5) - (mask.fade ?? 0.15),
|
|
417
|
+
(mask.threshold ?? 0.5) + (mask.fade ?? 0.15),
|
|
418
|
+
noise2(x / mask.scale, z / mask.scale, mask.seed ?? s.seed) * 0.5 + 0.5
|
|
419
|
+
);
|
|
420
|
+
case "biome": {
|
|
421
|
+
const field = s.biomes[mask.name];
|
|
422
|
+
return (i) => field?.[i] ?? 0;
|
|
423
|
+
}
|
|
424
|
+
case "material": {
|
|
425
|
+
const channel = MATERIAL_IDS.indexOf(mask.name);
|
|
426
|
+
if (channel < 0) throw Error(`Unknown material: ${String(mask.name)}`);
|
|
427
|
+
return (i) => s.splat[i * 8 + channel];
|
|
428
|
+
}
|
|
429
|
+
case "not": {
|
|
430
|
+
const inner = compileMask(mask.mask, s);
|
|
431
|
+
return (i, x, z) => 1 - inner(i, x, z);
|
|
432
|
+
}
|
|
433
|
+
case "and": {
|
|
434
|
+
const inner = (mask.masks ?? []).map((entry) => compileMask(entry, s));
|
|
435
|
+
return (i, x, z) => inner.reduce((value, sample) => value * sample(i, x, z), 1);
|
|
436
|
+
}
|
|
437
|
+
case "or": {
|
|
438
|
+
const inner = (mask.masks ?? []).map((entry) => compileMask(entry, s));
|
|
439
|
+
return (i, x, z) => 1 - inner.reduce((value, sample) => value * (1 - sample(i, x, z)), 1);
|
|
440
|
+
}
|
|
441
|
+
default:
|
|
442
|
+
throw Error(`Unknown mask: ${String(mask.type)}`);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
function rasterMask(mask, s) {
|
|
446
|
+
if (!mask || mask.type === "all") return null;
|
|
447
|
+
const out = new Float32Array(s.height.length);
|
|
448
|
+
const sample = compileMask(mask, s);
|
|
449
|
+
for (let z = 0; z < s.resolution; z += 1) {
|
|
450
|
+
for (let x = 0; x < s.resolution; x += 1) {
|
|
451
|
+
const i = z * s.resolution + x;
|
|
452
|
+
out[i] = clamp(sample(i, world(s, x), world(s, z)));
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
return out;
|
|
456
|
+
}
|
|
457
|
+
function paintWeights(s, i, channel, weight) {
|
|
458
|
+
const w = clamp(weight);
|
|
459
|
+
const base = i * 8;
|
|
460
|
+
for (let c = 0; c < 8; c += 1)
|
|
461
|
+
s.splat[base + c] = s.splat[base + c] * (1 - w) + (c === channel ? w : 0);
|
|
462
|
+
}
|
|
268
463
|
|
|
269
464
|
// src/core/erosion.ts
|
|
270
465
|
function thermal(height, n, size, { iterations = 20, talus = 32, rate = 0.22 } = {}, observations) {
|
|
@@ -892,104 +1087,6 @@ function gully(height, n, size, { iterations = 200, capacity = 0.05, rain = 5e-4
|
|
|
892
1087
|
return Float32Array.from(h, (surface, i) => surface + sediment[i]);
|
|
893
1088
|
}
|
|
894
1089
|
|
|
895
|
-
// src/core/masks.ts
|
|
896
|
-
var MATERIAL_IDS = Object.freeze([
|
|
897
|
-
"grass",
|
|
898
|
-
"dirt",
|
|
899
|
-
"rock",
|
|
900
|
-
"snow",
|
|
901
|
-
"sand",
|
|
902
|
-
"mud",
|
|
903
|
-
"road",
|
|
904
|
-
"moss"
|
|
905
|
-
]);
|
|
906
|
-
var range = (v, min, max, fade = 0) => fade > 0 ? smoothstep(min - fade, min, v) * (1 - smoothstep(max, max + fade, v)) : Number(v >= min && v <= max);
|
|
907
|
-
function compileMask(mask, s) {
|
|
908
|
-
if (!mask || mask.type === "all") return () => 1;
|
|
909
|
-
switch (mask.type) {
|
|
910
|
-
case "none":
|
|
911
|
-
return () => 0;
|
|
912
|
-
case "circle": {
|
|
913
|
-
const centre = mask.at;
|
|
914
|
-
return (i, x, z) => falloff(
|
|
915
|
-
Math.hypot(x - centre[0], z - centre[1]),
|
|
916
|
-
mask.radius,
|
|
917
|
-
mask.falloff ?? 0.6
|
|
918
|
-
);
|
|
919
|
-
}
|
|
920
|
-
case "rectangle": {
|
|
921
|
-
const centre = mask.at;
|
|
922
|
-
const rad = (mask.rotation ?? 0) * Math.PI / 180;
|
|
923
|
-
const c = Math.cos(rad);
|
|
924
|
-
const sn = Math.sin(rad);
|
|
925
|
-
const size = Array.isArray(mask.size) ? mask.size : [mask.size, mask.size];
|
|
926
|
-
return (i, x, z) => {
|
|
927
|
-
const dx = x - centre[0];
|
|
928
|
-
const dz = z - centre[1];
|
|
929
|
-
return falloff(
|
|
930
|
-
Math.max(
|
|
931
|
-
Math.abs(dx * c + dz * sn) / (size[0] / 2),
|
|
932
|
-
Math.abs(-dx * sn + dz * c) / (size[1] / 2)
|
|
933
|
-
),
|
|
934
|
-
1,
|
|
935
|
-
mask.falloff ?? 0.2
|
|
936
|
-
);
|
|
937
|
-
};
|
|
938
|
-
}
|
|
939
|
-
case "height":
|
|
940
|
-
return (i) => range(s.height[i], mask.min ?? -1e9, mask.max ?? 1e9, mask.fade);
|
|
941
|
-
case "slope":
|
|
942
|
-
return (i) => range(slopeAtIndex(s, i), mask.min ?? 0, mask.max ?? 90, mask.fade);
|
|
943
|
-
case "noise":
|
|
944
|
-
return (i, x, z) => smoothstep(
|
|
945
|
-
(mask.threshold ?? 0.5) - (mask.fade ?? 0.15),
|
|
946
|
-
(mask.threshold ?? 0.5) + (mask.fade ?? 0.15),
|
|
947
|
-
noise2(x / mask.scale, z / mask.scale, mask.seed ?? s.seed) * 0.5 + 0.5
|
|
948
|
-
);
|
|
949
|
-
case "biome": {
|
|
950
|
-
const field = s.biomes[mask.name];
|
|
951
|
-
return (i) => field?.[i] ?? 0;
|
|
952
|
-
}
|
|
953
|
-
case "material": {
|
|
954
|
-
const channel = MATERIAL_IDS.indexOf(mask.name);
|
|
955
|
-
if (channel < 0) throw Error(`Unknown material: ${String(mask.name)}`);
|
|
956
|
-
return (i) => s.splat[i * 8 + channel];
|
|
957
|
-
}
|
|
958
|
-
case "not": {
|
|
959
|
-
const inner = compileMask(mask.mask, s);
|
|
960
|
-
return (i, x, z) => 1 - inner(i, x, z);
|
|
961
|
-
}
|
|
962
|
-
case "and": {
|
|
963
|
-
const inner = (mask.masks ?? []).map((entry) => compileMask(entry, s));
|
|
964
|
-
return (i, x, z) => inner.reduce((value, sample) => value * sample(i, x, z), 1);
|
|
965
|
-
}
|
|
966
|
-
case "or": {
|
|
967
|
-
const inner = (mask.masks ?? []).map((entry) => compileMask(entry, s));
|
|
968
|
-
return (i, x, z) => 1 - inner.reduce((value, sample) => value * (1 - sample(i, x, z)), 1);
|
|
969
|
-
}
|
|
970
|
-
default:
|
|
971
|
-
throw Error(`Unknown mask: ${String(mask.type)}`);
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
function rasterMask(mask, s) {
|
|
975
|
-
if (!mask || mask.type === "all") return null;
|
|
976
|
-
const out = new Float32Array(s.height.length);
|
|
977
|
-
const sample = compileMask(mask, s);
|
|
978
|
-
for (let z = 0; z < s.resolution; z += 1) {
|
|
979
|
-
for (let x = 0; x < s.resolution; x += 1) {
|
|
980
|
-
const i = z * s.resolution + x;
|
|
981
|
-
out[i] = clamp(sample(i, world(s, x), world(s, z)));
|
|
982
|
-
}
|
|
983
|
-
}
|
|
984
|
-
return out;
|
|
985
|
-
}
|
|
986
|
-
function paintWeights(s, i, channel, weight) {
|
|
987
|
-
const w = clamp(weight);
|
|
988
|
-
const base = i * 8;
|
|
989
|
-
for (let c = 0; c < 8; c += 1)
|
|
990
|
-
s.splat[base + c] = s.splat[base + c] * (1 - w) + (c === channel ? w : 0);
|
|
991
|
-
}
|
|
992
|
-
|
|
993
1090
|
// src/core/scree.ts
|
|
994
1091
|
var NEIGHBOURS = [
|
|
995
1092
|
[-1, -1],
|
|
@@ -1171,7 +1268,7 @@ function gradeToGround(s, points, p) {
|
|
|
1171
1268
|
const maxFill = p.maxFill ?? 1.5;
|
|
1172
1269
|
const grade2 = p.maxGrade ?? 0.12;
|
|
1173
1270
|
const limit = (k) => grade2 * Math.max(0.5, run(k));
|
|
1174
|
-
for (let
|
|
1271
|
+
for (let round2 = 0; round2 < 16; round2 += 1) {
|
|
1175
1272
|
let moved = 0;
|
|
1176
1273
|
for (let k = 1; k < profile.length; k += 1) {
|
|
1177
1274
|
const y = clamp(
|
|
@@ -2293,7 +2390,7 @@ var TerrainEvaluator = class {
|
|
|
2293
2390
|
}
|
|
2294
2391
|
};
|
|
2295
2392
|
|
|
2296
|
-
// src/
|
|
2393
|
+
// src/package/world-package.ts
|
|
2297
2394
|
var HEIGHTMAP_MAX = 65535;
|
|
2298
2395
|
var RECORD_VALUES = 8;
|
|
2299
2396
|
var COMPONENTS = ["r", "g", "b", "a"];
|
|
@@ -2380,16 +2477,16 @@ function placementCells(state, assets, cellSize, extent) {
|
|
|
2380
2477
|
const records = new Float32Array(buffer);
|
|
2381
2478
|
const ordered = [];
|
|
2382
2479
|
const ids = [];
|
|
2383
|
-
let
|
|
2480
|
+
let written2 = 0;
|
|
2384
2481
|
const byId = (left, right) => left[0] < right[0] ? -1 : 1;
|
|
2385
2482
|
for (const cell of [...cells.values()].sort((a, b) => a.x - b.x || a.z - b.z)) {
|
|
2386
2483
|
const runs = [];
|
|
2387
2484
|
for (const [asset, items] of [...cell.items.entries()].sort(byId)) {
|
|
2388
|
-
runs.push({ asset, count: items.length, offset:
|
|
2485
|
+
runs.push({ asset, count: items.length, offset: written2 });
|
|
2389
2486
|
for (const item of items) {
|
|
2390
|
-
writePlacementRecord(records,
|
|
2487
|
+
writePlacementRecord(records, written2 * RECORD_VALUES, item, extent);
|
|
2391
2488
|
ids.push(item.id);
|
|
2392
|
-
|
|
2489
|
+
written2 += 1;
|
|
2393
2490
|
}
|
|
2394
2491
|
}
|
|
2395
2492
|
ordered.push({ runs, x: cell.x, z: cell.z });
|
|
@@ -2485,7 +2582,7 @@ function splatBytes(state, resolution) {
|
|
|
2485
2582
|
return bytes;
|
|
2486
2583
|
}
|
|
2487
2584
|
|
|
2488
|
-
// src/
|
|
2585
|
+
// src/population/placements.ts
|
|
2489
2586
|
function validatePlacementOverrides(value = {}) {
|
|
2490
2587
|
finiteTree(value, "placementOverrides");
|
|
2491
2588
|
if (!value || typeof value !== "object" || ![Object.prototype, null].includes(Object.getPrototypeOf(value)))
|
|
@@ -2584,10 +2681,10 @@ function flag(name, fallback) {
|
|
|
2584
2681
|
}
|
|
2585
2682
|
return resolve(value);
|
|
2586
2683
|
}
|
|
2587
|
-
async function bakeKit(url,
|
|
2684
|
+
async function bakeKit(url, options) {
|
|
2685
|
+
assertDistFresh();
|
|
2588
2686
|
const here = dirname(fileURLToPath(url));
|
|
2589
2687
|
const assetsDir = process.argv.includes("--assets") ? flag("assets", "") : await starterAssetsDir();
|
|
2590
|
-
const outDir = flag("out", join(here, "world"));
|
|
2591
2688
|
const recipe = JSON.parse(await readFile(join(here, "recipe.json"), "utf8"));
|
|
2592
2689
|
for (const layer of recipe.recipe.layers) {
|
|
2593
2690
|
const data = layer.params?.data;
|
|
@@ -2595,7 +2692,24 @@ async function bakeKit(url, { sky, cookTerrainWorld }) {
|
|
|
2595
2692
|
const raw = await readFile(join(here, data.raw16));
|
|
2596
2693
|
data.values = Array.from(decodeRAW16(raw, data).values, (value) => Math.round(value * 10) / 10);
|
|
2597
2694
|
}
|
|
2695
|
+
if (options.look !== void 0) {
|
|
2696
|
+
if (typeof recipe.world?.name !== "string")
|
|
2697
|
+
throw new Error("bake: a kit that passes `look` needs a `world` row in recipe.json");
|
|
2698
|
+
const { bakeStreamedWorld } = await import(new URL("./kit-world.js", import.meta.url).href);
|
|
2699
|
+
await bakeStreamedWorld({
|
|
2700
|
+
kitDir: here,
|
|
2701
|
+
assetsDir,
|
|
2702
|
+
out: flag("out", resolve("assets/terrain", recipe.world.name)),
|
|
2703
|
+
recipe,
|
|
2704
|
+
look: options.look
|
|
2705
|
+
});
|
|
2706
|
+
return;
|
|
2707
|
+
}
|
|
2708
|
+
const { sky, cookTerrainWorld } = options;
|
|
2709
|
+
const outDir = flag("out", join(here, "world"));
|
|
2598
2710
|
const assetTable = JSON.parse(await readFile(join(here, "assets.json"), "utf8"));
|
|
2711
|
+
const arborScratch = await mkdtemp(join(tmpdir(), "tn-kit-arbor-"));
|
|
2712
|
+
await growArborRows(assetTable, here, arborScratch, assetsDir);
|
|
2599
2713
|
const surface = JSON.parse(await readFile(join(here, "surface.json"), "utf8"));
|
|
2600
2714
|
const state = applyPlacementOverrides(
|
|
2601
2715
|
new TerrainEvaluator({ cacheMB: 0 }).evaluate(recipe.recipe),
|
|
@@ -2619,9 +2733,9 @@ async function bakeKit(url, { sky, cookTerrainWorld }) {
|
|
|
2619
2733
|
Object.entries(assetTable).map(([id, spec]) => [
|
|
2620
2734
|
id,
|
|
2621
2735
|
{
|
|
2622
|
-
glb:
|
|
2736
|
+
glb: resolve(assetsDir, spec.near),
|
|
2623
2737
|
bounds: spec.bounds,
|
|
2624
|
-
lods: spec.mid ? [{ distance: spec.midDistance ?? 40, glb:
|
|
2738
|
+
lods: spec.mid ? [{ distance: spec.midDistance ?? 40, glb: resolve(assetsDir, spec.mid) }] : [],
|
|
2625
2739
|
maxDistance: spec.maxDistance
|
|
2626
2740
|
}
|
|
2627
2741
|
])
|
|
@@ -2644,20 +2758,19 @@ async function bakeKit(url, { sky, cookTerrainWorld }) {
|
|
|
2644
2758
|
});
|
|
2645
2759
|
const manifest = cooked?.manifest ?? baked.manifest;
|
|
2646
2760
|
const { splat, files } = baked;
|
|
2647
|
-
const
|
|
2761
|
+
const written2 = /* @__PURE__ */ new Map();
|
|
2648
2762
|
async function write(relative, bytes) {
|
|
2649
2763
|
await mkdir(join(outDir, dirname(relative)), { recursive: true });
|
|
2650
2764
|
await writeFile(join(outDir, relative), bytes);
|
|
2651
|
-
|
|
2765
|
+
written2.set(relative, bytes.length);
|
|
2652
2766
|
}
|
|
2653
2767
|
async function copy(from, to) {
|
|
2654
2768
|
await mkdir(join(outDir, dirname(to)), { recursive: true });
|
|
2655
|
-
await copyFile(
|
|
2656
|
-
|
|
2769
|
+
await copyFile(resolve(assetsDir, from), join(outDir, to));
|
|
2770
|
+
written2.set(to, (await readFile(join(outDir, to))).byteLength);
|
|
2657
2771
|
}
|
|
2658
2772
|
if (!cooked) await write("world.json", `${JSON.stringify(manifest, null, 2)}
|
|
2659
2773
|
`);
|
|
2660
|
-
const round = (value) => Math.round(value * 1e3) / 1e3;
|
|
2661
2774
|
await write(
|
|
2662
2775
|
"water.json",
|
|
2663
2776
|
`${JSON.stringify(
|
|
@@ -2698,6 +2811,7 @@ async function bakeKit(url, { sky, cookTerrainWorld }) {
|
|
|
2698
2811
|
`${JSON.stringify({ ...surface, splat, textures: "textures" }, null, 2)}
|
|
2699
2812
|
`
|
|
2700
2813
|
);
|
|
2814
|
+
await rm(arborScratch, { recursive: true, force: true });
|
|
2701
2815
|
if (cooked) {
|
|
2702
2816
|
console.log(
|
|
2703
2817
|
`${Object.entries(cooked.placementCounts).map(([asset, count]) => `${asset} ${String(count)}`).join(
|
|
@@ -2708,11 +2822,11 @@ async function bakeKit(url, { sky, cookTerrainWorld }) {
|
|
|
2708
2822
|
const perAsset = {};
|
|
2709
2823
|
for (const cell of manifest.cells)
|
|
2710
2824
|
for (const run of cell.runs) perAsset[run.asset] = (perAsset[run.asset] ?? 0) + run.count;
|
|
2711
|
-
const total = [...
|
|
2825
|
+
const total = [...written2.values()].reduce((sum, bytes) => sum + bytes, 0);
|
|
2712
2826
|
console.log(
|
|
2713
|
-
`${Object.entries(perAsset).map(([asset, count]) => `${asset} ${String(count)}`).join(", ")} placements; ${[...
|
|
2827
|
+
`${Object.entries(perAsset).map(([asset, count]) => `${asset} ${String(count)}`).join(", ")} placements; ${[...written2].map(([name, bytes]) => `${name} ${String(bytes)} B`).join(", ")}; ${String(total)} B total; ${Math.round(performance.now() - started)} ms`
|
|
2714
2828
|
);
|
|
2715
2829
|
}
|
|
2716
2830
|
}
|
|
2717
2831
|
|
|
2718
|
-
export { bakeKit, starterAssetsDir };
|
|
2832
|
+
export { DEFAULT_SKY_HDRI, bakeKit, starterAssetsDir };
|