@threenative/core 0.3.13 → 0.3.16
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/capabilities.json +1008 -83
- package/dist/{chunk-4SRAQFTU.js → chunk-2GPHICV6.js} +1 -1
- package/dist/{chunk-EEABDLUC.js → chunk-5C4TCWI3.js} +2 -2
- package/dist/{chunk-DIPIJICH.js → chunk-FQURMC2F.js} +122 -18
- package/dist/{chunk-EFWZWLPY.js → chunk-OMGIYWW7.js} +400 -13
- package/dist/{chunk-SV4QVUTQ.js → chunk-QAQHYOWY.js} +1 -1
- package/dist/{chunk-GYUZJQ7U.js → chunk-VQRPEOKY.js} +4 -1
- package/dist/hot.js +10 -2
- package/dist/index.d.ts +608 -16
- package/dist/index.js +2037 -633
- package/dist/{model-lod-DPTzeCcD.d.ts → model-lod-B921F6kQ.d.ts} +0 -10
- package/dist/playtest.js +3 -3
- package/dist/{quality-BYqKGAwo.d.ts → quality-BADStsTn.d.ts} +2 -2
- package/dist/render-environment.d.ts +1 -1
- package/dist/ui-layer.js +2 -2
- package/dist/worker.js +2 -2
- package/dist/world.d.ts +14 -4
- package/dist/world.js +179 -36
- package/mcp/animation-server.mjs +1 -1
- package/mcp/engine-server.mjs +44 -17
- package/mcp/servers.mjs +4 -4
- package/package.json +6 -6
- package/patches/three@0.187.0-prd269-upgrade.patch +126 -2
- package/patches/three@0.187.0.patch +108 -21
- package/scripts/ensure-mcp.mjs +6 -1
- package/src/clustered-batch.ts +146 -3
- package/src/clustered-mesh.ts +548 -20
- package/src/debug.ts +16 -0
- package/src/displaced-surface.ts +428 -0
- package/src/engine-hooks.ts +29 -0
- package/src/frame-budget.ts +16 -0
- package/src/generated/console-markers.ts +1 -0
- package/src/grass-field.ts +628 -0
- package/src/grounding.ts +19 -10
- package/src/hot.ts +18 -2
- package/src/index.ts +112 -0
- package/src/instanced-batch-lod.ts +54 -0
- package/src/model-lod.ts +77 -1
- package/src/pose-measure.ts +27 -2
- package/src/render/material-key.ts +21 -1
- package/src/render/screen-probe-gather.ts +303 -0
- package/src/render/short-range-ao.ts +274 -0
- package/src/render/world-impostor-surface.ts +98 -3
- package/src/render-camera-cull.ts +9 -3
- package/src/renderer.ts +26 -4
- package/src/scatter-field.ts +497 -0
- package/src/startup-offscreen-bytes.ts +11 -5
- package/src/state.ts +16 -3
- package/src/static-transform.ts +109 -6
- package/src/tile-ring.ts +294 -0
- package/src/version.ts +1 -1
- package/src/warmup.ts +15 -2
- package/src/world-cell-batches.ts +4 -0
- package/src/world-cell-data.ts +7 -0
- package/src/world-cell-models.ts +36 -8
- package/src/world-cell-render-gpu-dress.ts +4 -2
- package/src/world-cell-render-util.ts +2 -2
- package/src/world-cells.ts +1 -1
- package/src/world-river-ribbon.ts +13 -22
- package/src/world-water-bodies.ts +113 -3
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type BufferGeometry,
|
|
3
|
+
Group,
|
|
4
|
+
InstancedMesh,
|
|
5
|
+
type Material,
|
|
6
|
+
Matrix4,
|
|
7
|
+
Quaternion,
|
|
8
|
+
Vector3,
|
|
9
|
+
} from "three";
|
|
10
|
+
import {
|
|
11
|
+
type ITileBounds,
|
|
12
|
+
type ITileRingField,
|
|
13
|
+
TileRing,
|
|
14
|
+
clamp01,
|
|
15
|
+
groundGrid,
|
|
16
|
+
hashInt,
|
|
17
|
+
mulberry32,
|
|
18
|
+
sample,
|
|
19
|
+
shuffled,
|
|
20
|
+
smoothstep,
|
|
21
|
+
} from "./tile-ring.js";
|
|
22
|
+
|
|
23
|
+
const DEG = Math.PI / 180;
|
|
24
|
+
|
|
25
|
+
/** One part of the scattered mesh — a glTF primitive — and the baked levels to draw it at. */
|
|
26
|
+
export interface IScatterPart {
|
|
27
|
+
/** Near to far: `lods[0]` is the full mesh, the last is the farthest level. */
|
|
28
|
+
readonly lods: readonly BufferGeometry[];
|
|
29
|
+
/** The game's material for this part. The field chooses neither geometry nor look. */
|
|
30
|
+
readonly material: Material;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The ground under a candidate, in field-local metres, with its unit normal. */
|
|
34
|
+
export interface IScatterPoint {
|
|
35
|
+
readonly x: number;
|
|
36
|
+
readonly y: number;
|
|
37
|
+
readonly z: number;
|
|
38
|
+
readonly nx: number;
|
|
39
|
+
readonly ny: number;
|
|
40
|
+
readonly nz: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Where a candidate goes. The game writes the transform; the field owns `priority`. */
|
|
44
|
+
export interface IScatterPlacement {
|
|
45
|
+
x: number;
|
|
46
|
+
y: number;
|
|
47
|
+
z: number;
|
|
48
|
+
/** Radians about the ground normal, so a prop's own facing is the game's choice. */
|
|
49
|
+
yaw: number;
|
|
50
|
+
/** Uniform scale on the baked metres. */
|
|
51
|
+
scale: number;
|
|
52
|
+
/** 0..1: the candidate's order in the seeded shuffle. Lower instances thin away first. */
|
|
53
|
+
readonly priority: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export type IScatterFieldBounds = ITileBounds;
|
|
57
|
+
|
|
58
|
+
export interface IScatterField3DOptions {
|
|
59
|
+
/** One entry per part of the baked mesh; a log's bark and its foliage are two. */
|
|
60
|
+
readonly parts: readonly IScatterPart[];
|
|
61
|
+
/**
|
|
62
|
+
* Decides one candidate: write `out` and return true to place it, or return false to leave the
|
|
63
|
+
* spot empty (a path, a rock, a stump's own footprint). Called once per candidate per tile build.
|
|
64
|
+
*/
|
|
65
|
+
readonly place: (point: IScatterPoint, out: IScatterPlacement) => boolean;
|
|
66
|
+
/** Ground height at (x, z), field-local metres. */
|
|
67
|
+
readonly getHeight: (x: number, z: number) => number;
|
|
68
|
+
/** Coverage 0..1 at (x, z, height, normal y): paths, clearings, biome masks. */
|
|
69
|
+
readonly getDensity?: (x: number, z: number, height: number, normalY: number) => number;
|
|
70
|
+
readonly bounds?: IScatterFieldBounds | null;
|
|
71
|
+
/** Candidates per square metre at full coverage. */
|
|
72
|
+
readonly density?: number;
|
|
73
|
+
readonly chunkSize?: number;
|
|
74
|
+
readonly seed?: number;
|
|
75
|
+
readonly heightSampleSpacing?: number;
|
|
76
|
+
/** Degrees: nothing is placed on ground steeper than `maxSlope`, thinning over `slopeFade`. */
|
|
77
|
+
readonly maxSlope?: number;
|
|
78
|
+
readonly slopeFade?: number;
|
|
79
|
+
readonly minAltitude?: number;
|
|
80
|
+
readonly maxAltitude?: number;
|
|
81
|
+
readonly drawDistance?: number;
|
|
82
|
+
/** Distances at which the next level takes over, one per step down a part's `lods`. */
|
|
83
|
+
readonly lodDistances?: readonly number[];
|
|
84
|
+
readonly thinStart?: number;
|
|
85
|
+
readonly thinEnd?: number;
|
|
86
|
+
/** The share of instances left past `thinEnd`. */
|
|
87
|
+
readonly farDensity?: number;
|
|
88
|
+
/** Milliseconds per frame allowed for building tiles; one tile always builds. */
|
|
89
|
+
readonly buildBudgetMs?: number;
|
|
90
|
+
/** Tilt each instance onto the ground normal. Off keeps every instance upright. */
|
|
91
|
+
readonly alignToGround?: boolean;
|
|
92
|
+
readonly receiveShadow?: boolean;
|
|
93
|
+
readonly castShadow?: boolean;
|
|
94
|
+
/** Scales every density at once, as Unreal's `grass.densityScale`. */
|
|
95
|
+
readonly densityScale?: number;
|
|
96
|
+
/** Scales every distance at once, as Unreal's `grass.CullDistanceScale`. */
|
|
97
|
+
readonly cullDistanceScale?: number;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface IScatterFieldStats {
|
|
101
|
+
tiles: number;
|
|
102
|
+
visibleTiles: number;
|
|
103
|
+
instances: number;
|
|
104
|
+
pending: number;
|
|
105
|
+
/** Tiles built since the field was made. */
|
|
106
|
+
built: number;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface IScatterTile {
|
|
110
|
+
/** One mesh per part, then per level; parts need not have the same number of levels. */
|
|
111
|
+
readonly meshes: InstancedMesh[][];
|
|
112
|
+
/** Per instance, in buffer order: the shuffled prefix the thinning rule walks. */
|
|
113
|
+
readonly priority: Float32Array;
|
|
114
|
+
readonly count: number;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const scratchPosition = new Vector3();
|
|
118
|
+
const scratchScale = new Vector3();
|
|
119
|
+
const scratchQuaternion = new Quaternion();
|
|
120
|
+
const scratchTilt = new Quaternion();
|
|
121
|
+
const scratchMatrix = new Matrix4();
|
|
122
|
+
const up = new Vector3(0, 1, 0);
|
|
123
|
+
const normal = new Vector3();
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Streamed, seeded, instanced scatter of any baked mesh around a follow point; the game owns the
|
|
127
|
+
* mesh, the material and where each instance lands.
|
|
128
|
+
*
|
|
129
|
+
* Square tiles round the follow point, built nearest first inside `buildBudgetMs` through the same
|
|
130
|
+
* ring `GrassField3D` uses (`TileRing`): a tile is built inside 1.3 × the draw distance and freed
|
|
131
|
+
* past 1.4 ×, Unreal Landscape grass's guard band (UE 5.8.3:
|
|
132
|
+
* Engine/Source/Runtime/Landscape/Private/LandscapeGrass.cpp:88, :94). Each part draws one
|
|
133
|
+
* `InstancedMesh` per level, so swapping detail is a visibility toggle on a baked level the game
|
|
134
|
+
* loaded. Thinning is a plain instance count: the ring's tiles hold instances in the seeded shuffle
|
|
135
|
+
* order, so any prefix covers the tile evenly and the far edge keeps a share of them.
|
|
136
|
+
*
|
|
137
|
+
* The field creates no colour, shading or shape: `parts` and `place` come from the game, and so does
|
|
138
|
+
* every appearance constant.
|
|
139
|
+
*
|
|
140
|
+
* @situation scatter fallen logs, twigs, leaf litter, stones and ferns over a forest floor
|
|
141
|
+
* @situation stream instanced props of any baked mesh round the player without one draw per prop
|
|
142
|
+
* @situation place rocks, debris or plants on a heightfield with distance thinning and level of detail
|
|
143
|
+
* @alias scatter rocks, debris and plants over terrain
|
|
144
|
+
* @constraint call `update(follow)` once per rendered frame
|
|
145
|
+
* @constraint `place` runs once per candidate per tile build: return false for paths and clearings
|
|
146
|
+
* @constraint rotating or scaling the field is not supported; translating it is
|
|
147
|
+
* @example
|
|
148
|
+
* import { ScatterField3D } from "@threenative/core";
|
|
149
|
+
* const floor = new ScatterField3D({
|
|
150
|
+
* parts: [{ lods: [logLod0, logLod1], material }],
|
|
151
|
+
* getHeight: (x, z) => height(x, z),
|
|
152
|
+
* density: 0.05,
|
|
153
|
+
* place: (point, out) => { out.x = point.x; out.y = point.y; out.z = point.z; out.yaw = point.x * 0.7; out.scale = 1; return !onPath(point.x, point.z); },
|
|
154
|
+
* });
|
|
155
|
+
* ctx.add(floor);
|
|
156
|
+
* ctx.beforeRender(() => floor.update(hero.position));
|
|
157
|
+
*/
|
|
158
|
+
export class ScatterField3D extends Group {
|
|
159
|
+
readonly parts: readonly IScatterPart[];
|
|
160
|
+
place: (point: IScatterPoint, out: IScatterPlacement) => boolean;
|
|
161
|
+
getHeight: (x: number, z: number) => number;
|
|
162
|
+
getDensity: ((x: number, z: number, height: number, normalY: number) => number) | null;
|
|
163
|
+
bounds: IScatterFieldBounds | null;
|
|
164
|
+
density: number;
|
|
165
|
+
chunkSize: number;
|
|
166
|
+
seed: number;
|
|
167
|
+
heightSampleSpacing: number;
|
|
168
|
+
maxSlope: number;
|
|
169
|
+
slopeFade: number;
|
|
170
|
+
minAltitude: number;
|
|
171
|
+
maxAltitude: number;
|
|
172
|
+
drawDistance: number;
|
|
173
|
+
lodDistances: readonly number[];
|
|
174
|
+
thinStart: number;
|
|
175
|
+
thinEnd: number;
|
|
176
|
+
farDensity: number;
|
|
177
|
+
buildBudgetMs: number;
|
|
178
|
+
alignToGround: boolean;
|
|
179
|
+
override receiveShadow: boolean;
|
|
180
|
+
override castShadow: boolean;
|
|
181
|
+
densityScale: number;
|
|
182
|
+
cullDistanceScale: number;
|
|
183
|
+
readonly #ring: TileRing<IScatterTile>;
|
|
184
|
+
readonly #shown = { instances: 0, visibleTiles: 0 };
|
|
185
|
+
#version = 0;
|
|
186
|
+
#layout = "";
|
|
187
|
+
|
|
188
|
+
constructor(options: IScatterField3DOptions) {
|
|
189
|
+
super();
|
|
190
|
+
this.name = "ScatterField3D";
|
|
191
|
+
if (typeof options.place !== "function")
|
|
192
|
+
throw new Error("ScatterField3D.place must be a function (point, out) => boolean.");
|
|
193
|
+
if (typeof options.getHeight !== "function")
|
|
194
|
+
throw new Error("ScatterField3D.getHeight must be a function (x, z) => height.");
|
|
195
|
+
if (!Array.isArray(options.parts) || options.parts.length === 0)
|
|
196
|
+
throw new Error("ScatterField3D.parts must list at least one part.");
|
|
197
|
+
options.parts.forEach((part, index) => {
|
|
198
|
+
if (!Array.isArray(part.lods) || part.lods.length === 0)
|
|
199
|
+
throw new Error(`ScatterField3D.parts[${index}].lods must list at least one geometry.`);
|
|
200
|
+
if (!part.material)
|
|
201
|
+
throw new Error(`ScatterField3D.parts[${index}] has no material; the game owns the look.`);
|
|
202
|
+
});
|
|
203
|
+
this.parts = options.parts;
|
|
204
|
+
this.place = options.place;
|
|
205
|
+
this.getHeight = options.getHeight;
|
|
206
|
+
this.getDensity = options.getDensity ?? null;
|
|
207
|
+
this.bounds = options.bounds ?? null;
|
|
208
|
+
this.density = options.density ?? 0.1;
|
|
209
|
+
this.chunkSize = options.chunkSize ?? 16;
|
|
210
|
+
this.seed = options.seed ?? 1337;
|
|
211
|
+
this.heightSampleSpacing = options.heightSampleSpacing ?? 0.75;
|
|
212
|
+
this.maxSlope = options.maxSlope ?? 42;
|
|
213
|
+
this.slopeFade = options.slopeFade ?? 12;
|
|
214
|
+
this.minAltitude = options.minAltitude ?? Number.NEGATIVE_INFINITY;
|
|
215
|
+
this.maxAltitude = options.maxAltitude ?? Number.POSITIVE_INFINITY;
|
|
216
|
+
this.drawDistance = options.drawDistance ?? 60;
|
|
217
|
+
this.lodDistances = options.lodDistances ?? [20, 45];
|
|
218
|
+
this.thinStart = options.thinStart ?? 10;
|
|
219
|
+
this.thinEnd = options.thinEnd ?? 50;
|
|
220
|
+
this.farDensity = options.farDensity ?? 0.3;
|
|
221
|
+
this.buildBudgetMs = options.buildBudgetMs ?? 4;
|
|
222
|
+
this.alignToGround = options.alignToGround ?? true;
|
|
223
|
+
this.receiveShadow = options.receiveShadow ?? true;
|
|
224
|
+
this.castShadow = options.castShadow ?? true;
|
|
225
|
+
this.densityScale = options.densityScale ?? 1;
|
|
226
|
+
this.cullDistanceScale = options.cullDistanceScale ?? 1;
|
|
227
|
+
this.#validate();
|
|
228
|
+
this.#ring = new TileRing<IScatterTile>(this.#hooks());
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
get stats(): IScatterFieldStats {
|
|
232
|
+
return {
|
|
233
|
+
built: this.#ring.stats.built,
|
|
234
|
+
instances: this.#shown.instances,
|
|
235
|
+
pending: this.#ring.stats.pending,
|
|
236
|
+
tiles: this.#ring.stats.tiles,
|
|
237
|
+
visibleTiles: this.#shown.visibleTiles,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Builds every tile in range on the next `update` regardless of budget: on load or after a teleport. */
|
|
242
|
+
prewarm(): void {
|
|
243
|
+
this.#ring.prewarm();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Re-scatter after the ground or the coverage changed: all tiles, or those touching a rectangle. */
|
|
247
|
+
refresh(rect?: IScatterFieldBounds): void {
|
|
248
|
+
if (rect === undefined) {
|
|
249
|
+
this.#version++;
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
this.#ring.disposeInside(rect);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Streams, thins and LODs the tiles round `follow`. Call once per rendered frame. */
|
|
256
|
+
update(follow: Vector3): void {
|
|
257
|
+
this.#validate();
|
|
258
|
+
const layout = this.#layoutKey();
|
|
259
|
+
if (layout !== this.#layout) {
|
|
260
|
+
this.#ring.clear();
|
|
261
|
+
this.#layout = layout;
|
|
262
|
+
}
|
|
263
|
+
scratchPosition.copy(follow);
|
|
264
|
+
this.worldToLocal(scratchPosition);
|
|
265
|
+
this.#shown.visibleTiles = this.#shown.instances = 0;
|
|
266
|
+
this.#ring.update(scratchPosition.x, scratchPosition.z);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
override dispose(): void {
|
|
270
|
+
this.#ring.clear();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
#hooks(): ITileRingField<IScatterTile> {
|
|
274
|
+
const field = this;
|
|
275
|
+
return {
|
|
276
|
+
build: (ix, iz) => this.#build(ix, iz),
|
|
277
|
+
get buildBudgetMs() {
|
|
278
|
+
return field.buildBudgetMs;
|
|
279
|
+
},
|
|
280
|
+
get chunkSize() {
|
|
281
|
+
return field.chunkSize;
|
|
282
|
+
},
|
|
283
|
+
get drawDistance() {
|
|
284
|
+
return field.drawDistance * field.cullDistanceScale;
|
|
285
|
+
},
|
|
286
|
+
dispose: (tile) => this.#disposeTile(tile),
|
|
287
|
+
hide: (tile) => {
|
|
288
|
+
for (const part of tile.meshes)
|
|
289
|
+
for (const mesh of part) if (mesh.visible) mesh.visible = false;
|
|
290
|
+
},
|
|
291
|
+
inBounds: (ix, iz) => this.#inBounds(ix, iz),
|
|
292
|
+
show: (tile, distance) => this.#show(tile, distance),
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
#validate(): void {
|
|
297
|
+
if (!(this.chunkSize > 0))
|
|
298
|
+
throw new Error(`ScatterField3D.chunkSize must be > 0, got ${this.chunkSize}.`);
|
|
299
|
+
if (!(this.density >= 0))
|
|
300
|
+
throw new Error(`ScatterField3D.density must be >= 0, got ${this.density}.`);
|
|
301
|
+
if (!(this.drawDistance > 0))
|
|
302
|
+
throw new Error(`ScatterField3D.drawDistance must be > 0, got ${this.drawDistance}.`);
|
|
303
|
+
if (!(this.buildBudgetMs >= 0))
|
|
304
|
+
throw new Error(`ScatterField3D.buildBudgetMs must be >= 0, got ${this.buildBudgetMs}.`);
|
|
305
|
+
if (!(this.densityScale >= 0) || !(this.cullDistanceScale > 0))
|
|
306
|
+
throw new Error("ScatterField3D.densityScale must be >= 0 and cullDistanceScale > 0.");
|
|
307
|
+
const b = this.bounds;
|
|
308
|
+
if (b === null) return;
|
|
309
|
+
for (const key of ["minX", "minZ", "maxX", "maxZ"] as const)
|
|
310
|
+
if (!Number.isFinite(b[key])) throw new Error(`ScatterField3D.bounds.${key} must be finite.`);
|
|
311
|
+
if (b.minX >= b.maxX) throw new Error("ScatterField3D.bounds.minX must be below bounds.maxX.");
|
|
312
|
+
if (b.minZ >= b.maxZ) throw new Error("ScatterField3D.bounds.minZ must be below bounds.maxZ.");
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
#layoutKey(): string {
|
|
316
|
+
return [
|
|
317
|
+
this.#version,
|
|
318
|
+
this.density * this.densityScale,
|
|
319
|
+
this.chunkSize,
|
|
320
|
+
this.seed,
|
|
321
|
+
this.heightSampleSpacing,
|
|
322
|
+
this.maxSlope,
|
|
323
|
+
this.slopeFade,
|
|
324
|
+
this.minAltitude,
|
|
325
|
+
this.maxAltitude,
|
|
326
|
+
this.alignToGround,
|
|
327
|
+
this.parts.length,
|
|
328
|
+
].join("|");
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
#inBounds(ix: number, iz: number): boolean {
|
|
332
|
+
const b = this.bounds;
|
|
333
|
+
const S = this.chunkSize;
|
|
334
|
+
return (
|
|
335
|
+
b === null ||
|
|
336
|
+
!(ix * S >= b.maxX || (ix + 1) * S <= b.minX || iz * S >= b.maxZ || (iz + 1) * S <= b.minZ)
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
#pointInBounds(x: number, z: number): boolean {
|
|
341
|
+
const b = this.bounds;
|
|
342
|
+
return b === null || (x >= b.minX && x <= b.maxX && z >= b.minZ && z <= b.maxZ);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** Slope limit times the game's coverage mask, 0..1. */
|
|
346
|
+
#coverage(x: number, z: number, y: number, ny: number, cosMax: number, span: number): number {
|
|
347
|
+
const m = clamp01((ny - cosMax) / span);
|
|
348
|
+
return m > 0 && this.getDensity ? m * clamp01(this.getDensity(x, z, y, ny)) : m;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** The thinning cut-off at a tile's nearest point: the same rule the material cannot apply here. */
|
|
352
|
+
#kept(tile: IScatterTile, distance: number): number {
|
|
353
|
+
const k = this.cullDistanceScale;
|
|
354
|
+
const thinEnd = Math.max(this.thinEnd, this.thinStart + 0.01) * k;
|
|
355
|
+
const edge = 1.1 + (this.farDensity - 1.1) * smoothstep(this.thinStart * k, thinEnd, distance);
|
|
356
|
+
if (edge >= 1) return tile.count;
|
|
357
|
+
// Instances sit in the seeded shuffle order, so the kept ones are a prefix of the buffer.
|
|
358
|
+
const pr = tile.priority;
|
|
359
|
+
let lo = 0;
|
|
360
|
+
let hi = tile.count;
|
|
361
|
+
while (lo < hi) {
|
|
362
|
+
const mid = (lo + hi) >> 1;
|
|
363
|
+
if ((pr[mid] as number) < edge) lo = mid + 1;
|
|
364
|
+
else hi = mid;
|
|
365
|
+
}
|
|
366
|
+
return lo;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
#show(tile: IScatterTile, distance: number): void {
|
|
370
|
+
const R = this.drawDistance * this.cullDistanceScale;
|
|
371
|
+
const k = this.cullDistanceScale;
|
|
372
|
+
const n = this.#kept(tile, distance);
|
|
373
|
+
const inRange = n > 0 && distance < R;
|
|
374
|
+
if (inRange) {
|
|
375
|
+
this.#shown.visibleTiles++;
|
|
376
|
+
this.#shown.instances += n;
|
|
377
|
+
}
|
|
378
|
+
for (let part = 0; part < this.parts.length; part++) {
|
|
379
|
+
const meshes = tile.meshes[part] as InstancedMesh[];
|
|
380
|
+
let level = 0;
|
|
381
|
+
while (
|
|
382
|
+
level < meshes.length - 1 &&
|
|
383
|
+
distance >= (this.lodDistances[level] ?? Number.POSITIVE_INFINITY) * k
|
|
384
|
+
)
|
|
385
|
+
level++;
|
|
386
|
+
for (let l = 0; l < meshes.length; l++) {
|
|
387
|
+
const mesh = meshes[l] as InstancedMesh;
|
|
388
|
+
// Every write below is guarded: a standing follow point re-plans nothing on the owned engine.
|
|
389
|
+
const visible = inRange && l === level;
|
|
390
|
+
if (mesh.visible !== visible) mesh.visible = visible;
|
|
391
|
+
if (mesh.count !== n) mesh.count = n;
|
|
392
|
+
if (mesh.receiveShadow !== this.receiveShadow) mesh.receiveShadow = this.receiveShadow;
|
|
393
|
+
if (mesh.castShadow !== this.castShadow) mesh.castShadow = this.castShadow;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
#build(ix: number, iz: number): IScatterTile | null {
|
|
399
|
+
const S = this.chunkSize;
|
|
400
|
+
const ox = ix * S;
|
|
401
|
+
const oz = iz * S;
|
|
402
|
+
const rng = mulberry32(hashInt(ix, iz, this.seed | 0));
|
|
403
|
+
const ground = groundGrid(this.getHeight, ox, oz, S, this.heightSampleSpacing);
|
|
404
|
+
const n = Math.max(1, Math.ceil(Math.sqrt(this.density * this.densityScale * S * S)));
|
|
405
|
+
const total = this.density * this.densityScale > 0 ? n * n : 0;
|
|
406
|
+
const cell = S / n;
|
|
407
|
+
const order = shuffled(total, rng);
|
|
408
|
+
const matrices: Matrix4[] = [];
|
|
409
|
+
const priority = new Float32Array(total);
|
|
410
|
+
const cosMax = Math.cos(this.maxSlope * DEG);
|
|
411
|
+
const slopeSpan = Math.max(
|
|
412
|
+
Math.cos(Math.max(this.maxSlope - this.slopeFade, 0) * DEG) - cosMax,
|
|
413
|
+
1e-5,
|
|
414
|
+
);
|
|
415
|
+
// Mutable in here, readonly to the game: the field owns `priority`.
|
|
416
|
+
const out = { priority: 0, scale: 1, x: 0, y: 0, z: 0, yaw: 0 };
|
|
417
|
+
let k = 0;
|
|
418
|
+
for (let q = 0; q < total; q++) {
|
|
419
|
+
const c = order[q] as number;
|
|
420
|
+
const lx = ((c % n) + rng()) * cell;
|
|
421
|
+
const lz = (((c / n) | 0) + rng()) * cell;
|
|
422
|
+
const rKeep = rng();
|
|
423
|
+
const wx = ox + lx;
|
|
424
|
+
const wz = oz + lz;
|
|
425
|
+
if (!this.#pointInBounds(wx, wz)) continue;
|
|
426
|
+
ground.sample(lx, lz);
|
|
427
|
+
const y = sample[0] as number;
|
|
428
|
+
const dx = sample[1] as number;
|
|
429
|
+
const dz = sample[2] as number;
|
|
430
|
+
if (y < this.minAltitude || y > this.maxAltitude) continue;
|
|
431
|
+
const ny = 1 / Math.sqrt(dx * dx + dz * dz + 1);
|
|
432
|
+
const m = this.#coverage(wx, wz, y, ny, cosMax, slopeSpan);
|
|
433
|
+
if (m <= 0.001 || rKeep > m * 2) continue;
|
|
434
|
+
out.x = wx;
|
|
435
|
+
out.y = y;
|
|
436
|
+
out.z = wz;
|
|
437
|
+
out.yaw = 0;
|
|
438
|
+
out.scale = 1;
|
|
439
|
+
out.priority = q / total;
|
|
440
|
+
const placed = this.place({ nx: -dx * ny, ny, nz: -dz * ny, x: wx, y, z: wz }, out);
|
|
441
|
+
if (!placed) continue;
|
|
442
|
+
if (!Number.isFinite(out.scale) || out.scale <= 0 || !Number.isFinite(out.yaw))
|
|
443
|
+
throw new Error(
|
|
444
|
+
`ScatterField3D.place returned scale ${out.scale} and yaw ${out.yaw} at (${wx}, ${wz}); both must be finite and the scale positive.`,
|
|
445
|
+
);
|
|
446
|
+
priority[k] = q / total;
|
|
447
|
+
scratchPosition.set(out.x, out.y, out.z);
|
|
448
|
+
scratchScale.setScalar(out.scale);
|
|
449
|
+
if (this.alignToGround) {
|
|
450
|
+
normal.set(-dx, 1, -dz).normalize();
|
|
451
|
+
scratchTilt.setFromUnitVectors(up, normal);
|
|
452
|
+
scratchQuaternion.setFromAxisAngle(up, out.yaw);
|
|
453
|
+
scratchQuaternion.premultiply(scratchTilt);
|
|
454
|
+
} else {
|
|
455
|
+
scratchQuaternion.setFromAxisAngle(up, out.yaw);
|
|
456
|
+
}
|
|
457
|
+
matrices.push(
|
|
458
|
+
scratchMatrix.clone().compose(scratchPosition, scratchQuaternion, scratchScale),
|
|
459
|
+
);
|
|
460
|
+
k++;
|
|
461
|
+
}
|
|
462
|
+
// Remembered as empty by the ring, so it is not rebuilt every frame.
|
|
463
|
+
if (k === 0) return null;
|
|
464
|
+
const meshes: InstancedMesh[][] = [];
|
|
465
|
+
for (let part = 0; part < this.parts.length; part++) {
|
|
466
|
+
const source = this.parts[part] as IScatterPart;
|
|
467
|
+
const levels: InstancedMesh[] = [];
|
|
468
|
+
for (let l = 0; l < source.lods.length; l++) {
|
|
469
|
+
const geometry = source.lods[l] as BufferGeometry;
|
|
470
|
+
const mesh = new InstancedMesh(geometry, source.material, k);
|
|
471
|
+
mesh.name = `scatter part${part} lod${l} ${ix},${iz}`;
|
|
472
|
+
for (let i = 0; i < k; i++) mesh.setMatrixAt(i, matrices[i] as Matrix4);
|
|
473
|
+
mesh.instanceMatrix.needsUpdate = true;
|
|
474
|
+
mesh.position.set(ox, 0, oz);
|
|
475
|
+
mesh.matrixAutoUpdate = false;
|
|
476
|
+
mesh.updateMatrix();
|
|
477
|
+
mesh.visible = false;
|
|
478
|
+
mesh.raycast = () => {};
|
|
479
|
+
// InstancedMesh's own bounds, not the shared geometry's: a tile culls by the instances it holds.
|
|
480
|
+
mesh.computeBoundingSphere();
|
|
481
|
+
this.add(mesh);
|
|
482
|
+
levels.push(mesh);
|
|
483
|
+
}
|
|
484
|
+
meshes.push(levels);
|
|
485
|
+
}
|
|
486
|
+
return { count: k, meshes, priority: priority.slice(0, k) };
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
#disposeTile(tile: IScatterTile): void {
|
|
490
|
+
for (const part of tile.meshes)
|
|
491
|
+
for (const mesh of part) {
|
|
492
|
+
this.remove(mesh);
|
|
493
|
+
// The geometry belongs to the game: only the instance buffer this tile allocated is freed.
|
|
494
|
+
mesh.dispose();
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
@@ -179,14 +179,19 @@ export const ASSET_UNUSED_AFTER_MS = 60_000;
|
|
|
179
179
|
export const ASSET_UNUSED_FIX =
|
|
180
180
|
"Nothing in the scene came from these models, which the reveal waited for. If nothing ever " +
|
|
181
181
|
"builds one, delete its load; if the game builds it later, ctx.assets.prefetch it after the " +
|
|
182
|
-
"reveal and load it then."
|
|
182
|
+
"reveal and load it then. A listed file may also be geometry the game merged into a new buffer, " +
|
|
183
|
+
"which drops its provenance: check that before deleting it.";
|
|
183
184
|
|
|
184
185
|
/** Models the reveal waited for that no scene object came from, `sinceRevealMs` after the reveal. */
|
|
185
186
|
export interface IUnusedModels {
|
|
187
|
+
/** The sum over the files whose size the manifest records. */
|
|
186
188
|
readonly bytes: number;
|
|
187
|
-
|
|
189
|
+
/** `bytes` is absent where the manifest records no size: unknown, not zero. */
|
|
190
|
+
readonly files: readonly { readonly bytes?: number; readonly path: string }[];
|
|
188
191
|
readonly fix: string;
|
|
189
192
|
readonly sinceRevealMs: number;
|
|
193
|
+
/** Listed files whose size the manifest does not record, so they are in no byte count. */
|
|
194
|
+
readonly unsizedFiles: number;
|
|
190
195
|
}
|
|
191
196
|
|
|
192
197
|
/**
|
|
@@ -210,14 +215,15 @@ export function measureUnusedModels(
|
|
|
210
215
|
});
|
|
211
216
|
const files = [...resolved]
|
|
212
217
|
.filter(([path, asset]) => asset.kind === "model" && !present.has(path))
|
|
213
|
-
.map(([path, asset]) => ({ bytes: asset.bytes
|
|
214
|
-
.sort((a, b) => b.bytes - a.bytes);
|
|
218
|
+
.map(([path, asset]) => (asset.bytes === undefined ? { path } : { bytes: asset.bytes, path }))
|
|
219
|
+
.sort((a, b) => (b.bytes ?? -1) - (a.bytes ?? -1));
|
|
215
220
|
if (files.length === 0) return undefined;
|
|
216
221
|
return {
|
|
217
|
-
bytes: files.reduce((sum, file) => sum + file.bytes, 0),
|
|
222
|
+
bytes: files.reduce((sum, file) => sum + (file.bytes ?? 0), 0),
|
|
218
223
|
files,
|
|
219
224
|
fix: ASSET_UNUSED_FIX,
|
|
220
225
|
sinceRevealMs,
|
|
226
|
+
unsizedFiles: files.filter((file) => file.bytes === undefined).length,
|
|
221
227
|
};
|
|
222
228
|
}
|
|
223
229
|
|
package/src/state.ts
CHANGED
|
@@ -13,6 +13,11 @@ export type GameStore<T extends Record<string, unknown>> = StoreApi<T> & {
|
|
|
13
13
|
/**
|
|
14
14
|
* A store the UI reads, coalesced to one publication per frame unless a game names an interval.
|
|
15
15
|
*
|
|
16
|
+
* A key whose new value is `Object.is`-equal to the current value does not enter `pending`, so a
|
|
17
|
+
* frame that changed nothing publishes nothing and `flush` stays quiet. The flip side: mutating an
|
|
18
|
+
* object in place and setting it again is the same object, so nothing publishes — publish a new
|
|
19
|
+
* object or bump a version field on the value.
|
|
20
|
+
*
|
|
16
21
|
* `intervalMs` is an override, not the rule. A fixed 100 ms default published the HUD ten times a
|
|
17
22
|
* second whatever the frame rate was, which reads as a HUD that is not live — the number a player
|
|
18
23
|
* watches moves in visible steps while the scene beside it runs smoothly. Left unset, `flush` is
|
|
@@ -47,9 +52,17 @@ export function createGameStore<T extends Record<string, unknown>>(
|
|
|
47
52
|
gameStore.getState = () => current;
|
|
48
53
|
gameStore.set = (patch: StatePatch<T>) => {
|
|
49
54
|
const next = typeof patch === "function" ? patch(current) : patch;
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
55
|
+
let changed = false;
|
|
56
|
+
for (const key in next) {
|
|
57
|
+
const value = next[key];
|
|
58
|
+
// The value the HUD would publish is the one it already has: no publish, no cross-process
|
|
59
|
+
// write on native. Five kits hand-wrote this comparison before every `set`.
|
|
60
|
+
if (Object.is(value, current[key])) continue;
|
|
61
|
+
Reflect.set(pending, key, value);
|
|
62
|
+
Reflect.set(current, key, value);
|
|
63
|
+
changed = true;
|
|
64
|
+
}
|
|
65
|
+
if (changed) hasPending = true;
|
|
53
66
|
};
|
|
54
67
|
gameStore.flush = flush;
|
|
55
68
|
gameStore.start = () => {
|