@relu-interactives/spatial-ecs 0.2.4 → 0.2.6

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 (50) hide show
  1. package/dist/{System-BTyYvKVH.d.cts → System-CIiWteft.d.cts} +2 -2
  2. package/dist/{System-BTyYvKVH.d.ts → System-CIiWteft.d.ts} +2 -2
  3. package/dist/{Video-BWhbvVPg.d.ts → Video-B-qMljTn.d.ts} +147 -75
  4. package/dist/{Video-D2H5H-K3.d.cts → Video-Dj4KBqQ1.d.cts} +147 -75
  5. package/dist/{chunk-XWIEEJ7Z.js → chunk-EIC5B3A7.js} +128 -82
  6. package/dist/chunk-EIC5B3A7.js.map +1 -0
  7. package/dist/{chunk-AYEF3X2P.js → chunk-FDWNCXU3.js} +3 -3
  8. package/dist/chunk-FDWNCXU3.js.map +1 -0
  9. package/dist/{chunk-NFDTIYR4.js → chunk-G2DUXNTQ.js} +18 -2
  10. package/dist/chunk-G2DUXNTQ.js.map +1 -0
  11. package/dist/{chunk-P2YDX5DB.js → chunk-NRSUYDBA.js} +275 -5
  12. package/dist/chunk-NRSUYDBA.js.map +1 -0
  13. package/dist/{chunk-MAWBTMCG.js → chunk-R5D5PWXV.js} +160 -88
  14. package/dist/chunk-R5D5PWXV.js.map +1 -0
  15. package/dist/components/index.cjs +165 -93
  16. package/dist/components/index.cjs.map +1 -1
  17. package/dist/components/index.d.cts +2 -2
  18. package/dist/components/index.d.ts +2 -2
  19. package/dist/components/index.js +1 -1
  20. package/dist/factories/index.cjs +221 -105
  21. package/dist/factories/index.cjs.map +1 -1
  22. package/dist/factories/index.d.cts +9 -3
  23. package/dist/factories/index.d.ts +9 -3
  24. package/dist/factories/index.js +3 -3
  25. package/dist/{index-KdEzEZox.d.cts → index-B12sx9yF.d.cts} +8 -3
  26. package/dist/{index-Dm6zF1MY.d.ts → index-Cf-PaKyN.d.ts} +8 -3
  27. package/dist/index.cjs +627 -179
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +7 -7
  30. package/dist/index.d.ts +7 -7
  31. package/dist/index.js +54 -12
  32. package/dist/index.js.map +1 -1
  33. package/dist/systems/index.cjs +403 -63
  34. package/dist/systems/index.cjs.map +1 -1
  35. package/dist/systems/index.d.cts +27 -3
  36. package/dist/systems/index.d.ts +27 -3
  37. package/dist/systems/index.js +3 -3
  38. package/dist/types.d.cts +1 -1
  39. package/dist/types.d.ts +1 -1
  40. package/dist/worldSnapshotUtils.cjs +16 -0
  41. package/dist/worldSnapshotUtils.cjs.map +1 -1
  42. package/dist/worldSnapshotUtils.d.cts +1 -1
  43. package/dist/worldSnapshotUtils.d.ts +1 -1
  44. package/dist/worldSnapshotUtils.js +1 -1
  45. package/package.json +1 -1
  46. package/dist/chunk-AYEF3X2P.js.map +0 -1
  47. package/dist/chunk-MAWBTMCG.js.map +0 -1
  48. package/dist/chunk-NFDTIYR4.js.map +0 -1
  49. package/dist/chunk-P2YDX5DB.js.map +0 -1
  50. package/dist/chunk-XWIEEJ7Z.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import { resolveDracoDecoderPath, applyTransform } from './chunk-AYEF3X2P.js';
2
- import { AnimationComponent, ModelComponent, CameraComponent, Object3DRef, Environment, Postprocessing, TransformComponent, PhysicsComponent, LightComponent, MaterialComponent, DEFAULT_MESH_GEOMETRY_PARAMETERS, MeshGeometryComponent, ParentId, ScriptComponent, AudioComponent, VideoComponent } from './chunk-MAWBTMCG.js';
1
+ import { resolveDracoDecoderPath, applyTransform } from './chunk-FDWNCXU3.js';
2
+ import { AnimationComponent, ModelComponent, CameraComponent, Object3DRef, Environment, Postprocessing, TransformComponent, PhysicsComponent, LightComponent, MaterialComponent, DEFAULT_MESH_GEOMETRY_PARAMETERS, MeshGeometryComponent, InstancedMeshComponent, ParentId, ScriptComponent, AudioComponent, VideoComponent } from './chunk-R5D5PWXV.js';
3
3
  import { __name } from './chunk-OY7ZODZP.js';
4
4
  import * as THREE7 from 'three';
5
5
  import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
@@ -2269,6 +2269,276 @@ var _MeshGeometrySystem = class _MeshGeometrySystem {
2269
2269
  };
2270
2270
  __name(_MeshGeometrySystem, "MeshGeometrySystem");
2271
2271
  var MeshGeometrySystem = _MeshGeometrySystem;
2272
+ var DRACO_DECODER_PATH = resolveDracoDecoderPath();
2273
+ var loadGLTF = /* @__PURE__ */ __name((path) => {
2274
+ return new Promise((resolve, reject) => {
2275
+ const loader = new GLTFLoader();
2276
+ try {
2277
+ const dracoLoader = new DRACOLoader();
2278
+ dracoLoader.setDecoderPath(DRACO_DECODER_PATH);
2279
+ loader.setDRACOLoader(dracoLoader);
2280
+ } catch (error) {
2281
+ console.debug("DRACOLoader setup failed or unavailable", error);
2282
+ }
2283
+ loader.load(
2284
+ path,
2285
+ (gltf) => {
2286
+ resolve(gltf);
2287
+ },
2288
+ void 0,
2289
+ (error) => {
2290
+ console.debug("GLTFLoader failed to load", error);
2291
+ reject(error);
2292
+ }
2293
+ );
2294
+ });
2295
+ }, "loadGLTF");
2296
+
2297
+ // core/systems/InstancedMeshSystem.ts
2298
+ function buildGeometry(component) {
2299
+ const p = component.getSanitizedParameters();
2300
+ switch (component.meshKind) {
2301
+ case "cube": {
2302
+ const params = p;
2303
+ return new THREE7.BoxGeometry(params.width, params.height, params.depth);
2304
+ }
2305
+ case "plane": {
2306
+ const params = p;
2307
+ return new THREE7.PlaneGeometry(params.width, params.height);
2308
+ }
2309
+ case "sphere": {
2310
+ const params = p;
2311
+ return new THREE7.SphereGeometry(params.radius, 24, 24);
2312
+ }
2313
+ case "cylinder": {
2314
+ const params = p;
2315
+ return new THREE7.CylinderGeometry(params.radius, params.radius, params.height, 20);
2316
+ }
2317
+ case "capsule": {
2318
+ const params = p;
2319
+ return new THREE7.CapsuleGeometry(params.radius, params.height, 4, 8);
2320
+ }
2321
+ default:
2322
+ return new THREE7.BoxGeometry(1, 1, 1);
2323
+ }
2324
+ }
2325
+ __name(buildGeometry, "buildGeometry");
2326
+ var _position = new THREE7.Vector3();
2327
+ var _quaternion = new THREE7.Quaternion();
2328
+ var _euler = new THREE7.Euler();
2329
+ var _scale = new THREE7.Vector3();
2330
+ var _matrix = new THREE7.Matrix4();
2331
+ var _modelMatrix = new THREE7.Matrix4();
2332
+ function instanceTransformToMatrix4(inst, out) {
2333
+ _position.set(inst.position.x, inst.position.y, inst.position.z);
2334
+ _euler.set(inst.rotation.x, inst.rotation.y, inst.rotation.z, "XYZ");
2335
+ _quaternion.setFromEuler(_euler);
2336
+ _scale.set(inst.scale.x, inst.scale.y, inst.scale.z);
2337
+ out.compose(_position, _quaternion, _scale);
2338
+ }
2339
+ __name(instanceTransformToMatrix4, "instanceTransformToMatrix4");
2340
+ function disposePrimitiveInstancedMesh(mesh) {
2341
+ mesh.geometry.dispose();
2342
+ if (Array.isArray(mesh.material)) {
2343
+ for (const m of mesh.material) {
2344
+ m.dispose();
2345
+ }
2346
+ } else {
2347
+ mesh.material.dispose();
2348
+ }
2349
+ }
2350
+ __name(disposePrimitiveInstancedMesh, "disposePrimitiveInstancedMesh");
2351
+ function disposeModelGroup(group) {
2352
+ group.traverse((child) => {
2353
+ const im = child;
2354
+ if (im.isInstancedMesh) {
2355
+ im.geometry.dispose();
2356
+ if (Array.isArray(im.material)) {
2357
+ for (const m of im.material) {
2358
+ m.dispose();
2359
+ }
2360
+ } else {
2361
+ im.material.dispose();
2362
+ }
2363
+ }
2364
+ });
2365
+ }
2366
+ __name(disposeModelGroup, "disposeModelGroup");
2367
+ var _InstancedMeshSystem = class _InstancedMeshSystem {
2368
+ constructor() {
2369
+ /** Signature tracking for geometry + instance count changes. */
2370
+ this.geometrySignatures = /* @__PURE__ */ new Map();
2371
+ /** Entities whose GLTF model is currently loading. Prevents duplicate load triggers. */
2372
+ this.loadingEntities = /* @__PURE__ */ new Set();
2373
+ /** Per-mesh data for model kind, populated after GLTF load. */
2374
+ this.modelMeshEntries = /* @__PURE__ */ new Map();
2375
+ }
2376
+ update(world) {
2377
+ const entityIds = world.query(InstancedMeshComponent);
2378
+ const alive = new Set(entityIds);
2379
+ for (const trackedId of this.geometrySignatures.keys()) {
2380
+ if (!alive.has(trackedId)) this.geometrySignatures.delete(trackedId);
2381
+ }
2382
+ for (const trackedId of this.loadingEntities) {
2383
+ if (!alive.has(trackedId)) this.loadingEntities.delete(trackedId);
2384
+ }
2385
+ for (const trackedId of this.modelMeshEntries.keys()) {
2386
+ if (!alive.has(trackedId)) this.modelMeshEntries.delete(trackedId);
2387
+ }
2388
+ for (const entityId of entityIds) {
2389
+ const comp = world.getComponent(
2390
+ entityId,
2391
+ InstancedMeshComponent
2392
+ );
2393
+ if (!comp) continue;
2394
+ const objectRef = world.getComponent(entityId, Object3DRef);
2395
+ const anchor = objectRef?.object ?? null;
2396
+ if (!anchor) continue;
2397
+ const instanceCount = comp.instances.length;
2398
+ const geometrySignature = comp.meshKind === "model" ? `model:${comp.modelPath}:${instanceCount}` : `${comp.meshKind}:${JSON.stringify(comp.meshParameters)}:${instanceCount}`;
2399
+ const needsRebuild = comp.instancedMesh === null || this.geometrySignatures.get(entityId) !== geometrySignature;
2400
+ if (needsRebuild) {
2401
+ if (comp.instancedMesh) {
2402
+ anchor.remove(comp.instancedMesh);
2403
+ if (comp.instancedMesh.isInstancedMesh) {
2404
+ disposePrimitiveInstancedMesh(comp.instancedMesh);
2405
+ } else {
2406
+ disposeModelGroup(comp.instancedMesh);
2407
+ }
2408
+ comp.instancedMesh = null;
2409
+ }
2410
+ this.loadingEntities.delete(entityId);
2411
+ this.modelMeshEntries.delete(entityId);
2412
+ if (comp.meshKind === "model") {
2413
+ if (comp.modelPath && !this.loadingEntities.has(entityId)) {
2414
+ this.loadingEntities.add(entityId);
2415
+ const targetSignature = geometrySignature;
2416
+ const targetEntityId = entityId;
2417
+ loadGLTF(comp.modelPath).then((gltf) => {
2418
+ if (!world.hasEntity(targetEntityId)) {
2419
+ this.loadingEntities.delete(targetEntityId);
2420
+ return;
2421
+ }
2422
+ const currentComp = world.getComponent(
2423
+ targetEntityId,
2424
+ InstancedMeshComponent
2425
+ );
2426
+ if (!currentComp) {
2427
+ this.loadingEntities.delete(targetEntityId);
2428
+ return;
2429
+ }
2430
+ const currentSig = `model:${currentComp.modelPath}:${currentComp.instances.length}`;
2431
+ if (currentSig !== targetSignature) {
2432
+ this.loadingEntities.delete(targetEntityId);
2433
+ return;
2434
+ }
2435
+ const currentRef = world.getComponent(targetEntityId, Object3DRef);
2436
+ const currentAnchor = currentRef?.object ?? null;
2437
+ if (!currentAnchor) {
2438
+ this.loadingEntities.delete(targetEntityId);
2439
+ return;
2440
+ }
2441
+ const count2 = Math.max(1, currentComp.instances.length);
2442
+ const entries = [];
2443
+ gltf.scene.updateWorldMatrix(true, true);
2444
+ gltf.scene.traverse((child) => {
2445
+ const meshChild = child;
2446
+ if (!meshChild.isMesh) return;
2447
+ const geometry2 = meshChild.geometry.clone();
2448
+ const sourceMaterial = Array.isArray(meshChild.material) ? meshChild.material.map((m) => m.clone()) : meshChild.material.clone();
2449
+ meshChild.updateWorldMatrix(true, false);
2450
+ const localMatrix = meshChild.matrixWorld.clone();
2451
+ const instancedMesh2 = new THREE7.InstancedMesh(
2452
+ geometry2,
2453
+ Array.isArray(sourceMaterial) ? sourceMaterial : sourceMaterial,
2454
+ count2
2455
+ );
2456
+ instancedMesh2.castShadow = currentComp.castShadow;
2457
+ instancedMesh2.receiveShadow = currentComp.receiveShadow;
2458
+ for (let i = 0; i < count2; i++) {
2459
+ const inst = currentComp.instances[i] ?? {
2460
+ position: { x: 0, y: 0, z: 0 },
2461
+ rotation: { x: 0, y: 0, z: 0 },
2462
+ scale: { x: 1, y: 1, z: 1 }
2463
+ };
2464
+ instanceTransformToMatrix4(inst, _matrix);
2465
+ _modelMatrix.multiplyMatrices(_matrix, localMatrix);
2466
+ instancedMesh2.setMatrixAt(i, _modelMatrix);
2467
+ }
2468
+ instancedMesh2.instanceMatrix.needsUpdate = true;
2469
+ entries.push({ localMatrix, instancedMesh: instancedMesh2 });
2470
+ });
2471
+ if (entries.length === 0) {
2472
+ this.loadingEntities.delete(targetEntityId);
2473
+ return;
2474
+ }
2475
+ const group = new THREE7.Group();
2476
+ for (const entry of entries) {
2477
+ group.add(entry.instancedMesh);
2478
+ }
2479
+ currentAnchor.add(group);
2480
+ currentComp.instancedMesh = group;
2481
+ this.modelMeshEntries.set(targetEntityId, entries);
2482
+ this.geometrySignatures.set(targetEntityId, targetSignature);
2483
+ this.loadingEntities.delete(targetEntityId);
2484
+ }).catch(() => {
2485
+ this.loadingEntities.delete(targetEntityId);
2486
+ });
2487
+ }
2488
+ continue;
2489
+ }
2490
+ const count = Math.max(1, instanceCount);
2491
+ const geometry = buildGeometry(comp);
2492
+ const material = new THREE7.MeshStandardMaterial({ color: "#ffffff" });
2493
+ const instancedMesh = new THREE7.InstancedMesh(geometry, material, count);
2494
+ instancedMesh.castShadow = comp.castShadow;
2495
+ instancedMesh.receiveShadow = comp.receiveShadow;
2496
+ for (let i = 0; i < count; i++) {
2497
+ const inst = comp.instances[i] ?? {
2498
+ position: { x: 0, y: 0, z: 0 },
2499
+ rotation: { x: 0, y: 0, z: 0 },
2500
+ scale: { x: 1, y: 1, z: 1 }
2501
+ };
2502
+ instanceTransformToMatrix4(inst, _matrix);
2503
+ instancedMesh.setMatrixAt(i, _matrix);
2504
+ }
2505
+ instancedMesh.instanceMatrix.needsUpdate = true;
2506
+ anchor.add(instancedMesh);
2507
+ comp.instancedMesh = instancedMesh;
2508
+ this.geometrySignatures.set(entityId, geometrySignature);
2509
+ } else {
2510
+ if (comp.meshKind === "model") {
2511
+ const entries = this.modelMeshEntries.get(entityId);
2512
+ if (entries) {
2513
+ const count = comp.instances.length;
2514
+ for (const entry of entries) {
2515
+ for (let i = 0; i < count; i++) {
2516
+ instanceTransformToMatrix4(comp.instances[i], _matrix);
2517
+ _modelMatrix.multiplyMatrices(_matrix, entry.localMatrix);
2518
+ entry.instancedMesh.setMatrixAt(i, _modelMatrix);
2519
+ }
2520
+ entry.instancedMesh.instanceMatrix.needsUpdate = true;
2521
+ entry.instancedMesh.castShadow = comp.castShadow;
2522
+ entry.instancedMesh.receiveShadow = comp.receiveShadow;
2523
+ }
2524
+ }
2525
+ } else {
2526
+ const im = comp.instancedMesh;
2527
+ const count = comp.instances.length;
2528
+ for (let i = 0; i < count; i++) {
2529
+ instanceTransformToMatrix4(comp.instances[i], _matrix);
2530
+ im.setMatrixAt(i, _matrix);
2531
+ }
2532
+ im.instanceMatrix.needsUpdate = true;
2533
+ im.castShadow = comp.castShadow;
2534
+ im.receiveShadow = comp.receiveShadow;
2535
+ }
2536
+ }
2537
+ }
2538
+ }
2539
+ };
2540
+ __name(_InstancedMeshSystem, "InstancedMeshSystem");
2541
+ var InstancedMeshSystem = _InstancedMeshSystem;
2272
2542
 
2273
2543
  // core/systems/RenderSystem.ts
2274
2544
  var _RenderSystem = class _RenderSystem {
@@ -3299,6 +3569,6 @@ var _VideoSystem = class _VideoSystem {
3299
3569
  __name(_VideoSystem, "VideoSystem");
3300
3570
  var VideoSystem = _VideoSystem;
3301
3571
 
3302
- export { AnimationSystem, AudioSystem, Behaviour, CameraSyncSystem, EnvironmentSyncSystem, InputSystem, KeyboardKeys, LightSyncSystem, MaterialSyncSystem, MeshGeometrySystem, ObjectVisibilitySystem, PhysicsSyncSystem, PostprocessingSyncSystem, RenderSystem, ScriptBehaviourSystem, TransformSyncSystem, VideoSystem };
3303
- //# sourceMappingURL=chunk-P2YDX5DB.js.map
3304
- //# sourceMappingURL=chunk-P2YDX5DB.js.map
3572
+ export { AnimationSystem, AudioSystem, Behaviour, CameraSyncSystem, EnvironmentSyncSystem, InputSystem, InstancedMeshSystem, KeyboardKeys, LightSyncSystem, MaterialSyncSystem, MeshGeometrySystem, ObjectVisibilitySystem, PhysicsSyncSystem, PostprocessingSyncSystem, RenderSystem, ScriptBehaviourSystem, TransformSyncSystem, VideoSystem };
3573
+ //# sourceMappingURL=chunk-NRSUYDBA.js.map
3574
+ //# sourceMappingURL=chunk-NRSUYDBA.js.map