@needle-tools/engine 2.65.2-pre → 2.66.1-pre

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 (93) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/dist/needle-engine.js +7866 -7756
  3. package/dist/needle-engine.umd.cjs +223 -223
  4. package/lib/engine/debug/debug_overlay.js +4 -1
  5. package/lib/engine/debug/debug_overlay.js.map +1 -1
  6. package/lib/engine/engine_addressables.js +2 -2
  7. package/lib/engine/engine_addressables.js.map +1 -1
  8. package/lib/engine/engine_element.d.ts +1 -0
  9. package/lib/engine/engine_element.js +4 -1
  10. package/lib/engine/engine_element.js.map +1 -1
  11. package/lib/engine/engine_element_loading.d.ts +3 -2
  12. package/lib/engine/engine_element_loading.js +18 -14
  13. package/lib/engine/engine_element_loading.js.map +1 -1
  14. package/lib/engine/engine_gameobject.js +6 -3
  15. package/lib/engine/engine_gameobject.js.map +1 -1
  16. package/lib/engine/engine_gizmos.js +3 -1
  17. package/lib/engine/engine_gizmos.js.map +1 -1
  18. package/lib/engine/engine_networking.d.ts +3 -1
  19. package/lib/engine/engine_networking.js +10 -8
  20. package/lib/engine/engine_networking.js.map +1 -1
  21. package/lib/engine/engine_physics.d.ts +29 -1
  22. package/lib/engine/engine_physics.js +99 -10
  23. package/lib/engine/engine_physics.js.map +1 -1
  24. package/lib/engine/engine_setup.js +3 -0
  25. package/lib/engine/engine_setup.js.map +1 -1
  26. package/lib/engine/extensions/NEEDLE_render_objects.js +9 -0
  27. package/lib/engine/extensions/NEEDLE_render_objects.js.map +1 -1
  28. package/lib/engine-components/Animator.js +0 -1
  29. package/lib/engine-components/Animator.js.map +1 -1
  30. package/lib/engine-components/CharacterController.d.ts +1 -0
  31. package/lib/engine-components/CharacterController.js +14 -9
  32. package/lib/engine-components/CharacterController.js.map +1 -1
  33. package/lib/engine-components/Collider.js +14 -1
  34. package/lib/engine-components/Collider.js.map +1 -1
  35. package/lib/engine-components/ParticleSystem.d.ts +5 -1
  36. package/lib/engine-components/ParticleSystem.js +41 -6
  37. package/lib/engine-components/ParticleSystem.js.map +1 -1
  38. package/lib/engine-components/ParticleSystemModules.d.ts +2 -0
  39. package/lib/engine-components/ParticleSystemModules.js +26 -0
  40. package/lib/engine-components/ParticleSystemModules.js.map +1 -1
  41. package/lib/engine-components/ParticleSystemSubEmitter.js +5 -2
  42. package/lib/engine-components/ParticleSystemSubEmitter.js.map +1 -1
  43. package/lib/engine-components/Renderer.js +9 -5
  44. package/lib/engine-components/Renderer.js.map +1 -1
  45. package/lib/engine-components/ScreenCapture.js +3 -3
  46. package/lib/engine-components/ScreenCapture.js.map +1 -1
  47. package/lib/engine-components/SpectatorCamera.js +3 -3
  48. package/lib/engine-components/SpectatorCamera.js.map +1 -1
  49. package/lib/engine-components/SyncedCamera.js +1 -1
  50. package/lib/engine-components/SyncedCamera.js.map +1 -1
  51. package/lib/engine-components/SyncedTransform.js +2 -2
  52. package/lib/engine-components/SyncedTransform.js.map +1 -1
  53. package/lib/engine-components/TestRunner.js +1 -1
  54. package/lib/engine-components/TestRunner.js.map +1 -1
  55. package/lib/engine-components/WebARSessionRoot.js +3 -2
  56. package/lib/engine-components/WebARSessionRoot.js.map +1 -1
  57. package/lib/engine-components/WebXRAvatar.js.map +1 -1
  58. package/lib/engine-components/WebXRGrabRendering.js +2 -2
  59. package/lib/engine-components/WebXRGrabRendering.js.map +1 -1
  60. package/lib/engine-components/WebXRSync.js +2 -2
  61. package/lib/engine-components/WebXRSync.js.map +1 -1
  62. package/lib/engine-components-experimental/networking/PlayerSync.js +1 -1
  63. package/lib/engine-components-experimental/networking/PlayerSync.js.map +1 -1
  64. package/package.json +1 -1
  65. package/plugins/vite/meta.js +3 -0
  66. package/plugins/vite/poster-client.js +6 -4
  67. package/src/engine/debug/debug_overlay.ts +4 -1
  68. package/src/engine/engine_addressables.ts +2 -2
  69. package/src/engine/engine_element.ts +8 -1
  70. package/src/engine/engine_element_loading.ts +18 -14
  71. package/src/engine/engine_gameobject.ts +584 -583
  72. package/src/engine/engine_gizmos.ts +3 -2
  73. package/src/engine/engine_networking.ts +10 -8
  74. package/src/engine/engine_physics.ts +113 -10
  75. package/src/engine/engine_setup.ts +4 -0
  76. package/src/engine/extensions/NEEDLE_render_objects.ts +10 -1
  77. package/src/engine-components/Animator.ts +0 -1
  78. package/src/engine-components/CharacterController.ts +12 -9
  79. package/src/engine-components/Collider.ts +16 -2
  80. package/src/engine-components/ParticleSystem.ts +45 -9
  81. package/src/engine-components/ParticleSystemModules.ts +28 -1
  82. package/src/engine-components/ParticleSystemSubEmitter.ts +5 -3
  83. package/src/engine-components/Renderer.ts +14 -11
  84. package/src/engine-components/ScreenCapture.ts +3 -3
  85. package/src/engine-components/SpectatorCamera.ts +3 -3
  86. package/src/engine-components/SyncedCamera.ts +1 -1
  87. package/src/engine-components/SyncedTransform.ts +2 -2
  88. package/src/engine-components/TestRunner.ts +1 -1
  89. package/src/engine-components/WebARSessionRoot.ts +3 -2
  90. package/src/engine-components/WebXRAvatar.ts +0 -1
  91. package/src/engine-components/WebXRGrabRendering.ts +2 -2
  92. package/src/engine-components/WebXRSync.ts +2 -2
  93. package/src/engine-components-experimental/networking/PlayerSync.ts +1 -1
@@ -1,584 +1,585 @@
1
- import { Object3D } from "three";
2
- import { processNewScripts } from "./engine_mainloop_utils";
3
- import { InstantiateIdProvider } from "./engine_networking_instantiate";
4
- import { Context, registerComponent } from "./engine_setup";
5
- import { logHierarchy, setWorldPosition, setWorldQuaternion } from "./engine_three_utils";
6
- import { GuidsMap, IComponent as Component, IComponent, IGameObject as GameObject, UIDProvider } from "./engine_types";
7
- import { getParam, tryFindObject } from "./engine_utils";
8
- import { apply } from "../engine-components/js-extensions/Object3D";
9
- import { $isUsingInstancing, InstancingUtil } from "./engine_instancing";
10
- import { activeInHierarchyFieldName } from "./engine_constants";
11
- import { assign } from "./engine_serialization_core";
12
-
13
- const debug = getParam("debuggetcomponent");
14
-
15
-
16
- export enum HideFlags {
17
- None = 0,
18
- HideInHierarchy = 1,
19
- HideInInspector = 2,
20
- DontSaveInEditor = 4,
21
- NotEditable = 8,
22
- DontSaveInBuild = 16, // 0x00000010
23
- DontUnloadUnusedAsset = 32, // 0x00000020
24
- DontSave = DontUnloadUnusedAsset | DontSaveInBuild | DontSaveInEditor, // 0x00000034
25
- HideAndDontSave = DontSave | NotEditable | HideInHierarchy, // 0x0000003D
26
- }
27
-
28
-
29
- export class InstantiateOptions {
30
- idProvider?: UIDProvider | undefined;
31
-
32
- //** parent guid */
33
- parent?: string | undefined | Object3D;
34
- /** for duplicatable parenting */
35
- keepWorldPosition?: boolean
36
- position?: THREE.Vector3 | undefined;
37
- rotation?: THREE.Quaternion | undefined;
38
- scale?: THREE.Vector3 | undefined;
39
-
40
- visible?: boolean | undefined;
41
-
42
- context?: Context | undefined;
43
- }
44
-
45
-
46
- // export function setActive(go: Object3D, active: boolean, processStart: boolean = true) {
47
- // if (!go) return;
48
- // go.visible = active;
49
- // main.updateActiveInHierarchyWithoutEventCall(go);
50
- // if (active && processStart)
51
- // main.processStart(Context.Current, go);
52
- // }
53
-
54
-
55
- // Object.defineProperty(Object3D.prototype, "visible", {
56
- // get: function () {
57
- // return this._visible;
58
- // },
59
- // set: function (val) {
60
- // // const changed = val !== this._visible;
61
- // this._visible = val;
62
- // // if (changed) {
63
- // // setActive(this, val);
64
- // // }
65
- // }
66
- // });
67
-
68
-
69
- const $isActive = Symbol("isActive");
70
- export function isActiveSelf(go: Object3D): boolean {
71
- // if (go[$isActive] === undefined) {
72
- // go[$isActive] = true;
73
- // }
74
- return go.visible;
75
- }
76
-
77
- export function setActive(go: Object3D, active: boolean | number): boolean {
78
- if (typeof active === "number") active = active > .5;
79
- // go[$isActive] = active;
80
- go.visible = active;
81
- return go.visible;// go[$isActive];
82
- }
83
-
84
- export function isActiveInHierarchy(go: Object3D): boolean {
85
- return go[activeInHierarchyFieldName] || isUsingInstancing(go);
86
- }
87
-
88
- export function markAsInstancedRendered(go: THREE.Object3D, instanced: boolean) {
89
- go[$isUsingInstancing] = instanced;
90
- }
91
-
92
- export function isUsingInstancing(instance: THREE.Object3D): boolean { return InstancingUtil.isUsingInstancing(instance); }
93
-
94
-
95
- export function findByGuid(guid: string, hierarchy: THREE.Object3D): GameObject | IComponent | null | undefined {
96
- return tryFindObject(guid, hierarchy, true, true);
97
- }
98
-
99
-
100
- const $isDestroyed = Symbol("isDestroyed");
101
- export function isDestroyed(go: Object3D): boolean {
102
- return go[$isDestroyed];
103
- }
104
- export function setDestroyed(go: Object3D, value: boolean) {
105
- go[$isDestroyed] = value;
106
- }
107
-
108
- export function destroy(instance: Object3D | Component, recursive: boolean = true, dispose: boolean = false) {
109
- internalDestroy(instance, recursive, dispose, true);
110
- }
111
-
112
- function internalDestroy(instance: Object3D | Component, recursive: boolean = true, dispose: boolean = false, isRoot: boolean = true) {
113
- const comp = instance as Component;
114
- if (comp.isComponent) {
115
- comp.__internalDisable();
116
- comp.__internalDestroy();
117
- return;
118
- }
119
-
120
-
121
- const obj = instance as GameObject;
122
- if (dispose) disposeObject(obj);
123
- setDestroyed(obj, true);
124
-
125
-
126
- if (debug) console.log(obj);
127
-
128
- if (recursive && obj.children) {
129
- for (const ch of obj.children) {
130
- internalDestroy(ch, recursive, false, false);
131
- }
132
- }
133
-
134
- const components = obj.userData.components;
135
- if (components) {
136
- let lastLength = components.length;
137
- for (let i = 0; i < components.length; i++) {
138
- const comp: Component = components[i];
139
- comp.__internalDisable();
140
- comp.__internalDestroy();
141
- // if (comp.destroy) {
142
- // if (debug) console.log("destroying", comp);
143
- // comp.destroy();
144
- // }
145
- // components will be removed from componentlist in destroy
146
- if (components.length < lastLength) {
147
- lastLength = components.length;
148
- i--;
149
- }
150
- }
151
- }
152
- if (isRoot)
153
- obj.removeFromParent();
154
- }
155
-
156
- function disposeObject(obj: Object3D) {
157
- if (!obj) return;
158
- const mesh = obj as THREE.Mesh;
159
- if (mesh.geometry) {
160
- mesh.geometry.dispose();
161
- }
162
- if (mesh.material) {
163
- if (Array.isArray(mesh.material)) {
164
- mesh.material.forEach(m => m.dispose());
165
- } else {
166
- mesh.material.dispose();
167
- }
168
- }
169
- }
170
-
171
- declare type ForEachComponentCallback = (comp: Component) => any;
172
-
173
- export function foreachComponent(instance: THREE.Object3D, cb: ForEachComponentCallback, recursive: boolean = true): any {
174
- return internalForEachComponent(instance, cb, recursive);
175
- }
176
-
177
- function internalForEachComponent(instance: Object3D, cb: ForEachComponentCallback, recursive: boolean, level: number = 0): any {
178
- if (!instance) return;
179
- if (!instance.isObject3D) {
180
- new Error("Expected Object3D but got " + instance);
181
- }
182
- if (level > 1000) {
183
- console.warn("Failed to iterate components: too many levels");
184
- return;
185
- }
186
- if (instance.userData?.components) {
187
- for (let i = 0; i < instance.userData.components.length; i++) {
188
- const comp = instance.userData.components[i];
189
- if (comp?.isComponent === true) {
190
- const res = cb(comp);
191
- if (res !== undefined) return res;
192
- }
193
- }
194
- }
195
-
196
- if (recursive && instance.children) {
197
- // childArrayBuffer.length = 0;
198
- // childArrayBuffer.push(...instance.children);
199
- const nextLevel = level + 1;
200
- for (let i = 0; i < instance.children.length; i++) {
201
- const child = instance.children[i];
202
- if (!child) continue;
203
- const res = internalForEachComponent(child, cb, recursive, nextLevel);
204
- if (res !== undefined) return res;
205
- }
206
- // childArrayBuffer.length = 0;
207
- }
208
-
209
- }
210
-
211
- declare class NewGameObjectReferenceInfo {
212
- original: THREE.Object3D;
213
- clone: THREE.Object3D;
214
- }
215
-
216
- export function instantiate(instance: GameObject | Object3D | null, opts: InstantiateOptions | null = null): GameObject | null {
217
- if (instance === null) return null;
218
-
219
- let options: InstantiateOptions | null = null;
220
- if (opts !== null) {
221
- // if x is defined assume this is a vec3 - this is just to not break everything at once and stay a little bit backwards compatible
222
- if (opts["x"] !== undefined) {
223
- options = new InstantiateOptions();
224
- options.position = opts as unknown as THREE.Vector3;
225
- }
226
- else {
227
- // if (opts instanceof InstantiateOptions)
228
- options = opts as InstantiateOptions;
229
- // else {
230
- // options = new InstantiateOptions();
231
- // Object.assign(options, opts);
232
- // }
233
- }
234
- }
235
-
236
- let context = Context.Current;
237
- if (options?.context) context = options.context;
238
- if (debug && context.alias)
239
- console.log("context", context.alias);
240
-
241
- // we need to create the id provider before calling internal instantiate because cloned gameobjects also create new guids
242
- if (options && !options.idProvider) {
243
- options.idProvider = new InstantiateIdProvider(Date.now());
244
- }
245
-
246
- const components: Array<Component> = [];
247
- const goMapping: { [key: string]: NewGameObjectReferenceInfo } = {}; // used to resolve references on components to components on other gameobjects to their new counterpart
248
- const skinnedMeshes: { [key: string]: NewGameObjectReferenceInfo } = {};
249
- const clone = internalInstantiate(context, instance, options, components, goMapping, skinnedMeshes);
250
-
251
- if (clone) {
252
- resolveReferences(goMapping);
253
- resolveAndBindSkinnedMeshBones(skinnedMeshes, goMapping);
254
- }
255
-
256
- if (debug) {
257
- logHierarchy(instance, true);
258
- logHierarchy(clone, true);
259
- }
260
-
261
- const guidsMap: GuidsMap = {};
262
- for (const i in components) {
263
- const copy = components[i];
264
- const oldGuid = copy.guid;
265
- if (options && options.idProvider) {
266
- copy.guid = options.idProvider.generateUUID();
267
- guidsMap[oldGuid] = copy.guid;
268
- if (debug)
269
- console.log(copy.name, copy.guid)
270
- }
271
- registerComponent(copy, context);
272
- if (copy.__internalNewInstanceCreated)
273
- copy.__internalNewInstanceCreated();
274
- }
275
- for (const i in components) {
276
- const copy = components[i];
277
- if (copy.resolveGuids)
278
- copy.resolveGuids(guidsMap);
279
- if (copy.enabled === false) continue;
280
- else copy.enabled = true;
281
- }
282
-
283
- processNewScripts(context);
284
-
285
- return clone as GameObject;
286
- }
287
-
288
-
289
- function internalInstantiate(
290
- context: Context, instance: GameObject | THREE.Object3D, opts: InstantiateOptions | null,
291
- componentsList: Array<Component>,
292
- newGameObjectsMap: { [key: string]: NewGameObjectReferenceInfo },
293
- skinnedMeshesMap: { [key: string]: NewGameObjectReferenceInfo }
294
- )
295
- : GameObject | THREE.Object3D | null {
296
- if (!instance) return null;
297
- // prepare, remove things that dont work out of the box
298
- // e.g. user data we want to manually clone
299
- // also children throw errors (e.g. recursive toJson with nested meshes)
300
- const userData = instance.userData;
301
- instance.userData = {};
302
- const children = instance.children;
303
- instance.children = [];
304
- let clone: THREE.Object3D | GameObject;
305
- clone = instance.clone(false);
306
- apply(clone);
307
- instance.userData = userData;
308
- instance.children = children;
309
-
310
- // make reference from old id to new object
311
- newGameObjectsMap[instance.uuid] = { original: instance, clone: clone };
312
-
313
- if (instance.type === "SkinnedMesh") {
314
- skinnedMeshesMap[instance.uuid] = { original: instance, clone: clone };
315
- }
316
-
317
- // DO NOT EVER RENAME BECAUSE IT BREAKS / MIGHT BREAK ANIMATIONS
318
- // clone.name += " (Clone)";
319
-
320
- if (opts?.visible !== undefined)
321
- clone.visible = opts.visible;
322
-
323
- if (opts?.idProvider) {
324
- clone.uuid = opts.idProvider.generateUUID();
325
- const cloneGo: GameObject = clone as GameObject;
326
- if (cloneGo) cloneGo.guid = clone.uuid;
327
- }
328
-
329
- if (instance.animations && instance.animations.length > 0) {
330
- clone.animations = [...instance.animations];
331
- }
332
-
333
- const parent = instance.parent;
334
- if (parent) {
335
- parent.add(clone);
336
- }
337
-
338
- // apply transform
339
- if (opts?.position) {
340
- setWorldPosition(clone, opts.position);
341
- }
342
- else clone.position.copy(instance.position);
343
- if (opts?.rotation) {
344
- setWorldQuaternion(clone, opts.rotation);
345
- // clone.quaternion.copy(opts.rotation);
346
- }
347
- else clone.quaternion.copy(instance.quaternion);
348
- if (opts?.scale) {
349
- // TODO: make set world scale work
350
- clone.scale.copy(opts.scale);
351
- }
352
- else clone.scale.copy(instance.scale);
353
-
354
- if (opts?.parent && opts.parent !== "scene") {
355
- let requestedParent: Object3D | null = null;
356
- if (typeof opts.parent === "string") {
357
- requestedParent = tryFindObject(opts.parent, context.scene, true);
358
- }
359
- else {
360
- requestedParent = opts.parent;
361
- }
362
- if (requestedParent) {
363
- const func = opts.keepWorldPosition === true ? requestedParent.attach : requestedParent.add;
364
- if (!func) console.error("Invalid parent object", requestedParent, "received when instantiating:", instance);
365
- else func.call(requestedParent, clone);
366
- }
367
- else console.warn("could not find parent:", opts.parent);
368
- }
369
-
370
- for (const [key, value] of Object.entries(instance.userData)) {
371
- if (key === "components") continue;
372
- clone.userData[key] = value;
373
- }
374
-
375
- if (instance.userData?.components) {
376
- const components = instance.userData.components;
377
- const newComponents: Component[] = [];
378
- clone.userData.components = newComponents;
379
- for (let i = 0; i < components.length; i++) {
380
- const comp = components[i];
381
- const copy = Object.create(comp);
382
- assign(copy, comp);
383
- newComponents.push(copy);
384
- copy.gameObject = clone;
385
- // copy.transform = clone;
386
- componentsList.push(copy);
387
- }
388
- }
389
-
390
- // children should just clone the original transform
391
- if (opts) {
392
- opts.position = undefined;
393
- opts.rotation = undefined;
394
- opts.scale = undefined;
395
- opts.parent = undefined;
396
- }
397
-
398
- for (const ch in instance.children) {
399
- const child = instance.children[ch];
400
- const newChild = internalInstantiate(context, child as GameObject, opts, componentsList, newGameObjectsMap, skinnedMeshesMap);
401
- if (newChild)
402
- clone.add(newChild);
403
- }
404
-
405
- return clone;
406
-
407
- }
408
-
409
- function resolveAndBindSkinnedMeshBones(
410
- skinnedMeshes: { [key: string]: NewGameObjectReferenceInfo },
411
- newObjectsMap: { [key: string]: NewGameObjectReferenceInfo }
412
- ) {
413
- for (const key in skinnedMeshes) {
414
- const val = skinnedMeshes[key];
415
- const original = val.original as THREE.SkinnedMesh;
416
- const originalSkeleton = original.skeleton;
417
- const clone = val.clone as THREE.SkinnedMesh;
418
- // clone.updateWorldMatrix(true, true);
419
- if (!originalSkeleton) {
420
- console.warn("Skinned mesh has no skeleton?", val);
421
- continue;
422
- }
423
- const originalBones = originalSkeleton.bones;
424
- const clonedSkeleton = clone.skeleton.clone();
425
-
426
- clone.skeleton = clonedSkeleton;
427
- clone.bindMatrix.clone().copy(original.bindMatrix);
428
- // console.log(clone.bindMatrix)
429
- clone.bindMatrixInverse.copy(original.bindMatrixInverse);
430
- // clone.bindMatrix.multiplyScalar(.025);
431
- // console.assert(originalSkeleton.uuid !== clonedSkeleton.uuid);
432
- // console.assert(originalBones.length === clonedSkeleton.bones.length);
433
- const bones: Array<THREE.Bone> = [];
434
- clonedSkeleton.bones = bones;
435
- for (let i = 0; i < originalBones.length; i++) {
436
- const bone = originalBones[i];
437
- const newBoneInfo = newObjectsMap[bone.uuid];
438
- const clonedBone = newBoneInfo.clone as THREE.Bone;
439
- // console.log("NEW BONE: ", clonedBone, "BEFORE", newBoneInfo.original);
440
- bones.push(clonedBone);
441
- }
442
- // clone.skeleton = new THREE.Skeleton(bones);
443
- // clone.skeleton.update();
444
- // clone.pose();
445
- // clone.scale.set(1,1,1);
446
- // clone.position.y += .1;
447
- // console.log("ORIG", original, "CLONE", clone);
448
- }
449
- for (const key in skinnedMeshes) {
450
- const clone = skinnedMeshes[key].clone as THREE.SkinnedMesh;
451
- clone.skeleton.update();
452
- // clone.skeleton.calculateInverses();
453
- clone.bind(clone.skeleton, clone.bindMatrix);
454
- clone.updateMatrixWorld(true);
455
- // clone.pose();
456
- }
457
- }
458
-
459
- // private static bindNewSkinnedMeshBones(source, clone) {
460
- // const sourceLookup = new Map();
461
- // const cloneLookup = new Map();
462
- // // const clone = source.clone(false);
463
-
464
- // function parallelTraverse(a, b, callback) {
465
- // callback(a, b);
466
- // for (let i = 0; i < a.children.length; i++) {
467
- // parallelTraverse(a.children[i], b.children[i], callback);
468
- // }
469
- // }
470
- // parallelTraverse(source, clone, function (sourceNode, clonedNode) {
471
- // sourceLookup.set(clonedNode, sourceNode);
472
- // cloneLookup.set(sourceNode, clonedNode);
473
- // });
474
-
475
- // clone.traverse(function (node) {
476
- // if (!node.isSkinnedMesh) return;
477
- // const clonedMesh = node;
478
- // const sourceMesh = sourceLookup.get(node);
479
- // const sourceBones = sourceMesh.skeleton.bones;
480
-
481
- // clonedMesh.skeleton = sourceMesh.skeleton.clone();
482
- // clonedMesh.bindMatrix.copy(sourceMesh.bindMatrix);
483
-
484
- // clonedMesh.skeleton.bones = sourceBones.map(function (bone) {
485
- // return cloneLookup.get(bone);
486
- // });
487
- // clonedMesh.bind(clonedMesh.skeleton, clonedMesh.bindMatrix);
488
- // });
489
- // return clone;
490
-
491
- // }
492
-
493
- function resolveReferences(newObjectsMap: { [key: string]: NewGameObjectReferenceInfo }) {
494
- // for every object that is newly created we want to update references to their newly created counterparts
495
- // e.g. a collider instance referencing a rigidbody instance should be updated so that
496
- // the cloned collider does not reference the cloned rigidbody (instead of the original rigidbody)
497
- for (const key in newObjectsMap) {
498
- const val = newObjectsMap[key];
499
- const clone = val.clone;
500
- // resolve references
501
- if (clone.userData?.components) {
502
- for (let i = 0; i < clone.userData.components.length; i++) {
503
- const copy = clone.userData.components[i];
504
- // find referenced within a cloned gameobject
505
- const entries = Object.entries(copy);
506
- // console.log(copy, entries);
507
- for (const [key, value] of entries) {
508
- if (Array.isArray(value)) {
509
- const clonedArray: Array<any> = [];
510
- copy[key] = clonedArray;
511
- // console.log(copy, key, value, copy[key]);
512
- for (let i = 0; i < value.length; i++) {
513
- const entry = value[i];
514
- // push value types into new array
515
- if (typeof entry !== "object") {
516
- clonedArray.push(entry);
517
- continue;
518
- }
519
- const res: any = postProcessNewInstance(copy, key, entry, newObjectsMap);
520
- if (res !== undefined)
521
- clonedArray.push(res);
522
- else clonedArray.push(entry);
523
- }
524
- // console.log(copy[key])
525
- }
526
- else if (typeof value === "object") {
527
- const res = postProcessNewInstance(copy, key, value as IComponent | Object3D, newObjectsMap);
528
- if (res !== undefined) {
529
- copy[key] = res;
530
- }
531
- }
532
- }
533
- }
534
- }
535
- }
536
-
537
- }
538
-
539
- function postProcessNewInstance(copy: THREE.Object3D, key: string, value: IComponent | Object3D | any, newObjectsMap: { [key: string]: NewGameObjectReferenceInfo }) {
540
- if (value === null || value === undefined) return;
541
- if ((value as IComponent).isComponent === true) {
542
- const originalGameObjectReference = value["gameObject"];
543
- // console.log(key, value, originalGameObjectReference);
544
- if (originalGameObjectReference) {
545
- const id = originalGameObjectReference.uuid;
546
- const newGameObject = newObjectsMap[id]?.clone;
547
- if (!newGameObject) {
548
- // reference has not changed!
549
- if (debug)
550
- console.log("reference did not change", key, copy, value);
551
- return;
552
- }
553
- const index = originalGameObjectReference.userData.components.indexOf(value);
554
- if (index >= 0) {
555
- if (debug)
556
- console.log(key, id);
557
- const found = newGameObject.userData.components[index];
558
- return found;
559
- }
560
- else {
561
- console.warn("could not find component", key, value);
562
- }
563
- }
564
- } else if ((value as Object3D).isObject3D === true) {
565
- // console.log(value);
566
- if (key === "gameObject") return;
567
- const originalGameObjectReference = value as Object3D;
568
- if (originalGameObjectReference) {
569
- const id = originalGameObjectReference.uuid;
570
- const newGameObject = newObjectsMap[id]?.clone;
571
- if (newGameObject) {
572
- if (debug)
573
- console.log(key, "old", value, "new", newGameObject);
574
- return newGameObject;
575
- }
576
- }
577
- }
578
- else {
579
- // create new instances for some types that we know should usually not be shared and can safely be cloned
580
- if (value.isVector4 || value.isVector3 || value.isVector2 || value.isQuaternion || value.isEuler) {
581
- return value.clone();
582
- }
583
- }
1
+ import { Object3D } from "three";
2
+ import { processNewScripts } from "./engine_mainloop_utils";
3
+ import { InstantiateIdProvider } from "./engine_networking_instantiate";
4
+ import { Context, registerComponent } from "./engine_setup";
5
+ import { logHierarchy, setWorldPosition, setWorldQuaternion } from "./engine_three_utils";
6
+ import { GuidsMap, IComponent as Component, IComponent, IGameObject as GameObject, UIDProvider } from "./engine_types";
7
+ import { getParam, tryFindObject } from "./engine_utils";
8
+ import { apply } from "../engine-components/js-extensions/Object3D";
9
+ import { $isUsingInstancing, InstancingUtil } from "./engine_instancing";
10
+ import { activeInHierarchyFieldName } from "./engine_constants";
11
+ import { assign } from "./engine_serialization_core";
12
+
13
+ const debug = getParam("debuggetcomponent");
14
+ const debugInstantiate = getParam("debuginstantiate");
15
+
16
+ export enum HideFlags {
17
+ None = 0,
18
+ HideInHierarchy = 1,
19
+ HideInInspector = 2,
20
+ DontSaveInEditor = 4,
21
+ NotEditable = 8,
22
+ DontSaveInBuild = 16, // 0x00000010
23
+ DontUnloadUnusedAsset = 32, // 0x00000020
24
+ DontSave = DontUnloadUnusedAsset | DontSaveInBuild | DontSaveInEditor, // 0x00000034
25
+ HideAndDontSave = DontSave | NotEditable | HideInHierarchy, // 0x0000003D
26
+ }
27
+
28
+
29
+ export class InstantiateOptions {
30
+ idProvider?: UIDProvider | undefined;
31
+
32
+ //** parent guid */
33
+ parent?: string | undefined | Object3D;
34
+ /** for duplicatable parenting */
35
+ keepWorldPosition?: boolean
36
+ position?: THREE.Vector3 | undefined;
37
+ rotation?: THREE.Quaternion | undefined;
38
+ scale?: THREE.Vector3 | undefined;
39
+
40
+ visible?: boolean | undefined;
41
+
42
+ context?: Context | undefined;
43
+ }
44
+
45
+
46
+ // export function setActive(go: Object3D, active: boolean, processStart: boolean = true) {
47
+ // if (!go) return;
48
+ // go.visible = active;
49
+ // main.updateActiveInHierarchyWithoutEventCall(go);
50
+ // if (active && processStart)
51
+ // main.processStart(Context.Current, go);
52
+ // }
53
+
54
+
55
+ // Object.defineProperty(Object3D.prototype, "visible", {
56
+ // get: function () {
57
+ // return this._visible;
58
+ // },
59
+ // set: function (val) {
60
+ // // const changed = val !== this._visible;
61
+ // this._visible = val;
62
+ // // if (changed) {
63
+ // // setActive(this, val);
64
+ // // }
65
+ // }
66
+ // });
67
+
68
+
69
+ const $isActive = Symbol("isActive");
70
+ export function isActiveSelf(go: Object3D): boolean {
71
+ // if (go[$isActive] === undefined) {
72
+ // go[$isActive] = true;
73
+ // }
74
+ return go.visible;
75
+ }
76
+
77
+ export function setActive(go: Object3D, active: boolean | number): boolean {
78
+ if (typeof active === "number") active = active > .5;
79
+ // go[$isActive] = active;
80
+ go.visible = active;
81
+ return go.visible;// go[$isActive];
82
+ }
83
+
84
+ export function isActiveInHierarchy(go: Object3D): boolean {
85
+ return go[activeInHierarchyFieldName] || isUsingInstancing(go);
86
+ }
87
+
88
+ export function markAsInstancedRendered(go: THREE.Object3D, instanced: boolean) {
89
+ go[$isUsingInstancing] = instanced;
90
+ }
91
+
92
+ export function isUsingInstancing(instance: THREE.Object3D): boolean { return InstancingUtil.isUsingInstancing(instance); }
93
+
94
+
95
+ export function findByGuid(guid: string, hierarchy: THREE.Object3D): GameObject | IComponent | null | undefined {
96
+ return tryFindObject(guid, hierarchy, true, true);
97
+ }
98
+
99
+
100
+ const $isDestroyed = Symbol("isDestroyed");
101
+ export function isDestroyed(go: Object3D): boolean {
102
+ return go[$isDestroyed];
103
+ }
104
+ export function setDestroyed(go: Object3D, value: boolean) {
105
+ go[$isDestroyed] = value;
106
+ }
107
+
108
+ export function destroy(instance: Object3D | Component, recursive: boolean = true, dispose: boolean = false) {
109
+ internalDestroy(instance, recursive, dispose, true);
110
+ }
111
+
112
+ function internalDestroy(instance: Object3D | Component, recursive: boolean = true, dispose: boolean = false, isRoot: boolean = true) {
113
+ const comp = instance as Component;
114
+ if (comp.isComponent) {
115
+ comp.__internalDisable();
116
+ comp.__internalDestroy();
117
+ return;
118
+ }
119
+
120
+
121
+ const obj = instance as GameObject;
122
+ if (dispose) disposeObject(obj);
123
+ setDestroyed(obj, true);
124
+
125
+
126
+ if (debug) console.log(obj);
127
+
128
+ if (recursive && obj.children) {
129
+ for (const ch of obj.children) {
130
+ internalDestroy(ch, recursive, false, false);
131
+ }
132
+ }
133
+
134
+ const components = obj.userData.components;
135
+ if (components) {
136
+ let lastLength = components.length;
137
+ for (let i = 0; i < components.length; i++) {
138
+ const comp: Component = components[i];
139
+ comp.__internalDisable();
140
+ comp.__internalDestroy();
141
+ // if (comp.destroy) {
142
+ // if (debug) console.log("destroying", comp);
143
+ // comp.destroy();
144
+ // }
145
+ // components will be removed from componentlist in destroy
146
+ if (components.length < lastLength) {
147
+ lastLength = components.length;
148
+ i--;
149
+ }
150
+ }
151
+ }
152
+ if (isRoot)
153
+ obj.removeFromParent();
154
+ }
155
+
156
+ function disposeObject(obj: Object3D) {
157
+ if (!obj) return;
158
+ const mesh = obj as THREE.Mesh;
159
+ if (mesh.geometry) {
160
+ mesh.geometry.dispose();
161
+ }
162
+ if (mesh.material) {
163
+ if (Array.isArray(mesh.material)) {
164
+ mesh.material.forEach(m => m.dispose());
165
+ } else {
166
+ mesh.material.dispose();
167
+ }
168
+ }
169
+ }
170
+
171
+ declare type ForEachComponentCallback = (comp: Component) => any;
172
+
173
+ export function foreachComponent(instance: THREE.Object3D, cb: ForEachComponentCallback, recursive: boolean = true): any {
174
+ return internalForEachComponent(instance, cb, recursive);
175
+ }
176
+
177
+ function internalForEachComponent(instance: Object3D, cb: ForEachComponentCallback, recursive: boolean, level: number = 0): any {
178
+ if (!instance) return;
179
+ if (!instance.isObject3D) {
180
+ new Error("Expected Object3D but got " + instance);
181
+ }
182
+ if (level > 1000) {
183
+ console.warn("Failed to iterate components: too many levels");
184
+ return;
185
+ }
186
+ if (instance.userData?.components) {
187
+ for (let i = 0; i < instance.userData.components.length; i++) {
188
+ const comp = instance.userData.components[i];
189
+ if (comp?.isComponent === true) {
190
+ const res = cb(comp);
191
+ if (res !== undefined) return res;
192
+ }
193
+ }
194
+ }
195
+
196
+ if (recursive && instance.children) {
197
+ // childArrayBuffer.length = 0;
198
+ // childArrayBuffer.push(...instance.children);
199
+ const nextLevel = level + 1;
200
+ for (let i = 0; i < instance.children.length; i++) {
201
+ const child = instance.children[i];
202
+ if (!child) continue;
203
+ const res = internalForEachComponent(child, cb, recursive, nextLevel);
204
+ if (res !== undefined) return res;
205
+ }
206
+ // childArrayBuffer.length = 0;
207
+ }
208
+
209
+ }
210
+
211
+ declare class NewGameObjectReferenceInfo {
212
+ original: THREE.Object3D;
213
+ clone: THREE.Object3D;
214
+ }
215
+
216
+ export function instantiate(instance: GameObject | Object3D | null, opts: InstantiateOptions | null = null): GameObject | null {
217
+ if (instance === null) return null;
218
+
219
+ let options: InstantiateOptions | null = null;
220
+ if (opts !== null) {
221
+ // if x is defined assume this is a vec3 - this is just to not break everything at once and stay a little bit backwards compatible
222
+ if (opts["x"] !== undefined) {
223
+ options = new InstantiateOptions();
224
+ options.position = opts as unknown as THREE.Vector3;
225
+ }
226
+ else {
227
+ // if (opts instanceof InstantiateOptions)
228
+ options = opts as InstantiateOptions;
229
+ // else {
230
+ // options = new InstantiateOptions();
231
+ // Object.assign(options, opts);
232
+ // }
233
+ }
234
+ }
235
+
236
+ let context = Context.Current;
237
+ if (options?.context) context = options.context;
238
+ if (debug && context.alias)
239
+ console.log("context", context.alias);
240
+
241
+ // we need to create the id provider before calling internal instantiate because cloned gameobjects also create new guids
242
+ if (options && !options.idProvider) {
243
+ options.idProvider = new InstantiateIdProvider(Date.now());
244
+ }
245
+
246
+ const components: Array<Component> = [];
247
+ const goMapping: { [key: string]: NewGameObjectReferenceInfo } = {}; // used to resolve references on components to components on other gameobjects to their new counterpart
248
+ const skinnedMeshes: { [key: string]: NewGameObjectReferenceInfo } = {};
249
+ const clone = internalInstantiate(context, instance, options, components, goMapping, skinnedMeshes);
250
+
251
+ if (clone) {
252
+ resolveReferences(goMapping);
253
+ resolveAndBindSkinnedMeshBones(skinnedMeshes, goMapping);
254
+ }
255
+
256
+ if (debug) {
257
+ logHierarchy(instance, true);
258
+ logHierarchy(clone, true);
259
+ }
260
+
261
+ const guidsMap: GuidsMap = {};
262
+ for (const i in components) {
263
+ const copy = components[i];
264
+ const oldGuid = copy.guid;
265
+ if (options && options.idProvider) {
266
+ copy.guid = options.idProvider.generateUUID();
267
+ guidsMap[oldGuid] = copy.guid;
268
+ if (debug)
269
+ console.log(copy.name, copy.guid)
270
+ }
271
+ registerComponent(copy, context);
272
+ if (copy.__internalNewInstanceCreated)
273
+ copy.__internalNewInstanceCreated();
274
+ }
275
+ for (const i in components) {
276
+ const copy = components[i];
277
+ if (copy.resolveGuids)
278
+ copy.resolveGuids(guidsMap);
279
+ if (copy.enabled === false) continue;
280
+ else copy.enabled = true;
281
+ }
282
+
283
+ processNewScripts(context);
284
+
285
+ return clone as GameObject;
286
+ }
287
+
288
+
289
+ function internalInstantiate(
290
+ context: Context, instance: GameObject | THREE.Object3D, opts: InstantiateOptions | null,
291
+ componentsList: Array<Component>,
292
+ newGameObjectsMap: { [key: string]: NewGameObjectReferenceInfo },
293
+ skinnedMeshesMap: { [key: string]: NewGameObjectReferenceInfo }
294
+ )
295
+ : GameObject | THREE.Object3D | null {
296
+ if (!instance) return null;
297
+ // prepare, remove things that dont work out of the box
298
+ // e.g. user data we want to manually clone
299
+ // also children throw errors (e.g. recursive toJson with nested meshes)
300
+ const userData = instance.userData;
301
+ instance.userData = {};
302
+ const children = instance.children;
303
+ instance.children = [];
304
+ let clone: THREE.Object3D | GameObject;
305
+ clone = instance.clone(false);
306
+ apply(clone);
307
+ instance.userData = userData;
308
+ instance.children = children;
309
+
310
+ // make reference from old id to new object
311
+ newGameObjectsMap[instance.uuid] = { original: instance, clone: clone };
312
+ if(debugInstantiate) console.log("ADD", instance, clone)
313
+
314
+ if (instance.type === "SkinnedMesh") {
315
+ skinnedMeshesMap[instance.uuid] = { original: instance, clone: clone };
316
+ }
317
+
318
+ // DO NOT EVER RENAME BECAUSE IT BREAKS / MIGHT BREAK ANIMATIONS
319
+ // clone.name += " (Clone)";
320
+
321
+ if (opts?.visible !== undefined)
322
+ clone.visible = opts.visible;
323
+
324
+ if (opts?.idProvider) {
325
+ clone.uuid = opts.idProvider.generateUUID();
326
+ const cloneGo: GameObject = clone as GameObject;
327
+ if (cloneGo) cloneGo.guid = clone.uuid;
328
+ }
329
+
330
+ if (instance.animations && instance.animations.length > 0) {
331
+ clone.animations = [...instance.animations];
332
+ }
333
+
334
+ const parent = instance.parent;
335
+ if (parent) {
336
+ parent.add(clone);
337
+ }
338
+
339
+ // apply transform
340
+ if (opts?.position) {
341
+ setWorldPosition(clone, opts.position);
342
+ }
343
+ else clone.position.copy(instance.position);
344
+ if (opts?.rotation) {
345
+ setWorldQuaternion(clone, opts.rotation);
346
+ // clone.quaternion.copy(opts.rotation);
347
+ }
348
+ else clone.quaternion.copy(instance.quaternion);
349
+ if (opts?.scale) {
350
+ // TODO: make set world scale work
351
+ clone.scale.copy(opts.scale);
352
+ }
353
+ else clone.scale.copy(instance.scale);
354
+
355
+ if (opts?.parent && opts.parent !== "scene") {
356
+ let requestedParent: Object3D | null = null;
357
+ if (typeof opts.parent === "string") {
358
+ requestedParent = tryFindObject(opts.parent, context.scene, true);
359
+ }
360
+ else {
361
+ requestedParent = opts.parent;
362
+ }
363
+ if (requestedParent) {
364
+ const func = opts.keepWorldPosition === true ? requestedParent.attach : requestedParent.add;
365
+ if (!func) console.error("Invalid parent object", requestedParent, "received when instantiating:", instance);
366
+ else func.call(requestedParent, clone);
367
+ }
368
+ else console.warn("could not find parent:", opts.parent);
369
+ }
370
+
371
+ for (const [key, value] of Object.entries(instance.userData)) {
372
+ if (key === "components") continue;
373
+ clone.userData[key] = value;
374
+ }
375
+
376
+ if (instance.userData?.components) {
377
+ const components = instance.userData.components;
378
+ const newComponents: Component[] = [];
379
+ clone.userData.components = newComponents;
380
+ for (let i = 0; i < components.length; i++) {
381
+ const comp = components[i];
382
+ const copy = Object.create(comp);
383
+ assign(copy, comp);
384
+ newComponents.push(copy);
385
+ copy.gameObject = clone;
386
+ // copy.transform = clone;
387
+ componentsList.push(copy);
388
+ }
389
+ }
390
+
391
+ // children should just clone the original transform
392
+ if (opts) {
393
+ opts.position = undefined;
394
+ opts.rotation = undefined;
395
+ opts.scale = undefined;
396
+ opts.parent = undefined;
397
+ }
398
+
399
+ for (const ch in instance.children) {
400
+ const child = instance.children[ch];
401
+ const newChild = internalInstantiate(context, child as GameObject, opts, componentsList, newGameObjectsMap, skinnedMeshesMap);
402
+ if (newChild)
403
+ clone.add(newChild);
404
+ }
405
+
406
+ return clone;
407
+
408
+ }
409
+
410
+ function resolveAndBindSkinnedMeshBones(
411
+ skinnedMeshes: { [key: string]: NewGameObjectReferenceInfo },
412
+ newObjectsMap: { [key: string]: NewGameObjectReferenceInfo }
413
+ ) {
414
+ for (const key in skinnedMeshes) {
415
+ const val = skinnedMeshes[key];
416
+ const original = val.original as THREE.SkinnedMesh;
417
+ const originalSkeleton = original.skeleton;
418
+ const clone = val.clone as THREE.SkinnedMesh;
419
+ // clone.updateWorldMatrix(true, true);
420
+ if (!originalSkeleton) {
421
+ console.warn("Skinned mesh has no skeleton?", val);
422
+ continue;
423
+ }
424
+ const originalBones = originalSkeleton.bones;
425
+ const clonedSkeleton = clone.skeleton.clone();
426
+
427
+ clone.skeleton = clonedSkeleton;
428
+ clone.bindMatrix.clone().copy(original.bindMatrix);
429
+ // console.log(clone.bindMatrix)
430
+ clone.bindMatrixInverse.copy(original.bindMatrixInverse);
431
+ // clone.bindMatrix.multiplyScalar(.025);
432
+ // console.assert(originalSkeleton.uuid !== clonedSkeleton.uuid);
433
+ // console.assert(originalBones.length === clonedSkeleton.bones.length);
434
+ const bones: Array<THREE.Bone> = [];
435
+ clonedSkeleton.bones = bones;
436
+ for (let i = 0; i < originalBones.length; i++) {
437
+ const bone = originalBones[i];
438
+ const newBoneInfo = newObjectsMap[bone.uuid];
439
+ const clonedBone = newBoneInfo.clone as THREE.Bone;
440
+ // console.log("NEW BONE: ", clonedBone, "BEFORE", newBoneInfo.original);
441
+ bones.push(clonedBone);
442
+ }
443
+ // clone.skeleton = new THREE.Skeleton(bones);
444
+ // clone.skeleton.update();
445
+ // clone.pose();
446
+ // clone.scale.set(1,1,1);
447
+ // clone.position.y += .1;
448
+ // console.log("ORIG", original, "CLONE", clone);
449
+ }
450
+ for (const key in skinnedMeshes) {
451
+ const clone = skinnedMeshes[key].clone as THREE.SkinnedMesh;
452
+ clone.skeleton.update();
453
+ // clone.skeleton.calculateInverses();
454
+ clone.bind(clone.skeleton, clone.bindMatrix);
455
+ clone.updateMatrixWorld(true);
456
+ // clone.pose();
457
+ }
458
+ }
459
+
460
+ // private static bindNewSkinnedMeshBones(source, clone) {
461
+ // const sourceLookup = new Map();
462
+ // const cloneLookup = new Map();
463
+ // // const clone = source.clone(false);
464
+
465
+ // function parallelTraverse(a, b, callback) {
466
+ // callback(a, b);
467
+ // for (let i = 0; i < a.children.length; i++) {
468
+ // parallelTraverse(a.children[i], b.children[i], callback);
469
+ // }
470
+ // }
471
+ // parallelTraverse(source, clone, function (sourceNode, clonedNode) {
472
+ // sourceLookup.set(clonedNode, sourceNode);
473
+ // cloneLookup.set(sourceNode, clonedNode);
474
+ // });
475
+
476
+ // clone.traverse(function (node) {
477
+ // if (!node.isSkinnedMesh) return;
478
+ // const clonedMesh = node;
479
+ // const sourceMesh = sourceLookup.get(node);
480
+ // const sourceBones = sourceMesh.skeleton.bones;
481
+
482
+ // clonedMesh.skeleton = sourceMesh.skeleton.clone();
483
+ // clonedMesh.bindMatrix.copy(sourceMesh.bindMatrix);
484
+
485
+ // clonedMesh.skeleton.bones = sourceBones.map(function (bone) {
486
+ // return cloneLookup.get(bone);
487
+ // });
488
+ // clonedMesh.bind(clonedMesh.skeleton, clonedMesh.bindMatrix);
489
+ // });
490
+ // return clone;
491
+
492
+ // }
493
+
494
+ function resolveReferences(newObjectsMap: { [key: string]: NewGameObjectReferenceInfo }) {
495
+ // for every object that is newly created we want to update references to their newly created counterparts
496
+ // e.g. a collider instance referencing a rigidbody instance should be updated so that
497
+ // the cloned collider does not reference the cloned rigidbody (instead of the original rigidbody)
498
+ for (const key in newObjectsMap) {
499
+ const val = newObjectsMap[key];
500
+ const clone = val.clone;
501
+ // resolve references
502
+ if (clone.userData?.components) {
503
+ for (let i = 0; i < clone.userData.components.length; i++) {
504
+ const copy = clone.userData.components[i];
505
+ // find referenced within a cloned gameobject
506
+ const entries = Object.entries(copy);
507
+ // console.log(copy, entries);
508
+ for (const [key, value] of entries) {
509
+ if (Array.isArray(value)) {
510
+ const clonedArray: Array<any> = [];
511
+ copy[key] = clonedArray;
512
+ // console.log(copy, key, value, copy[key]);
513
+ for (let i = 0; i < value.length; i++) {
514
+ const entry = value[i];
515
+ // push value types into new array
516
+ if (typeof entry !== "object") {
517
+ clonedArray.push(entry);
518
+ continue;
519
+ }
520
+ const res: any = postProcessNewInstance(copy, key, entry, newObjectsMap);
521
+ if (res !== undefined)
522
+ clonedArray.push(res);
523
+ else clonedArray.push(entry);
524
+ }
525
+ // console.log(copy[key])
526
+ }
527
+ else if (typeof value === "object") {
528
+ const res = postProcessNewInstance(copy, key, value as IComponent | Object3D, newObjectsMap);
529
+ if (res !== undefined) {
530
+ copy[key] = res;
531
+ }
532
+ }
533
+ }
534
+ }
535
+ }
536
+ }
537
+
538
+ }
539
+
540
+ function postProcessNewInstance(copy: THREE.Object3D, key: string, value: IComponent | Object3D | any, newObjectsMap: { [key: string]: NewGameObjectReferenceInfo }) {
541
+ if (value === null || value === undefined) return;
542
+ if ((value as IComponent).isComponent === true) {
543
+ const originalGameObjectReference = value["gameObject"];
544
+ // console.log(key, value, originalGameObjectReference);
545
+ if (originalGameObjectReference) {
546
+ const id = originalGameObjectReference.uuid;
547
+ const newGameObject = newObjectsMap[id]?.clone;
548
+ if (!newGameObject) {
549
+ // reference has not changed!
550
+ if (debugInstantiate)
551
+ console.log("reference did not change", key, copy, value);
552
+ return;
553
+ }
554
+ const index = originalGameObjectReference.userData.components.indexOf(value);
555
+ if (index >= 0) {
556
+ if (debugInstantiate)
557
+ console.log(key, id);
558
+ const found = newGameObject.userData.components[index];
559
+ return found;
560
+ }
561
+ else {
562
+ console.warn("could not find component", key, value);
563
+ }
564
+ }
565
+ } else if ((value as Object3D).isObject3D === true) {
566
+ // console.log(value);
567
+ if (key === "gameObject") return;
568
+ const originalGameObjectReference = value as Object3D;
569
+ if (originalGameObjectReference) {
570
+ const id = originalGameObjectReference.uuid;
571
+ const newGameObject = newObjectsMap[id]?.clone;
572
+ if (newGameObject) {
573
+ if (debugInstantiate)
574
+ console.log(key, "old", value, "new", newGameObject);
575
+ return newGameObject;
576
+ }
577
+ }
578
+ }
579
+ else {
580
+ // create new instances for some types that we know should usually not be shared and can safely be cloned
581
+ if (value.isVector4 || value.isVector3 || value.isVector2 || value.isQuaternion || value.isEuler) {
582
+ return value.clone();
583
+ }
584
+ }
584
585
  }