@threenative/core 0.3.4 → 0.3.5
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 +1505 -79
- package/dist/{audio-7i3Xl0l3.d.ts → audio-DIJE5HTr.d.ts} +43 -3
- package/dist/{canvas-layer-DDmC_VVF.d.ts → canvas-layer-C1-5CviZ.d.ts} +5 -1
- package/dist/{game-CljaDv4D.d.ts → game-Bj8G1wmD.d.ts} +102 -11
- package/dist/hot.d.ts +4 -4
- package/dist/index.d.ts +712 -118
- package/dist/index.js +12423 -4955
- package/dist/{gpu-readback-CqJEfWNQ.d.ts → model-lod-BDAUYgsS.d.ts} +80 -5
- package/dist/playtest.d.ts +6 -10
- package/dist/playtest.js +47 -24
- package/dist/react-css.d.ts +62 -0
- package/dist/react-css.js +591 -0
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-CfsS2hxi.d.ts → renderer-9yEo9hPa.d.ts} +489 -22
- package/dist/terrain-jobs-worker.d.ts +2 -0
- package/dist/terrain-jobs-worker.js +130 -0
- package/dist/ui-layer.js +59 -8
- package/dist/world.d.ts +766 -73
- package/dist/world.js +9307 -3543
- package/gpl/recipes/export_world.py +18 -1
- package/mcp/blender-server.mjs +7 -4
- package/mcp/blender.mjs +3 -4
- package/mcp/engine-server.mjs +2 -2
- package/mcp/servers.mjs +4 -4
- package/package.json +15 -9
- package/patches/three@0.185.1-prd269-upgrade.patch +22418 -0
- package/patches/three@0.185.1.patch +6594 -700
- package/scripts/apply-three-patch.mjs +62 -4
- package/src/animation-blend.ts +264 -0
- package/src/animation-composition.ts +362 -0
- package/src/animation-layers.ts +260 -0
- package/src/animation-root-motion.ts +283 -0
- package/src/animation.ts +896 -0
- package/src/assets.ts +1303 -0
- package/src/atmosphere/index.ts +335 -0
- package/src/atmosphere/luts.ts +298 -0
- package/src/atmosphere/params.ts +323 -0
- package/src/audio.ts +912 -0
- package/src/billboard.ts +124 -0
- package/src/bone-lengths.ts +157 -0
- package/src/camera-shake.ts +116 -0
- package/src/canvas-layer.ts +47 -0
- package/src/clip-audit.ts +401 -0
- package/src/clustered-batch.ts +492 -0
- package/src/clustered-mesh.ts +638 -0
- package/src/compute-driven.ts +176 -0
- package/src/compute-timing.ts +318 -0
- package/src/config.ts +593 -0
- package/src/debug.ts +82 -0
- package/src/engine-render-hook.ts +25 -0
- package/src/entities.ts +79 -0
- package/src/entity-snapshot.ts +53 -0
- package/src/flight.ts +683 -0
- package/src/fluid-field.ts +748 -0
- package/src/fluid-particles.ts +1253 -0
- package/src/frame-budget.ts +1368 -0
- package/src/game.ts +2390 -0
- package/src/geometry-capture.ts +1098 -0
- package/src/gpu-class.ts +160 -0
- package/src/gpu-frame-observation.ts +424 -0
- package/src/gpu-readback.ts +143 -0
- package/src/gpu-scene-bvh.ts +464 -0
- package/src/grounding.ts +100 -0
- package/src/hot.ts +161 -0
- package/src/import-meta.d.ts +13 -0
- package/src/index.ts +1536 -0
- package/src/input.ts +690 -0
- package/src/instanced-batch-lod.ts +283 -0
- package/src/instanced-batch.ts +274 -0
- package/src/interpolation.ts +129 -0
- package/src/launch-diagnostics.ts +161 -0
- package/src/loop.ts +564 -0
- package/src/matrix-world.ts +245 -0
- package/src/merge-parts.ts +812 -0
- package/src/model-lod.ts +1315 -0
- package/src/net-protocol.ts +333 -0
- package/src/net-session.ts +477 -0
- package/src/net.ts +80 -0
- package/src/ocean/fft.ts +72 -0
- package/src/ocean/spectral.ts +728 -0
- package/src/particle-significance.ts +102 -0
- package/src/particles.ts +648 -0
- package/src/path-follow.ts +175 -0
- package/src/picking.ts +250 -0
- package/src/pipeline-census.ts +1566 -0
- package/src/platform.ts +210 -0
- package/src/playtest.ts +839 -0
- package/src/pointer-events.ts +411 -0
- package/src/pose-measure.ts +435 -0
- package/src/profiling/FrameCounters.ts +231 -0
- package/src/profiling/Spans.ts +548 -0
- package/src/profiling/opacity-convention.ts +21 -0
- package/src/profiling/render-list-validate.ts +175 -0
- package/src/profiling/render-trap-audit.ts +84 -0
- package/src/profiling/scene-warning.ts +190 -0
- package/src/profiling/span-probes.ts +191 -0
- package/src/profiling/unlit-foliage-warning.ts +149 -0
- package/src/projection-apply.ts +1529 -0
- package/src/projection-marker.ts +142 -0
- package/src/projection-plan.ts +1258 -0
- package/src/projection-skinned.ts +419 -0
- package/src/projection-stability.ts +409 -0
- package/src/projection-uniform.ts +328 -0
- package/src/random.ts +41 -0
- package/src/react-css.ts +604 -0
- package/src/react-glyphs.ts +114 -0
- package/src/react-host.ts +576 -0
- package/src/react-layout.ts +265 -0
- package/src/react.ts +60 -0
- package/src/render/alpha-antialiasing.ts +159 -0
- package/src/render/batched-velocity.ts +126 -0
- package/src/render/chain.ts +1021 -0
- package/src/render/daylight.ts +189 -0
- package/src/render/depth-pyramid.ts +596 -0
- package/src/render/foliage-alpha.ts +166 -0
- package/src/render/material-key.ts +60 -0
- package/src/render/mesh-pool.ts +144 -0
- package/src/render/probe-volume.ts +1668 -0
- package/src/render/resolution-subrect.ts +253 -0
- package/src/render/velocity.ts +442 -0
- package/src/render/virtual-shadow-pages.ts +827 -0
- package/src/render/virtual-shadow-partial.ts +174 -0
- package/src/render/virtual-shadow.ts +3330 -0
- package/src/render/world-impostor-surface.ts +722 -0
- package/src/render/world-impostor.ts +558 -0
- package/src/render-camera-cull.ts +441 -0
- package/src/render-pass-budget.ts +501 -0
- package/src/renderProjection.ts +758 -0
- package/src/renderer-config.ts +127 -0
- package/src/renderer.ts +1847 -0
- package/src/replay-protocol.ts +85 -0
- package/src/replay.ts +205 -0
- package/src/resolution-scaler.ts +819 -0
- package/src/rig-preparation.ts +271 -0
- package/src/ripple-field.ts +432 -0
- package/src/scale.ts +109 -0
- package/src/scene.ts +239 -0
- package/src/schedule.ts +155 -0
- package/src/skeletal-mesh.ts +61 -0
- package/src/skeleton.ts +100 -0
- package/src/snow-field.ts +744 -0
- package/src/softbody-topology.ts +269 -0
- package/src/softbody.ts +516 -0
- package/src/sprite-animator.ts +222 -0
- package/src/startup-readiness.ts +386 -0
- package/src/state.ts +66 -0
- package/src/static-transform.ts +274 -0
- package/src/storage-buffer.ts +253 -0
- package/src/streaming.ts +308 -0
- package/src/target-fps.ts +92 -0
- package/src/terrain-jobs-worker.ts +17 -0
- package/src/terrain-jobs.ts +353 -0
- package/src/textures.ts +39 -0
- package/src/tracers.ts +237 -0
- package/src/ui-bridge.ts +368 -0
- package/src/ui-hit-regions.ts +380 -0
- package/src/ui-layer.ts +83 -0
- package/src/ui-state.ts +220 -0
- package/src/version.ts +2 -0
- package/src/viewport.ts +253 -0
- package/src/warmup.ts +1357 -0
- package/src/water-surface.ts +384 -0
- package/src/wave-field.ts +524 -0
- package/src/world-capabilities.ts +131 -0
- package/src/world-cell-proxy.ts +392 -0
- package/src/world-cells.ts +10564 -0
- package/src/world-gpu-scene.ts +3354 -0
- package/src/world-heightmap.ts +82 -0
- package/src/world-package.ts +469 -0
- package/src/world-passes.ts +582 -0
- package/src/world-region.ts +17 -0
- package/src/world-terrain-splat.ts +564 -0
- package/src/world-tiles.ts +3826 -0
- package/src/world-topology.ts +370 -0
- package/src/world-validate.ts +48 -0
- package/src/world.ts +805 -0
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Box3,
|
|
3
|
+
BufferGeometry,
|
|
4
|
+
Material,
|
|
5
|
+
type Matrix4,
|
|
6
|
+
Mesh,
|
|
7
|
+
type Object3D,
|
|
8
|
+
Sphere,
|
|
9
|
+
Texture,
|
|
10
|
+
TextureUtils,
|
|
11
|
+
Vector3,
|
|
12
|
+
} from "three";
|
|
13
|
+
import { isEngineRenderHook } from "./engine-render-hook.js";
|
|
14
|
+
import { INSTANCED_LOD_MAX_PIXEL_ERROR } from "./instanced-batch-lod.js";
|
|
15
|
+
import { baseGeometryOf } from "./model-lod.js";
|
|
16
|
+
import { DISCRETE_LOD_DEFAULT_HYSTERESIS, type ILodView, selectLodLevel } from "./model-lod.js";
|
|
17
|
+
import { displacesVertices } from "./projection-plan.js";
|
|
18
|
+
import type { IWorldCellProxy } from "./world-package.js";
|
|
19
|
+
|
|
20
|
+
export interface ICellProxySelectionInput {
|
|
21
|
+
/** Existing admission owner sets this after compilation and a main-context backend submission observation. */
|
|
22
|
+
readonly proxyReady: boolean;
|
|
23
|
+
readonly sourceReady: boolean;
|
|
24
|
+
/** False for runtime materials/deformation or per-placement culls the cook cannot reproduce. */
|
|
25
|
+
readonly sourceCompatible: boolean;
|
|
26
|
+
/** Cost of the selected source LODs at this view even while hidden, including distant impostors. Never the cook's LOD0 census. */
|
|
27
|
+
readonly sourceTriangles: number;
|
|
28
|
+
/** Draws actually retired by this swap. A shared batch with other cells still live contributes 0. */
|
|
29
|
+
readonly replaceableSourceDraws: number;
|
|
30
|
+
/** Existing conservativeViewDepth/lod views, with finest set throughout the protected near field. */
|
|
31
|
+
readonly views: readonly ILodView[];
|
|
32
|
+
/** Error scales with the WorldCells transform, just like its conservative bounds. */
|
|
33
|
+
readonly errorScale?: number;
|
|
34
|
+
readonly maxPixelError?: number;
|
|
35
|
+
readonly hysteresis?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function count(value: number, name: string): void {
|
|
39
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
40
|
+
throw new Error(`Cell proxy ${name} must be a non-negative safe integer.`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Internal selection only. WorldCells owns models, admission, cache paths, generation cancellation,
|
|
45
|
+
* collision and shadow/bundle attachment. The owner commits this result and source visibility in
|
|
46
|
+
* one admission unit, and resets the selector on eviction. No material, loader or scheduler lives here.
|
|
47
|
+
*/
|
|
48
|
+
export class CellProxySelection {
|
|
49
|
+
readonly #proxy: IWorldCellProxy;
|
|
50
|
+
#level = 0;
|
|
51
|
+
|
|
52
|
+
constructor(proxy: IWorldCellProxy) {
|
|
53
|
+
if (!Number.isFinite(proxy.error) || proxy.error < 0)
|
|
54
|
+
throw new Error("Cell proxy error must be finite and non-negative.");
|
|
55
|
+
count(proxy.triangles, "triangles");
|
|
56
|
+
count(proxy.materialGroups, "materialGroups");
|
|
57
|
+
if (proxy.triangles === 0 || proxy.materialGroups === 0)
|
|
58
|
+
throw new Error("Cell proxy must contain drawn geometry.");
|
|
59
|
+
this.#proxy = proxy;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
select(input: ICellProxySelectionInput): "detail" | "proxy" | "pending" {
|
|
63
|
+
count(input.sourceTriangles, "sourceTriangles");
|
|
64
|
+
count(input.replaceableSourceDraws, "replaceableSourceDraws");
|
|
65
|
+
const scale = input.errorScale ?? 1;
|
|
66
|
+
if (!Number.isFinite(scale) || scale <= 0)
|
|
67
|
+
throw new Error("Cell proxy error scale must be finite and positive.");
|
|
68
|
+
const budget = input.maxPixelError ?? INSTANCED_LOD_MAX_PIXEL_ERROR;
|
|
69
|
+
const hysteresis = input.hysteresis ?? DISCRETE_LOD_DEFAULT_HYSTERESIS;
|
|
70
|
+
if (
|
|
71
|
+
!Number.isFinite(budget) ||
|
|
72
|
+
budget <= 0 ||
|
|
73
|
+
!Number.isFinite(hysteresis) ||
|
|
74
|
+
hysteresis < 0 ||
|
|
75
|
+
hysteresis >= 1
|
|
76
|
+
)
|
|
77
|
+
throw new Error("Cell proxy needs a positive pixel budget and hysteresis in [0, 1).");
|
|
78
|
+
if (!input.proxyReady || !input.sourceCompatible) {
|
|
79
|
+
this.#level = 0;
|
|
80
|
+
return input.sourceReady ? "detail" : "pending";
|
|
81
|
+
}
|
|
82
|
+
// Returning detail remains in the existing load queue; a ready proxy stays drawn meanwhile.
|
|
83
|
+
if (!input.sourceReady && this.#level === 1) return "proxy";
|
|
84
|
+
if (
|
|
85
|
+
this.#proxy.triangles >= input.sourceTriangles ||
|
|
86
|
+
this.#proxy.materialGroups > input.replaceableSourceDraws
|
|
87
|
+
) {
|
|
88
|
+
this.#level = 0;
|
|
89
|
+
return input.sourceReady ? "detail" : "pending";
|
|
90
|
+
}
|
|
91
|
+
this.#level = selectLodLevel(
|
|
92
|
+
[0, this.#proxy.error * scale],
|
|
93
|
+
this.#level,
|
|
94
|
+
budget,
|
|
95
|
+
hysteresis,
|
|
96
|
+
input.views,
|
|
97
|
+
);
|
|
98
|
+
return this.#level === 1 ? "proxy" : input.sourceReady ? "detail" : "pending";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
reset(): void {
|
|
102
|
+
this.#level = 0;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Fixed static representation only; a managed AutoLOD chain must never replace parked geometry. */
|
|
107
|
+
export function cellProxyMaterialSafe(material: Material): boolean {
|
|
108
|
+
return (
|
|
109
|
+
(Reflect.get(material, "isMeshBasicMaterial") === true ||
|
|
110
|
+
(Reflect.get(material, "isMeshStandardMaterial") === true &&
|
|
111
|
+
Reflect.get(material, "isMeshPhysicalMaterial") !== true)) &&
|
|
112
|
+
Reflect.get(material, "isNodeMaterial") !== true &&
|
|
113
|
+
!material.transparent &&
|
|
114
|
+
!displacesVertices(material) &&
|
|
115
|
+
Reflect.get(material, "vertexNode") == null &&
|
|
116
|
+
Reflect.get(material, "positionNode") == null &&
|
|
117
|
+
material.onBeforeCompile === Material.prototype.onBeforeCompile &&
|
|
118
|
+
material.clippingPlanes == null &&
|
|
119
|
+
Object.entries(Reflect.get(material, "defines") ?? {}).every(
|
|
120
|
+
([name, value]) => name === "STANDARD" && value === "",
|
|
121
|
+
)
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
export function cellProxyGeometrySafe(geometry: BufferGeometry): boolean {
|
|
125
|
+
return (
|
|
126
|
+
geometry.drawRange.start === 0 &&
|
|
127
|
+
geometry.drawRange.count === Number.POSITIVE_INFINITY &&
|
|
128
|
+
Object.keys(geometry.morphAttributes).length === 0 &&
|
|
129
|
+
geometry.getAttribute("position") != null &&
|
|
130
|
+
Object.keys(geometry.attributes).every((name) =>
|
|
131
|
+
["position", "normal", "uv", "color"].includes(name),
|
|
132
|
+
) &&
|
|
133
|
+
(geometry.getAttribute("color") == null ||
|
|
134
|
+
([3, 4].includes(geometry.getAttribute("color").itemSize) &&
|
|
135
|
+
geometry.getAttribute("color").count === geometry.getAttribute("position").count))
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
export function cellProxySourceSafe(root: Object3D, staticInstances?: ReadonlySet<Mesh>): boolean {
|
|
139
|
+
let safe = (Reflect.get(root, "animations")?.length ?? 0) === 0;
|
|
140
|
+
root.traverse((node) => {
|
|
141
|
+
if (
|
|
142
|
+
Reflect.get(node, "isLight") ||
|
|
143
|
+
Reflect.get(node, "isPoints") ||
|
|
144
|
+
Reflect.get(node, "isLine")
|
|
145
|
+
)
|
|
146
|
+
safe = false;
|
|
147
|
+
if (!(node instanceof Mesh)) return;
|
|
148
|
+
if (
|
|
149
|
+
Reflect.get(node, "isSkinnedMesh") ||
|
|
150
|
+
(Reflect.get(node, "isInstancedMesh") &&
|
|
151
|
+
(staticInstances?.has(node) !== true ||
|
|
152
|
+
Reflect.get(node, "morphTexture") != null ||
|
|
153
|
+
Reflect.get(node, "instanceColor") != null)) ||
|
|
154
|
+
Array.isArray(node.material) ||
|
|
155
|
+
!cellProxyGeometrySafe(node.geometry) ||
|
|
156
|
+
!cellProxyMaterialSafe(node.material as Material) ||
|
|
157
|
+
(node.onBeforeRender !== Mesh.prototype.onBeforeRender &&
|
|
158
|
+
!isEngineRenderHook(node.onBeforeRender)) ||
|
|
159
|
+
(node.onAfterRender !== Mesh.prototype.onAfterRender &&
|
|
160
|
+
!isEngineRenderHook(node.onAfterRender))
|
|
161
|
+
)
|
|
162
|
+
safe = false;
|
|
163
|
+
});
|
|
164
|
+
return safe;
|
|
165
|
+
}
|
|
166
|
+
export interface IPreparedCellProxy {
|
|
167
|
+
readonly root: Object3D;
|
|
168
|
+
readonly meshes: readonly Mesh[];
|
|
169
|
+
readonly ranges: readonly { readonly start: number; readonly count: number }[];
|
|
170
|
+
readonly bounds: Sphere;
|
|
171
|
+
/** Decoded source buffers/textures plus owned fixed copies, not physical driver allocation. */
|
|
172
|
+
readonly bytes: number;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Conservative decoded texture cost. An unreadable image cannot be admitted under a byte cap. */
|
|
176
|
+
function textureBytes(texture: Texture): number {
|
|
177
|
+
const images = Array.isArray(texture.image) ? texture.image : [texture.image];
|
|
178
|
+
const buffers = new Set<ArrayBufferLike>();
|
|
179
|
+
let storage = 0;
|
|
180
|
+
let bytes = 0;
|
|
181
|
+
for (const image of images) {
|
|
182
|
+
const width = image?.width;
|
|
183
|
+
const height = image?.height;
|
|
184
|
+
const depth = image?.depth ?? 1;
|
|
185
|
+
if (![width, height, depth].every((value) => Number.isSafeInteger(value) && value > 0))
|
|
186
|
+
throw new Error("Cell proxy texture has no measurable decoded extent.");
|
|
187
|
+
const mips = image?.mipmaps ?? texture.mipmaps;
|
|
188
|
+
// Three uses the supplied chain, or generates one only when explicitly requested without mips.
|
|
189
|
+
const levels =
|
|
190
|
+
mips.length > 0
|
|
191
|
+
? mips.length
|
|
192
|
+
: texture.generateMipmaps && Reflect.get(texture, "isCompressedTexture") !== true
|
|
193
|
+
? Math.floor(Math.log2(Math.max(width, height))) + 1
|
|
194
|
+
: 1;
|
|
195
|
+
for (let level = 0; level < levels; level++) {
|
|
196
|
+
const mip = mips[level] ?? (level === 0 ? image : undefined);
|
|
197
|
+
const w = Math.max(1, Math.floor(width / 2 ** level));
|
|
198
|
+
const h = Math.max(1, Math.floor(height / 2 ** level));
|
|
199
|
+
try {
|
|
200
|
+
bytes += TextureUtils.getByteLength(w, h, texture.format, texture.type) * depth;
|
|
201
|
+
} catch {
|
|
202
|
+
// An unknown format keeps the float32 RGBA upper bound; never guess a compression ratio.
|
|
203
|
+
bytes += w * h * depth * 16;
|
|
204
|
+
}
|
|
205
|
+
const data = mip?.data;
|
|
206
|
+
if (ArrayBuffer.isView(data) && !buffers.has(data.buffer)) {
|
|
207
|
+
buffers.add(data.buffer);
|
|
208
|
+
storage += data.buffer.byteLength;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
bytes = Math.max(bytes, storage);
|
|
213
|
+
if (!Number.isSafeInteger(bytes)) throw new Error("Cell proxy texture bytes overflow.");
|
|
214
|
+
return bytes;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Reject oversize decoded resources when IO settles, before an admission queue can retain them. */
|
|
218
|
+
export function measureCellProxy(
|
|
219
|
+
model: Object3D,
|
|
220
|
+
proxy: IWorldCellProxy,
|
|
221
|
+
includeCopies = false,
|
|
222
|
+
): number {
|
|
223
|
+
if (!cellProxySourceSafe(model))
|
|
224
|
+
throw new Error("Cell proxy has unsupported static geometry or material.");
|
|
225
|
+
const buffers = new Set<ArrayBufferLike>();
|
|
226
|
+
const textures = new Set<Texture>();
|
|
227
|
+
let bytes = 0;
|
|
228
|
+
let copies = 0;
|
|
229
|
+
let triangles = 0;
|
|
230
|
+
let draws = 0;
|
|
231
|
+
let nodes = 0;
|
|
232
|
+
model.traverse((node) => {
|
|
233
|
+
if (++nodes > 4096) throw new Error("Cell proxy exceeds the bounded node count.");
|
|
234
|
+
if (!(node instanceof Mesh)) return;
|
|
235
|
+
const geometry = baseGeometryOf(node);
|
|
236
|
+
for (const attribute of [geometry.index, ...Object.values(geometry.attributes)]) {
|
|
237
|
+
if (attribute == null) continue;
|
|
238
|
+
const array = Reflect.get(attribute, "array") ?? Reflect.get(attribute, "data")?.array;
|
|
239
|
+
if (array == null) throw new Error("Cell proxy has an unreadable buffer.");
|
|
240
|
+
// Plain clones copy the view; interleaved clones may share a copied store. Charging each
|
|
241
|
+
// attribute view is conservative for that sharing and bounds every private allocation.
|
|
242
|
+
copies += array.byteLength;
|
|
243
|
+
if (!buffers.has(array.buffer)) {
|
|
244
|
+
buffers.add(array.buffer);
|
|
245
|
+
bytes += array.buffer.byteLength;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
triangles += (geometry.index?.count ?? geometry.getAttribute("position").count) / 3;
|
|
249
|
+
draws++;
|
|
250
|
+
for (const value of Object.values(node.material))
|
|
251
|
+
if (value instanceof Texture) textures.add(value);
|
|
252
|
+
});
|
|
253
|
+
if (triangles !== proxy.triangles || draws !== proxy.materialGroups)
|
|
254
|
+
throw new Error("Cell proxy draw census does not match its manifest.");
|
|
255
|
+
for (const texture of textures) bytes += textureBytes(texture);
|
|
256
|
+
if (!Number.isSafeInteger(bytes) || bytes > 4 * 1024 * 1024)
|
|
257
|
+
throw new Error("Cell proxy exceeds the decoded admission unit.");
|
|
258
|
+
return bytes + (includeCopies ? copies : 0);
|
|
259
|
+
}
|
|
260
|
+
export function prepareCellProxy(
|
|
261
|
+
model: Object3D,
|
|
262
|
+
proxy: IWorldCellProxy,
|
|
263
|
+
maxBytes = Number.POSITIVE_INFINITY,
|
|
264
|
+
): IPreparedCellProxy {
|
|
265
|
+
if (!cellProxySourceSafe(model))
|
|
266
|
+
throw new Error("Cell proxy has unsupported static geometry or material.");
|
|
267
|
+
const bytes = measureCellProxy(model, proxy, true);
|
|
268
|
+
if (bytes > maxBytes) throw new Error("Cell proxy exceeds its reserved decoded budget.");
|
|
269
|
+
const root = model.clone(true);
|
|
270
|
+
const meshes: Mesh[] = [];
|
|
271
|
+
const ranges: { start: number; count: number }[] = [];
|
|
272
|
+
root.updateMatrixWorld(true);
|
|
273
|
+
const box = new Box3().setFromObject(root, false);
|
|
274
|
+
if (box.isEmpty() || ![...box.min.toArray(), ...box.max.toArray()].every(Number.isFinite))
|
|
275
|
+
throw new Error("Cell proxy has no finite bounds.");
|
|
276
|
+
if (proxy.bounds !== undefined) {
|
|
277
|
+
const declared = new Box3(new Vector3(...proxy.bounds.min), new Vector3(...proxy.bounds.max));
|
|
278
|
+
const margin = 1e-4;
|
|
279
|
+
declared.expandByScalar(margin);
|
|
280
|
+
// The cook declares the exact vertex extent. `box` above is the cheaper geometry-box hull, which
|
|
281
|
+
// a rotated mesh inflates past that exact extent, so test the vertices themselves.
|
|
282
|
+
const exact = new Box3().setFromObject(root, true);
|
|
283
|
+
if (!declared.containsBox(exact))
|
|
284
|
+
throw new Error("Cell proxy lies outside its declared bounds.");
|
|
285
|
+
}
|
|
286
|
+
// copy() does not invoke the installed clone hook that registers another managed LOD chain.
|
|
287
|
+
root.traverse((node) => {
|
|
288
|
+
if (!(node instanceof Mesh)) return;
|
|
289
|
+
const fixed = new BufferGeometry().copy(baseGeometryOf(node));
|
|
290
|
+
node.geometry = fixed;
|
|
291
|
+
meshes.push(node);
|
|
292
|
+
ranges.push({ ...fixed.drawRange });
|
|
293
|
+
fixed.setDrawRange(0, 0);
|
|
294
|
+
node.castShadow = false;
|
|
295
|
+
node.frustumCulled = false;
|
|
296
|
+
});
|
|
297
|
+
return { root, meshes, ranges, bounds: box.getBoundingSphere(new Sphere()), bytes };
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Cook-compatible fixed materials. No image serialization or quantized/HDR-clamped colors. */
|
|
301
|
+
export function cellProxyMaterialMatches(a: Material, b: Material): boolean {
|
|
302
|
+
if (a.type !== b.type) return false;
|
|
303
|
+
const descriptor = (material: Material): string => {
|
|
304
|
+
const fields: Record<string, unknown> = {};
|
|
305
|
+
for (const [name, value] of Object.entries(material)) {
|
|
306
|
+
if (
|
|
307
|
+
["id", "uuid", "name", "version", "userData"].includes(name) ||
|
|
308
|
+
value instanceof Texture ||
|
|
309
|
+
typeof value === "function"
|
|
310
|
+
)
|
|
311
|
+
continue;
|
|
312
|
+
if (value == null || ["string", "number", "boolean"].includes(typeof value))
|
|
313
|
+
fields[name] = value;
|
|
314
|
+
else if (Reflect.get(value, "isColor") === true)
|
|
315
|
+
fields[name] = [Reflect.get(value, "r"), Reflect.get(value, "g"), Reflect.get(value, "b")];
|
|
316
|
+
else if (Reflect.get(value, "isEuler") === true)
|
|
317
|
+
fields[name] = [
|
|
318
|
+
Reflect.get(value, "x"),
|
|
319
|
+
Reflect.get(value, "y"),
|
|
320
|
+
Reflect.get(value, "z"),
|
|
321
|
+
Reflect.get(value, "order"),
|
|
322
|
+
];
|
|
323
|
+
else if (Reflect.get(value, "isVector2") === true)
|
|
324
|
+
fields[name] = [Reflect.get(value, "x"), Reflect.get(value, "y")];
|
|
325
|
+
else if (name === "defines") fields[name] = value;
|
|
326
|
+
}
|
|
327
|
+
return JSON.stringify(fields);
|
|
328
|
+
};
|
|
329
|
+
if (descriptor(a) !== descriptor(b)) return false;
|
|
330
|
+
const textureFields = new Set(
|
|
331
|
+
[...Object.entries(a), ...Object.entries(b)]
|
|
332
|
+
.filter(([, value]) => value instanceof Texture)
|
|
333
|
+
.map(([name]) => name),
|
|
334
|
+
);
|
|
335
|
+
for (const name of textureFields) {
|
|
336
|
+
const value = Reflect.get(a, name);
|
|
337
|
+
const other = Reflect.get(b, name);
|
|
338
|
+
if (!(value instanceof Texture) || !(other instanceof Texture)) return false;
|
|
339
|
+
if (
|
|
340
|
+
value.source !== other.source &&
|
|
341
|
+
value.image !== other.image &&
|
|
342
|
+
!(
|
|
343
|
+
typeof value.image?.src === "string" &&
|
|
344
|
+
value.image.src.length > 0 &&
|
|
345
|
+
value.image.src === other.image?.src
|
|
346
|
+
)
|
|
347
|
+
)
|
|
348
|
+
return false;
|
|
349
|
+
if (value.mipmaps !== other.mipmaps && value.mipmaps.length + other.mipmaps.length > 0)
|
|
350
|
+
return false;
|
|
351
|
+
const parameters = (texture: Texture): string =>
|
|
352
|
+
JSON.stringify([
|
|
353
|
+
texture.mapping,
|
|
354
|
+
texture.channel,
|
|
355
|
+
texture.wrapS,
|
|
356
|
+
texture.wrapT,
|
|
357
|
+
texture.magFilter,
|
|
358
|
+
texture.minFilter,
|
|
359
|
+
texture.anisotropy,
|
|
360
|
+
texture.flipY,
|
|
361
|
+
texture.colorSpace,
|
|
362
|
+
texture.format,
|
|
363
|
+
texture.type,
|
|
364
|
+
texture.internalFormat,
|
|
365
|
+
texture.matrix.toArray(),
|
|
366
|
+
texture.offset.toArray(),
|
|
367
|
+
texture.repeat.toArray(),
|
|
368
|
+
texture.center.toArray(),
|
|
369
|
+
texture.rotation,
|
|
370
|
+
texture.matrixAutoUpdate,
|
|
371
|
+
texture.generateMipmaps,
|
|
372
|
+
texture.premultiplyAlpha,
|
|
373
|
+
texture.unpackAlignment,
|
|
374
|
+
]);
|
|
375
|
+
if (parameters(value) !== parameters(other)) return false;
|
|
376
|
+
}
|
|
377
|
+
return true;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** Gershgorin bound of M-transpose-M, also conservative for composed TRS that contains shear. */
|
|
381
|
+
export function cellProxyWorldScale(matrix: Matrix4): number {
|
|
382
|
+
const e = matrix.elements;
|
|
383
|
+
const a = new Vector3(e[0], e[1], e[2]);
|
|
384
|
+
const b = new Vector3(e[4], e[5], e[6]);
|
|
385
|
+
const c = new Vector3(e[8], e[9], e[10]);
|
|
386
|
+
const ab = Math.abs(a.dot(b));
|
|
387
|
+
const ac = Math.abs(a.dot(c));
|
|
388
|
+
const bc = Math.abs(b.dot(c));
|
|
389
|
+
return Math.sqrt(
|
|
390
|
+
Math.max(a.lengthSq() + ab + ac, b.lengthSq() + ab + bc, c.lengthSq() + ac + bc),
|
|
391
|
+
);
|
|
392
|
+
}
|