@nebula-spatial/viewer 0.5.18 → 0.6.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +63 -35
  2. package/dist/asset-ir-BEDGD9CL.js +637 -0
  3. package/dist/assets/source-types.d.ts +10 -0
  4. package/dist/assets/types.d.ts +59 -6
  5. package/dist/index-DoxM1Msk.js +17223 -0
  6. package/dist/index.d.ts +20 -1
  7. package/dist/index.js +29 -18
  8. package/dist/{mjcf-visual-CtY2Wk3w.js → mjcf-visual-CQEeWkL_.js} +12 -13
  9. package/dist/mujoco-runtime-BmuEJ_ST.js +791 -0
  10. package/dist/physics-guide-size-CplAlsve.js +12 -0
  11. package/dist/scene/instances.d.ts +36 -0
  12. package/dist/scene/joints.d.ts +129 -0
  13. package/dist/scene/session.d.ts +26 -0
  14. package/dist/scene/types.d.ts +284 -0
  15. package/dist/session-BnNntPUE.js +9169 -0
  16. package/dist/{session-DxTeRKCd.js → session-Cz4tFSlO.js} +6 -8
  17. package/dist/session-sBOJEWpK.js +559 -0
  18. package/dist/simulation/physics-runtime-state.d.ts +73 -0
  19. package/dist/simulation/types.d.ts +151 -0
  20. package/dist/simulation/usd-dependency-probe.d.ts +42 -0
  21. package/dist/simulation/usd-inspection.d.ts +66 -0
  22. package/dist/types.d.ts +46 -1
  23. package/dist/ui/camera-axis-gizmo.d.ts +5 -0
  24. package/dist/urdf-session-HKelZnMM.js +1230 -0
  25. package/dist/{usd-session-tW3LxVFs.js → usd-session-Cn6whyh4.js} +3915 -3687
  26. package/dist/version.d.ts +1 -1
  27. package/dist/visual/classification.d.ts +20 -0
  28. package/dist/{xacro-x2TeKQ7T.js → xacro-Cl7vUCSC.js} +1 -1
  29. package/package.json +2 -1
  30. package/runtime-assets.d.mts +9 -9
  31. package/runtime-assets.mjs +11 -11
  32. package/vite.d.mts +7 -7
  33. package/vite.mjs +115 -115
  34. package/dist/collision-debug-BSJoz5J7.js +0 -2947
  35. package/dist/index-E8DHSeyT.js +0 -11499
  36. package/dist/mujoco-runtime-fDWdXNEc.js +0 -739
  37. package/dist/resources-CHhCm2f7.js +0 -27
  38. package/dist/session-DB-osaBL.js +0 -801
  39. package/dist/stage-BnRjc-Rp.js +0 -592
  40. package/dist/urdf-session-Q_c3Rd1M.js +0 -1264
@@ -1,592 +0,0 @@
1
- var R = Object.defineProperty;
2
- var S = (a, t, e) => t in a ? R(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
- var s = (a, t, e) => S(a, typeof t != "symbol" ? t + "" : t, e);
4
- import * as i from "three";
5
- import { Quaternion as P, Vector3 as V } from "three";
6
- import { V as b, D as A, y as W } from "./index-E8DHSeyT.js";
7
- import { disposeModelResources as T } from "./resources-CHhCm2f7.js";
8
- const v = 1, B = (
9
- /* glsl */
10
- `
11
- uniform mat4 textureMatrix;
12
- varying vec4 vReflectionUv;
13
- varying vec3 vWorldPosition;
14
-
15
- void main() {
16
- vec4 worldPosition = modelMatrix * vec4(position, 1.0);
17
- vWorldPosition = worldPosition.xyz;
18
- vReflectionUv = textureMatrix * vec4(position, 1.0);
19
- // Match Three.js built-in materials: transform to view space before projection.
20
- vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
21
- gl_Position = projectionMatrix * mvPosition;
22
- }
23
- `
24
- ), L = (
25
- /* glsl */
26
- `
27
- uniform sampler2D tDiffuse;
28
- uniform vec2 resolution;
29
- uniform vec3 fineGridColor;
30
- uniform vec3 coarseGridColor;
31
- uniform float blurIntensity;
32
- uniform float reflectionMix;
33
- uniform float gridOpacity;
34
- uniform float gridScale;
35
- uniform vec2 groundCenter;
36
- uniform float maxRadius;
37
- uniform float fadeSoftness;
38
-
39
- varying vec4 vReflectionUv;
40
- varying vec3 vWorldPosition;
41
-
42
- vec4 blur9(sampler2D image, vec2 uv, vec2 res, float radius) {
43
- vec4 color = vec4(0.0);
44
- vec2 stepSize = radius / res;
45
- color += texture2D(image, uv + vec2(-stepSize.x, -stepSize.y)) * 0.0625;
46
- color += texture2D(image, uv + vec2(0.0, -stepSize.y)) * 0.125;
47
- color += texture2D(image, uv + vec2(stepSize.x, -stepSize.y)) * 0.0625;
48
- color += texture2D(image, uv + vec2(-stepSize.x, 0.0)) * 0.125;
49
- color += texture2D(image, uv) * 0.25;
50
- color += texture2D(image, uv + vec2(stepSize.x, 0.0)) * 0.125;
51
- color += texture2D(image, uv + vec2(-stepSize.x, stepSize.y)) * 0.0625;
52
- color += texture2D(image, uv + vec2(0.0, stepSize.y)) * 0.125;
53
- color += texture2D(image, uv + vec2(stepSize.x, stepSize.y)) * 0.0625;
54
- return color;
55
- }
56
-
57
- float gridLine(vec2 position, float scale) {
58
- vec2 coordinate = position / scale;
59
- vec2 width = max(fwidth(coordinate), vec2(0.0001));
60
- vec2 distanceToLine = abs(fract(coordinate - 0.5) - 0.5) / width;
61
- return 1.0 - min(min(distanceToLine.x, distanceToLine.y), 1.0);
62
- }
63
-
64
- void main() {
65
- vec2 groundPosition = vWorldPosition.xy;
66
- float edgeDistance = length(groundPosition - groundCenter);
67
- float edgeFade = 1.0 - smoothstep(maxRadius - fadeSoftness, maxRadius, edgeDistance);
68
-
69
- float fineLine = gridLine(groundPosition, gridScale);
70
- float coarseLine = gridLine(groundPosition, gridScale * 10.0);
71
- float gridAlpha = max(fineLine * 0.16, coarseLine * 0.42) * gridOpacity * edgeFade;
72
- vec3 gridColor = mix(fineGridColor, coarseGridColor, coarseLine);
73
-
74
- vec2 reflectionUv = vReflectionUv.xy / vReflectionUv.w;
75
- bool validReflection = reflectionUv.x >= 0.0 && reflectionUv.x <= 1.0
76
- && reflectionUv.y >= 0.0 && reflectionUv.y <= 1.0;
77
- vec4 reflection = validReflection
78
- ? blur9(tDiffuse, reflectionUv, resolution, blurIntensity)
79
- : vec4(0.0);
80
- float reflectionAlpha = reflectionMix * edgeFade * (validReflection ? 1.0 : 0.0);
81
-
82
- float alpha = reflectionAlpha + gridAlpha * (1.0 - reflectionAlpha);
83
- if (alpha <= 0.001) discard;
84
- vec3 color = mix(reflection.rgb, gridColor, gridAlpha / max(alpha, 0.001));
85
- gl_FragColor = vec4(color, alpha);
86
- #include <tonemapping_fragment>
87
- #include <colorspace_fragment>
88
- }
89
- `
90
- );
91
- class O extends i.Mesh {
92
- constructor({ size: e = 20, textureWidth: r = 512, textureHeight: o = 512 } = {}) {
93
- const n = new i.Matrix4(), l = new i.WebGLRenderTarget(r, o, {
94
- type: i.HalfFloatType,
95
- minFilter: i.LinearFilter,
96
- magFilter: i.LinearFilter,
97
- format: i.RGBAFormat,
98
- depthBuffer: !0,
99
- samples: 0
100
- }), u = new i.ShaderMaterial({
101
- uniforms: {
102
- tDiffuse: { value: l.texture },
103
- textureMatrix: { value: n },
104
- resolution: { value: new i.Vector2(r, o) },
105
- fineGridColor: { value: new i.Color(2502702) },
106
- coarseGridColor: { value: new i.Color(7897726) },
107
- blurIntensity: { value: 1.7 },
108
- reflectionMix: { value: 0.07 },
109
- gridOpacity: { value: 1 },
110
- gridScale: { value: 0.2 },
111
- groundCenter: { value: new i.Vector2() },
112
- maxRadius: { value: e * 0.5 },
113
- fadeSoftness: { value: e * 0.325 }
114
- },
115
- vertexShader: B,
116
- fragmentShader: L,
117
- transparent: !0,
118
- depthWrite: !1,
119
- toneMapped: !0,
120
- fog: !1
121
- });
122
- super(new i.PlaneGeometry(e, e), u);
123
- s(this, "renderTarget");
124
- s(this, "textureMatrix");
125
- s(this, "virtualCamera");
126
- s(this, "clipBias");
127
- s(this, "reflectorPlane");
128
- s(this, "normal");
129
- s(this, "reflectorWorldPosition");
130
- s(this, "cameraWorldPosition");
131
- s(this, "rotationMatrix");
132
- s(this, "lookAtPosition");
133
- s(this, "clipPlane");
134
- s(this, "view");
135
- s(this, "target");
136
- s(this, "q");
137
- this.renderTarget = l, this.textureMatrix = n, this.virtualCamera = new i.PerspectiveCamera(), this.virtualCamera.layers.set(v), this.clipBias = 3e-3, this.frustumCulled = !1, this.renderOrder = 1, this.position.z = 2e-3, this.reflectorPlane = new i.Plane(), this.normal = new i.Vector3(), this.reflectorWorldPosition = new i.Vector3(), this.cameraWorldPosition = new i.Vector3(), this.rotationMatrix = new i.Matrix4(), this.lookAtPosition = new i.Vector3(0, 0, -1), this.clipPlane = new i.Vector4(), this.view = new i.Vector3(), this.target = new i.Vector3(), this.q = new i.Vector4();
138
- }
139
- renderReflection(e, r, o) {
140
- if (this.reflectorWorldPosition.setFromMatrixPosition(this.matrixWorld), this.cameraWorldPosition.setFromMatrixPosition(o.matrixWorld), this.rotationMatrix.extractRotation(this.matrixWorld), this.normal.set(0, 0, 1).applyMatrix4(this.rotationMatrix), this.view.subVectors(this.reflectorWorldPosition, this.cameraWorldPosition), this.view.dot(this.normal) > 0) return;
141
- this.view.reflect(this.normal).negate().add(this.reflectorWorldPosition), this.rotationMatrix.extractRotation(o.matrixWorld), this.lookAtPosition.set(0, 0, -1).applyMatrix4(this.rotationMatrix).add(this.cameraWorldPosition), this.target.subVectors(this.reflectorWorldPosition, this.lookAtPosition).reflect(this.normal).negate().add(this.reflectorWorldPosition), this.virtualCamera.position.copy(this.view), this.virtualCamera.up.set(0, 1, 0).applyMatrix4(this.rotationMatrix).reflect(this.normal), this.virtualCamera.lookAt(this.target), this.virtualCamera.near = o.near, this.virtualCamera.far = o.far, this.virtualCamera.updateMatrixWorld(), this.virtualCamera.projectionMatrix.copy(o.projectionMatrix), this.textureMatrix.set(
142
- 0.5,
143
- 0,
144
- 0,
145
- 0.5,
146
- 0,
147
- 0.5,
148
- 0,
149
- 0.5,
150
- 0,
151
- 0,
152
- 0.5,
153
- 0.5,
154
- 0,
155
- 0,
156
- 0,
157
- 1
158
- ).multiply(this.virtualCamera.projectionMatrix).multiply(this.virtualCamera.matrixWorldInverse).multiply(this.matrixWorld), this.reflectorPlane.setFromNormalAndCoplanarPoint(this.normal, this.reflectorWorldPosition).applyMatrix4(this.virtualCamera.matrixWorldInverse), this.clipPlane.set(
159
- this.reflectorPlane.normal.x,
160
- this.reflectorPlane.normal.y,
161
- this.reflectorPlane.normal.z,
162
- this.reflectorPlane.constant
163
- );
164
- const n = this.virtualCamera.projectionMatrix;
165
- this.q.set(
166
- (Math.sign(this.clipPlane.x) + n.elements[8]) / n.elements[0],
167
- (Math.sign(this.clipPlane.y) + n.elements[9]) / n.elements[5],
168
- -1,
169
- (1 + n.elements[10]) / n.elements[14]
170
- ), this.clipPlane.multiplyScalar(2 / this.clipPlane.dot(this.q)), n.elements[2] = this.clipPlane.x, n.elements[6] = this.clipPlane.y, n.elements[10] = this.clipPlane.z + 1 - this.clipBias, n.elements[14] = this.clipPlane.w, this.virtualCamera.projectionMatrixInverse.copy(n).invert();
171
- const l = e.getRenderTarget(), u = e.xr.enabled, d = e.shadowMap.autoUpdate, c = e.shadowMap.enabled;
172
- try {
173
- e.xr.enabled = !1, e.shadowMap.enabled = !1, e.shadowMap.autoUpdate = !1, e.setRenderTarget(this.renderTarget), e.state.buffers.depth.setMask(!0), e.clear(), e.render(r, this.virtualCamera);
174
- } finally {
175
- e.setRenderTarget(l), e.xr.enabled = u, e.shadowMap.enabled = c, e.shadowMap.autoUpdate = d;
176
- }
177
- }
178
- setRenderSize(e, r) {
179
- this.renderTarget.setSize(e, r), this.material.uniforms.resolution.value.set(e, r);
180
- }
181
- // Radius is in world units; the outer 65% fades before the square mesh edge.
182
- setRadius(e, r, o = this.material.uniforms.groundCenter.value) {
183
- this.scale.setScalar(e * 2 / this.geometry.parameters.width), this.position.x = o.x, this.position.y = o.y, this.material.uniforms.groundCenter.value.set(o.x, o.y), this.material.uniforms.gridScale.value = r, this.material.uniforms.maxRadius.value = e, this.material.uniforms.fadeSoftness.value = e * 0.65;
184
- }
185
- }
186
- const G = {
187
- light: {
188
- background: b.light,
189
- fineGrid: 5859168,
190
- coarseGrid: 3427136,
191
- gridOpacity: 0.85,
192
- reflectionMix: 0.06
193
- },
194
- dark: {
195
- background: b.dark,
196
- fineGrid: 7375236,
197
- coarseGrid: 5552554,
198
- gridOpacity: 0.9,
199
- reflectionMix: 0.06
200
- }
201
- }, E = {
202
- simulation: 1.2,
203
- model: 1.2
204
- }, I = new P().setFromAxisAngle(new V(1, 0, 0), Math.PI / 2), D = new P();
205
- function tt(a) {
206
- return a === "glb" || a === "gltf" || a === "fbx" ? "y" : "z";
207
- }
208
- function F(a) {
209
- return a === "y" ? I : D;
210
- }
211
- function et(a, t) {
212
- const e = F(t);
213
- return e.equals(a.quaternion) ? !1 : (a.quaternion.copy(e), a.updateMatrixWorld(!0), !0);
214
- }
215
- const m = 2213822, _ = 6000111, z = 6, x = 2e-3, U = 1e-4, N = 1e-4, k = 0.18, H = 0.35, q = 0.18, j = 0.75, Y = Math.PI / 3, y = new i.Vector3(), M = new i.Matrix4(), C = new i.Matrix4();
216
- function Q(a, t, e) {
217
- t.updateMatrixWorld();
218
- const r = new i.Vector3();
219
- return t.getWorldDirection(r).normalize(), a.setFromNormalAndCoplanarPoint(r, e);
220
- }
221
- function p(a) {
222
- a.updateWorldMatrix(!0, !0);
223
- const t = new i.Box3();
224
- return a.traverse((e) => {
225
- if (!(e instanceof i.Mesh)) return;
226
- let r = e;
227
- for (; r; ) {
228
- if (r.userData.excludeFromSimulationFit || !r.visible) return;
229
- r = r.parent;
230
- }
231
- X(t, e);
232
- }), t.isEmpty() ? new i.Box3().setFromObject(a) : t;
233
- }
234
- function X(a, t) {
235
- const e = t.geometry.getAttribute("position");
236
- if (!e) {
237
- t.geometry.boundingBox || t.geometry.computeBoundingBox(), t.geometry.boundingBox && a.union(t.geometry.boundingBox.clone().applyMatrix4(t.matrixWorld));
238
- return;
239
- }
240
- if (t instanceof i.InstancedMesh) {
241
- for (let r = 0; r < t.count; r += 1)
242
- t.getMatrixAt(r, M), C.multiplyMatrices(t.matrixWorld, M), w(a, t, e.count, C);
243
- return;
244
- }
245
- w(a, t, e.count, t.matrixWorld);
246
- }
247
- function w(a, t, e, r) {
248
- for (let o = 0; o < e; o += 1)
249
- t.getVertexPosition(o, y), y.applyMatrix4(r), a.expandByPoint(y);
250
- }
251
- class rt {
252
- constructor(t, e = "dark", r, o = "simulation", n) {
253
- s(this, "root", new i.Group());
254
- /**
255
- * Owns the up-axis rotation for everything that is *not* driven by the
256
- * physics runtime: the URDF kinematic preview, the static collision overlay
257
- * and the review guides. When a runtime is attached the rotation moves into
258
- * the compiled model instead (see `applyUpAxis`), because rotating the scene
259
- * graph on top of an already-rotated physics pose would apply it twice.
260
- */
261
- s(this, "modelWrapper", new i.Group());
262
- s(this, "upAxis", "z");
263
- /** A flip changes the asset footprint; the ground disc follows on the next frame. */
264
- s(this, "groundNeedsRefit", !1);
265
- s(this, "reflector", new O({ size: 100, textureWidth: 256, textureHeight: 256 }));
266
- s(this, "values");
267
- s(this, "fog");
268
- s(this, "raycaster", new i.Raycaster());
269
- s(this, "pointer", new i.Vector2());
270
- s(this, "dragPlane", new i.Plane());
271
- s(this, "dragTarget", new i.Vector3());
272
- s(this, "dragIntersection", new i.Vector3());
273
- s(this, "dragOffset", new i.Vector3());
274
- s(this, "groundPlane", new i.Plane(new i.Vector3(0, 0, 1), 0));
275
- s(this, "groundRaycaster", new i.Raycaster());
276
- s(this, "groundNdc", new i.Vector2());
277
- s(this, "groundHit", new i.Vector3());
278
- s(this, "groundGrabPoint", new i.Vector3());
279
- s(this, "groundCenter", new i.Vector2());
280
- s(this, "groundModelFocus", new i.Vector2());
281
- s(this, "groundCameraFocus", new i.Vector2());
282
- s(this, "groundGrabFocus", new i.Vector2());
283
- s(this, "groundCameraPosition", new i.Vector3());
284
- s(this, "groundCameraQuaternion", new i.Quaternion());
285
- s(this, "bodyGroups", /* @__PURE__ */ new Map());
286
- s(this, "auxiliaries", /* @__PURE__ */ new Set());
287
- s(this, "forceLine");
288
- s(this, "forceBoosted", !1);
289
- s(this, "grabCanvas", null);
290
- s(this, "previousCursor", "");
291
- s(this, "size", "");
292
- s(this, "groundVisible");
293
- s(this, "groundGridScale", A);
294
- s(this, "groundPlacement");
295
- s(this, "groundFollowDirty", !0);
296
- s(this, "groundInteractionActive", !1);
297
- s(this, "groundGrabFocusValid", !1);
298
- s(this, "groundGrabBlend", 0);
299
- s(this, "groundCameraPoseValid", !1);
300
- s(this, "disposed", !1);
301
- s(this, "invalidationCallback");
302
- this.model = t, this.runtime = r, this.presentation = o, this.groundPlacement = n ?? (o === "model" ? "visual" : "physics"), this.values = G[e];
303
- const l = this.values, u = p(t), d = Math.max(...u.getSize(new i.Vector3()).toArray());
304
- if (!Number.isFinite(d) || d <= 0) throw new Error("Empty simulation geometry.");
305
- this.modelWrapper.name = "simulation-model-wrapper", this.root.add(this.modelWrapper), this.modelWrapper.add(t), this.forceLine = this.createForceLine(), this.root.add(this.forceLine), t.traverse((g) => {
306
- g instanceof i.Mesh && g.layers.enable(v);
307
- }), this.reflector.userData.hdrTransparentBackdrop = !1, this.root.add(this.reflector);
308
- const c = Math.max(u.getSize(new i.Vector3()).length() * 0.5, 0.02), h = this.reflector.material.uniforms;
309
- h.fineGridColor.value.setHex(l.fineGrid), h.coarseGridColor.value.setHex(l.coarseGrid), h.gridOpacity.value = l.gridOpacity, h.reflectionMix.value = l.reflectionMix, this.layoutGround(u, c), this.groundVisible = !0, this.setGroundVisible(this.groundVisible);
310
- const f = c / Math.sin(i.MathUtils.degToRad(38) / 2) * 1.25;
311
- this.fog = new i.Fog(l.background, f * 2.5, f * 8);
312
- }
313
- setRuntime(t) {
314
- this.disposed || this.runtime === t || (this.endGrab(), this.runtime = t, t && (this.groundPlacement = "physics"), this.bodyGroups.clear(), this.groundGrabFocusValid = !1, this.groundGrabBlend = 0, this.groundFollowDirty = !0, this.applyUpAxis());
315
- }
316
- setCacheInvalidationCallback(t) {
317
- this.invalidationCallback = t;
318
- }
319
- replaceModel(t, e = !0) {
320
- var r;
321
- this.disposed || t === this.model || (this.endGrab(), this.bodyGroups.clear(), this.model.removeFromParent(), this.model = t, this.modelWrapper.add(t), t.traverse((o) => {
322
- o instanceof i.Mesh && o.layers.enable(v);
323
- }), e && this.reseatGround(), (r = this.invalidationCallback) == null || r.call(this));
324
- }
325
- /**
326
- * Re-orients the scene to the given authored up axis. The rotation is a real
327
- * physics transform whenever a runtime is attached: the compiled model is
328
- * rotated about the origin so collisions, contacts and the force line follow,
329
- * while gravity stays -Z and the viewer ground is re-seated under the new pose
330
- * — a cup that was standing topples and settles on its side instead of only
331
- * its mesh flipping. Without a runtime (kinematic preview, physics fallback)
332
- * the wrapper carries the rotation, exactly like the model preview path.
333
- * Returns whether the orientation actually changed.
334
- */
335
- setUpAxis(t) {
336
- return this.disposed || t === this.upAxis ? !1 : (this.upAxis = t, this.applyUpAxis(), !0);
337
- }
338
- getUpAxis() {
339
- return this.upAxis;
340
- }
341
- applyUpAxis() {
342
- const t = F(this.upAxis);
343
- this.runtime ? (this.modelWrapper.quaternion.identity(), this.runtime.setWorldRotation(t)) : this.modelWrapper.quaternion.copy(t), this.modelWrapper.updateMatrixWorld(!0), this.groundNeedsRefit = !0;
344
- }
345
- resetGround() {
346
- var e;
347
- if (this.disposed) return;
348
- this.groundInteractionActive = !1, this.groundGrabFocusValid = !1, this.groundGrabBlend = 0, this.root.updateMatrixWorld(!0);
349
- const t = p(this.model);
350
- t.isEmpty() ? this.reflector.position.z = (((e = this.runtime) == null ? void 0 : e.defaultGroundHeight) ?? 0) + x : (this.setGroundModelFocus(t), this.groundFollowDirty = !0, this.positionGround(t));
351
- }
352
- setGroundPlacement(t) {
353
- this.disposed || t === this.groundPlacement || (this.groundPlacement = t, this.reseatGround());
354
- }
355
- setTheme(t) {
356
- if (this.disposed) return;
357
- const e = this.values = G[t], r = this.reflector.material.uniforms;
358
- r.fineGridColor.value.setHex(e.fineGrid), r.coarseGridColor.value.setHex(e.coarseGrid), r.gridOpacity.value = e.gridOpacity, r.reflectionMix.value = e.reflectionMix, this.fog.color.setHex(e.background);
359
- }
360
- layoutGround(t, e) {
361
- this.groundGridScale = W(t.getSize(new i.Vector3()).length()), this.setGroundModelFocus(t), this.groundCenter.copy(this.groundModelFocus), this.groundInteractionActive = !1, this.groundGrabFocusValid = !1, this.groundGrabBlend = 0, this.groundFollowDirty = !0, this.reflector.setRadius(e * 8, this.groundGridScale, this.groundCenter), this.positionGround(t);
362
- }
363
- setGroundModelFocus(t) {
364
- const e = t.getCenter(new i.Vector3());
365
- this.groundModelFocus.set(e.x, e.y);
366
- }
367
- /**
368
- * Physics-owned defaults keep their contact-derived height. Visual-only
369
- * sessions instead sit the grid immediately below the visible geometry.
370
- */
371
- positionGround(t) {
372
- var n;
373
- const e = (n = this.runtime) == null ? void 0 : n.defaultGroundHeight;
374
- if (e !== void 0) {
375
- this.reflector.position.z = e + x;
376
- return;
377
- }
378
- if (this.groundPlacement === "physics") {
379
- this.reflector.position.z = x;
380
- return;
381
- }
382
- const r = t.getSize(new i.Vector3()).length(), o = Math.max(r * U, N);
383
- this.reflector.position.z = t.min.z - o;
384
- }
385
- /**
386
- * The ground keeps its model-sized fade radius; only this local pool follows
387
- * the camera focus or an active grab. World-space grid lines therefore remain
388
- * stationary while the illuminated patch moves like a handheld light.
389
- */
390
- refreshGroundFocus(t) {
391
- const e = this.reflector.material.uniforms.maxRadius.value, r = this.detectGroundCameraJump(t, e), o = this.groundInteractionActive && this.measureGrabbedBodyFocus(), n = this.measureCameraGroundFocus(t, this.reflector.position.z);
392
- n && (this.limitCameraGroundFocus(e), this.groundCameraFocus.set(this.groundHit.x, this.groundHit.y));
393
- const l = this.groundInteractionActive && this.groundGrabFocusValid ? 1 : 0;
394
- if (this.groundGrabBlend += (l - this.groundGrabBlend) * q, Math.abs(l - this.groundGrabBlend) < 1e-3 && (this.groundGrabBlend = l), this.groundFollowDirty) {
395
- o ? this.groundCenter.copy(this.groundGrabFocus) : this.groundCenter.copy(this.groundModelFocus), this.reflector.setRadius(e, this.groundGridScale, this.groundCenter), this.reflector.updateMatrixWorld(!0), this.groundFollowDirty = !1;
396
- return;
397
- }
398
- let u = n ? this.groundCameraFocus.x : this.groundCenter.x, d = n ? this.groundCameraFocus.y : this.groundCenter.y;
399
- o && this.groundModelFocus.copy(this.groundGrabFocus), this.groundGrabFocusValid && this.groundGrabBlend > 0 && (u += (this.groundGrabFocus.x - u) * this.groundGrabBlend, d += (this.groundGrabFocus.y - d) * this.groundGrabBlend);
400
- const c = u - this.groundCenter.x, h = d - this.groundCenter.y, f = Math.hypot(c, h);
401
- if (!(!r && f <= Math.max(e * 5e-4, 1e-5))) {
402
- if (r)
403
- this.groundCenter.set(u, d);
404
- else {
405
- const g = this.groundInteractionActive ? H : k;
406
- this.groundCenter.x += c * g, this.groundCenter.y += h * g;
407
- }
408
- this.reflector.setRadius(e, this.groundGridScale, this.groundCenter), this.reflector.updateMatrixWorld(!0);
409
- }
410
- }
411
- detectGroundCameraJump(t, e) {
412
- const r = this.groundCameraPoseValid && (t.position.distanceTo(this.groundCameraPosition) > Math.max(e * j, 0.1) || t.quaternion.angleTo(this.groundCameraQuaternion) > Y);
413
- return this.groundCameraPosition.copy(t.position), this.groundCameraQuaternion.copy(t.quaternion), this.groundCameraPoseValid = !0, r;
414
- }
415
- measureGrabbedBodyFocus() {
416
- var r;
417
- const t = (r = this.runtime) == null ? void 0 : r.grab;
418
- if (!t) return !1;
419
- const e = this.getBodyGroup(t.bodyId);
420
- return e ? (this.groundGrabPoint.fromArray(t.localPoint), e.localToWorld(this.groundGrabPoint), this.root.worldToLocal(this.groundGrabPoint), this.groundHit.copy(this.groundGrabPoint), this.groundGrabFocus.set(this.groundGrabPoint.x, this.groundGrabPoint.y), this.groundGrabFocusValid = !0, !0) : !1;
421
- }
422
- limitCameraGroundFocus(t) {
423
- const e = t * 0.25, r = this.groundHit.x - this.groundModelFocus.x, o = this.groundHit.y - this.groundModelFocus.y, n = Math.hypot(r, o);
424
- if (n <= e || n <= 0) return;
425
- const l = e / n;
426
- this.groundHit.x = this.groundModelFocus.x + r * l, this.groundHit.y = this.groundModelFocus.y + o * l;
427
- }
428
- measureCameraGroundFocus(t, e) {
429
- return this.groundPlane.constant = -e, this.groundNdc.set(0, 0), this.groundRaycaster.setFromCamera(this.groundNdc, t), this.groundRaycaster.ray.intersectPlane(this.groundPlane, this.groundHit) !== null;
430
- }
431
- /**
432
- * Re-fits the ground disc and reflection to the model's current world bounds.
433
- * A flip is not rotation invariant for an axis-aligned bounding box, so the
434
- * floor that was sized to a wide-flat footprint can end up clipping, or
435
- * sitting off to the side of, the re-oriented model.
436
- */
437
- reseatGround() {
438
- if (this.disposed) return;
439
- this.root.updateMatrixWorld(!0);
440
- const t = p(this.model);
441
- t.isEmpty() || this.layoutGround(t, Math.max(t.getSize(new i.Vector3()).length() * 0.5, 0.02));
442
- }
443
- createForceLine() {
444
- const t = new i.BufferGeometry().setFromPoints([
445
- new i.Vector3(),
446
- new i.Vector3()
447
- ]), e = new i.LineBasicMaterial({
448
- color: m,
449
- depthTest: !1,
450
- transparent: !0
451
- }), r = new i.Line(t, e);
452
- return r.frustumCulled = !1, r.renderOrder = 1e3, r.visible = !1, r;
453
- }
454
- findBodyGroup(t) {
455
- var r;
456
- let e = t;
457
- for (; e && e !== this.model && e !== this.root; ) {
458
- if (typeof ((r = e.userData) == null ? void 0 : r.bodyId) == "number") return e;
459
- e = e.parent;
460
- }
461
- return null;
462
- }
463
- getBodyGroup(t) {
464
- let e = this.bodyGroups.get(t);
465
- return e || this.model.traverse((r) => {
466
- var o;
467
- ((o = r.userData) == null ? void 0 : o.bodyId) === t && (e = r, this.bodyGroups.set(t, r));
468
- }), e;
469
- }
470
- pointerRay(t, e, r) {
471
- const o = r.getBoundingClientRect();
472
- this.pointer.set(
473
- (t.clientX - o.left) / o.width * 2 - 1,
474
- -((t.clientY - o.top) / o.height) * 2 + 1
475
- ), this.raycaster.setFromCamera(this.pointer, e);
476
- }
477
- setForceBoost(t) {
478
- var e;
479
- !((e = this.runtime) != null && e.grab) || t === this.forceBoosted || (this.forceBoosted = t, this.runtime.updateGrabStrength(t ? z : 1), this.forceLine.material.color.setHex(t ? _ : m));
480
- }
481
- beginGrab(t, e, r) {
482
- if (!this.runtime || this.disposed) return !1;
483
- this.pointerRay(t, e, r), this.raycaster.firstHitOnly = !0;
484
- const o = this.raycaster.intersectObject(this.model, !0).find((d) => d.object.isMesh);
485
- if (!o) return !1;
486
- const n = this.findBodyGroup(o.object);
487
- if (!n) return !1;
488
- const l = n.worldToLocal(o.point.clone());
489
- this.dragTarget.copy(o.point), Q(this.dragPlane, e, o.point), this.dragOffset.set(0, 0, 0);
490
- const u = this.root.worldToLocal(o.point.clone());
491
- return this.groundInteractionActive = !0, this.bodyGroups.set(n.userData.bodyId, n), this.runtime.beginGrab({
492
- bodyId: n.userData.bodyId,
493
- localPoint: [l.x, l.y, l.z],
494
- target: [u.x, u.y, u.z]
495
- }), this.forceBoosted = !1, this.setForceBoost(t.shiftKey), this.forceLine.visible = !0, this.updateForceLine(), this.grabCanvas = r, r != null && r.style && (this.previousCursor = r.style.cursor, r.style.cursor = "grabbing"), !0;
496
- }
497
- moveGrab(t, e, r) {
498
- var n;
499
- if (!((n = this.runtime) != null && n.grab)) return;
500
- if (this.pointerRay(t, e, r), this.setForceBoost(t.shiftKey), r != null && r.style && r.style.cursor !== "grabbing" && (r.style.cursor = "grabbing"), this.raycaster.ray.intersectPlane(this.dragPlane, this.dragIntersection))
501
- this.dragTarget.copy(this.dragIntersection).add(this.dragOffset);
502
- else
503
- return;
504
- const o = this.root.worldToLocal(this.dragTarget.clone());
505
- this.runtime.updateGrabTarget([o.x, o.y, o.z]), this.updateForceLine();
506
- }
507
- endGrab() {
508
- var t;
509
- this.runtime && (this.runtime.endGrab(), this.groundInteractionActive = !1, this.forceBoosted = !1, this.forceLine.visible = !1, this.forceLine.material.color.setHex(m), (t = this.grabCanvas) != null && t.style && (this.grabCanvas.style.cursor = this.previousCursor), this.grabCanvas = null);
510
- }
511
- updateForceLine() {
512
- var u;
513
- const t = (u = this.runtime) == null ? void 0 : u.grab;
514
- if (!t) return;
515
- const e = this.getBodyGroup(t.bodyId);
516
- if (!e) return;
517
- const r = new i.Vector3().fromArray(t.localPoint), o = e.localToWorld(r), n = this.root.worldToLocal(o), l = this.forceLine.geometry.attributes.position;
518
- l.setXYZ(0, n.x, n.y, n.z), l.setXYZ(1, t.target[0], t.target[1], t.target[2]), l.needsUpdate = !0;
519
- }
520
- beforeRender(t, e, r) {
521
- var l, u;
522
- const o = {
523
- tone: t.toneMapping,
524
- exposure: t.toneMappingExposure,
525
- color: t.outputColorSpace,
526
- environment: e.environment,
527
- fog: e.fog,
528
- background: e.background
529
- }, n = () => {
530
- t.toneMapping = o.tone, t.toneMappingExposure = o.exposure, t.outputColorSpace = o.color, e.environment = o.environment, e.fog = o.fog, e.background = o.background;
531
- };
532
- if (this.disposed) return n;
533
- try {
534
- this.groundNeedsRefit && (this.groundNeedsRefit = !1, this.reseatGround()), (l = this.runtime) != null && l.grab && this.updateForceLine();
535
- const d = (u = this.runtime) == null ? void 0 : u.defaultGroundHeight;
536
- d !== void 0 && (this.reflector.position.z = d + 2e-3), t.toneMapping = i.ACESFilmicToneMapping, t.toneMappingExposure = E[this.presentation], t.outputColorSpace = i.SRGBColorSpace, e.background = new i.Color(this.values.background), e.fog = this.fog;
537
- const c = t.getSize(new i.Vector2()), h = `${c.x}:${c.y}`;
538
- return h !== this.size && (this.size = h, this.reflector.setRenderSize(Math.max(1, Math.floor(c.x * 0.35)), Math.max(1, Math.floor(c.y * 0.35)))), e.updateMatrixWorld(!0), r.updateMatrixWorld(!0), this.groundVisible && this.refreshGroundFocus(r), this.groundVisible && r instanceof i.PerspectiveCamera && this.reflector.renderReflection(t, e, r), n;
539
- } catch (d) {
540
- throw n(), d;
541
- }
542
- }
543
- /**
544
- * The reflector mesh carries both the grid lines and the reflection. Hiding
545
- * it also skips the reflection render pass, which only feeds its shader.
546
- */
547
- setGroundVisible(t) {
548
- t && !this.groundVisible && (this.groundCameraPoseValid = !1, this.reseatGround()), this.groundVisible = t, this.reflector.visible = t;
549
- }
550
- getGroundVisible() {
551
- return this.groundVisible;
552
- }
553
- getGroundGridScale() {
554
- return this.groundGridScale;
555
- }
556
- /**
557
- * Adds a debug/auxiliary object next to the model rather than inside it.
558
- * The model subtree is what the reflector pass and `getModelBounds` measure,
559
- * so nesting hidden aids (collision meshes, which are far larger than the
560
- * visual shell) would inflate both the reflection extent and the camera fit.
561
- * Auxiliaries ride in the up-axis wrapper: they are kinematic content, while
562
- * anything the physics runtime drives is rotated inside the compiled model.
563
- */
564
- attachAuxiliary(t) {
565
- var e;
566
- this.auxiliaries.add(t), this.modelWrapper.add(t), (e = this.invalidationCallback) == null || e.call(this);
567
- }
568
- detachAuxiliary(t) {
569
- var e;
570
- this.auxiliaries.delete(t) && (t.removeFromParent(), (e = this.invalidationCallback) == null || e.call(this));
571
- }
572
- getModelBounds() {
573
- this.root.updateMatrixWorld(!0);
574
- const t = p(this.model);
575
- if (!t.isEmpty()) return t;
576
- const e = new i.Box3();
577
- return e.min.set(-5e3, -5e3, -5e3), e.max.set(5e3, 5e3, 5e3), e;
578
- }
579
- dispose() {
580
- if (!this.disposed) {
581
- this.disposed = !0, this.endGrab(), this.model.removeFromParent();
582
- for (const t of this.auxiliaries) t.removeFromParent();
583
- this.auxiliaries.clear(), this.modelWrapper.removeFromParent(), this.root.removeFromParent(), this.reflector.renderTarget.dispose(), T(this.root);
584
- }
585
- }
586
- }
587
- export {
588
- rt as S,
589
- et as a,
590
- tt as d,
591
- F as m
592
- };