@needle-tools/engine 4.8.7-next.e134730 → 4.8.8-next.12b5946

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 (118) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +55 -42
  3. package/components.needle.json +1 -1
  4. package/dist/{needle-engine.bundle-DrGsgE4t.min.js → needle-engine.bundle-CS7vqRb3.min.js} +144 -144
  5. package/dist/{needle-engine.bundle-X9nxhICu.umd.cjs → needle-engine.bundle-kYzccQZF.umd.cjs} +147 -147
  6. package/dist/{needle-engine.bundle-CvRpjtJj.js → needle-engine.bundle-lC7eSFno.js} +7090 -7010
  7. package/dist/needle-engine.d.ts +7 -0
  8. package/dist/needle-engine.js +2 -2
  9. package/dist/needle-engine.min.js +1 -1
  10. package/dist/needle-engine.umd.cjs +1 -1
  11. package/lib/engine/engine_addressables.d.ts +12 -12
  12. package/lib/engine/engine_addressables.js +32 -23
  13. package/lib/engine/engine_addressables.js.map +1 -1
  14. package/lib/engine/engine_animation.d.ts +1 -3
  15. package/lib/engine/engine_animation.js +18 -13
  16. package/lib/engine/engine_animation.js.map +1 -1
  17. package/lib/engine/engine_assetdatabase.js +6 -6
  18. package/lib/engine/engine_assetdatabase.js.map +1 -1
  19. package/lib/engine/engine_camera.d.ts +23 -3
  20. package/lib/engine/engine_camera.js +34 -2
  21. package/lib/engine/engine_camera.js.map +1 -1
  22. package/lib/engine/engine_context.d.ts +15 -0
  23. package/lib/engine/engine_context.js +33 -0
  24. package/lib/engine/engine_context.js.map +1 -1
  25. package/lib/engine/engine_create_objects.d.ts +3 -3
  26. package/lib/engine/engine_create_objects.js +5 -4
  27. package/lib/engine/engine_create_objects.js.map +1 -1
  28. package/lib/engine/engine_gameobject.js +2 -2
  29. package/lib/engine/engine_gameobject.js.map +1 -1
  30. package/lib/engine/engine_gltf_builtin_components.js +11 -11
  31. package/lib/engine/engine_gltf_builtin_components.js.map +1 -1
  32. package/lib/engine/engine_loaders.callbacks.d.ts +1 -0
  33. package/lib/engine/engine_loaders.callbacks.js +1 -0
  34. package/lib/engine/engine_loaders.callbacks.js.map +1 -1
  35. package/lib/engine/engine_loaders.js +15 -11
  36. package/lib/engine/engine_loaders.js.map +1 -1
  37. package/lib/engine/extensions/NEEDLE_lighting_settings.js +5 -2
  38. package/lib/engine/extensions/NEEDLE_lighting_settings.js.map +1 -1
  39. package/lib/engine/extensions/NEEDLE_lightmaps.js +1 -1
  40. package/lib/engine/extensions/NEEDLE_lightmaps.js.map +1 -1
  41. package/lib/engine/js-extensions/Object3D.d.ts +1 -1
  42. package/lib/engine/js-extensions/Vector.d.ts +5 -0
  43. package/lib/engine/js-extensions/Vector.js.map +1 -1
  44. package/lib/engine/webcomponents/needle-engine.attributes.d.ts +1 -0
  45. package/lib/engine/webcomponents/needle-engine.d.ts +2 -2
  46. package/lib/engine/webcomponents/needle-engine.js +19 -21
  47. package/lib/engine/webcomponents/needle-engine.js.map +1 -1
  48. package/lib/engine-components/Animation.js +2 -1
  49. package/lib/engine-components/Animation.js.map +1 -1
  50. package/lib/engine-components/AnimationUtilsAutoplay.js +1 -6
  51. package/lib/engine-components/AnimationUtilsAutoplay.js.map +1 -1
  52. package/lib/engine-components/Camera.d.ts +2 -0
  53. package/lib/engine-components/Camera.js +5 -1
  54. package/lib/engine-components/Camera.js.map +1 -1
  55. package/lib/engine-components/ContactShadows.d.ts +12 -2
  56. package/lib/engine-components/ContactShadows.js +24 -4
  57. package/lib/engine-components/ContactShadows.js.map +1 -1
  58. package/lib/engine-components/DropListener.d.ts +21 -15
  59. package/lib/engine-components/DropListener.js +38 -34
  60. package/lib/engine-components/DropListener.js.map +1 -1
  61. package/lib/engine-components/LookAtConstraint.d.ts +5 -1
  62. package/lib/engine-components/LookAtConstraint.js +8 -0
  63. package/lib/engine-components/LookAtConstraint.js.map +1 -1
  64. package/lib/engine-components/NestedGltf.d.ts +9 -4
  65. package/lib/engine-components/NestedGltf.js +32 -26
  66. package/lib/engine-components/NestedGltf.js.map +1 -1
  67. package/lib/engine-components/OrbitControls.d.ts +30 -9
  68. package/lib/engine-components/OrbitControls.js +56 -19
  69. package/lib/engine-components/OrbitControls.js.map +1 -1
  70. package/lib/engine-components/Renderer.js +2 -1
  71. package/lib/engine-components/Renderer.js.map +1 -1
  72. package/lib/engine-components/Skybox.js +8 -9
  73. package/lib/engine-components/Skybox.js.map +1 -1
  74. package/lib/engine-components/api.d.ts +1 -0
  75. package/lib/engine-components/api.js.map +1 -1
  76. package/lib/engine-components/export/usdz/Extension.d.ts +1 -1
  77. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +1 -1
  78. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js.map +1 -1
  79. package/lib/engine-components/export/usdz/USDZExporter.d.ts +7 -0
  80. package/lib/engine-components/export/usdz/USDZExporter.js +8 -1
  81. package/lib/engine-components/export/usdz/USDZExporter.js.map +1 -1
  82. package/lib/engine-components/webxr/WebXRImageTracking.d.ts +4 -2
  83. package/lib/engine-components/webxr/WebXRImageTracking.js +117 -81
  84. package/lib/engine-components/webxr/WebXRImageTracking.js.map +1 -1
  85. package/package.json +2 -2
  86. package/plugins/common/files.js +6 -3
  87. package/plugins/vite/alias.js +26 -17
  88. package/plugins/vite/editor-connection.js +4 -4
  89. package/src/engine/engine_addressables.ts +46 -33
  90. package/src/engine/engine_animation.ts +20 -13
  91. package/src/engine/engine_assetdatabase.ts +7 -7
  92. package/src/engine/engine_camera.ts +54 -3
  93. package/src/engine/engine_context.ts +39 -1
  94. package/src/engine/engine_create_objects.ts +8 -7
  95. package/src/engine/engine_gameobject.ts +2 -2
  96. package/src/engine/engine_gltf_builtin_components.ts +12 -11
  97. package/src/engine/engine_loaders.callbacks.ts +1 -0
  98. package/src/engine/engine_loaders.ts +18 -13
  99. package/src/engine/extensions/NEEDLE_lighting_settings.ts +5 -2
  100. package/src/engine/extensions/NEEDLE_lightmaps.ts +1 -1
  101. package/src/engine/js-extensions/Vector.ts +6 -0
  102. package/src/engine/webcomponents/needle-engine.attributes.ts +2 -0
  103. package/src/engine/webcomponents/needle-engine.ts +21 -21
  104. package/src/engine-components/Animation.ts +1 -1
  105. package/src/engine-components/AnimationUtilsAutoplay.ts +1 -6
  106. package/src/engine-components/Camera.ts +7 -1
  107. package/src/engine-components/ContactShadows.ts +27 -6
  108. package/src/engine-components/DropListener.ts +44 -34
  109. package/src/engine-components/LookAtConstraint.ts +9 -1
  110. package/src/engine-components/NestedGltf.ts +33 -24
  111. package/src/engine-components/OrbitControls.ts +81 -32
  112. package/src/engine-components/Renderer.ts +2 -1
  113. package/src/engine-components/Skybox.ts +9 -10
  114. package/src/engine-components/api.ts +2 -1
  115. package/src/engine-components/export/usdz/Extension.ts +1 -1
  116. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +1 -1
  117. package/src/engine-components/export/usdz/USDZExporter.ts +21 -12
  118. package/src/engine-components/webxr/WebXRImageTracking.ts +138 -90
@@ -2,6 +2,7 @@ import { AnimationAction, AnimationClip, AnimationMixer, Object3D, PropertyBindi
2
2
 
3
3
  import type { Context } from "./engine_context.js";
4
4
  import { GLTF, IAnimationComponent, Model } from "./engine_types.js";
5
+ import { TypeStore } from "./engine_typestore.js";
5
6
 
6
7
  /**
7
8
  * Registry for animation related data. Use {@link registerAnimationMixer} to register an animation mixer instance.
@@ -88,12 +89,15 @@ export class AnimationUtils {
88
89
  * This method will look for objects in the scene that have animations and assign them to the correct objects.
89
90
  * @param file The GLTF file to assign the animations from
90
91
  */
91
- static assignAnimationsFromFile(file: Pick<Model, "animations" | "scene">, opts?: { createAnimationComponent(obj: Object3D, animation: AnimationClip): IAnimationComponent }) {
92
+ static autoplayAnimations(file: Object3D | Pick<Model, "animations" | "scene">): Array<IAnimationComponent> | null {
92
93
  if (!file || !file.animations) {
93
94
  console.debug("No animations found in file");
94
- return;
95
+ return null;
95
96
  }
96
97
 
98
+ const scene = "scene" in file ? file.scene : file as Object3D;
99
+ const animationComponents = new Array<IAnimationComponent>();
100
+
97
101
  for (let i = 0; i < file.animations.length; i++) {
98
102
  const animation = file.animations[i];
99
103
  if (!animation.tracks || animation.tracks.length <= 0) {
@@ -103,12 +107,12 @@ export class AnimationUtils {
103
107
  for (const t in animation.tracks) {
104
108
  const track = animation.tracks[t];
105
109
  const parsedPath = PropertyBinding.parseTrackName(track.name);
106
- let obj = PropertyBinding.findNode(file.scene, parsedPath.nodeName);
110
+ let obj = PropertyBinding.findNode(scene, parsedPath.nodeName);
107
111
  if (!obj) {
108
112
  const objectName = track["__objectName"] ?? track.name.substring(0, track.name.indexOf("."));
109
113
  // let obj = gltf.scene.getObjectByName(objectName);
110
114
  // this finds unnamed objects that still have tracks targeting them
111
- obj = file.scene.getObjectByProperty('uuid', objectName);
115
+ obj = scene.getObjectByProperty('uuid', objectName);
112
116
 
113
117
  if (!obj) {
114
118
  // console.warn("could not find " + objectName, animation, gltf.scene);
@@ -116,32 +120,35 @@ export class AnimationUtils {
116
120
  }
117
121
  }
118
122
 
119
- let animationComponent = findAnimationGameObjectInParent(obj);
123
+ let animationComponent = findAnimationComponentInParent(obj) || findAnimationComponentInParent(scene);
120
124
  if (!animationComponent) {
121
- if (!opts?.createAnimationComponent) {
122
- console.warn("No AnimationComponent found in parent hierarchy of object and no 'createAnimationComponent' callback was provided in options.")
123
- continue;
125
+ const anim = TypeStore.get("Animation");
126
+ animationComponent = scene.addComponent(anim);
127
+ if (!animationComponent) {
128
+ console.error("Failed creating Animation component: No 'Animation' component found in TypeStore");
129
+ break;
124
130
  }
125
- animationComponent = opts.createAnimationComponent(file.scene, animation);
126
- obj.addComponent(animationComponent);
127
131
  }
132
+ animationComponents.push(animationComponent);
128
133
  if (animationComponent.addClip) {
129
134
  animationComponent.addClip(animation);
130
135
  }
131
136
  }
132
137
  }
133
- function findAnimationGameObjectInParent(obj): IAnimationComponent | null {
138
+ return animationComponents;
139
+
140
+ function findAnimationComponentInParent(obj): IAnimationComponent | null {
134
141
  if (!obj) return null;
135
142
  const components = obj.userData?.components;
136
143
  if (components && components.length > 0) {
137
144
  for (let i = 0; i < components.length; i++) {
138
145
  const component = components[i] as IAnimationComponent;
139
146
  if (component.isAnimationComponent === true) {
140
- return obj;
147
+ return component;
141
148
  }
142
149
  }
143
150
  }
144
- return findAnimationGameObjectInParent(obj.parent);
151
+ return findAnimationComponentInParent(obj.parent);
145
152
  }
146
153
  }
147
154
  }
@@ -61,7 +61,7 @@ export function disposeObjectResources(obj: object | null | undefined) {
61
61
  return;
62
62
  }
63
63
 
64
- if(typeof obj === "object") {
64
+ if (typeof obj === "object") {
65
65
  obj[$disposed] = true;
66
66
  }
67
67
 
@@ -79,8 +79,8 @@ export function disposeObjectResources(obj: object | null | undefined) {
79
79
  disposeObjectResources(obj.bindMatrixInverse);
80
80
  disposeObjectResources(obj.customDepthMaterial);
81
81
  disposeObjectResources(obj.customDistanceMaterial);
82
- obj.geometry = null;
83
- obj.material = null;
82
+ obj.geometry = {};
83
+ obj.material = {};
84
84
  obj.visible = false;
85
85
  }
86
86
  else if (obj instanceof Mesh) {
@@ -88,8 +88,8 @@ export function disposeObjectResources(obj: object | null | undefined) {
88
88
  disposeObjectResources(obj.material);
89
89
  disposeObjectResources(obj.customDepthMaterial);
90
90
  disposeObjectResources(obj.customDistanceMaterial);
91
- obj.geometry = null;
92
- obj.material = null;
91
+ obj.geometry = {};
92
+ obj.material = {};
93
93
  obj.visible = false;
94
94
  }
95
95
  else if (obj instanceof BufferGeometry) {
@@ -175,8 +175,8 @@ function free(obj: any) {
175
175
 
176
176
  export function __internalNotifyObjectDestroyed(obj: Object3D) {
177
177
  if (obj instanceof Mesh || obj instanceof SkinnedMesh) {
178
- obj.material = null;
179
- obj.geometry = null;
178
+ obj.material = {};
179
+ obj.geometry = {};
180
180
  }
181
181
  }
182
182
 
@@ -1,5 +1,6 @@
1
- import { Camera, Object3D } from "three";
1
+ import { Camera, HemisphereLightHelper, Object3D, PerspectiveCamera, Vector2, WebGLRenderer } from "three";
2
2
 
3
+ import { Mathf } from "./engine_math.js";
3
4
  import type { ICameraController } from "./engine_types.js";
4
5
 
5
6
 
@@ -24,7 +25,11 @@ export function setCameraController(cam: Camera, cameraController: ICameraContro
24
25
 
25
26
  const autofit = "needle:autofit";
26
27
 
27
- /** @internal */
28
+ /**
29
+ * Used by e.g. getBoundingBox via ContactShadows or OrbitControls when fitting the camera or shadow planes. Objects can be marked to be excluded from bounding box calculations via `setAutoFitEnabled(obj, <bool>)`
30
+ * @see setAutoFitEnabled
31
+ * @internal
32
+ */
28
33
  export function useForAutoFit(obj: Object3D): boolean {
29
34
  // if autofit is not defined we assume it may be included
30
35
  if (obj[autofit] === undefined) return true;
@@ -33,8 +38,54 @@ export function useForAutoFit(obj: Object3D): boolean {
33
38
  }
34
39
 
35
40
  /**
36
- * Enable or disable autofitting for the given object
41
+ * Enable or disable autofitting for the given object. Objects that are 'disabled' will be excluded in getBoundingBox calculations.
42
+ * This is used by ContactShadows or OrbitControls when fitting the shadow plane or camera to the given objects or scene.
43
+ * @see useForAutoFit
37
44
  */
38
45
  export function setAutoFitEnabled(obj: Object3D, enabled: boolean): void {
39
46
  obj[autofit] = enabled;
47
+ }
48
+
49
+
50
+
51
+ export type FocusRectSettings = {
52
+ /** Lower values will result in faster alignment with the rect (value ~= seconds to reach target)
53
+ * Minimum value is 0.
54
+ */
55
+ damping: number
56
+ }
57
+ export type FocusRect = DOMRect | Element | { x: number, y: number, width: number, height: number };
58
+
59
+ let rendererRect: DOMRect | undefined = undefined;
60
+ const overlapRect = { x: 0, y: 0, width: 0, height: 0 };
61
+
62
+ /** Used internally by the Needle Engine context via 'setFocusRect(<rect>)' */
63
+ export function updateCameraFocusRect(focusRect: FocusRect, dt: number, camera: PerspectiveCamera, renderer: WebGLRenderer) {
64
+
65
+ if (focusRect instanceof Element) {
66
+ focusRect = focusRect.getBoundingClientRect();
67
+ }
68
+ rendererRect = renderer.domElement.getBoundingClientRect();
69
+
70
+ const rect = overlapRect;
71
+ rect.x = focusRect.x;
72
+ rect.y = focusRect.y;
73
+ rect.width = focusRect.width;
74
+ rect.height = focusRect.height;
75
+
76
+ rect.x -= rendererRect.x;
77
+ rect.y -= rendererRect.y;
78
+
79
+ const targetX = rect.width / -2 - (rect.x - (rendererRect.width / 2));
80
+ const targetY = rect.height / -2 - (rect.y - (rendererRect.height / 2));
81
+
82
+ const view = camera.view;
83
+
84
+ let offsetX = view?.offsetX || 0;
85
+ let offsetY = view?.offsetY || 0;
86
+ offsetX = Mathf.lerp(offsetX, targetX, dt);
87
+ offsetY = Mathf.lerp(offsetY, targetY, dt);
88
+
89
+ camera.setViewOffset(rendererRect.width, rendererRect.height, offsetX, offsetY, rendererRect.width, rendererRect.height);
90
+ camera.updateProjectionMatrix();
40
91
  }
@@ -18,6 +18,7 @@ import { Addressables } from './engine_addressables.js';
18
18
  import { AnimationsRegistry } from './engine_animation.js';
19
19
  import { Application } from './engine_application.js';
20
20
  import { AssetDatabase } from './engine_assetdatabase.js';
21
+ import { FocusRect, FocusRectSettings, updateCameraFocusRect } from './engine_camera.js';
21
22
  import { VERSION } from './engine_constants.js';
22
23
  import { ContextEvent, ContextRegistry } from './engine_context_registry.js';
23
24
  import { WaitForPromise } from './engine_coroutine.js';
@@ -1371,6 +1372,35 @@ export class Context implements IContext {
1371
1372
  this.internalUpdatePhysics(steps);
1372
1373
  }
1373
1374
 
1375
+
1376
+
1377
+ /**
1378
+ * Set a rect or dom element. The camera center will be moved to the center of the rect.
1379
+ * This is useful if you have Needle Engine embedded in a HTML layout and while you want the webgl background to fill e.g. the whole screen you want to move the camera center to free space.
1380
+ * For that you can simply pass in the rect or HMTL div that you want the camera to center on.
1381
+ * @param rect The focus rect or null to disable
1382
+ * @param settings Optional settings for the focus rect. These will override the `focusRectSettings` property
1383
+ */
1384
+ public setCameraFocusRect(rect: FocusRect | null, settings?: Partial<FocusRectSettings>) {
1385
+ this._focusRect = rect;
1386
+ if (settings) {
1387
+ Object.assign(this.focusRectSettings, settings);
1388
+ }
1389
+ }
1390
+ get focusRect() { return this._focusRect; }
1391
+ /** Settings when a focus rect is set. Use `setCameraFocusRect(...)` to do so.
1392
+ * This can be used to offset the renderer center e.g. to a specific DOM element.
1393
+ */
1394
+ readonly focusRectSettings: FocusRectSettings = {
1395
+ /** Controls how fast the rect is centered. Smaller values mean the rect is centered faster.
1396
+ * A minimum value of 0 means the rect is centered instantly.
1397
+ * @default .05
1398
+ */
1399
+ damping: .05
1400
+ };
1401
+ private _focusRect: FocusRect | null = null;
1402
+
1403
+
1374
1404
  private _lastTimestamp = 0;
1375
1405
  private _accumulatedTime = 0;
1376
1406
  private _dispatchReadyAfterFrame = false;
@@ -1421,7 +1451,7 @@ export class Context implements IContext {
1421
1451
 
1422
1452
  Context.Current = this;
1423
1453
  this.time.update();
1424
-
1454
+
1425
1455
  if (debugframerate) console.log("FPS", (this.time.smoothedFps).toFixed(0));
1426
1456
 
1427
1457
 
@@ -1498,6 +1528,14 @@ export class Context implements IContext {
1498
1528
 
1499
1529
  if (this.isVisibleToUser || this.runInBackground) {
1500
1530
 
1531
+ if (this._focusRect) {
1532
+ if (this.mainCamera instanceof PerspectiveCamera) {
1533
+ const settings = this.focusRectSettings;
1534
+ const dt = settings.damping > 0 ? this.time.deltaTime / settings.damping : 1;
1535
+ updateCameraFocusRect(this._focusRect, dt, this.mainCamera, this.renderer);
1536
+ }
1537
+ }
1538
+
1501
1539
  this._currentFrameEvent = FrameEvent.OnBeforeRender;
1502
1540
 
1503
1541
  // should we move these callbacks in the regular three onBeforeRender events?
@@ -42,13 +42,13 @@ export type ObjectOptions = {
42
42
  /**
43
43
  * The position of the object in local space
44
44
  */
45
- position?: Vec3 | [number, number, number],
45
+ position?: Partial<Vec3> | [number, number, number],
46
46
  /** The rotation of the object in local space */
47
- rotation?: Vec3 | [number, number, number],
47
+ rotation?: Partial<Vec3> | [number, number, number],
48
48
  /**
49
49
  * The scale of the object in local space
50
50
  */
51
- scale?: Vec3 | number | [number, number, number],
51
+ scale?: Partial<Vec3> | number | [number, number, number],
52
52
  /**
53
53
  * If the object should receive shadows
54
54
  * @default true
@@ -251,14 +251,15 @@ export class ObjectUtils {
251
251
  if (opts?.position) {
252
252
  if (Array.isArray(opts.position))
253
253
  obj.position.set(opts.position[0], opts.position[1], opts.position[2]);
254
- else
255
- obj.position.set(opts.position.x, opts.position.y, opts.position.z);
254
+ else {
255
+ obj.position.set(opts.position.x || 0, opts.position.y || 0, opts.position.z || 0);
256
+ }
256
257
  }
257
258
  if (opts?.rotation) {
258
259
  if (Array.isArray(opts.rotation))
259
260
  obj.rotation.set(opts.rotation[0], opts.rotation[1], opts.rotation[2]);
260
261
  else
261
- obj.rotation.set(opts.rotation.x, opts.rotation.y, opts.rotation.z);
262
+ obj.rotation.set(opts.rotation.x || 0, opts.rotation.y || 0, opts.rotation.z || 0);
262
263
  }
263
264
  if (opts?.scale) {
264
265
  if (typeof opts.scale === "number")
@@ -267,7 +268,7 @@ export class ObjectUtils {
267
268
  obj.scale.set(opts.scale[0], opts.scale[1], opts.scale[2]);
268
269
  }
269
270
  else {
270
- obj.scale.set(opts.scale.x, opts.scale.y, opts.scale.z);
271
+ obj.scale.set(opts.scale.x || 1, opts.scale.y || 1, opts.scale.z || 1);
271
272
 
272
273
  }
273
274
  }
@@ -166,9 +166,9 @@ export function destroy(instance: Object3D | Component, recursive: boolean = tru
166
166
  setDestroyed(obj, true);
167
167
  if (dispose) {
168
168
  disposeObjectResources(obj);
169
+ // This needs to be called after disposing because it removes the references to resources
170
+ __internalRemoveReferences(obj);
169
171
  }
170
- // This needs to be called after disposing because it removes the references to resources
171
- __internalRemoveReferences(obj);
172
172
  }
173
173
  destroyed_objects.length = 0;
174
174
  destroyed_components.length = 0;
@@ -72,13 +72,13 @@ export async function createBuiltinComponents(context: Context, gltfId: SourceId
72
72
 
73
73
  if (gltf.scenes) {
74
74
  for (const scene of gltf.scenes) {
75
- await onCreateBuiltinComponents(serializationContext, scene, deserializeQueue, lateResolve);
75
+ await onCreateBuiltinComponents(serializationContext, scene, deserializeQueue, lateResolve, 0);
76
76
  }
77
77
  }
78
78
  // TODO: when is the gltf here an object3d?
79
79
  if (gltf.children) {
80
80
  for (const ch of gltf.children) {
81
- await onCreateBuiltinComponents(serializationContext, ch, deserializeQueue, lateResolve);
81
+ await onCreateBuiltinComponents(serializationContext, ch, deserializeQueue, lateResolve, 0);
82
82
  }
83
83
  }
84
84
 
@@ -202,7 +202,7 @@ const unknownComponentsBuffer: Array<string> = [];
202
202
 
203
203
 
204
204
  async function onCreateBuiltinComponents(context: SerializationContext, obj: Object3D,
205
- deserialize: DeserializeData[], lateResolve: LateResolveCallback[]) {
205
+ deserialize: DeserializeData[], lateResolve: LateResolveCallback[], level: number) {
206
206
  if (!obj) return;
207
207
 
208
208
  // iterate injected data
@@ -267,20 +267,21 @@ async function onCreateBuiltinComponents(context: SerializationContext, obj: Obj
267
267
  }
268
268
  // console.debug("finished adding gltf builtin components", obj);
269
269
  }
270
- if (unknownComponentsBuffer.length > 0) {
271
- const unknown = unknownComponentsBuffer.join(", ");
272
- console.warn("unknown components: " + unknown);
273
- unknownComponentsBuffer.length = 0;
274
- if (isLocalNetwork())
275
- showBalloonMessage(`<strong>Unknown components in scene</strong>:\n\n${unknown}\n\nThis could mean you forgot to add a npmdef to your ExportInfo\n<a href="https://engine.needle.tools/docs/project_structure.html#creating-and-installing-a-npmdef" target="_blank">documentation</a>`, LogType.Warn);
276
- }
277
270
  }
278
271
 
279
272
  if (obj.children) {
280
273
  for (const ch of obj.children) {
281
- await onCreateBuiltinComponents(context, ch, deserialize, lateResolve);
274
+ await onCreateBuiltinComponents(context, ch, deserialize, lateResolve, level + 1);
282
275
  }
283
276
  }
277
+
278
+ if (unknownComponentsBuffer.length > 0 && level === 0) {
279
+ const unknown = unknownComponentsBuffer.join(", ");
280
+ console.warn(`Unknown components in scene: ${unknown}`);
281
+ unknownComponentsBuffer.length = 0;
282
+ if (isLocalNetwork())
283
+ showBalloonMessage(`<strong>Unknown components in scene</strong>:\n\n${unknown}\n\nThis could mean you forgot to add a npmdef to your ExportInfo\n<a href="https://engine.needle.tools/docs/project_structure.html#creating-and-installing-a-npmdef" target="_blank">documentation</a>`, LogType.Warn);
284
+ }
284
285
  }
285
286
 
286
287
  function handleDeserialization(data: DeserializeData, context: SerializationContext) {
@@ -98,6 +98,7 @@ export namespace NeedleEngineModelLoader {
98
98
  * }
99
99
  * return null;
100
100
  * });
101
+ * ```
101
102
  */
102
103
  export function onCreateCustomModelLoader(callback: CustomLoaderCallback, opts?: CustomLoaderOptions) {
103
104
  const entry = { name: opts?.name, priority: opts?.priority ?? 0, callback };
@@ -61,7 +61,7 @@ export async function onCreateLoader(url: string, context: Context): Promise<Cus
61
61
  switch (type) {
62
62
  case "unsupported":
63
63
  return null;
64
-
64
+
65
65
  default:
66
66
  case "unknown":
67
67
  {
@@ -287,15 +287,6 @@ async function onAfterLoaded(loader: Loader | CustomLoader, context: Context, gl
287
287
  gltfId = gltfId.split("?")[0];
288
288
  }
289
289
 
290
- // assign animations of loaded glTF to all scenes
291
- if ("scenes" in model) {
292
- for (const scene of model.scenes) {
293
- if (scene && !scene.animations?.length) {
294
- scene.animations = [...model.animations];
295
- }
296
- }
297
- }
298
-
299
290
  // E.g. fbx material cleanup
300
291
  postprocessLoadedFile(loader, model);
301
292
 
@@ -357,13 +348,27 @@ function checkIfUserAttemptedToLoadALocalFile(url: string) {
357
348
  /**
358
349
  * Postprocess the loaded file. This is used to apply any custom postprocessing to the loaded file.
359
350
  */
360
- function postprocessLoadedFile(loader: object, result: Model) {
351
+ function postprocessLoadedFile(loader: object, model: Model) {
352
+
353
+
354
+ // assign animations of loaded glTF to all scenes
355
+ if ("scenes" in model) {
356
+ for (const scene of model.scenes) {
357
+ if (scene && !scene.animations?.length) {
358
+ for (const anim of model.animations) {
359
+ if (!scene.animations.includes(anim)) {
360
+ scene.animations.push(anim);
361
+ }
362
+ }
363
+ }
364
+ }
365
+ }
361
366
 
362
367
  if (loader instanceof FBXLoader || loader instanceof OBJLoader) {
363
368
 
364
- let obj: Object3D | Model = result;
369
+ let obj: Object3D | Model = model;
365
370
  if (!(obj instanceof Object3D)) {
366
- obj = (result as GLTF).scene;
371
+ obj = (model as GLTF).scene || model.scenes.find(s => s);
367
372
  }
368
373
 
369
374
  obj.traverse((child) => {
@@ -169,8 +169,11 @@ export class SceneLightSettings extends Behaviour {
169
169
  this._hemisphereLightObj.removeFromParent();
170
170
  }
171
171
 
172
- if (this.sourceId)
173
- this.context.sceneLighting.internalEnableReflection(this.sourceId);
172
+ if (this.sourceId) {
173
+ if (!this.context.domElement.getAttribute("environment-image")) {
174
+ this.context.sceneLighting.internalEnableReflection(this.sourceId);
175
+ }
176
+ }
174
177
  }
175
178
 
176
179
  onDisable() {
@@ -70,7 +70,7 @@ export class NEEDLE_lightmaps implements GLTFLoaderPlugin {
70
70
  console.log(entry);
71
71
  let res: Promise<any> | null = null;
72
72
  // Check if the pointer is a json pointer:
73
- if (entry.pointer.startsWith("/textures/")) {
73
+ if (entry.pointer.startsWith("/textures/") || /** legacy support e.g. SOC */entry.pointer.startsWith("textures/")) {
74
74
  if (debug) console.log("Load texture from gltf", entry.pointer);
75
75
  res = resolveReferences(this.parser, entry.pointer).then(res => this.resolveTexture(entry, res));
76
76
  }
@@ -10,6 +10,12 @@ export function apply(object: Vector3) {
10
10
  }
11
11
  }
12
12
 
13
+ // NOTE: keep in sync with method declarations below
14
+ declare module 'three' {
15
+ export interface Vector3 {
16
+ slerp(end: Vector3, t: number): Vector3;
17
+ }
18
+ }
13
19
 
14
20
  Vector3.prototype["slerp"] = function (end: Vector3, t: number) {
15
21
  return slerp(this, end, t);
@@ -60,6 +60,8 @@ type SkyboxAttributes = {
60
60
  "background-color"?: string,
61
61
  /** URL to .exr, .hdr, .png, .jpg to be used for lighting */
62
62
  "environment-image"?: string,
63
+
64
+ "environment-intensity"?: number,
63
65
  }
64
66
 
65
67
  export type TonemappingAttributeOptions = "none" | "linear" | "neutral" | "agx";
@@ -45,6 +45,7 @@ const observedAttributes = [
45
45
  "tone-mapping-exposure",
46
46
  "background-blurriness",
47
47
  "background-color",
48
+ "environment-intensity",
48
49
  ]
49
50
 
50
51
  // https://developers.google.com/web/fundamentals/web-components/customelements
@@ -54,8 +55,8 @@ const observedAttributes = [
54
55
  * The needle engine web component creates and manages a needle engine context which is responsible for rendering a 3D scene using threejs.
55
56
  * The needle engine context is created when the src attribute is set and disposed when the needle engine is removed from the document (you can prevent this by setting the keep-alive attribute to true).
56
57
  * The needle engine context is accessible via the context property on the needle engine element (e.g. document.querySelector("needle-engine").context).
57
- * @link https://engine.needle.tools/docs/reference/needle-engine-attributes
58
- *
58
+ * See {@link https://engine.needle.tools/docs/reference/needle-engine-attributes}
59
+ *
59
60
  * @example
60
61
  * <needle-engine src="https://example.com/scene.glb"></needle-engine>
61
62
  * @example
@@ -64,7 +65,7 @@ const observedAttributes = [
64
65
  export class NeedleEngineWebComponent extends HTMLElement implements INeedleEngineComponent {
65
66
 
66
67
  static get observedAttributes() {
67
- return observedAttributes
68
+ return observedAttributes;
68
69
  }
69
70
 
70
71
  public get loadingProgress01(): number { return this._loadingProgress01; }
@@ -94,7 +95,7 @@ export class NeedleEngineWebComponent extends HTMLElement implements INeedleEngi
94
95
  /**
95
96
  * Get the current context for this web component instance. The context is created when the src attribute is set and the loading has finished.
96
97
  * The context is disposed when the needle engine is removed from the document (you can prevent this by setting the keep-alive attribute to true).
97
- * @returns {Promise<Context>} a promise that resolves to the context when the loading has finished
98
+ * @returns a promise that resolves to the context when the loading has finished
98
99
  */
99
100
  public getContext(): Promise<Context> {
100
101
  return new Promise((res, _rej) => {
@@ -325,22 +326,14 @@ export class NeedleEngineWebComponent extends HTMLElement implements INeedleEngi
325
326
  if (debug) console.log("ktx2DecoderPath", newValue);
326
327
  setKtx2TranscoderPath(newValue);
327
328
  break;
328
- case "tone-mapping": {
329
- this.applyAttributes();
330
- break;
331
- }
332
- case "tone-mapping-exposure": {
333
- this.applyAttributes();
334
- break;
335
- }
336
- case "background-blurriness": {
337
- const value = parseFloat(newValue);
338
- if (value != undefined && this._context) {
339
- this._context.scene.backgroundBlurriness = value;
340
- }
341
- break;
342
- }
343
- case "background-color": {
329
+
330
+ case "tonemapping":
331
+ case "tone-mapping":
332
+ case "tone-mapping-exposure":
333
+ case "background-blurriness":
334
+ case "background-color":
335
+ case "environment-intensity":
336
+ {
344
337
  this.applyAttributes();
345
338
  break;
346
339
  }
@@ -552,11 +545,18 @@ export class NeedleEngineWebComponent extends HTMLElement implements INeedleEngi
552
545
  const backgroundBlurriness = this.getAttribute("background-blurriness");
553
546
  if (backgroundBlurriness !== null && backgroundBlurriness !== undefined) {
554
547
  const value = parseFloat(backgroundBlurriness);
555
- if (value !== undefined && this._context) {
548
+ if (!isNaN(value) && this._context) {
556
549
  this._context.scene.backgroundBlurriness = value;
557
550
  }
558
551
  }
559
552
 
553
+ const environmentIntensity = this.getAttribute("environment-intensity");
554
+ if (environmentIntensity != undefined && this._context) {
555
+ const value = parseFloat(environmentIntensity);
556
+ if (!isNaN(value) && this._context)
557
+ this._context.scene.environmentIntensity = value;
558
+ }
559
+
560
560
  const backgroundColor = this.getAttribute("background-color");
561
561
  if (this._context?.renderer) {
562
562
  if (typeof backgroundColor === "string" && backgroundColor.length > 0) {
@@ -67,7 +67,7 @@ export class Animation extends Behaviour implements IAnimationComponent {
67
67
  get isAnimationComponent(): boolean { return true; }
68
68
  addClip(clip: AnimationClip) {
69
69
  if (!this.animations) this.animations = [];
70
- this.animations.push(clip);
70
+ if (!this.animations.includes(clip)) this.animations.push(clip);
71
71
  }
72
72
 
73
73
  /**
@@ -1,7 +1,6 @@
1
1
  import type { GLTF } from "three/examples/jsm/loaders/GLTFLoader.js";
2
2
 
3
3
  import { AnimationUtils } from "../engine/engine_animation.js";
4
- import { addComponent } from "../engine/engine_components.js";
5
4
  import { ContextEvent, ContextRegistry } from "../engine/engine_context_registry.js";
6
5
  import { Animation } from "./Animation.js";
7
6
  import { Animator } from "./Animator.js";
@@ -29,11 +28,7 @@ ContextRegistry.registerCallback(ContextEvent.ContextCreated, args => {
29
28
  return undefined;
30
29
  }, true);
31
30
  if (hasAnimation !== true) {
32
- AnimationUtils.assignAnimationsFromFile(file.file as GLTF, {
33
- createAnimationComponent: (obj, _clip) => {
34
- return addComponent(obj, Animation);
35
- },
36
- });
31
+ AnimationUtils.autoplayAnimations(file.file as GLTF);
37
32
  }
38
33
  }
39
34
  }
@@ -9,6 +9,7 @@ import { RenderTexture } from "../engine/engine_texture.js";
9
9
  import { getTempColor, getWorldPosition } from "../engine/engine_three_utils.js";
10
10
  import type { ICamera } from "../engine/engine_types.js"
11
11
  import { getParam } from "../engine/engine_utils.js";
12
+ import { NeedleXREventArgs } from "../engine/engine_xr.js";
12
13
  import { RGBAColor } from "../engine/js-extensions/index.js";
13
14
  import { Behaviour, GameObject } from "./Component.js";
14
15
  import { OrbitControls } from "./OrbitControls.js";
@@ -446,6 +447,11 @@ export class Camera extends Behaviour implements ICamera {
446
447
  this.context.removeCamera(this);
447
448
  }
448
449
 
450
+ onLeaveXR(_args: NeedleXREventArgs): void {
451
+ // Restore previous FOV
452
+ this.fieldOfView = this._fov;
453
+ }
454
+
449
455
 
450
456
  /** @internal */
451
457
  onBeforeRender() {
@@ -553,7 +559,7 @@ export class Camera extends Behaviour implements ICamera {
553
559
  }
554
560
 
555
561
  // restore previous fov (e.g. when user was in VR or AR and the camera's fov has changed)
556
- this.fieldOfView = this._fov;
562
+ this.fieldOfView = this.fieldOfView;
557
563
 
558
564
  if (debug) {
559
565
  const msg = `[Camera] Apply ClearFlags: ${ClearFlags[this._clearFlags]} - \"${this.name}\"`;