@ignifx/core 0.2.0 → 0.3.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/dist/async-pipelines-CI5uT5iZ.js +26 -0
- package/dist/async-pipelines-CI5uT5iZ.js.map +1 -0
- package/dist/color-DrR6GFTd.js +351 -0
- package/dist/color-DrR6GFTd.js.map +1 -0
- package/dist/effect-task-CMbuO6Kb.js +205 -0
- package/dist/effect-task-CMbuO6Kb.js.map +1 -0
- package/dist/environment-C_JfuAjj.js +224 -0
- package/dist/environment-C_JfuAjj.js.map +1 -0
- package/dist/ignifx-error-R1ma_Lzv.js +544 -0
- package/dist/ignifx-error-R1ma_Lzv.js.map +1 -0
- package/dist/index.d.ts +3778 -1658
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6349 -5585
- package/dist/index.js.map +1 -1
- package/dist/instanced-mesh-instance-Cgx1H87p.js +701 -0
- package/dist/instanced-mesh-instance-Cgx1H87p.js.map +1 -0
- package/dist/material-plugin-CTaWZXEk.js +485 -0
- package/dist/material-plugin-CTaWZXEk.js.map +1 -0
- package/dist/math-utils-BcDnL-gi.js +277 -0
- package/dist/math-utils-BcDnL-gi.js.map +1 -0
- package/dist/mesh-CnK_PX46.js +811 -0
- package/dist/mesh-CnK_PX46.js.map +1 -0
- package/dist/post-effect-C42XJFmo.js +155 -0
- package/dist/post-effect-C42XJFmo.js.map +1 -0
- package/dist/post-effect-compiler-DTlkT1si.js +311 -0
- package/dist/post-effect-compiler-DTlkT1si.js.map +1 -0
- package/dist/shader-declaration-5q__jycG.js +165 -0
- package/dist/shader-declaration-5q__jycG.js.map +1 -0
- package/dist/shader-material-DssOqsvZ.js +1009 -0
- package/dist/shader-material-DssOqsvZ.js.map +1 -0
- package/dist/shader-material-Xk7Oc9UG.js +207 -0
- package/dist/shader-material-Xk7Oc9UG.js.map +1 -0
- package/dist/shader-pragma-CNAK4SIb.js +685 -0
- package/dist/shader-pragma-CNAK4SIb.js.map +1 -0
- package/dist/surface-shader-C2HtysNN.js +1397 -0
- package/dist/surface-shader-C2HtysNN.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,701 @@
|
|
|
1
|
+
import { B as tagNode, C as setMeshSubtreeVisible, S as setMeshRenderOrder, _ as setMeshBounds, b as setMeshPickable, f as detachMeshBounds, g as removeMeshFromScene, n as cloneMeshUnderNode, t as addMeshToScene, v as setMeshId, x as setMeshReceiveShadows, y as setMeshMaterial } from "./mesh-CnK_PX46.js";
|
|
2
|
+
import { clearThinInstanceLodPartner, enableThinInstanceDynamicDrawCount, enableThinInstanceGpuCulling, enableThinInstanceWorldBounds, flushThinInstances, setThinInstanceColors, setThinInstanceCount, setThinInstanceDrawCount, setThinInstanceLodPartner, setThinInstances } from "@babylonjs/lite";
|
|
3
|
+
//#region src/lite/gpu/thin-instances.ts
|
|
4
|
+
/**
|
|
5
|
+
* Thin-instance half of the Babylon Lite adapter (`docs/architecture/07-rendering.md` §2.3,
|
|
6
|
+
* `docs/plan/2026-09-terrain-particles-shaders.md` §3.5). Everything here is `@internal` and needs a
|
|
7
|
+
* WebGPU device, so the browser project is what covers it.
|
|
8
|
+
*
|
|
9
|
+
* Lite fixes the order these must be called in, and both the culling opt-in and the LOD pairing only
|
|
10
|
+
* take effect through the renderable `registerScene` compiles:
|
|
11
|
+
*
|
|
12
|
+
* 1. {@link setThinInstances} — its `count` argument becomes `_capacity`, and the GPU buffer is
|
|
13
|
+
* sized `_capacity * 64` bytes for the life of the mesh, so pass the **capacity** here once and
|
|
14
|
+
* lower the active count with {@link setThinInstanceCount} straight afterwards.
|
|
15
|
+
* 2. {@link enableThinInstanceWorldBounds} — gives the scene AABB the cloud rather than the
|
|
16
|
+
* prototype. It does not fix a shadow frustum: `computeDirectionalLightMatrix` reads
|
|
17
|
+
* `boundMin`/`boundMax` directly (`lib/shadow/shadow-base.js` 54).
|
|
18
|
+
* 3. {@link enableThinInstanceGpuCulling} — compute-based, opaque only.
|
|
19
|
+
* 4. {@link enableThinInstanceDynamicDrawCount} — indirect draw arguments, so a later count change
|
|
20
|
+
* keeps the cached bundle valid.
|
|
21
|
+
* 5. {@link setThinInstanceLodPartner} — needs both meshes instanced already and stamps both
|
|
22
|
+
* `_clone`, so neither can be cloned afterwards.
|
|
23
|
+
* 6. `addMeshToScene`, and only then `registerScene`.
|
|
24
|
+
*
|
|
25
|
+
* Moving instances costs three different amounts (`lib/mesh/thin-instance.js`):
|
|
26
|
+
* {@link flushThinInstances} re-uploads the whole active range and is the only sub-range-free path
|
|
27
|
+
* Lite exposes; {@link setThinInstanceCount} re-uploads as well; {@link setThinInstanceDrawCount}
|
|
28
|
+
* uploads nothing but throws until the pool has completed one GPU synchronisation, which is private
|
|
29
|
+
* state — {@link trySetThinInstanceDrawCount} asks by trying.
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Points a mesh at a matrix slab and fixes its capacity.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* The `capacity` argument is written straight to Lite's `_capacity`, which sizes the GPU buffer
|
|
36
|
+
* (`_capacity * 64` bytes) once and for good. Only the first `count` matrices are ever read from
|
|
37
|
+
* `matrices`, and Lite clamps its dirty range to the active count
|
|
38
|
+
* (`lib/mesh/thin-instance-gpu.js`), so a slab shorter than `capacity * 16` floats is safe as long
|
|
39
|
+
* as the active count is lowered with {@link setThinInstanceCount} before the next frame.
|
|
40
|
+
*
|
|
41
|
+
* Lite keeps the array by reference and never copies it: the caller owns it, and mutating it is the
|
|
42
|
+
* intended way to move instances, followed by {@link flushThinInstances}.
|
|
43
|
+
*
|
|
44
|
+
* @param mesh - The mesh to instance.
|
|
45
|
+
* @param matrices - 16 floats per instance, column-major, caller-owned.
|
|
46
|
+
* @param capacity - The largest instance count this mesh will ever draw.
|
|
47
|
+
*
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
function setThinInstances$1(mesh, matrices, capacity) {
|
|
51
|
+
setThinInstances(mesh, matrices, capacity);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Sets how many instances draw, re-uploading the active range.
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* Prefer {@link trySetThinInstanceDrawCount} for a per-frame change: this one marks `[0, count)`
|
|
58
|
+
* dirty as well, so it costs a `count * 64`-byte upload.
|
|
59
|
+
*
|
|
60
|
+
* @param mesh - The instanced mesh.
|
|
61
|
+
* @param count - The active count, at most the capacity.
|
|
62
|
+
*
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
function setThinInstanceCount$1(mesh, count) {
|
|
66
|
+
setThinInstanceCount(mesh, count);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Sets how many instances draw without re-uploading anything, when Lite accepts it.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* Lite requires the pool to have completed one full-capacity GPU synchronisation first and throws
|
|
73
|
+
* otherwise (`lib/mesh/thin-instance.js`, `ThrowLiteError(369)`); the state it checks — the matrix
|
|
74
|
+
* buffer's existence and its version — is private, so the only way to ask is to call. The throw
|
|
75
|
+
* happens before anything is mutated, so a refusal leaves the mesh untouched and the caller can
|
|
76
|
+
* fall back to {@link setThinInstanceCount}.
|
|
77
|
+
*
|
|
78
|
+
* @param mesh - The instanced mesh.
|
|
79
|
+
* @param count - The active count, at most the capacity.
|
|
80
|
+
* @returns `true` when the count was changed, `false` when Lite is not ready for a bare count
|
|
81
|
+
* change yet.
|
|
82
|
+
*
|
|
83
|
+
* @internal
|
|
84
|
+
*/
|
|
85
|
+
function trySetThinInstanceDrawCount(mesh, count) {
|
|
86
|
+
try {
|
|
87
|
+
setThinInstanceDrawCount(mesh, count);
|
|
88
|
+
return true;
|
|
89
|
+
} catch {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Marks the whole active matrix range dirty, after the caller mutated the slab in place.
|
|
95
|
+
*
|
|
96
|
+
* @param mesh - The instanced mesh.
|
|
97
|
+
*
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
function flushThinInstances$1(mesh) {
|
|
101
|
+
flushThinInstances(mesh);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Installs, replaces, or removes the per-instance RGBA colours.
|
|
105
|
+
*
|
|
106
|
+
* @remarks
|
|
107
|
+
* Lite exports no way to *remove* colours: `setThinInstanceColors` only assigns, and the colour
|
|
108
|
+
* version counters that drive the upload are private. `ThinInstanceData.colors` is declared
|
|
109
|
+
* optional and mutable (`index.d.ts` 12985), so clearing is a direct field write —
|
|
110
|
+
* which is enough, because `syncThinInstanceGpuData` skips the colour upload entirely while
|
|
111
|
+
* `colors` is null, and the renderable rebuild the caller triggers is what drops
|
|
112
|
+
* `useThinInstanceColors` from the compiled pipeline.
|
|
113
|
+
*
|
|
114
|
+
* @param mesh - The instanced mesh.
|
|
115
|
+
* @param colors - Four floats per instance (linear RGBA), or `null` to stop reading colours.
|
|
116
|
+
*
|
|
117
|
+
* @internal
|
|
118
|
+
*/
|
|
119
|
+
function setThinInstanceColors$1(mesh, colors) {
|
|
120
|
+
if (colors !== null) {
|
|
121
|
+
setThinInstanceColors(mesh, colors);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const data = mesh.thinInstances;
|
|
125
|
+
if (data !== null && data !== void 0) data.colors = null;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Turns compute-based GPU frustum culling on or off for an instanced mesh.
|
|
129
|
+
*
|
|
130
|
+
* @remarks
|
|
131
|
+
* Opaque meshes only, and it must run after {@link setThinInstances} and before the scene is
|
|
132
|
+
* registered.
|
|
133
|
+
*
|
|
134
|
+
* @param mesh - The instanced mesh.
|
|
135
|
+
* @param enabled - Whether the culling compute pass runs.
|
|
136
|
+
*
|
|
137
|
+
* @internal
|
|
138
|
+
*/
|
|
139
|
+
function enableThinInstanceGpuCulling$1(mesh, enabled) {
|
|
140
|
+
enableThinInstanceGpuCulling(mesh, enabled);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Makes a mesh's world bounds the union of its instances' boxes rather than the prototype's own.
|
|
144
|
+
*
|
|
145
|
+
* @remarks
|
|
146
|
+
* The hook it installs is `O(count)` per bounds computation, and it clamps to
|
|
147
|
+
* `matrices.length / 16`, so a short slab cannot walk off the end. It is read by
|
|
148
|
+
* `expandWorldAabbForMesh` — the scene camera's extent and PBR's scene-size probe — and **not** by
|
|
149
|
+
* the directional shadow fit; see the module note.
|
|
150
|
+
*
|
|
151
|
+
* @param mesh - The instanced mesh.
|
|
152
|
+
*
|
|
153
|
+
* @internal
|
|
154
|
+
*/
|
|
155
|
+
function enableThinInstanceWorldBounds$1(mesh) {
|
|
156
|
+
enableThinInstanceWorldBounds(mesh);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Opts a fixed-capacity pool into indirect draw arguments, so a later count change keeps the
|
|
160
|
+
* cached render bundle valid.
|
|
161
|
+
*
|
|
162
|
+
* @param mesh - The instanced mesh.
|
|
163
|
+
*
|
|
164
|
+
* @internal
|
|
165
|
+
*/
|
|
166
|
+
function enableThinInstanceDynamicDrawCount$1(mesh) {
|
|
167
|
+
enableThinInstanceDynamicDrawCount(mesh);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Pairs a GPU-culled instanced mesh with a lower-detail partner.
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* Both meshes must already carry thin instances, and the partner may reference the very same matrix
|
|
174
|
+
* slab. While paired the partner draws **only** the far bucket, and the pairing switches its own
|
|
175
|
+
* GPU culling on. Lite refuses the pairing when either mesh has more than one thin-instance owner,
|
|
176
|
+
* when the partner is already paired, or when the partner carries colours the full mesh does not
|
|
177
|
+
* (`ThrowLiteError(375)`, `(377)`, `(379)`). Calling it again with the same pair re-sets `distance`
|
|
178
|
+
* and `band` live.
|
|
179
|
+
*
|
|
180
|
+
* @param fullMesh - The near mesh, which owns the culling pass.
|
|
181
|
+
* @param lodMesh - The far mesh.
|
|
182
|
+
* @param options - Where the switch happens, and how softly.
|
|
183
|
+
*
|
|
184
|
+
* @internal
|
|
185
|
+
*/
|
|
186
|
+
function setThinInstanceLodPartner$1(fullMesh, lodMesh, options) {
|
|
187
|
+
setThinInstanceLodPartner(fullMesh, lodMesh, {
|
|
188
|
+
distance: options.distance,
|
|
189
|
+
band: options.band
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Dissolves a LOD pairing, returning both meshes to independent rendering.
|
|
194
|
+
*
|
|
195
|
+
* @remarks
|
|
196
|
+
* A no-op when the mesh is not paired, so it is safe on the teardown path.
|
|
197
|
+
*
|
|
198
|
+
* @param fullMesh - The near mesh of the pair.
|
|
199
|
+
*
|
|
200
|
+
* @internal
|
|
201
|
+
*/
|
|
202
|
+
function clearThinInstanceLodPartner$1(fullMesh) {
|
|
203
|
+
clearThinInstanceLodPartner(fullMesh);
|
|
204
|
+
}
|
|
205
|
+
//#endregion
|
|
206
|
+
//#region src/render/gpu/instanced-mesh-instance.ts
|
|
207
|
+
/**
|
|
208
|
+
* A live instanced mesh, and its LOD partner, in a scene.
|
|
209
|
+
*
|
|
210
|
+
* @internal
|
|
211
|
+
*/
|
|
212
|
+
var InstancedMeshInstance = class {
|
|
213
|
+
/** The cloned near mesh, parented under the entity's node. */
|
|
214
|
+
clone;
|
|
215
|
+
#lod;
|
|
216
|
+
#material;
|
|
217
|
+
#receiveShadows = null;
|
|
218
|
+
#renderOrder = NaN;
|
|
219
|
+
#pickable = null;
|
|
220
|
+
#visible = false;
|
|
221
|
+
#lodDistance;
|
|
222
|
+
#lodBand;
|
|
223
|
+
#nearBox;
|
|
224
|
+
#lodBox;
|
|
225
|
+
#union = /* @__PURE__ */ new Float64Array(6);
|
|
226
|
+
/**
|
|
227
|
+
* Clones the templates, gives them thin instances, applies the baked settings in Lite's required
|
|
228
|
+
* order, and adds them to the scene.
|
|
229
|
+
*
|
|
230
|
+
* @param scene - The render scene.
|
|
231
|
+
* @param template - The near mesh's template.
|
|
232
|
+
* @param parent - The entity's transform node.
|
|
233
|
+
* @param tag - The owning entity and component, for picking.
|
|
234
|
+
* @param meshId - The string id Lite's light include/exclude sets match on.
|
|
235
|
+
* @param material - The material both meshes draw with. Assigned **before** the meshes join the
|
|
236
|
+
* scene, for the reason `MeshInstance`'s constructor records: `addToScene` buckets a mesh by its
|
|
237
|
+
* material's build group.
|
|
238
|
+
* @param settings - Capacity, GPU culling, and the LOD pairing.
|
|
239
|
+
* @param matrices - The matrix slab, caller-owned and never copied.
|
|
240
|
+
* @param count - How many instances draw.
|
|
241
|
+
* @param colors - Four floats per instance, or `null`.
|
|
242
|
+
*/
|
|
243
|
+
constructor(scene, template, parent, tag, meshId, material, settings, matrices, count, colors) {
|
|
244
|
+
const clone = cloneMeshUnderNode(template, parent);
|
|
245
|
+
tagNode(clone, tag);
|
|
246
|
+
const mesh = asMesh(clone);
|
|
247
|
+
setMeshId(mesh, meshId);
|
|
248
|
+
setMeshMaterial(mesh, material);
|
|
249
|
+
detachMeshBounds(mesh);
|
|
250
|
+
this.#nearBox = readBox(mesh);
|
|
251
|
+
installThinInstances(mesh, settings.capacity, matrices, count, colors);
|
|
252
|
+
enableThinInstanceGpuCulling$1(mesh, settings.gpuCulling);
|
|
253
|
+
enableThinInstanceDynamicDrawCount$1(mesh);
|
|
254
|
+
const lodConfig = settings.lod;
|
|
255
|
+
let lodClone = null;
|
|
256
|
+
let lodBox = null;
|
|
257
|
+
if (lodConfig !== null) {
|
|
258
|
+
lodClone = cloneMeshUnderNode(lodConfig.template, parent);
|
|
259
|
+
tagNode(lodClone, tag);
|
|
260
|
+
const lodMesh = asMesh(lodClone);
|
|
261
|
+
setMeshId(lodMesh, `${meshId}#lod`);
|
|
262
|
+
setMeshMaterial(lodMesh, material);
|
|
263
|
+
detachMeshBounds(lodMesh);
|
|
264
|
+
lodBox = readBox(lodMesh);
|
|
265
|
+
installThinInstances(lodMesh, settings.capacity, matrices, count, colors);
|
|
266
|
+
setThinInstanceLodPartner$1(mesh, lodMesh, {
|
|
267
|
+
distance: lodConfig.distance,
|
|
268
|
+
band: lodConfig.band
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
this.#lodBox = lodBox;
|
|
272
|
+
this.#material = material;
|
|
273
|
+
this.#lodDistance = lodConfig?.distance ?? 0;
|
|
274
|
+
this.#lodBand = lodConfig?.band ?? 0;
|
|
275
|
+
setMeshSubtreeVisible(clone, false);
|
|
276
|
+
addMeshToScene(scene, clone);
|
|
277
|
+
if (lodClone !== null) {
|
|
278
|
+
setMeshSubtreeVisible(lodClone, false);
|
|
279
|
+
addMeshToScene(scene, lodClone);
|
|
280
|
+
}
|
|
281
|
+
this.clone = clone;
|
|
282
|
+
this.#lod = lodClone;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Whether the meshes are currently drawn.
|
|
286
|
+
*
|
|
287
|
+
* @returns `true` when Lite's `visible` is set on them.
|
|
288
|
+
*/
|
|
289
|
+
get isVisible() {
|
|
290
|
+
return this.#visible;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* The near mesh.
|
|
294
|
+
*
|
|
295
|
+
* @returns The clone, typed as a mesh.
|
|
296
|
+
*/
|
|
297
|
+
get mesh() {
|
|
298
|
+
return asMesh(this.clone);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* The LOD partner.
|
|
302
|
+
*
|
|
303
|
+
* @returns The partner clone, or `null` when the renderer declares no LOD.
|
|
304
|
+
*/
|
|
305
|
+
get lodMesh() {
|
|
306
|
+
const lod = this.#lod;
|
|
307
|
+
return lod === null ? null : asMesh(lod);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Assigns the material to both meshes, when it changed.
|
|
311
|
+
*
|
|
312
|
+
* @param material - The Lite material to draw with.
|
|
313
|
+
*/
|
|
314
|
+
applyMaterial(material) {
|
|
315
|
+
if (material === this.#material) return;
|
|
316
|
+
this.#material = material;
|
|
317
|
+
setMeshMaterial(this.mesh, material);
|
|
318
|
+
const lod = this.lodMesh;
|
|
319
|
+
if (lod !== null) setMeshMaterial(lod, material);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Writes the per-mesh flags that changed, to both meshes.
|
|
323
|
+
*
|
|
324
|
+
* @param receiveShadows - Whether shadow maps darken the meshes.
|
|
325
|
+
* @param renderOrder - The sort key within the phase.
|
|
326
|
+
* @param pickable - Whether picking considers the meshes at all.
|
|
327
|
+
*/
|
|
328
|
+
applyFlags(receiveShadows, renderOrder, pickable) {
|
|
329
|
+
if (this.#receiveShadows !== receiveShadows) {
|
|
330
|
+
this.#receiveShadows = receiveShadows;
|
|
331
|
+
this.#eachMesh(setMeshReceiveShadows, receiveShadows);
|
|
332
|
+
}
|
|
333
|
+
if (this.#renderOrder !== renderOrder) {
|
|
334
|
+
this.#renderOrder = renderOrder;
|
|
335
|
+
this.#eachMesh(setMeshRenderOrder, renderOrder);
|
|
336
|
+
}
|
|
337
|
+
if (this.#pickable !== pickable) {
|
|
338
|
+
this.#pickable = pickable;
|
|
339
|
+
this.#eachMesh(setMeshPickable, pickable);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Re-sets the LOD switch distance and dither band, when either changed.
|
|
344
|
+
*
|
|
345
|
+
* @param distance - The camera distance at which an instance switches to the partner.
|
|
346
|
+
* @param band - The width of the per-instance dither window centred on `distance`.
|
|
347
|
+
*/
|
|
348
|
+
applyLod(distance, band) {
|
|
349
|
+
const lod = this.lodMesh;
|
|
350
|
+
if (lod === null || distance === this.#lodDistance && band === this.#lodBand) return;
|
|
351
|
+
this.#lodDistance = distance;
|
|
352
|
+
this.#lodBand = band;
|
|
353
|
+
setThinInstanceLodPartner$1(this.mesh, lod, {
|
|
354
|
+
distance,
|
|
355
|
+
band
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Shows or hides both meshes, when the state changed.
|
|
360
|
+
*
|
|
361
|
+
* @param visible - `entity.activeInHierarchy && component.enabled`.
|
|
362
|
+
*/
|
|
363
|
+
applyVisible(visible) {
|
|
364
|
+
if (visible === this.#visible) return;
|
|
365
|
+
this.#visible = visible;
|
|
366
|
+
setMeshSubtreeVisible(this.clone, visible);
|
|
367
|
+
const lod = this.#lod;
|
|
368
|
+
if (lod !== null) setMeshSubtreeVisible(lod, visible);
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Re-points both meshes at a matrix slab and re-uploads the active range.
|
|
372
|
+
*
|
|
373
|
+
* @param capacity - The capacity the meshes were built with; re-stating it keeps `_capacity`
|
|
374
|
+
* unchanged, which is what stops Lite from re-creating the GPU buffer.
|
|
375
|
+
* @param matrices - The slab, caller-owned.
|
|
376
|
+
* @param count - How many instances draw.
|
|
377
|
+
* @param colors - Four floats per instance, or `null`.
|
|
378
|
+
*/
|
|
379
|
+
applySlab(capacity, matrices, count, colors) {
|
|
380
|
+
installThinInstances(this.mesh, capacity, matrices, count, colors);
|
|
381
|
+
const lod = this.lodMesh;
|
|
382
|
+
if (lod !== null) installThinInstances(lod, capacity, matrices, count, colors);
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Changes how many instances draw, by the cheapest route Lite accepts.
|
|
386
|
+
*
|
|
387
|
+
* @param count - The active count, at most the capacity.
|
|
388
|
+
*/
|
|
389
|
+
applyCount(count) {
|
|
390
|
+
applyMeshCount(this.mesh, count);
|
|
391
|
+
const lod = this.lodMesh;
|
|
392
|
+
if (lod !== null) applyMeshCount(lod, count);
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Marks the whole active range dirty, after the caller mutated the slab in place.
|
|
396
|
+
*
|
|
397
|
+
* @param colors - The colour slab, when the renderer has one, so its own dirty range moves too.
|
|
398
|
+
*/
|
|
399
|
+
flush(colors) {
|
|
400
|
+
flushThinInstances$1(this.mesh);
|
|
401
|
+
const lod = this.lodMesh;
|
|
402
|
+
if (lod !== null) flushThinInstances$1(lod);
|
|
403
|
+
if (colors !== null) {
|
|
404
|
+
setThinInstanceColors$1(this.mesh, colors);
|
|
405
|
+
if (lod !== null) setThinInstanceColors$1(lod, colors);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Publishes the union of the live instance placements as both meshes' object-local bounds, so a
|
|
410
|
+
* directional light's shadow frustum is fitted to the cloud rather than to the prototype.
|
|
411
|
+
*
|
|
412
|
+
* @remarks
|
|
413
|
+
* `O(count)`, allocation-free, and worth paying only for a caster — see the module note. A count
|
|
414
|
+
* of zero restores the prototype's own box, so an empty cloud does not report an infinite one.
|
|
415
|
+
*
|
|
416
|
+
* @param matrices - The live matrix slab.
|
|
417
|
+
* @param count - How many instances are drawn.
|
|
418
|
+
*/
|
|
419
|
+
applyBounds(matrices, count) {
|
|
420
|
+
const near = this.#nearBox;
|
|
421
|
+
const lodBox = this.#lodBox;
|
|
422
|
+
const lod = this.lodMesh;
|
|
423
|
+
if (count <= 0) {
|
|
424
|
+
writeBox(this.mesh, near);
|
|
425
|
+
if (lod !== null && lodBox !== null) writeBox(lod, lodBox);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const union = this.#union;
|
|
429
|
+
unionOfPlacements(matrices, count, near, union);
|
|
430
|
+
setMeshBounds(this.mesh, union[0] ?? 0, union[1] ?? 0, union[2] ?? 0, union[3] ?? 0, union[4] ?? 0, union[5] ?? 0);
|
|
431
|
+
if (lod !== null && lodBox !== null) {
|
|
432
|
+
unionOfPlacements(matrices, count, lodBox, union);
|
|
433
|
+
setMeshBounds(lod, union[0] ?? 0, union[1] ?? 0, union[2] ?? 0, union[3] ?? 0, union[4] ?? 0, union[5] ?? 0);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Writes everything the component changed since the last frame onto both meshes.
|
|
438
|
+
*
|
|
439
|
+
* @param state - The component's upload state, mutated in place.
|
|
440
|
+
* @param material - The material both meshes draw with.
|
|
441
|
+
* @param receiveShadows - Whether shadow maps darken them.
|
|
442
|
+
* @param renderOrder - The sort key within the phase.
|
|
443
|
+
* @param pickable - Whether picking considers them.
|
|
444
|
+
* @param lod - The LOD record, whose `distance` and `band` Lite re-applies live; `null` for none.
|
|
445
|
+
* @param matrices - The caller-owned matrix slab.
|
|
446
|
+
* @param count - How many instances draw.
|
|
447
|
+
* @param colors - Four floats per instance, or `null`.
|
|
448
|
+
* @param castShadows - Whether the union bounds have to be published for a shadow frustum.
|
|
449
|
+
* @param visible - `entity.activeInHierarchy && component.enabled`.
|
|
450
|
+
* @returns `true` when the compiled pipeline has to change, which only a colour slab arriving or
|
|
451
|
+
* leaving does.
|
|
452
|
+
*/
|
|
453
|
+
sync(state, material, receiveShadows, renderOrder, pickable, lod, matrices, count, colors, castShadows, visible) {
|
|
454
|
+
this.applyMaterial(material);
|
|
455
|
+
this.applyFlags(receiveShadows, renderOrder, pickable);
|
|
456
|
+
if (lod !== null) this.applyLod(lod.distance, lod.band);
|
|
457
|
+
let renderablesChanged = false;
|
|
458
|
+
let placementsMoved = false;
|
|
459
|
+
if (state.isSlabDirty) {
|
|
460
|
+
state.isSlabDirty = false;
|
|
461
|
+
state.isRangeDirty = false;
|
|
462
|
+
this.applySlab(state.appliedCapacity, matrices, count, colors);
|
|
463
|
+
state.appliedCount = count;
|
|
464
|
+
placementsMoved = true;
|
|
465
|
+
const hasColors = colors !== null;
|
|
466
|
+
renderablesChanged = hasColors !== state.hadColors;
|
|
467
|
+
state.hadColors = hasColors;
|
|
468
|
+
} else {
|
|
469
|
+
if (state.isRangeDirty) {
|
|
470
|
+
state.isRangeDirty = false;
|
|
471
|
+
this.flush(colors);
|
|
472
|
+
placementsMoved = true;
|
|
473
|
+
}
|
|
474
|
+
if (count !== state.appliedCount) {
|
|
475
|
+
state.appliedCount = count;
|
|
476
|
+
this.applyCount(count);
|
|
477
|
+
placementsMoved = true;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (castShadows && (placementsMoved || !state.hasAppliedBounds)) {
|
|
481
|
+
state.hasAppliedBounds = true;
|
|
482
|
+
this.applyBounds(matrices, count);
|
|
483
|
+
}
|
|
484
|
+
this.applyVisible(visible);
|
|
485
|
+
return renderablesChanged;
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Appends both meshes to a shadow caster list, when the renderer casts and they are drawn.
|
|
489
|
+
*
|
|
490
|
+
* @param castShadows - Whether the renderer casts.
|
|
491
|
+
* @param out - The caster list being built.
|
|
492
|
+
*/
|
|
493
|
+
collectCasters(castShadows, out) {
|
|
494
|
+
if (!castShadows || !this.#visible) return;
|
|
495
|
+
out.push(this.mesh);
|
|
496
|
+
const lod = this.lodMesh;
|
|
497
|
+
if (lod !== null) out.push(lod);
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Dissolves the LOD pairing and removes both meshes from the scene, releasing their share of the
|
|
501
|
+
* templates' buffers.
|
|
502
|
+
*
|
|
503
|
+
* @param scene - The render scene.
|
|
504
|
+
*/
|
|
505
|
+
destroy(scene) {
|
|
506
|
+
const lod = this.#lod;
|
|
507
|
+
if (lod !== null) {
|
|
508
|
+
clearThinInstanceLodPartner$1(this.mesh);
|
|
509
|
+
removeMeshFromScene(scene, lod);
|
|
510
|
+
}
|
|
511
|
+
removeMeshFromScene(scene, this.clone);
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Runs a per-mesh Lite setter over the near mesh and the partner.
|
|
515
|
+
*
|
|
516
|
+
* @typeParam T - The setter's value type.
|
|
517
|
+
* @param write - The adapter function to call.
|
|
518
|
+
* @param value - What to write.
|
|
519
|
+
*/
|
|
520
|
+
#eachMesh(write, value) {
|
|
521
|
+
write(this.mesh, value);
|
|
522
|
+
const lod = this.lodMesh;
|
|
523
|
+
if (lod !== null) write(lod, value);
|
|
524
|
+
}
|
|
525
|
+
};
|
|
526
|
+
/**
|
|
527
|
+
* Builds the live meshes, filling in the LOD pairing when both templates exist.
|
|
528
|
+
*
|
|
529
|
+
* @param scene - The render scene.
|
|
530
|
+
* @param template - The near mesh's template.
|
|
531
|
+
* @param lodTemplate - The coarse template, or `null`.
|
|
532
|
+
* @param lod - The LOD record the component declares, or `null`.
|
|
533
|
+
* @param parent - The entity's transform node.
|
|
534
|
+
* @param tag - The owning entity and component, for picking.
|
|
535
|
+
* @param meshId - The string id Lite's light include/exclude sets match on.
|
|
536
|
+
* @param material - The material both meshes draw with.
|
|
537
|
+
* @param capacity - The largest instance count the meshes will ever draw.
|
|
538
|
+
* @param gpuCulling - Whether the compute culling pass runs.
|
|
539
|
+
* @param matrices - The matrix slab.
|
|
540
|
+
* @param count - How many instances draw.
|
|
541
|
+
* @param colors - Four floats per instance, or `null`.
|
|
542
|
+
* @returns The instance.
|
|
543
|
+
*
|
|
544
|
+
* @internal
|
|
545
|
+
*/
|
|
546
|
+
function createInstancedMesh(scene, template, lodTemplate, lod, parent, tag, meshId, material, capacity, gpuCulling, matrices, count, colors) {
|
|
547
|
+
return new InstancedMeshInstance(scene, template, parent, tag, meshId, material, {
|
|
548
|
+
capacity,
|
|
549
|
+
gpuCulling,
|
|
550
|
+
lod: lodTemplate === null || lod === null ? null : {
|
|
551
|
+
template: lodTemplate,
|
|
552
|
+
distance: lod.distance,
|
|
553
|
+
band: lod.band
|
|
554
|
+
}
|
|
555
|
+
}, matrices, count, colors);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Removes the meshes from the scene, when there are any.
|
|
559
|
+
*
|
|
560
|
+
* @param instance - The live meshes, or `null`.
|
|
561
|
+
* @param scene - The render scene.
|
|
562
|
+
* @returns `true` when something left the scene.
|
|
563
|
+
*
|
|
564
|
+
* @internal
|
|
565
|
+
*/
|
|
566
|
+
function releaseInstancedMesh(instance, scene) {
|
|
567
|
+
if (instance === null) return false;
|
|
568
|
+
instance.destroy(scene);
|
|
569
|
+
return true;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Gives one mesh its thin instances in the order Lite requires, at a fixed capacity.
|
|
573
|
+
*
|
|
574
|
+
* @remarks
|
|
575
|
+
* `setThinInstances` is handed the **capacity** so that `_capacity` — and the GPU buffer it sizes —
|
|
576
|
+
* never moves; the active count is then lowered in the same breath, before any frame can read the
|
|
577
|
+
* range in between (`src/lite/gpu/thin-instances.ts`).
|
|
578
|
+
*
|
|
579
|
+
* @param mesh - The mesh to instance.
|
|
580
|
+
* @param capacity - The capacity to fix.
|
|
581
|
+
* @param matrices - The slab, caller-owned.
|
|
582
|
+
* @param count - How many instances draw.
|
|
583
|
+
* @param colors - Four floats per instance, or `null`.
|
|
584
|
+
*/
|
|
585
|
+
function installThinInstances(mesh, capacity, matrices, count, colors) {
|
|
586
|
+
setThinInstances$1(mesh, matrices, capacity);
|
|
587
|
+
setThinInstanceCount$1(mesh, count);
|
|
588
|
+
setThinInstanceColors$1(mesh, colors);
|
|
589
|
+
enableThinInstanceWorldBounds$1(mesh);
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Changes one mesh's active count without re-uploading when Lite allows it, and with a re-upload
|
|
593
|
+
* when it does not.
|
|
594
|
+
*
|
|
595
|
+
* @param mesh - The instanced mesh.
|
|
596
|
+
* @param count - The active count.
|
|
597
|
+
*/
|
|
598
|
+
function applyMeshCount(mesh, count) {
|
|
599
|
+
if (!trySetThinInstanceDrawCount(mesh, count)) setThinInstanceCount$1(mesh, count);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Reads a mesh's object-local box into six numbers.
|
|
603
|
+
*
|
|
604
|
+
* @remarks
|
|
605
|
+
* A mesh built by any Lite factory carries bounds; the fallback half-metre cube is the same default
|
|
606
|
+
* `computeDirectionalLightMatrix` uses for a mesh that does not
|
|
607
|
+
* (`lib/shadow/shadow-base.js` 62).
|
|
608
|
+
*
|
|
609
|
+
* @param mesh - The mesh to read.
|
|
610
|
+
* @returns `minX, minY, minZ, maxX, maxY, maxZ`.
|
|
611
|
+
*/
|
|
612
|
+
function readBox(mesh) {
|
|
613
|
+
const min = mesh.boundMin ?? [
|
|
614
|
+
-.5,
|
|
615
|
+
-.5,
|
|
616
|
+
-.5
|
|
617
|
+
];
|
|
618
|
+
const max = mesh.boundMax ?? [
|
|
619
|
+
.5,
|
|
620
|
+
.5,
|
|
621
|
+
.5
|
|
622
|
+
];
|
|
623
|
+
return Float64Array.of(min[0], min[1], min[2], max[0], max[1], max[2]);
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Writes a six-number box back onto a mesh.
|
|
627
|
+
*
|
|
628
|
+
* @param mesh - The mesh to re-bound.
|
|
629
|
+
* @param box - `minX, minY, minZ, maxX, maxY, maxZ`.
|
|
630
|
+
*/
|
|
631
|
+
function writeBox(mesh, box) {
|
|
632
|
+
setMeshBounds(mesh, box[0] ?? 0, box[1] ?? 0, box[2] ?? 0, box[3] ?? 0, box[4] ?? 0, box[5] ?? 0);
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* The axis-aligned union of a prototype box placed under every live instance matrix.
|
|
636
|
+
*
|
|
637
|
+
* @remarks
|
|
638
|
+
* The same oriented-box-to-AABB composition Lite's own
|
|
639
|
+
* `expandThinInstanceWorldBounds` uses, minus the world matrix — because `Mesh.boundMin`/`boundMax`
|
|
640
|
+
* are documented to be **object-local**, the frame `worldMatrix` maps out of. Degenerate matrices
|
|
641
|
+
* are skipped, exactly as Lite skips them, so a slot that has not been written yet does not drag
|
|
642
|
+
* the box to the origin.
|
|
643
|
+
*
|
|
644
|
+
* @param matrices - The slab, 16 column-major floats per instance.
|
|
645
|
+
* @param count - How many instances to consider.
|
|
646
|
+
* @param box - The prototype's own box.
|
|
647
|
+
* @param out - Receives `minX, minY, minZ, maxX, maxY, maxZ`.
|
|
648
|
+
*/
|
|
649
|
+
function unionOfPlacements(matrices, count, box, out) {
|
|
650
|
+
const centreX = ((box[0] ?? 0) + (box[3] ?? 0)) * .5;
|
|
651
|
+
const centreY = ((box[1] ?? 0) + (box[4] ?? 0)) * .5;
|
|
652
|
+
const centreZ = ((box[2] ?? 0) + (box[5] ?? 0)) * .5;
|
|
653
|
+
const extentX = ((box[3] ?? 0) - (box[0] ?? 0)) * .5;
|
|
654
|
+
const extentY = ((box[4] ?? 0) - (box[1] ?? 0)) * .5;
|
|
655
|
+
const extentZ = ((box[5] ?? 0) - (box[2] ?? 0)) * .5;
|
|
656
|
+
out[0] = Number.POSITIVE_INFINITY;
|
|
657
|
+
out[1] = Number.POSITIVE_INFINITY;
|
|
658
|
+
out[2] = Number.POSITIVE_INFINITY;
|
|
659
|
+
out[3] = Number.NEGATIVE_INFINITY;
|
|
660
|
+
out[4] = Number.NEGATIVE_INFINITY;
|
|
661
|
+
out[5] = Number.NEGATIVE_INFINITY;
|
|
662
|
+
const usable = Math.min(count, Math.floor(matrices.length / 16));
|
|
663
|
+
let placed = 0;
|
|
664
|
+
for (let index = 0; index < usable; index += 1) {
|
|
665
|
+
const base = index * 16;
|
|
666
|
+
let scale = 0;
|
|
667
|
+
for (let element = 0; element < 12; element += 1) if (element % 4 !== 3) scale += Math.abs(matrices[base + element] ?? 0);
|
|
668
|
+
if (scale < 1e-9) continue;
|
|
669
|
+
placed += 1;
|
|
670
|
+
for (let row = 0; row < 3; row += 1) {
|
|
671
|
+
const m0 = matrices[base + row] ?? 0;
|
|
672
|
+
const m1 = matrices[base + 4 + row] ?? 0;
|
|
673
|
+
const m2 = matrices[base + 8 + row] ?? 0;
|
|
674
|
+
const centre = (matrices[base + 12 + row] ?? 0) + m0 * centreX + m1 * centreY + m2 * centreZ;
|
|
675
|
+
const radius = Math.abs(m0) * extentX + Math.abs(m1) * extentY + Math.abs(m2) * extentZ;
|
|
676
|
+
const low = centre - radius;
|
|
677
|
+
const high = centre + radius;
|
|
678
|
+
if (low < (out[row] ?? 0)) out[row] = low;
|
|
679
|
+
if (high > (out[row + 3] ?? 0)) out[row + 3] = high;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
if (placed === 0) out.set(box);
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Narrows a cloned subtree root to a mesh.
|
|
686
|
+
*
|
|
687
|
+
* @remarks
|
|
688
|
+
* The same boundary assertion `MeshInstance` makes, for the same reason: `cloneTransformNode` is
|
|
689
|
+
* typed as returning the wider `SceneNode` because it also clones plain transform nodes, and the
|
|
690
|
+
* clone of a mesh is a mesh (coding standards §5.2).
|
|
691
|
+
*
|
|
692
|
+
* @param node - The clone.
|
|
693
|
+
* @returns The same object, typed as a mesh.
|
|
694
|
+
*/
|
|
695
|
+
function asMesh(node) {
|
|
696
|
+
return node;
|
|
697
|
+
}
|
|
698
|
+
//#endregion
|
|
699
|
+
export { InstancedMeshInstance, createInstancedMesh, releaseInstancedMesh };
|
|
700
|
+
|
|
701
|
+
//# sourceMappingURL=instanced-mesh-instance-Cgx1H87p.js.map
|