@wildshard/shard-format 0.15.0 → 0.17.0
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/package.json +36 -3
- package/src/format/assetAdmission.ts +5 -2
- package/src/format/baked-terrain.golden.json +2760 -0
- package/src/format/colliders.golden.json +1619 -0
- package/src/format/colliders.ts +568 -0
- package/src/format/contentBounds.ts +6 -0
- package/src/format/creature-perch-admission.ts +86 -0
- package/src/format/creature-perch.ts +269 -0
- package/src/format/creatures.ts +11 -8
- package/src/format/forest-placement.golden.json +4588 -0
- package/src/format/forest-placement.ts +385 -0
- package/src/format/geometry-layout.golden.json +180 -0
- package/src/format/grass-field.ts +287 -0
- package/src/format/headless-sections.ts +49 -0
- package/src/format/interact-table.golden.json +340 -0
- package/src/format/interact-table.ts +541 -0
- package/src/format/item-catalogue.golden.json +2310 -0
- package/src/format/item-catalogue.ts +148 -0
- package/src/format/mover-pose.ts +46 -0
- package/src/format/native-geometry-channels.ts +64 -0
- package/src/format/noise.ts +106 -0
- package/src/format/road-light.ts +47 -0
- package/src/format/seeded-rng.golden.json +857 -0
- package/src/format/seeded-rng.ts +111 -0
- package/src/format/shardSchema.ts +3 -0
- package/src/format/small-laws.golden.json +57 -0
- package/src/format/terrain-field.golden.json +3998 -0
- package/src/format/terrain-field.ts +405 -0
- package/src/format/whole-partition.ts +170 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
/* oxlint-disable max-lines -- verbatim port of the engine law (SDK-SPLIT SP4b), one file as in the engine */
|
|
2
|
+
import { CHUNK_HALF, ROAD_WIDTH, ROAD_LENGTH } from './contentBounds.ts'
|
|
3
|
+
/* oxlint-disable capitalized-comments, complexity, curly, id-length, import/exports-last, import/group-exports, max-lines-per-function, max-params, max-statements, no-bitwise, no-continue, no-inline-comments, no-magic-numbers, no-plusplus, no-underscore-dangle, prefer-destructuring, sort-vars, typescript/explicit-function-return-type, typescript/prefer-readonly-parameter-types, unicorn/no-null, unicorn/no-zero-fractions, unicorn/number-literal-case, unicorn/numeric-separators-style, unicorn/prefer-at, unicorn/prefer-math-trunc -- verbatim port of the engine's law (SDK-SPLIT SP4); bit-exact output is pinned by golden vectors, so it keeps the engine's form */
|
|
4
|
+
/** SDK-SPLIT SP4b (Q35): moved from engine `shared/engine/world/terrainField.ts`; the bake and the engine build one field. */
|
|
5
|
+
/**
|
|
6
|
+
* buildTerrain(seed, spec) — compiles a `TerrainSpec` into the pure `TerrainField` functions.
|
|
7
|
+
*
|
|
8
|
+
* The def supplies the raw landscape, the trails, cabin sites, an optional pond and the ground
|
|
9
|
+
* splat rule; this adds what every Wildshard shard must have on top of that:
|
|
10
|
+
* · the four entry roads at the edge midpoints, levelled to y = 0 at the boundary and ramping
|
|
11
|
+
* up into the chunk over ROAD_LENGTH (a fundamentals requirement, not tuneable per chunk)
|
|
12
|
+
* · flat cabin pads at the landscape height of each site
|
|
13
|
+
* · a shallow bed carved along every trail
|
|
14
|
+
* · the pond basin dished below its water line
|
|
15
|
+
* · an open-water shard's sea level (`oceanLevel`) as `waterLevel()`, no pond dish
|
|
16
|
+
* All of it stays closure-based (no `this`), so the functions can be re-exported and called
|
|
17
|
+
* millions of times from the terrain mesh / placement loops without indirection.
|
|
18
|
+
*/
|
|
19
|
+
import { Noise2D, smoothstep, clamp, lerp } from './noise.ts'
|
|
20
|
+
// Engine-owned level data; structurally compatible with the transitional manifests.
|
|
21
|
+
export type Vec2 = [number, number]
|
|
22
|
+
export interface CabinSite {
|
|
23
|
+
x: number
|
|
24
|
+
z: number
|
|
25
|
+
rot: number
|
|
26
|
+
}
|
|
27
|
+
export interface PondDef {
|
|
28
|
+
x: number
|
|
29
|
+
z: number
|
|
30
|
+
r: number
|
|
31
|
+
}
|
|
32
|
+
export interface TerrainNoise {
|
|
33
|
+
n: Noise2D
|
|
34
|
+
n2: Noise2D
|
|
35
|
+
}
|
|
36
|
+
/** a level's terrain as functions: height, normal, ground-layer weights, trails, pads, pond and water */
|
|
37
|
+
export interface TerrainField {
|
|
38
|
+
heightAt: (x: number, z: number) => number
|
|
39
|
+
normalAt: (x: number, z: number, eps?: number) => [number, number, number]
|
|
40
|
+
splatAt: (x: number, z: number) => [number, number, number, number]
|
|
41
|
+
trailDistance: (x: number, z: number) => number
|
|
42
|
+
cabinMask: (x: number, z: number) => number
|
|
43
|
+
pondMask: (x: number, z: number) => number
|
|
44
|
+
waterLevel: () => number
|
|
45
|
+
streamAt?: (x: number, z: number) => number | null
|
|
46
|
+
trails: Vec2[][]
|
|
47
|
+
cabinSites: CabinSite[]
|
|
48
|
+
pond: PondDef | null
|
|
49
|
+
/**
|
|
50
|
+
* Metres this field's `heightAt` (and `waterLevel`) sit from the heights its offline bake was made from: a level that
|
|
51
|
+
* shifts its whole world vertically at runtime (a Debug variant) applies the shift in its own functions, and the baked
|
|
52
|
+
* grid (public/assets/baked/<slug>/terrain.bin) is installed shifted by the same amount. Read once, when the bake is
|
|
53
|
+
* installed; absent or 0, the bake installs exactly as baked.
|
|
54
|
+
*/
|
|
55
|
+
readonly datum?: number
|
|
56
|
+
}
|
|
57
|
+
export interface TerrainSpec {
|
|
58
|
+
landscape: (x: number, z: number, noise: TerrainNoise) => number
|
|
59
|
+
trails: Vec2[][]
|
|
60
|
+
cabinSites: CabinSite[]
|
|
61
|
+
pond?: PondDef
|
|
62
|
+
pondFill?: number
|
|
63
|
+
oceanLevel?: number
|
|
64
|
+
graded?: { paths: Vec2[][]; maxGrade: number; bench?: boolean }
|
|
65
|
+
finish?: (x: number, z: number, h: number) => number
|
|
66
|
+
streamAt?: (x: number, z: number) => number | null
|
|
67
|
+
splat?: (
|
|
68
|
+
x: number,
|
|
69
|
+
z: number,
|
|
70
|
+
t: TerrainField,
|
|
71
|
+
noise: TerrainNoise,
|
|
72
|
+
) => [number, number, number, number]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function distToSegmentSquared(
|
|
76
|
+
px: number,
|
|
77
|
+
pz: number,
|
|
78
|
+
ax: number,
|
|
79
|
+
az: number,
|
|
80
|
+
bx: number,
|
|
81
|
+
bz: number,
|
|
82
|
+
) {
|
|
83
|
+
const vx = bx - ax,
|
|
84
|
+
vz = bz - az
|
|
85
|
+
const wx = px - ax,
|
|
86
|
+
wz = pz - az
|
|
87
|
+
const l2 = vx * vx + vz * vz
|
|
88
|
+
const t = l2 > 0 ? clamp((wx * vx + wz * vz) / l2, 0, 1) : 0
|
|
89
|
+
const dx = px - (ax + vx * t),
|
|
90
|
+
dz = pz - (az + vz * t)
|
|
91
|
+
return dx * dx + dz * dz
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** a graded path's cut / fill: full within GRADE_IN metres of its centreline, faded out by GRADE_OUT; sampled every GRADE_STEP */
|
|
95
|
+
const GRADE_IN = 3,
|
|
96
|
+
GRADE_OUT = 7,
|
|
97
|
+
GRADE_STEP = 0.5
|
|
98
|
+
/** the shelf starts this far (m, along the path) before the first spot the grading moves, and runs on as far past the last */
|
|
99
|
+
const GRADE_LEAD = 5
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A graded path (TerrainSpec.graded): the ground along the polyline every GRADE_STEP, and the shelf that climbs no
|
|
103
|
+
* steeper than `maxGrade` — the mean of the profile's upper and lower `maxGrade` envelopes (the highest / lowest
|
|
104
|
+
* profiles that slope that gently and never cross the ground). Both equal the ground wherever it is already that
|
|
105
|
+
* gentle; at a crag step the shelf is half cut into the top, half filled below. `need` says where the shelf replaces
|
|
106
|
+
* the ground (levelled across, so the crag's roughness beside the line doesn't come back between grid vertices): 1
|
|
107
|
+
* within GRADE_LEAD of any sample the grading moved by 0.4 m or more, 0 where nothing within it moved 5 cm.
|
|
108
|
+
*/
|
|
109
|
+
function gradeProfile(
|
|
110
|
+
poly: Vec2[],
|
|
111
|
+
maxGrade: number,
|
|
112
|
+
ground: (x: number, z: number) => number,
|
|
113
|
+
bench = false,
|
|
114
|
+
) {
|
|
115
|
+
const cum: number[] = [0]
|
|
116
|
+
for (let k = 1; k < poly.length; k++) {
|
|
117
|
+
const a = poly[k - 1],
|
|
118
|
+
b = poly[k]
|
|
119
|
+
cum.push(
|
|
120
|
+
(cum[k - 1] ?? 0) + (a && b ? Math.hypot(b[0] - a[0], b[1] - a[1]) : 0),
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
const total = cum[cum.length - 1] ?? 0,
|
|
124
|
+
n = Math.max(2, Math.ceil(total / GRADE_STEP) + 1)
|
|
125
|
+
const g: number[] = []
|
|
126
|
+
let k = 0
|
|
127
|
+
for (let i = 0; i < n; i++) {
|
|
128
|
+
const s = Math.min(total, i * GRADE_STEP)
|
|
129
|
+
while (k < poly.length - 2 && s > (cum[k + 1] ?? 0)) k++
|
|
130
|
+
const a = poly[k],
|
|
131
|
+
b = poly[k + 1],
|
|
132
|
+
l = (cum[k + 1] ?? 0) - (cum[k] ?? 0),
|
|
133
|
+
t = l > 0 ? (s - (cum[k] ?? 0)) / l : 0
|
|
134
|
+
g.push(
|
|
135
|
+
a && b ? ground(a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t) : 0,
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
const up = [...g],
|
|
139
|
+
dn = [...g],
|
|
140
|
+
rise = maxGrade * GRADE_STEP
|
|
141
|
+
for (let i = 1; i < n; i++) {
|
|
142
|
+
up[i] = Math.max(up[i] ?? 0, (up[i - 1] ?? 0) - rise)
|
|
143
|
+
dn[i] = Math.min(dn[i] ?? 0, (dn[i - 1] ?? 0) + rise)
|
|
144
|
+
}
|
|
145
|
+
for (let i = n - 2; i >= 0; i--) {
|
|
146
|
+
up[i] = Math.max(up[i] ?? 0, (up[i + 1] ?? 0) - rise)
|
|
147
|
+
dn[i] = Math.min(dn[i] ?? 0, (dn[i + 1] ?? 0) + rise)
|
|
148
|
+
}
|
|
149
|
+
const xs = poly.map(p => p[0]),
|
|
150
|
+
zs = poly.map(p => p[1])
|
|
151
|
+
const shelf = g.map((_, i) => ((up[i] ?? 0) + (dn[i] ?? 0)) / 2),
|
|
152
|
+
moved = shelf.map((v, i) => Math.abs(v - (g[i] ?? 0)))
|
|
153
|
+
const lead = Math.round(GRADE_LEAD / GRADE_STEP)
|
|
154
|
+
const need = moved.map((_, i) => {
|
|
155
|
+
if (bench) return 1 // a bench the whole way (TerrainSpec.graded.bench)
|
|
156
|
+
let m = 0
|
|
157
|
+
for (let j = Math.max(0, i - lead); j <= Math.min(n - 1, i + lead); j++)
|
|
158
|
+
m = Math.max(m, moved[j] ?? 0)
|
|
159
|
+
return smoothstep(0.05, 0.4, m)
|
|
160
|
+
})
|
|
161
|
+
return {
|
|
162
|
+
poly,
|
|
163
|
+
cum,
|
|
164
|
+
shelf,
|
|
165
|
+
need,
|
|
166
|
+
x0: Math.min(...xs) - GRADE_OUT,
|
|
167
|
+
x1: Math.max(...xs) + GRADE_OUT,
|
|
168
|
+
z0: Math.min(...zs) - GRADE_OUT,
|
|
169
|
+
z1: Math.max(...zs) + GRADE_OUT,
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 0..1: how much this point is inside the mandated flat entry road at an edge midpoint */
|
|
174
|
+
function entryRoadMask(x: number, z: number): number {
|
|
175
|
+
const half = ROAD_WIDTH / 2 + 3
|
|
176
|
+
const along = (a: number, b: number) =>
|
|
177
|
+
smoothstep(half + 22, half, Math.abs(a)) *
|
|
178
|
+
smoothstep(
|
|
179
|
+
CHUNK_HALF - ROAD_LENGTH - 25,
|
|
180
|
+
CHUNK_HALF - ROAD_LENGTH + 5,
|
|
181
|
+
Math.abs(b),
|
|
182
|
+
)
|
|
183
|
+
return Math.max(along(x, z), along(z, x))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* A 32-bit fingerprint of a height field: FNV-1a over `heightAt` sampled on a 16 × 16 grid across the
|
|
188
|
+
* chunk, quantised to millimetres. scripts/bake-chunk.mjs stores it in the bake's header and
|
|
189
|
+
* src/shared/engine/world/BakedTerrain.ts refuses a bake whose fingerprint differs from the live def's functions —
|
|
190
|
+
* so a stale terrain.bin (an old build in the service worker, a def edited since the last vite start)
|
|
191
|
+
* can never be installed over the analytic field.
|
|
192
|
+
*/
|
|
193
|
+
export function landscapeHash(
|
|
194
|
+
t: Pick<TerrainField, 'heightAt'>,
|
|
195
|
+
size: number = CHUNK_HALF * 2,
|
|
196
|
+
): number {
|
|
197
|
+
let h = 0x811c9dc5
|
|
198
|
+
const n = 16,
|
|
199
|
+
d = size / (n - 1)
|
|
200
|
+
for (let iz = 0; iz < n; iz++)
|
|
201
|
+
for (let ix = 0; ix < n; ix++) {
|
|
202
|
+
const v =
|
|
203
|
+
Math.round(t.heightAt(-size / 2 + ix * d, -size / 2 + iz * d) * 1000) |
|
|
204
|
+
0
|
|
205
|
+
for (let b = 0; b < 4; b++) {
|
|
206
|
+
h ^= (v >>> (b * 8)) & 0xff
|
|
207
|
+
h = Math.imul(h, 0x01000193) >>> 0
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return h >>> 0
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Terrain metadata over authored trails, pads and pond; height/splat sampling stays with the admitted lattice. */
|
|
214
|
+
export function buildTerrainMetadata(
|
|
215
|
+
seed: number,
|
|
216
|
+
spec: Pick<TerrainSpec, 'trails' | 'cabinSites' | 'pond' | 'streamAt'>,
|
|
217
|
+
waterLevel: TerrainField['waterLevel'],
|
|
218
|
+
n: Noise2D = new Noise2D(seed),
|
|
219
|
+
): Omit<TerrainField, 'heightAt' | 'normalAt' | 'splatAt'> {
|
|
220
|
+
const trails = spec.trails,
|
|
221
|
+
cabinSites = spec.cabinSites,
|
|
222
|
+
pond = spec.pond ?? null
|
|
223
|
+
function trailDistance(x: number, z: number): number {
|
|
224
|
+
let d = Infinity
|
|
225
|
+
for (const poly of trails)
|
|
226
|
+
for (let i = 0; i < poly.length - 1; i++) {
|
|
227
|
+
const a = poly[i],
|
|
228
|
+
b = poly[i + 1]
|
|
229
|
+
if (a && b)
|
|
230
|
+
d = Math.min(d, distToSegmentSquared(x, z, a[0], a[1], b[0], b[1]))
|
|
231
|
+
}
|
|
232
|
+
// sqrt is monotone: keep the same winning squared value and the exact final root, once per field query.
|
|
233
|
+
return Math.sqrt(d)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function cabinMask(x: number, z: number): number {
|
|
237
|
+
let m = 0
|
|
238
|
+
for (const c of cabinSites) {
|
|
239
|
+
const d = Math.hypot(x - c.x, z - c.z)
|
|
240
|
+
m = Math.max(m, smoothstep(22, 11, d))
|
|
241
|
+
}
|
|
242
|
+
return m
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const pondMask = pond
|
|
246
|
+
? (x: number, z: number): number => {
|
|
247
|
+
const d =
|
|
248
|
+
Math.hypot(x - pond.x, z - pond.z) + n.get(x * 0.05, z * 0.05) * 3
|
|
249
|
+
return smoothstep(pond.r + 10, pond.r * 0.3, d)
|
|
250
|
+
}
|
|
251
|
+
: () => 0
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
trailDistance,
|
|
255
|
+
cabinMask,
|
|
256
|
+
pondMask,
|
|
257
|
+
waterLevel,
|
|
258
|
+
...(spec.streamAt ? { streamAt: spec.streamAt } : {}),
|
|
259
|
+
trails,
|
|
260
|
+
cabinSites,
|
|
261
|
+
pond,
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function buildTerrain(seed: number, spec: TerrainSpec): TerrainField {
|
|
266
|
+
const noise: TerrainNoise = {
|
|
267
|
+
n: new Noise2D(seed),
|
|
268
|
+
n2: new Noise2D(seed + 7),
|
|
269
|
+
}
|
|
270
|
+
const { n } = noise
|
|
271
|
+
const trails: Vec2[][] = spec.trails
|
|
272
|
+
const cabinSites: CabinSite[] = spec.cabinSites
|
|
273
|
+
const pond = spec.pond ?? null
|
|
274
|
+
const pondFill = spec.pondFill ?? 1.0
|
|
275
|
+
const oceanLevel = spec.oceanLevel ?? null
|
|
276
|
+
const landscape = (x: number, z: number) => spec.landscape(x, z, noise)
|
|
277
|
+
const finish = spec.finish ?? null
|
|
278
|
+
const graded = (spec.graded?.paths ?? []).map(poly =>
|
|
279
|
+
gradeProfile(
|
|
280
|
+
poly,
|
|
281
|
+
spec.graded?.maxGrade ?? 1,
|
|
282
|
+
landscape,
|
|
283
|
+
spec.graded?.bench === true,
|
|
284
|
+
),
|
|
285
|
+
)
|
|
286
|
+
/** a graded path's pull on (x, z): the shelf height there, and how far toward it (1 within GRADE_IN of a stretch
|
|
287
|
+
* that needed grading, 0 past GRADE_OUT or where the ground was gentle anyway) */
|
|
288
|
+
const _grade = { y: 0, w: 0 }
|
|
289
|
+
function gradeAt(x: number, z: number): { y: number; w: number } {
|
|
290
|
+
let best = GRADE_OUT,
|
|
291
|
+
y = 0,
|
|
292
|
+
need = 0
|
|
293
|
+
for (const g of graded) {
|
|
294
|
+
if (x < g.x0 || x > g.x1 || z < g.z0 || z > g.z1) continue
|
|
295
|
+
for (let k = 0; k + 1 < g.poly.length; k++) {
|
|
296
|
+
const a = g.poly[k],
|
|
297
|
+
b = g.poly[k + 1]
|
|
298
|
+
if (!a || !b) continue
|
|
299
|
+
const vx = b[0] - a[0],
|
|
300
|
+
vz = b[1] - a[1],
|
|
301
|
+
l2 = vx * vx + vz * vz
|
|
302
|
+
const raw = l2 > 0 ? ((x - a[0]) * vx + (z - a[1]) * vz) / l2 : 0,
|
|
303
|
+
t = clamp(raw, 0, 1)
|
|
304
|
+
// past the path's first or last point the shelf stops (square ends): the bridge the path meets there keeps
|
|
305
|
+
// its gully
|
|
306
|
+
const past =
|
|
307
|
+
(k === 0 && raw < 0) || (k === g.poly.length - 2 && raw > 1)
|
|
308
|
+
const d = past
|
|
309
|
+
? GRADE_OUT
|
|
310
|
+
: Math.hypot(x - a[0] - vx * t, z - a[1] - vz * t)
|
|
311
|
+
if (d >= best) continue
|
|
312
|
+
best = d
|
|
313
|
+
const f = ((g.cum[k] ?? 0) + t * Math.sqrt(l2)) / GRADE_STEP,
|
|
314
|
+
i = Math.min(g.shelf.length - 2, Math.floor(f)),
|
|
315
|
+
u = f - i
|
|
316
|
+
y = (g.shelf[i] ?? 0) * (1 - u) + (g.shelf[i + 1] ?? 0) * u
|
|
317
|
+
need = (g.need[i] ?? 0) * (1 - u) + (g.need[i + 1] ?? 0) * u
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
_grade.y = y
|
|
321
|
+
_grade.w = need * smoothstep(GRADE_OUT, GRADE_IN, best)
|
|
322
|
+
return _grade
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
let _waterLevel: number | null = null
|
|
326
|
+
function waterLevel(): number {
|
|
327
|
+
_waterLevel ??=
|
|
328
|
+
oceanLevel ?? (pond ? landscape(pond.x, pond.z) + pondFill : -1e4) // ocean, or the pond fills the natural basin below its rim
|
|
329
|
+
return _waterLevel
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const { trailDistance, cabinMask, pondMask } = buildTerrainMetadata(
|
|
333
|
+
seed,
|
|
334
|
+
spec,
|
|
335
|
+
waterLevel,
|
|
336
|
+
n,
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
function heightAt(x: number, z: number): number {
|
|
340
|
+
let h = landscape(x, z)
|
|
341
|
+
if (graded.length > 0) {
|
|
342
|
+
const g = gradeAt(x, z)
|
|
343
|
+
if (g.w > 0) h = lerp(h, g.y, g.w)
|
|
344
|
+
}
|
|
345
|
+
// pond basin: dish down to ~3 m below the water line
|
|
346
|
+
const pm = pondMask(x, z)
|
|
347
|
+
if (pm > 0)
|
|
348
|
+
h = lerp(
|
|
349
|
+
h,
|
|
350
|
+
Math.min(h, waterLevel() - 1.6 - pm * 1.6),
|
|
351
|
+
smoothstep(0.0, 0.5, pm),
|
|
352
|
+
)
|
|
353
|
+
// trails: flatten a little and carve a shallow bed
|
|
354
|
+
const td = trailDistance(x, z)
|
|
355
|
+
const trailW = smoothstep(9, 2.5, td)
|
|
356
|
+
h -= trailW * 0.35
|
|
357
|
+
// cabin pads: flatten to the pad's height
|
|
358
|
+
for (const c of cabinSites) {
|
|
359
|
+
const d = Math.hypot(x - c.x, z - c.z)
|
|
360
|
+
const m = smoothstep(20, 9, d)
|
|
361
|
+
if (m > 0) h = lerp(h, landscape(c.x, c.z), m)
|
|
362
|
+
}
|
|
363
|
+
if (finish) h = finish(x, z, h)
|
|
364
|
+
// entry roads: must be level with no-man's land (y = 0) at the chunk boundary
|
|
365
|
+
const rm = entryRoadMask(x, z)
|
|
366
|
+
if (rm > 0) {
|
|
367
|
+
const inward = CHUNK_HALF - Math.max(Math.abs(x), Math.abs(z))
|
|
368
|
+
const ramp = smoothstep(ROAD_LENGTH - 15, ROAD_LENGTH + 30, inward) // 0 at edge → 1 deep inside
|
|
369
|
+
h = lerp(lerp(0, h, ramp), h, 1 - rm)
|
|
370
|
+
}
|
|
371
|
+
return h
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function normalAt(x: number, z: number, eps = 0.6): [number, number, number] {
|
|
375
|
+
const hl = heightAt(x - eps, z),
|
|
376
|
+
hr = heightAt(x + eps, z)
|
|
377
|
+
const hd = heightAt(x, z - eps),
|
|
378
|
+
hu = heightAt(x, z + eps)
|
|
379
|
+
const nx = hl - hr,
|
|
380
|
+
nz = hd - hu,
|
|
381
|
+
ny = 2 * eps
|
|
382
|
+
const l = Math.hypot(nx, ny, nz)
|
|
383
|
+
return [nx / l, ny / l, nz / l]
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const terrain: TerrainField = {
|
|
387
|
+
heightAt,
|
|
388
|
+
normalAt,
|
|
389
|
+
trailDistance,
|
|
390
|
+
cabinMask,
|
|
391
|
+
pondMask,
|
|
392
|
+
waterLevel,
|
|
393
|
+
...(spec.streamAt ? { streamAt: spec.streamAt } : {}),
|
|
394
|
+
splatAt: (x, z) => {
|
|
395
|
+
if (!spec.splat) return [1, 0, 0, 0] // no splat ground (TerrainSpec.splat omitted)
|
|
396
|
+
const [a, b, c, d] = spec.splat(x, z, terrain, noise)
|
|
397
|
+
const s = a + b + c + d
|
|
398
|
+
return [a / s, b / s, c / s, d / s]
|
|
399
|
+
},
|
|
400
|
+
trails,
|
|
401
|
+
cabinSites,
|
|
402
|
+
pond,
|
|
403
|
+
}
|
|
404
|
+
return terrain
|
|
405
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/* oxlint-disable curly, id-length, import/group-exports, import/no-namespace, init-declarations, no-bitwise, no-magic-numbers, no-plusplus, sort-vars, typescript/explicit-function-return-type, typescript/prefer-readonly-parameter-types, unicorn/max-nested-calls -- verbatim port of the engine's law (SDK-SPLIT SP4); the run-list grammar is pinned by golden vectors, so it keeps the engine's form */
|
|
2
|
+
/** SDK-SPLIT SP4e (Q35): a whole piece's partition row and its run-list form moved from engine
|
|
3
|
+
* `client/game/shardfile/wholeGeometryPieces.ts`, and the `WholeGeometryPiece` shape from `client/game/plugin/geometry.ts`
|
|
4
|
+
* (generic over the host's geometry and matrix); decoding a GLB node into pieces stays in the engine. */
|
|
5
|
+
import * as wholeV from 'valibot'
|
|
6
|
+
|
|
7
|
+
/** A run list expands to at most this many ordinals (a 2 MB GLB JSON chunk cannot otherwise bound it). */
|
|
8
|
+
const WHOLE_ORDINALS_MAX = 1 << 24
|
|
9
|
+
const WholeOrdinal = wholeV.pipe(
|
|
10
|
+
wholeV.number(),
|
|
11
|
+
wholeV.integer(),
|
|
12
|
+
wholeV.minValue(0),
|
|
13
|
+
)
|
|
14
|
+
/** Ordinals as a plain list, or the baker's run-length form `{ runs: [start, length, …] }` (sharddev/bake/props `wholePartitionRuns`). */
|
|
15
|
+
/** A plain ordinal list, checked in one loop with WholeOrdinal's rules (valibot's per-element pipe over whole tiles' lists
|
|
16
|
+
* was ~150 ms of a phone-tier home region) */
|
|
17
|
+
const isOrdinals = (list: unknown): list is number[] =>
|
|
18
|
+
Array.isArray(list) &&
|
|
19
|
+
list.every(
|
|
20
|
+
(ordinal: unknown) =>
|
|
21
|
+
typeof ordinal === 'number' && Number.isInteger(ordinal) && ordinal >= 0,
|
|
22
|
+
)
|
|
23
|
+
interface WholeOrdinalValues {
|
|
24
|
+
readonly values: number[]
|
|
25
|
+
readonly length: number
|
|
26
|
+
readonly dense: boolean
|
|
27
|
+
}
|
|
28
|
+
function explicitWholeOrdinals(values: number[]): WholeOrdinalValues {
|
|
29
|
+
// Explicit caller arrays retain their original reference and live length.
|
|
30
|
+
return {
|
|
31
|
+
values,
|
|
32
|
+
get length(): number {
|
|
33
|
+
return values.length
|
|
34
|
+
},
|
|
35
|
+
dense: false,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function runWholeOrdinals(runs: number[]): WholeOrdinalValues {
|
|
39
|
+
let values: number[] | undefined
|
|
40
|
+
const length = runs.reduce(
|
|
41
|
+
(sum, ordinal, i) => sum + (i % 2 === 1 ? ordinal : 0),
|
|
42
|
+
0,
|
|
43
|
+
)
|
|
44
|
+
return {
|
|
45
|
+
length,
|
|
46
|
+
dense: runs.length === 2 && runs[0] === 0,
|
|
47
|
+
get values(): number[] {
|
|
48
|
+
if (values === undefined) {
|
|
49
|
+
values = []
|
|
50
|
+
for (let i = 0; i < runs.length; i += 2) {
|
|
51
|
+
const start = runs[i] ?? 0,
|
|
52
|
+
count = runs[i + 1] ?? 0
|
|
53
|
+
for (let k = 0; k < count; k++) values.push(start + k)
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return values
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const WholeOrdinals = wholeV.union([
|
|
61
|
+
wholeV.pipe(
|
|
62
|
+
wholeV.custom<number[]>(
|
|
63
|
+
isOrdinals,
|
|
64
|
+
'whole ordinals are non-negative integers',
|
|
65
|
+
),
|
|
66
|
+
wholeV.transform(explicitWholeOrdinals),
|
|
67
|
+
),
|
|
68
|
+
wholeV.pipe(
|
|
69
|
+
wholeV.strictObject({ runs: wholeV.array(WholeOrdinal) }),
|
|
70
|
+
wholeV.check(
|
|
71
|
+
row =>
|
|
72
|
+
row.runs.length % 2 === 0 &&
|
|
73
|
+
row.runs.every((ordinal, i) => i % 2 === 0 || ordinal > 0) &&
|
|
74
|
+
row.runs.reduce(
|
|
75
|
+
(sum, ordinal, i) => sum + (i % 2 === 1 ? ordinal : 0),
|
|
76
|
+
0,
|
|
77
|
+
) <= WHOLE_ORDINALS_MAX,
|
|
78
|
+
'whole ordinal runs are bounded start/length pairs',
|
|
79
|
+
),
|
|
80
|
+
wholeV.transform(row => runWholeOrdinals(row.runs)),
|
|
81
|
+
),
|
|
82
|
+
])
|
|
83
|
+
const WholePartition = wholeV.pipe(
|
|
84
|
+
wholeV.strictObject({
|
|
85
|
+
id: wholeV.string(),
|
|
86
|
+
vertices: WholeOrdinals,
|
|
87
|
+
triangles: WholeOrdinals,
|
|
88
|
+
vertexCount: wholeV.pipe(
|
|
89
|
+
wholeV.number(),
|
|
90
|
+
wholeV.integer(),
|
|
91
|
+
wholeV.minValue(1),
|
|
92
|
+
),
|
|
93
|
+
indexCount: wholeV.pipe(
|
|
94
|
+
wholeV.number(),
|
|
95
|
+
wholeV.integer(),
|
|
96
|
+
wholeV.minValue(0),
|
|
97
|
+
),
|
|
98
|
+
index: wholeV.picklist(['none', 'u16', 'u32']),
|
|
99
|
+
// I3: the partition's positions are unnormalized Int16 on this grid of its whole shape (sharddev/bake/props `wholeGrid`)
|
|
100
|
+
grid: wholeV.exactOptional(
|
|
101
|
+
wholeV.strictObject({
|
|
102
|
+
origin: wholeV.tuple([
|
|
103
|
+
wholeV.number(),
|
|
104
|
+
wholeV.number(),
|
|
105
|
+
wholeV.number(),
|
|
106
|
+
]),
|
|
107
|
+
step: wholeV.pipe(
|
|
108
|
+
wholeV.number(),
|
|
109
|
+
wholeV.minValue(1 / 4096),
|
|
110
|
+
wholeV.maxValue(1),
|
|
111
|
+
),
|
|
112
|
+
}),
|
|
113
|
+
),
|
|
114
|
+
}),
|
|
115
|
+
// The proof lives only in this parsed row: caller-owned explicit arrays
|
|
116
|
+
// never qualify, even when they currently look like a complete run.
|
|
117
|
+
wholeV.transform(row => ({
|
|
118
|
+
...row,
|
|
119
|
+
get vertices(): number[] {
|
|
120
|
+
return row.vertices.values
|
|
121
|
+
},
|
|
122
|
+
get vertexLength(): number {
|
|
123
|
+
return row.vertices.length
|
|
124
|
+
},
|
|
125
|
+
get triangles(): number[] {
|
|
126
|
+
return row.triangles.values
|
|
127
|
+
},
|
|
128
|
+
get triangleLength(): number {
|
|
129
|
+
return row.triangles.length
|
|
130
|
+
},
|
|
131
|
+
denseVertices: row.vertices.dense,
|
|
132
|
+
denseTriangles: row.triangles.dense,
|
|
133
|
+
})),
|
|
134
|
+
)
|
|
135
|
+
/** Admitted partition lanes retain their existing lazy ordinal expansion. */
|
|
136
|
+
export interface WholePartitionRow {
|
|
137
|
+
readonly id: string
|
|
138
|
+
readonly vertices: number[]
|
|
139
|
+
readonly triangles: number[]
|
|
140
|
+
readonly vertexCount: number
|
|
141
|
+
readonly indexCount: number
|
|
142
|
+
readonly index: 'none' | 'u16' | 'u32'
|
|
143
|
+
readonly grid?: {
|
|
144
|
+
readonly origin: [number, number, number]
|
|
145
|
+
readonly step: number
|
|
146
|
+
}
|
|
147
|
+
readonly vertexLength: number
|
|
148
|
+
readonly triangleLength: number
|
|
149
|
+
readonly denseVertices: boolean
|
|
150
|
+
readonly denseTriangles: boolean
|
|
151
|
+
}
|
|
152
|
+
/** The same bounded ordinal grammar for partial and complete whole intake. */
|
|
153
|
+
export function readWholePartition(input: unknown): WholePartitionRow {
|
|
154
|
+
return wholeV.parse(WholePartition, input)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** A verified subset of one original draw. Ordinals identify its exact source vertices and triangles; completeness belongs
|
|
158
|
+
* to the full-model reader. `Geometry` and `Matrix` are the host's (three's `BufferGeometry` and `Matrix4` in the engine). */
|
|
159
|
+
export interface WholeGeometryPiece<Geometry = unknown, Matrix = unknown> {
|
|
160
|
+
readonly geometry: Geometry
|
|
161
|
+
readonly matrix: Matrix
|
|
162
|
+
/** Forget decoded-source ownership after adopting or copying it into a scope-owned render object. */
|
|
163
|
+
readonly release?: () => void
|
|
164
|
+
readonly part: string
|
|
165
|
+
readonly vertices: readonly number[]
|
|
166
|
+
readonly triangles: readonly number[]
|
|
167
|
+
readonly vertexCount: number
|
|
168
|
+
readonly indexCount: number
|
|
169
|
+
readonly layout: string
|
|
170
|
+
}
|