@relu-interactives/spatial-ecs 0.2.4 → 0.2.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.
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-D2H5H-K3.d.cts → Video-ChVFs9Tl.d.cts} +139 -75
  4. package/dist/{Video-BWhbvVPg.d.ts → Video-DCMCLInA.d.ts} +139 -75
  5. package/dist/{chunk-XWIEEJ7Z.js → chunk-4A66FSFT.js} +128 -82
  6. package/dist/chunk-4A66FSFT.js.map +1 -0
  7. package/dist/{chunk-AYEF3X2P.js → chunk-CRP3W2GK.js} +3 -3
  8. package/dist/chunk-CRP3W2GK.js.map +1 -0
  9. package/dist/{chunk-MAWBTMCG.js → chunk-KRZLEPRI.js} +156 -88
  10. package/dist/chunk-KRZLEPRI.js.map +1 -0
  11. package/dist/{chunk-NFDTIYR4.js → chunk-NB6I5JJN.js} +17 -2
  12. package/dist/chunk-NB6I5JJN.js.map +1 -0
  13. package/dist/{chunk-P2YDX5DB.js → chunk-RXCUHGRB.js} +124 -5
  14. package/dist/chunk-RXCUHGRB.js.map +1 -0
  15. package/dist/components/index.cjs +161 -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 +217 -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-D1I5L7bA.d.cts} +8 -3
  26. package/dist/{index-Dm6zF1MY.d.ts → index-DGZDgt3p.d.ts} +8 -3
  27. package/dist/index.cjs +468 -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 +51 -12
  32. package/dist/index.js.map +1 -1
  33. package/dist/systems/index.cjs +248 -63
  34. package/dist/systems/index.cjs.map +1 -1
  35. package/dist/systems/index.d.cts +15 -3
  36. package/dist/systems/index.d.ts +15 -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 +15 -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
@@ -1251,6 +1251,134 @@ var _Postprocessing = class _Postprocessing extends Component {
1251
1251
  };
1252
1252
  __name(_Postprocessing, "Postprocessing");
1253
1253
  var Postprocessing = _Postprocessing;
1254
+ var DEFAULT_MESH_GEOMETRY_PARAMETERS = {
1255
+ capsule: { radius: 0.5, height: 1 },
1256
+ cube: { width: 1, height: 1, depth: 1 },
1257
+ cylinder: { radius: 0.5, height: 1 },
1258
+ plane: { width: 100, height: 100 },
1259
+ sphere: { radius: 0.5 }
1260
+ };
1261
+ var GEOMETRY_TYPE_BY_KIND = {
1262
+ capsule: "CapsuleGeometry",
1263
+ cube: "BoxGeometry",
1264
+ cylinder: "CylinderGeometry",
1265
+ model: "Model",
1266
+ plane: "PlaneGeometry",
1267
+ sphere: "SphereGeometry"
1268
+ };
1269
+ var _MeshGeometryComponent = class _MeshGeometryComponent extends Component {
1270
+ constructor(kind, object, parameters, options) {
1271
+ super();
1272
+ this.kind = kind;
1273
+ this.geometryType = GEOMETRY_TYPE_BY_KIND[kind];
1274
+ this.object = object;
1275
+ this.modelName = options?.modelName ?? "";
1276
+ this.castShadow = options?.castShadow ?? false;
1277
+ this.receiveShadow = options?.receiveShadow ?? false;
1278
+ switch (kind) {
1279
+ case "cube":
1280
+ this.parameters = {
1281
+ ...DEFAULT_MESH_GEOMETRY_PARAMETERS.cube,
1282
+ ...parameters
1283
+ };
1284
+ break;
1285
+ case "plane":
1286
+ this.parameters = {
1287
+ ...DEFAULT_MESH_GEOMETRY_PARAMETERS.plane,
1288
+ ...parameters
1289
+ };
1290
+ break;
1291
+ case "sphere":
1292
+ this.parameters = {
1293
+ ...DEFAULT_MESH_GEOMETRY_PARAMETERS.sphere,
1294
+ ...parameters
1295
+ };
1296
+ break;
1297
+ case "cylinder":
1298
+ this.parameters = {
1299
+ ...DEFAULT_MESH_GEOMETRY_PARAMETERS.cylinder,
1300
+ ...parameters
1301
+ };
1302
+ break;
1303
+ case "capsule":
1304
+ this.parameters = {
1305
+ ...DEFAULT_MESH_GEOMETRY_PARAMETERS.capsule,
1306
+ ...parameters
1307
+ };
1308
+ break;
1309
+ case "model":
1310
+ this.parameters = {};
1311
+ break;
1312
+ }
1313
+ }
1314
+ };
1315
+ __name(_MeshGeometryComponent, "MeshGeometryComponent");
1316
+ var MeshGeometryComponent = _MeshGeometryComponent;
1317
+
1318
+ // core/components/InstancedMesh.ts
1319
+ var DEFAULT_INSTANCE_TRANSFORM = {
1320
+ position: { x: 0, y: 0, z: 0 },
1321
+ rotation: { x: 0, y: 0, z: 0 },
1322
+ scale: { x: 1, y: 1, z: 1 }
1323
+ };
1324
+ var DEFAULT_INSTANCED_MESH_PARAMETERS = {
1325
+ capsule: DEFAULT_MESH_GEOMETRY_PARAMETERS.capsule,
1326
+ cube: DEFAULT_MESH_GEOMETRY_PARAMETERS.cube,
1327
+ cylinder: DEFAULT_MESH_GEOMETRY_PARAMETERS.cylinder,
1328
+ plane: DEFAULT_MESH_GEOMETRY_PARAMETERS.plane,
1329
+ sphere: DEFAULT_MESH_GEOMETRY_PARAMETERS.sphere
1330
+ };
1331
+ var _InstancedMeshComponent = class _InstancedMeshComponent extends Component {
1332
+ constructor(meshKind = "cube", meshParameters, options) {
1333
+ super();
1334
+ this.meshKind = meshKind;
1335
+ this.meshParameters = {
1336
+ ...DEFAULT_INSTANCED_MESH_PARAMETERS[meshKind],
1337
+ ...meshParameters ?? {}
1338
+ };
1339
+ this.castShadow = options?.castShadow ?? false;
1340
+ this.receiveShadow = options?.receiveShadow ?? false;
1341
+ this.instances = options?.instances && options.instances.length > 0 ? options.instances : [{ ...DEFAULT_INSTANCE_TRANSFORM }];
1342
+ this.instancedMesh = null;
1343
+ }
1344
+ /** Sanitize and return a copy of the current mesh parameters with valid values. */
1345
+ getSanitizedParameters() {
1346
+ const MIN_DIM = 0.01;
1347
+ const MIN_RAD = 1e-3;
1348
+ const sanitize = /* @__PURE__ */ __name((v, min) => Number.isFinite(v) ? Math.max(min, v) : min, "sanitize");
1349
+ switch (this.meshKind) {
1350
+ case "cube": {
1351
+ const p = this.meshParameters;
1352
+ return {
1353
+ width: sanitize(p.width, MIN_DIM),
1354
+ height: sanitize(p.height, MIN_DIM),
1355
+ depth: sanitize(p.depth, MIN_DIM)
1356
+ };
1357
+ }
1358
+ case "plane": {
1359
+ const p = this.meshParameters;
1360
+ return {
1361
+ width: sanitize(p.width, MIN_DIM),
1362
+ height: sanitize(p.height, MIN_DIM)
1363
+ };
1364
+ }
1365
+ case "sphere": {
1366
+ const p = this.meshParameters;
1367
+ return { radius: sanitize(p.radius, MIN_RAD) };
1368
+ }
1369
+ case "cylinder":
1370
+ case "capsule": {
1371
+ const p = this.meshParameters;
1372
+ return {
1373
+ radius: sanitize(p.radius, MIN_RAD),
1374
+ height: sanitize(p.height, MIN_DIM)
1375
+ };
1376
+ }
1377
+ }
1378
+ }
1379
+ };
1380
+ __name(_InstancedMeshComponent, "InstancedMeshComponent");
1381
+ var InstancedMeshComponent = _InstancedMeshComponent;
1254
1382
 
1255
1383
  // core/components/Physics.ts
1256
1384
  var DEFAULT_RIGIDBODY_STATE = {
@@ -1611,69 +1739,6 @@ var _MaterialComponent = class _MaterialComponent extends Component {
1611
1739
  };
1612
1740
  __name(_MaterialComponent, "MaterialComponent");
1613
1741
  var MaterialComponent = _MaterialComponent;
1614
- var DEFAULT_MESH_GEOMETRY_PARAMETERS = {
1615
- capsule: { radius: 0.5, height: 1 },
1616
- cube: { width: 1, height: 1, depth: 1 },
1617
- cylinder: { radius: 0.5, height: 1 },
1618
- plane: { width: 100, height: 100 },
1619
- sphere: { radius: 0.5 }
1620
- };
1621
- var GEOMETRY_TYPE_BY_KIND = {
1622
- capsule: "CapsuleGeometry",
1623
- cube: "BoxGeometry",
1624
- cylinder: "CylinderGeometry",
1625
- model: "Model",
1626
- plane: "PlaneGeometry",
1627
- sphere: "SphereGeometry"
1628
- };
1629
- var _MeshGeometryComponent = class _MeshGeometryComponent extends Component {
1630
- constructor(kind, object, parameters, options) {
1631
- super();
1632
- this.kind = kind;
1633
- this.geometryType = GEOMETRY_TYPE_BY_KIND[kind];
1634
- this.object = object;
1635
- this.modelName = options?.modelName ?? "";
1636
- this.castShadow = options?.castShadow ?? false;
1637
- this.receiveShadow = options?.receiveShadow ?? false;
1638
- switch (kind) {
1639
- case "cube":
1640
- this.parameters = {
1641
- ...DEFAULT_MESH_GEOMETRY_PARAMETERS.cube,
1642
- ...parameters
1643
- };
1644
- break;
1645
- case "plane":
1646
- this.parameters = {
1647
- ...DEFAULT_MESH_GEOMETRY_PARAMETERS.plane,
1648
- ...parameters
1649
- };
1650
- break;
1651
- case "sphere":
1652
- this.parameters = {
1653
- ...DEFAULT_MESH_GEOMETRY_PARAMETERS.sphere,
1654
- ...parameters
1655
- };
1656
- break;
1657
- case "cylinder":
1658
- this.parameters = {
1659
- ...DEFAULT_MESH_GEOMETRY_PARAMETERS.cylinder,
1660
- ...parameters
1661
- };
1662
- break;
1663
- case "capsule":
1664
- this.parameters = {
1665
- ...DEFAULT_MESH_GEOMETRY_PARAMETERS.capsule,
1666
- ...parameters
1667
- };
1668
- break;
1669
- case "model":
1670
- this.parameters = {};
1671
- break;
1672
- }
1673
- }
1674
- };
1675
- __name(_MeshGeometryComponent, "MeshGeometryComponent");
1676
- var MeshGeometryComponent = _MeshGeometryComponent;
1677
1742
 
1678
1743
  // core/components/Model.ts
1679
1744
  var _ModelComponent = class _ModelComponent extends Component {
@@ -4068,6 +4133,125 @@ var _MeshGeometrySystem = class _MeshGeometrySystem {
4068
4133
  };
4069
4134
  __name(_MeshGeometrySystem, "MeshGeometrySystem");
4070
4135
  var MeshGeometrySystem = _MeshGeometrySystem;
4136
+ function buildGeometry(component) {
4137
+ const p = component.getSanitizedParameters();
4138
+ switch (component.meshKind) {
4139
+ case "cube": {
4140
+ const params = p;
4141
+ return new THREE13__namespace.BoxGeometry(params.width, params.height, params.depth);
4142
+ }
4143
+ case "plane": {
4144
+ const params = p;
4145
+ return new THREE13__namespace.PlaneGeometry(params.width, params.height);
4146
+ }
4147
+ case "sphere": {
4148
+ const params = p;
4149
+ return new THREE13__namespace.SphereGeometry(params.radius, 24, 24);
4150
+ }
4151
+ case "cylinder": {
4152
+ const params = p;
4153
+ return new THREE13__namespace.CylinderGeometry(params.radius, params.radius, params.height, 20);
4154
+ }
4155
+ case "capsule": {
4156
+ const params = p;
4157
+ return new THREE13__namespace.CapsuleGeometry(params.radius, params.height, 4, 8);
4158
+ }
4159
+ }
4160
+ }
4161
+ __name(buildGeometry, "buildGeometry");
4162
+ var _position = new THREE13__namespace.Vector3();
4163
+ var _quaternion = new THREE13__namespace.Quaternion();
4164
+ var _euler = new THREE13__namespace.Euler();
4165
+ var _scale = new THREE13__namespace.Vector3();
4166
+ var _matrix = new THREE13__namespace.Matrix4();
4167
+ function instanceTransformToMatrix4(inst, out) {
4168
+ _position.set(inst.position.x, inst.position.y, inst.position.z);
4169
+ _euler.set(inst.rotation.x, inst.rotation.y, inst.rotation.z, "XYZ");
4170
+ _quaternion.setFromEuler(_euler);
4171
+ _scale.set(inst.scale.x, inst.scale.y, inst.scale.z);
4172
+ out.compose(_position, _quaternion, _scale);
4173
+ }
4174
+ __name(instanceTransformToMatrix4, "instanceTransformToMatrix4");
4175
+ var _InstancedMeshSystem = class _InstancedMeshSystem {
4176
+ constructor() {
4177
+ /** Signature tracking for geometry + instance count changes. */
4178
+ this.geometrySignatures = /* @__PURE__ */ new Map();
4179
+ }
4180
+ update(world) {
4181
+ const entityIds = world.query(InstancedMeshComponent);
4182
+ const alive = new Set(entityIds);
4183
+ for (const trackedId of this.geometrySignatures.keys()) {
4184
+ if (!alive.has(trackedId)) {
4185
+ this.geometrySignatures.delete(trackedId);
4186
+ }
4187
+ }
4188
+ for (const entityId of entityIds) {
4189
+ const comp = world.getComponent(
4190
+ entityId,
4191
+ InstancedMeshComponent
4192
+ );
4193
+ if (!comp) {
4194
+ continue;
4195
+ }
4196
+ let anchor = null;
4197
+ const objectRef = world.getComponent(entityId, Object3DRef);
4198
+ if (objectRef?.object) {
4199
+ anchor = objectRef.object;
4200
+ }
4201
+ if (!anchor) {
4202
+ continue;
4203
+ }
4204
+ const instanceCount = comp.instances.length;
4205
+ const geometrySignature = `${comp.meshKind}:${JSON.stringify(comp.meshParameters)}:${instanceCount}`;
4206
+ const needsRebuild = comp.instancedMesh === null || this.geometrySignatures.get(entityId) !== geometrySignature;
4207
+ if (needsRebuild) {
4208
+ if (comp.instancedMesh) {
4209
+ anchor.remove(comp.instancedMesh);
4210
+ comp.instancedMesh.geometry.dispose();
4211
+ if (Array.isArray(comp.instancedMesh.material)) {
4212
+ for (const m of comp.instancedMesh.material) {
4213
+ m.dispose();
4214
+ }
4215
+ } else {
4216
+ comp.instancedMesh.material.dispose();
4217
+ }
4218
+ comp.instancedMesh = null;
4219
+ }
4220
+ const count = Math.max(1, instanceCount);
4221
+ const geometry = buildGeometry(comp);
4222
+ const material = new THREE13__namespace.MeshStandardMaterial({ color: "#ffffff" });
4223
+ const instancedMesh = new THREE13__namespace.InstancedMesh(geometry, material, count);
4224
+ instancedMesh.castShadow = comp.castShadow;
4225
+ instancedMesh.receiveShadow = comp.receiveShadow;
4226
+ for (let i = 0; i < count; i++) {
4227
+ const inst = comp.instances[i] ?? {
4228
+ position: { x: 0, y: 0, z: 0 },
4229
+ rotation: { x: 0, y: 0, z: 0 },
4230
+ scale: { x: 1, y: 1, z: 1 }
4231
+ };
4232
+ instanceTransformToMatrix4(inst, _matrix);
4233
+ instancedMesh.setMatrixAt(i, _matrix);
4234
+ }
4235
+ instancedMesh.instanceMatrix.needsUpdate = true;
4236
+ anchor.add(instancedMesh);
4237
+ comp.instancedMesh = instancedMesh;
4238
+ this.geometrySignatures.set(entityId, geometrySignature);
4239
+ } else {
4240
+ const im = comp.instancedMesh;
4241
+ const count = comp.instances.length;
4242
+ for (let i = 0; i < count; i++) {
4243
+ instanceTransformToMatrix4(comp.instances[i], _matrix);
4244
+ im.setMatrixAt(i, _matrix);
4245
+ }
4246
+ im.instanceMatrix.needsUpdate = true;
4247
+ im.castShadow = comp.castShadow;
4248
+ im.receiveShadow = comp.receiveShadow;
4249
+ }
4250
+ }
4251
+ }
4252
+ };
4253
+ __name(_InstancedMeshSystem, "InstancedMeshSystem");
4254
+ var InstancedMeshSystem = _InstancedMeshSystem;
4071
4255
 
4072
4256
  // core/systems/RenderSystem.ts
4073
4257
  var _RenderSystem = class _RenderSystem {
@@ -5103,6 +5287,7 @@ exports.AudioSystem = AudioSystem;
5103
5287
  exports.CameraSyncSystem = CameraSyncSystem;
5104
5288
  exports.EnvironmentSyncSystem = EnvironmentSyncSystem;
5105
5289
  exports.InputSystem = InputSystem;
5290
+ exports.InstancedMeshSystem = InstancedMeshSystem;
5106
5291
  exports.KeyboardKeys = KeyboardKeys;
5107
5292
  exports.LightSyncSystem = LightSyncSystem;
5108
5293
  exports.MaterialSyncSystem = MaterialSyncSystem;