@relu-interactives/spatial-ecs 0.2.22 → 0.2.24

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/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { MeshGeometrySystem, InstancedMeshSystem, EnvironmentSyncSystem, PostprocessingSyncSystem, LightSyncSystem, MaterialSyncSystem, PhysicsSyncSystem, AnimationSystem, AudioSystem, VideoSystem, CameraSyncSystem, TransformSyncSystem, ObjectVisibilitySystem, RenderSystem, spawnPrefab, despawnPrefab, buildNodeKeyMapsFromSnapshot, indexSnapshotByEntity, findTopLevelNestedRegions, collectSnapshotSubtreeIds, groupOverridePaths, OVERRIDE_DIFF_EXCLUDED_COMPONENTS, OVERRIDE_ROOT_EXCLUDED_COMPONENTS, canonicalizeComponentValue, mergeComponentValue, decanonicalizeComponentValue, computeInstanceOverrides, collectEntitySubtree, ADDED_NODE_MARKER_COMPONENT } from './chunk-GZ2NKSMV.js';
2
- export { ADDED_NODE_MARKER_COMPONENT, AnimationSystem, AudioSystem, Behaviour, CameraSyncSystem, EnvironmentSyncSystem, HYDRATABLE_ENTITY_KINDS, InputSystem, InstancedMeshSystem, KeyboardKeys, LightSyncSystem, MaterialSyncSystem, MeshGeometrySystem, NODE_KEY_SEPARATOR, OVERRIDE_DIFF_EXCLUDED_COMPONENTS, OVERRIDE_ROOT_EXCLUDED_COMPONENTS, ObjectVisibilitySystem, PREFAB_FILE_VERSION, PREFAB_FILE_VERSION_NESTED, PhysicsSyncSystem, PostprocessingSyncSystem, RenderSystem, SCRIPT_OBJECT_ROOT_FLAG, ScriptBehaviourSystem, ScriptObject, ScriptObjectSystem, TransformSyncSystem, VideoSystem, buildComponentValueMaps, buildNodeKeyMapsFromSnapshot, buildNodeMapsFromSnapshot, canonicalizeComponentValue, clearPrefabCache, collectDiffPaths, collectEntitySubtree, collectSnapshotSubtreeIds, computeInstanceOverrides, decanonicalizeComponentValue, despawnPrefab, encodeOverride, findSnapshotRootEntityIds, findTopLevelNestedRegions, groupOverridePaths, hydrateWorldFromSnapshot, hydrateWorldFromSnapshotDetailed, indexSnapshotByEntity, instantiateSubtree, loadPrefab, mergeComponentValue, parseOverride, parsePrefabAsset, prefabFileVersionFor, restoreComponentState, restorePostCreateComponents, setPrefabAssetUrlResolver, spawnPrefab } from './chunk-GZ2NKSMV.js';
1
+ import { MeshGeometrySystem, InstancedMeshSystem, EnvironmentSyncSystem, PostprocessingSyncSystem, LightSyncSystem, MaterialSyncSystem, PhysicsSyncSystem, AnimationSystem, AudioSystem, VideoSystem, CameraSyncSystem, TransformSyncSystem, ObjectVisibilitySystem, RenderSystem, spawnPrefab, despawnPrefab, buildNodeKeyMapsFromSnapshot, indexSnapshotByEntity, findTopLevelNestedRegions, collectSnapshotSubtreeIds, groupOverridePaths, OVERRIDE_DIFF_EXCLUDED_COMPONENTS, OVERRIDE_ROOT_EXCLUDED_COMPONENTS, canonicalizeComponentValue, mergeComponentValue, decanonicalizeComponentValue, computeInstanceOverrides, collectEntitySubtree, ADDED_NODE_MARKER_COMPONENT } from './chunk-CCE5K2TU.js';
2
+ export { ADDED_NODE_MARKER_COMPONENT, AnimationSystem, AudioSystem, Behaviour, CameraSyncSystem, EnvironmentSyncSystem, HYDRATABLE_ENTITY_KINDS, InputSystem, InstancedMeshSystem, KeyboardKeys, LightSyncSystem, MaterialSyncSystem, MeshGeometrySystem, NODE_KEY_SEPARATOR, OVERRIDE_DIFF_EXCLUDED_COMPONENTS, OVERRIDE_ROOT_EXCLUDED_COMPONENTS, ObjectVisibilitySystem, PREFAB_FILE_VERSION, PREFAB_FILE_VERSION_NESTED, PhysicsSyncSystem, PostprocessingSyncSystem, RenderSystem, SCRIPT_OBJECT_ROOT_FLAG, ScriptBehaviourSystem, ScriptObject, ScriptObjectSystem, TransformSyncSystem, VideoSystem, buildComponentValueMaps, buildNodeKeyMapsFromSnapshot, buildNodeMapsFromSnapshot, canonicalizeComponentValue, clearPrefabCache, collectDiffPaths, collectEntitySubtree, collectSnapshotSubtreeIds, computeInstanceOverrides, decanonicalizeComponentValue, despawnPrefab, encodeOverride, findSnapshotRootEntityIds, findTopLevelNestedRegions, groupOverridePaths, hydrateWorldFromSnapshot, hydrateWorldFromSnapshotDetailed, indexSnapshotByEntity, instantiateSubtree, loadPrefab, mergeComponentValue, parseOverride, parsePrefabAsset, prefabFileVersionFor, restoreComponentState, restorePostCreateComponents, setPrefabAssetUrlResolver, spawnPrefab } from './chunk-CCE5K2TU.js';
3
3
  import { registerDefaultFactories } from './chunk-52CHDPRQ.js';
4
4
  export { createAnimationComponent, createAudioComponent, createCameraComponent, createEntityTypeComponent, createEnvironmentComponent, createImageComponent, createImageTargetAnchorComponent, createImageTargetComponent, createInstanceSlotComponent, createInstancedMeshComponent, createLightComponent, createMaterialComponent, createMeshComponent, createMeshGeometryComponent, createModelComponent, createNameComponent, createObject3DRefComponent, createParentIdComponent, createPhysicsComponent, createPostprocessingComponent, createScriptComponent, createScriptObjectComponent, createSelectableComponent, createSelectionTransformStateComponent, createSpriteComponent, createTransformComponent, createVideoComponent, registerDefaultComponentFactories, registerDefaultFactories } from './chunk-52CHDPRQ.js';
5
5
  import { ObjectManagementSystem } from './chunk-WKGCC7YL.js';
@@ -3013,82 +3013,108 @@ function createColliderDesc(RAPIER, values, triMeshData) {
3013
3013
  return desc;
3014
3014
  }
3015
3015
  __name(createColliderDesc, "createColliderDesc");
3016
- function findFirstMeshWithGeometry(object) {
3017
- if (object instanceof THREE14__namespace.Mesh && object.geometry) {
3018
- return object;
3016
+ function collectTriMeshParts(object) {
3017
+ if (!object) {
3018
+ return null;
3019
3019
  }
3020
- let found = null;
3020
+ object.updateWorldMatrix(true, true);
3021
+ const worldScale = new THREE14__namespace.Vector3();
3022
+ object.getWorldScale(worldScale);
3023
+ const scale = {
3024
+ x: Math.abs(worldScale.x) || 1,
3025
+ y: Math.abs(worldScale.y) || 1,
3026
+ z: Math.abs(worldScale.z) || 1
3027
+ };
3028
+ const worldToObject = new THREE14__namespace.Matrix4().copy(object.matrixWorld).invert();
3029
+ const parts = [];
3021
3030
  object.traverse((child) => {
3022
- if (found) {
3031
+ if (!(child instanceof THREE14__namespace.Mesh) || !child.geometry) {
3023
3032
  return;
3024
3033
  }
3025
- if (child instanceof THREE14__namespace.Mesh && child.geometry) {
3026
- found = child;
3034
+ const geometry = child.geometry;
3035
+ const positionAttribute = geometry.getAttribute("position");
3036
+ if (!positionAttribute || positionAttribute.itemSize < 3) {
3037
+ return;
3027
3038
  }
3039
+ const positionCount = positionAttribute.count;
3040
+ if (positionCount < 3) {
3041
+ return;
3042
+ }
3043
+ const indexAttribute = geometry.getIndex();
3044
+ const indexCount = indexAttribute ? indexAttribute.count : Math.floor(positionCount / 3) * 3;
3045
+ if (indexCount < 3) {
3046
+ return;
3047
+ }
3048
+ const relativeMatrix = new THREE14__namespace.Matrix4().multiplyMatrices(
3049
+ worldToObject,
3050
+ child.matrixWorld
3051
+ );
3052
+ const matrixSignature = relativeMatrix.elements.map((value) => value.toFixed(5)).join(",");
3053
+ parts.push({
3054
+ positionAttribute,
3055
+ indexAttribute,
3056
+ positionCount,
3057
+ indexCount,
3058
+ relativeMatrix,
3059
+ signature: `${child.uuid}:${geometry.uuid}:${positionCount}:${indexCount}:${matrixSignature}`
3060
+ });
3028
3061
  });
3029
- return found;
3030
- }
3031
- __name(findFirstMeshWithGeometry, "findFirstMeshWithGeometry");
3032
- function createTriMeshData(object) {
3033
- if (!object) {
3062
+ if (parts.length === 0) {
3034
3063
  return null;
3035
3064
  }
3036
- const mesh = findFirstMeshWithGeometry(object);
3037
- if (!mesh) {
3065
+ const scaleSignature = `${scale.x.toFixed(5)},${scale.y.toFixed(5)},${scale.z.toFixed(5)}`;
3066
+ return {
3067
+ parts,
3068
+ scale,
3069
+ signature: `${scaleSignature}|${parts.map((p) => p.signature).join("|")}`
3070
+ };
3071
+ }
3072
+ __name(collectTriMeshParts, "collectTriMeshParts");
3073
+ function createTriMeshData(collected) {
3074
+ if (!collected) {
3038
3075
  return null;
3039
3076
  }
3040
- const geometry = mesh.geometry;
3041
- const positionAttribute = geometry.getAttribute("position");
3042
- if (!positionAttribute || positionAttribute.itemSize < 3) {
3043
- return null;
3077
+ const { parts, scale } = collected;
3078
+ let vertexTotal = 0;
3079
+ let indexTotal = 0;
3080
+ for (const part of parts) {
3081
+ vertexTotal += part.positionCount;
3082
+ indexTotal += part.indexCount;
3044
3083
  }
3045
- const positionCount = positionAttribute.count;
3046
- if (positionCount < 3) {
3084
+ if (vertexTotal < 3 || indexTotal < 3) {
3047
3085
  return null;
3048
3086
  }
3049
- object.updateWorldMatrix(true, true);
3050
- mesh.updateWorldMatrix(true, true);
3051
- const worldToObject = new THREE14__namespace.Matrix4().copy(object.matrixWorld).invert();
3052
- const relativeMatrix = new THREE14__namespace.Matrix4().multiplyMatrices(
3053
- worldToObject,
3054
- mesh.matrixWorld
3055
- );
3056
- const vertices = new Float32Array(positionCount * 3);
3087
+ const vertices = new Float32Array(vertexTotal * 3);
3088
+ const indices = new Uint32Array(indexTotal);
3057
3089
  const point = new THREE14__namespace.Vector3();
3058
- for (let index = 0; index < positionCount; index += 1) {
3059
- point.set(
3060
- positionAttribute.getX(index),
3061
- positionAttribute.getY(index),
3062
- positionAttribute.getZ(index)
3063
- ).applyMatrix4(relativeMatrix);
3064
- const outputIndex = index * 3;
3065
- vertices[outputIndex] = point.x;
3066
- vertices[outputIndex + 1] = point.y;
3067
- vertices[outputIndex + 2] = point.z;
3068
- }
3069
- const indexAttribute = geometry.getIndex();
3070
- let indices;
3071
- if (indexAttribute) {
3072
- indices = new Uint32Array(indexAttribute.count);
3073
- for (let index = 0; index < indexAttribute.count; index += 1) {
3074
- indices[index] = indexAttribute.getX(index);
3075
- }
3076
- } else {
3077
- const triangleIndexCount = Math.floor(positionCount / 3) * 3;
3078
- if (triangleIndexCount < 3) {
3079
- return null;
3080
- }
3081
- indices = new Uint32Array(triangleIndexCount);
3082
- for (let index = 0; index < triangleIndexCount; index += 1) {
3083
- indices[index] = index;
3090
+ let vertexOffset = 0;
3091
+ let indexOffset = 0;
3092
+ for (const part of parts) {
3093
+ const { positionAttribute, indexAttribute, relativeMatrix } = part;
3094
+ for (let index = 0; index < part.positionCount; index += 1) {
3095
+ point.set(
3096
+ positionAttribute.getX(index),
3097
+ positionAttribute.getY(index),
3098
+ positionAttribute.getZ(index)
3099
+ ).applyMatrix4(relativeMatrix);
3100
+ const outputIndex = (vertexOffset + index) * 3;
3101
+ vertices[outputIndex] = point.x * scale.x;
3102
+ vertices[outputIndex + 1] = point.y * scale.y;
3103
+ vertices[outputIndex + 2] = point.z * scale.z;
3104
+ }
3105
+ if (indexAttribute) {
3106
+ for (let index = 0; index < part.indexCount; index += 1) {
3107
+ indices[indexOffset + index] = vertexOffset + indexAttribute.getX(index);
3108
+ }
3109
+ } else {
3110
+ for (let index = 0; index < part.indexCount; index += 1) {
3111
+ indices[indexOffset + index] = vertexOffset + index;
3112
+ }
3084
3113
  }
3114
+ vertexOffset += part.positionCount;
3115
+ indexOffset += part.indexCount;
3085
3116
  }
3086
- const matrixSignature = relativeMatrix.elements.map((value) => value.toFixed(5)).join(",");
3087
- return {
3088
- vertices,
3089
- indices,
3090
- signature: `${mesh.uuid}:${geometry.uuid}:${positionCount}:${indices.length}:${matrixSignature}`
3091
- };
3117
+ return { vertices, indices, signature: collected.signature };
3092
3118
  }
3093
3119
  __name(createTriMeshData, "createTriMeshData");
3094
3120
  function syncBodyFromTransform(body, transform) {
@@ -3265,7 +3291,7 @@ var _PhysicsSyncSystem = class _PhysicsSyncSystem {
3265
3291
  syncBodyFromTransform(body, transform);
3266
3292
  }
3267
3293
  const effectiveCollider = computeColliderState(physics.values.collider, object);
3268
- const triMeshData = effectiveCollider.shape === "trimesh" ? createTriMeshData(object) : null;
3294
+ const triMeshParts = effectiveCollider.shape === "trimesh" ? collectTriMeshParts(object) : null;
3269
3295
  if (!effectiveCollider || !effectiveCollider.enabled) {
3270
3296
  const colliderHandle = this.colliderHandles.get(entityId);
3271
3297
  if (typeof colliderHandle === "number") {
@@ -3279,7 +3305,7 @@ var _PhysicsSyncSystem = class _PhysicsSyncSystem {
3279
3305
  physics.collider = null;
3280
3306
  continue;
3281
3307
  }
3282
- const colliderSignature = `${JSON.stringify(effectiveCollider)}:${triMeshData?.signature ?? ""}`;
3308
+ const colliderSignature = `${JSON.stringify(effectiveCollider)}:${triMeshParts?.signature ?? ""}`;
3283
3309
  const previousColliderSignature = this.colliderSignatures.get(entityId);
3284
3310
  if (!this.colliderHandles.has(entityId) || previousColliderSignature !== colliderSignature) {
3285
3311
  const previousColliderHandle = this.colliderHandles.get(entityId);
@@ -3292,7 +3318,7 @@ var _PhysicsSyncSystem = class _PhysicsSyncSystem {
3292
3318
  const colliderDesc = createColliderDesc(
3293
3319
  this.rapier,
3294
3320
  effectiveCollider,
3295
- triMeshData
3321
+ createTriMeshData(triMeshParts)
3296
3322
  );
3297
3323
  const createdCollider = physicsWorld.createCollider(colliderDesc, body);
3298
3324
  this.colliderHandles.set(entityId, createdCollider.handle);
@@ -6906,9 +6932,9 @@ var _ScriptObjectSystem = class _ScriptObjectSystem {
6906
6932
  } catch {
6907
6933
  instance = new ctor(world, entityId, currentData);
6908
6934
  }
6909
- const defaults = normalizeScriptData(instance.data);
6910
- const merged = mergeDataByCurrentDefaults(defaults, currentData);
6911
- instance.data = resolveInputData(world, merged);
6935
+ const literalDefaults = normalizeScriptData(instance.data);
6936
+ const seed = Object.keys(literalDefaults).length > 0 ? mergeDataByCurrentDefaults(literalDefaults, currentData) : { ...currentData };
6937
+ instance.data = resolveInputData(world, seed);
6912
6938
  const parent = world.getComponent(entityId, Object3DRef)?.object;
6913
6939
  if (!parent) {
6914
6940
  throw new Error("Script object entity has no Object3D to attach to.");
@@ -6938,6 +6964,13 @@ var _ScriptObjectSystem = class _ScriptObjectSystem {
6938
6964
  built.userData[SCRIPT_OBJECT_ROOT_FLAG] = true;
6939
6965
  parent.add(built);
6940
6966
  instance.root = built;
6967
+ const postBuildData = normalizeScriptData(instance.data);
6968
+ const schemaDefaults = { ...literalDefaults };
6969
+ for (const [key2, value] of Object.entries(postBuildData)) {
6970
+ if (!(key2 in schemaDefaults)) schemaDefaults[key2] = value;
6971
+ }
6972
+ const merged = mergeDataByCurrentDefaults(schemaDefaults, currentData);
6973
+ instance.data = resolveInputData(world, merged);
6941
6974
  const stored = serializeDataForStorage(merged, merged);
6942
6975
  component.data = stored;
6943
6976
  component.runtimeState = "ready";