@needle-tools/engine 3.3.0-alpha → 3.5.0-alpha

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 (149) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/needle-engine.js +62620 -61118
  3. package/dist/needle-engine.min.js +434 -410
  4. package/dist/needle-engine.umd.cjs +435 -411
  5. package/lib/engine/api.d.ts +1 -0
  6. package/lib/engine/api.js +1 -0
  7. package/lib/engine/api.js.map +1 -1
  8. package/lib/engine/codegen/register_types.js +50 -2
  9. package/lib/engine/codegen/register_types.js.map +1 -1
  10. package/lib/engine/engine_context.d.ts +1 -1
  11. package/lib/engine/engine_context.js +21 -15
  12. package/lib/engine/engine_context.js.map +1 -1
  13. package/lib/engine/engine_context_registry.d.ts +5 -3
  14. package/lib/engine/engine_context_registry.js +10 -2
  15. package/lib/engine/engine_context_registry.js.map +1 -1
  16. package/lib/engine/engine_element.js.map +1 -1
  17. package/lib/engine/engine_element_loading.js +2 -3
  18. package/lib/engine/engine_element_loading.js.map +1 -1
  19. package/lib/engine/engine_gameobject.d.ts +1 -1
  20. package/lib/engine/engine_gameobject.js +4 -2
  21. package/lib/engine/engine_gameobject.js.map +1 -1
  22. package/lib/engine/engine_input.d.ts +2 -2
  23. package/lib/engine/engine_physics.d.ts +20 -93
  24. package/lib/engine/engine_physics.js +20 -892
  25. package/lib/engine/engine_physics.js.map +1 -1
  26. package/lib/engine/engine_physics.types.js.map +1 -1
  27. package/lib/engine/engine_physics_rapier.d.ts +103 -0
  28. package/lib/engine/engine_physics_rapier.js +1003 -0
  29. package/lib/engine/engine_physics_rapier.js.map +1 -0
  30. package/lib/engine/engine_three_utils.js +2 -2
  31. package/lib/engine/engine_three_utils.js.map +1 -1
  32. package/lib/engine/engine_types.d.ts +50 -1
  33. package/lib/engine/engine_types.js +8 -0
  34. package/lib/engine/engine_types.js.map +1 -1
  35. package/lib/engine-components/Animation.js +4 -0
  36. package/lib/engine-components/Animation.js.map +1 -1
  37. package/lib/engine-components/Collider.js +6 -6
  38. package/lib/engine-components/Collider.js.map +1 -1
  39. package/lib/engine-components/Joints.js +2 -2
  40. package/lib/engine-components/Joints.js.map +1 -1
  41. package/lib/engine-components/RigidBody.d.ts +0 -1
  42. package/lib/engine-components/RigidBody.js +24 -30
  43. package/lib/engine-components/RigidBody.js.map +1 -1
  44. package/lib/engine-components/codegen/components.d.ts +25 -1
  45. package/lib/engine-components/codegen/components.js +25 -1
  46. package/lib/engine-components/codegen/components.js.map +1 -1
  47. package/lib/engine-components/export/usdz/Extension.d.ts +4 -4
  48. package/lib/engine-components/export/usdz/ThreeUSDZExporter.d.ts +86 -0
  49. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +858 -0
  50. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js.map +1 -0
  51. package/lib/engine-components/export/usdz/USDZExporter.d.ts +6 -3
  52. package/lib/engine-components/export/usdz/USDZExporter.js +34 -11
  53. package/lib/engine-components/export/usdz/USDZExporter.js.map +1 -1
  54. package/lib/engine-components/export/usdz/extensions/Animation.d.ts +15 -15
  55. package/lib/engine-components/export/usdz/extensions/Animation.js +24 -29
  56. package/lib/engine-components/export/usdz/extensions/Animation.js.map +1 -1
  57. package/lib/engine-components/export/usdz/extensions/DocumentExtension.d.ts +5 -0
  58. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js +7 -0
  59. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js.map +1 -0
  60. package/lib/engine-components/export/usdz/extensions/USDZText.d.ts +47 -0
  61. package/lib/engine-components/export/usdz/extensions/USDZText.js +114 -0
  62. package/lib/engine-components/export/usdz/extensions/USDZText.js.map +1 -0
  63. package/lib/engine-components/export/usdz/extensions/behavior/Actions.d.ts +30 -0
  64. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js +89 -0
  65. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js.map +1 -0
  66. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.d.ts +23 -0
  67. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js +114 -0
  68. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js.map +1 -0
  69. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.d.ts +102 -0
  70. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js +458 -0
  71. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js.map +1 -0
  72. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.d.ts +111 -0
  73. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js +409 -0
  74. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js.map +1 -0
  75. package/lib/engine-components/postprocessing/PostProcessingHandler.js.map +1 -1
  76. package/lib/engine-components/ui/BaseUIComponent.d.ts +2 -0
  77. package/lib/engine-components/ui/BaseUIComponent.js +6 -0
  78. package/lib/engine-components/ui/BaseUIComponent.js.map +1 -1
  79. package/lib/engine-components/ui/Canvas.d.ts +11 -1
  80. package/lib/engine-components/ui/Canvas.js +72 -3
  81. package/lib/engine-components/ui/Canvas.js.map +1 -1
  82. package/lib/engine-components/ui/Graphic.js.map +1 -1
  83. package/lib/engine-components/ui/Image.js +4 -4
  84. package/lib/engine-components/ui/Image.js.map +1 -1
  85. package/lib/engine-components/ui/Interfaces.d.ts +11 -0
  86. package/lib/engine-components/ui/Interfaces.js +11 -0
  87. package/lib/engine-components/ui/Interfaces.js.map +1 -1
  88. package/lib/engine-components/ui/Layout.d.ts +65 -3
  89. package/lib/engine-components/ui/Layout.js +304 -3
  90. package/lib/engine-components/ui/Layout.js.map +1 -1
  91. package/lib/engine-components/ui/RectTransform.d.ts +8 -7
  92. package/lib/engine-components/ui/RectTransform.js +66 -36
  93. package/lib/engine-components/ui/RectTransform.js.map +1 -1
  94. package/lib/engine-components/utils/LookAt.d.ts +7 -1
  95. package/lib/engine-components/utils/LookAt.js +43 -6
  96. package/lib/engine-components/utils/LookAt.js.map +1 -1
  97. package/lib/engine-components/webxr/WebXRImageTracking.d.ts +4 -3
  98. package/lib/engine-components/webxr/WebXRImageTracking.js +81 -25
  99. package/lib/engine-components/webxr/WebXRImageTracking.js.map +1 -1
  100. package/lib/tsconfig.tsbuildinfo +1 -1
  101. package/package.json +1 -1
  102. package/plugins/vite/config.js +2 -1
  103. package/plugins/vite/defines.js +30 -0
  104. package/plugins/vite/dependency-watcher.js +173 -0
  105. package/plugins/vite/editor-connection.js +37 -39
  106. package/plugins/vite/index.js +5 -1
  107. package/plugins/vite/reload.js +16 -3
  108. package/src/engine/api.ts +1 -0
  109. package/src/engine/codegen/register_types.js +50 -2
  110. package/src/engine/engine_context.ts +32 -23
  111. package/src/engine/engine_context_registry.ts +13 -6
  112. package/src/engine/engine_element.ts +2 -1
  113. package/src/engine/engine_element_loading.ts +2 -3
  114. package/src/engine/engine_gameobject.ts +3 -2
  115. package/src/engine/engine_input.ts +2 -2
  116. package/src/engine/engine_physics.ts +25 -1020
  117. package/src/engine/engine_physics.types.ts +1 -3
  118. package/src/engine/engine_physics_rapier.ts +1127 -0
  119. package/src/engine/engine_three_utils.ts +2 -2
  120. package/src/engine/engine_types.ts +66 -4
  121. package/src/engine-components/Animation.ts +4 -0
  122. package/src/engine-components/Collider.ts +6 -6
  123. package/src/engine-components/Joints.ts +2 -2
  124. package/src/engine-components/RigidBody.ts +24 -31
  125. package/src/engine-components/codegen/components.ts +25 -1
  126. package/src/engine-components/export/usdz/Extension.ts +4 -5
  127. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +1312 -0
  128. package/src/engine-components/export/usdz/USDZExporter.ts +39 -17
  129. package/src/engine-components/export/usdz/extensions/Animation.ts +37 -45
  130. package/src/engine-components/export/usdz/extensions/DocumentExtension.ts +10 -0
  131. package/src/engine-components/export/usdz/extensions/USDZText.ts +142 -0
  132. package/src/engine-components/export/usdz/extensions/behavior/Actions.ts +99 -0
  133. package/src/engine-components/export/usdz/extensions/behavior/Behaviour.ts +181 -0
  134. package/src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +545 -0
  135. package/src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts +459 -0
  136. package/src/engine-components/postprocessing/PostProcessingHandler.ts +1 -1
  137. package/src/engine-components/ui/BaseUIComponent.ts +7 -1
  138. package/src/engine-components/ui/Canvas.ts +80 -5
  139. package/src/engine-components/ui/Graphic.ts +2 -0
  140. package/src/engine-components/ui/Image.ts +3 -3
  141. package/src/engine-components/ui/Interfaces.ts +30 -6
  142. package/src/engine-components/ui/Layout.ts +303 -4
  143. package/src/engine-components/ui/RectTransform.ts +67 -41
  144. package/src/engine-components/utils/LookAt.ts +60 -7
  145. package/src/engine-components/webxr/WebXRImageTracking.ts +100 -27
  146. package/lib/engine-components/export/usdz/types.d.ts +0 -34
  147. package/lib/engine-components/export/usdz/types.js +0 -2
  148. package/lib/engine-components/export/usdz/types.js.map +0 -1
  149. package/src/engine-components/export/usdz/types.ts +0 -39
@@ -1,7 +1,8 @@
1
- import { BufferGeometry, Camera, Clock, Color, DepthTexture, Group,
2
- Material, NearestFilter, NoToneMapping, Object3D, PCFSoftShadowMap,
3
- PerspectiveCamera, RGBAFormat, Scene, sRGBEncoding,
4
- Texture, WebGLRenderer, WebGLRenderTarget
1
+ import {
2
+ BufferGeometry, Camera, Clock, Color, DepthTexture, Group,
3
+ Material, NearestFilter, NoToneMapping, Object3D, PCFSoftShadowMap,
4
+ PerspectiveCamera, RGBAFormat, Scene, sRGBEncoding,
5
+ Texture, WebGLRenderer, WebGLRenderTarget
5
6
  } from 'three'
6
7
  import { Input } from './engine_input';
7
8
  import { Physics } from './engine_physics';
@@ -27,6 +28,7 @@ import { PlayerViewManager } from './engine_playerview';
27
28
  import { CoroutineData, ICamera, IComponent, IContext, ILight } from "./engine_types"
28
29
  import { destroy, foreachComponent } from './engine_gameobject';
29
30
  import { ContextEvent, ContextRegistry } from './engine_context_registry';
31
+ import { delay } from './engine_utils';
30
32
  // import { createCameraWithOrbitControl } from '../engine-components/CameraUtils';
31
33
 
32
34
 
@@ -251,8 +253,8 @@ export class Context implements IContext {
251
253
  this.isManagedExternally = true;
252
254
  }
253
255
  else {
254
- this.renderer = new WebGLRenderer({
255
- antialias: true
256
+ this.renderer = new WebGLRenderer({
257
+ antialias: true
256
258
  });
257
259
 
258
260
  // some tonemapping other than "NONE" is required for adjusting exposure with EXR environments
@@ -346,12 +348,13 @@ export class Context implements IContext {
346
348
  camera.updateProjectionMatrix();
347
349
  }
348
350
 
349
- onCreate(buildScene?: (context: Context, loadingOptions?: LoadingOptions) => Promise<void>, opts?: LoadingOptions) {
351
+ async onCreate(buildScene?: (context: Context, loadingOptions?: LoadingOptions) => Promise<void>, opts?: LoadingOptions) {
350
352
  if (this._isCreated) {
351
353
  console.warn("Context already created");
352
354
  return null;
353
355
  }
354
356
  this._isCreated = true;
357
+ await delay(1);
355
358
  return this.internalOnCreate(buildScene, opts);
356
359
  }
357
360
 
@@ -498,8 +501,8 @@ export class Context implements IContext {
498
501
 
499
502
  private async internalOnCreate(buildScene?: (context: Context, opts?: LoadingOptions) => Promise<void>, opts?: LoadingOptions) {
500
503
 
501
- // TODO: we could configure if we need physics
502
- await this.physics.createWorld();
504
+ Context.Current = this;
505
+ await ContextRegistry.dispatchCallback(ContextEvent.ContextCreationStart, this);
503
506
 
504
507
  // load and create scene
505
508
  let prepare_succeeded = true;
@@ -512,7 +515,7 @@ export class Context implements IContext {
512
515
  console.error(err);
513
516
  prepare_succeeded = false;
514
517
  }
515
- if (!prepare_succeeded) return;
518
+ if (!prepare_succeeded) return false;
516
519
 
517
520
  this.internalOnUpdateVisible();
518
521
 
@@ -523,6 +526,9 @@ export class Context implements IContext {
523
526
 
524
527
  Context.Current = this;
525
528
 
529
+ // TODO: we could configure if we need physics
530
+ // await this.physics.engine?.initialize();
531
+
526
532
  // Setup
527
533
  Context.Current = this;
528
534
  for (let i = 0; i < this.new_scripts.length; i++) {
@@ -609,13 +615,13 @@ export class Context implements IContext {
609
615
  if (debug)
610
616
  logHierarchy(this.scene, true);
611
617
 
612
- ContextRegistry.dispatchCallback(ContextEvent.ContextCreated, this);
618
+ return ContextRegistry.dispatchCallback(ContextEvent.ContextCreated, this);
613
619
  }
614
620
 
615
621
  private _accumulatedTime = 0;
616
622
  private _framerateClock = new Clock();
617
623
 
618
- private render(_, frame : XRFrame) {
624
+ private render(_, frame: XRFrame) {
619
625
  this._xrFrame = frame;
620
626
 
621
627
  this._currentFrameEvent = FrameEvent.Undefined;
@@ -627,7 +633,7 @@ export class Context implements IContext {
627
633
  }
628
634
  this._accumulatedTime = 0;
629
635
  }
630
-
636
+
631
637
  this._stats?.begin();
632
638
 
633
639
  Context.Current = this;
@@ -696,16 +702,19 @@ export class Context implements IContext {
696
702
  this.executeCoroutines(FrameEvent.LateUpdate);
697
703
  if (this.onHandlePaused()) return;
698
704
 
699
- const physicsSteps = 1;
700
- const dt = this.time.deltaTime / physicsSteps;
701
- for (let i = 0; i < physicsSteps; i++) {
702
- this._currentFrameEvent = FrameEvent.PrePhysicsStep;
703
- this.executeCoroutines(FrameEvent.PrePhysicsStep);
704
- this.physics.step(dt);
705
- this._currentFrameEvent = FrameEvent.PostPhysicsStep;
706
- this.executeCoroutines(FrameEvent.PostPhysicsStep);
705
+ if (this.physics.engine) {
706
+ const physicsSteps = 1;
707
+ const dt = this.time.deltaTime / physicsSteps;
708
+ for (let i = 0; i < physicsSteps; i++) {
709
+ this._currentFrameEvent = FrameEvent.PrePhysicsStep;
710
+ this.executeCoroutines(FrameEvent.PrePhysicsStep);
711
+ this.physics.engine.step(dt);
712
+ this._currentFrameEvent = FrameEvent.PostPhysicsStep;
713
+ this.executeCoroutines(FrameEvent.PostPhysicsStep);
714
+ }
715
+ this.physics.engine.postStep();
707
716
  }
708
- this.physics.postStep();
717
+
709
718
  if (this.onHandlePaused()) return;
710
719
 
711
720
  if (this.isVisibleToUser) {
@@ -781,7 +790,7 @@ export class Context implements IContext {
781
790
  this.renderRequiredTextures();
782
791
  // if (camera === this.mainCameraComponent?.cam) {
783
792
  // if (this.mainCameraComponent.activeTexture) {
784
-
793
+
785
794
  // }
786
795
  // }
787
796
  if (this.composer && !this.isInXR) {
@@ -1,8 +1,10 @@
1
- import { IContext } from "./engine_types";
1
+ import { IComponent, IContext } from "./engine_types";
2
2
 
3
3
  export enum ContextEvent {
4
4
  /** called when the context is registered to the registry, the context is not fully initialized at this point */
5
5
  ContextRegistered = "ContextRegistered",
6
+ /** called before the first glb is loaded, can be used to initialize physics engine, is awaited */
7
+ ContextCreationStart = "ContextCreationStart",
6
8
  ContextCreated = "ContextCreated",
7
9
  ContextDestroyed = "ContextDestroyed",
8
10
  MissingCamera = "MissingCamera",
@@ -13,11 +15,11 @@ export type ContextEventArgs = {
13
15
  context: IContext;
14
16
  }
15
17
 
16
- export type ContextCallback = (evt: ContextEventArgs) => void;
18
+ export type ContextCallback = (evt: ContextEventArgs) => void | Promise<any> | IComponent;
17
19
 
18
20
  export class ContextRegistry {
19
21
 
20
- static get Current(): IContext{
22
+ static get Current(): IContext {
21
23
  return globalThis["NeedleEngine.Context.Current"]
22
24
  }
23
25
  static set Current(ctx: IContext) {
@@ -51,10 +53,15 @@ export class ContextRegistry {
51
53
  this._callbacks[evt].splice(index, 1);
52
54
  }
53
55
 
54
- static dispatchCallback(evt: ContextEvent, context:IContext) {
55
- if (!this._callbacks[evt]) return;
56
+ static dispatchCallback(evt: ContextEvent, context: IContext) {
57
+ if (!this._callbacks[evt]) return true;
56
58
  const args = { event: evt, context }
57
- this._callbacks[evt].forEach(cb => cb(args));
59
+ const promises = new Array<Promise<any>>();
60
+ this._callbacks[evt].forEach(cb => {
61
+ const res = cb(args)
62
+ if (res instanceof Promise) promises.push(res);
63
+ });
64
+ return Promise.all(promises);
58
65
  }
59
66
 
60
67
  static addContextCreatedCallback(callback: ContextCallback) {
@@ -164,6 +164,7 @@ export class NeedleEngineHTMLElement extends HTMLElement implements INeedleEngin
164
164
  return;
165
165
  }
166
166
 
167
+
167
168
  this._previousSrc = src;
168
169
 
169
170
  // Set the source attribute so codegen doesnt try to re-assign it again and we communicate to the outside which root files are being loaded
@@ -221,7 +222,7 @@ export class NeedleEngineHTMLElement extends HTMLElement implements INeedleEngin
221
222
  if (!url.includes(".glb") && !url.includes(".gltf")) {
222
223
  const warning = `Needle Engine: found suspicious src "${url}"`;
223
224
  console.warn(warning);
224
- if(isLocalNetwork()) showBalloonWarning(warning);
225
+ if (isLocalNetwork()) showBalloonWarning(warning);
225
226
  }
226
227
  const fileName = getNameFromUrl(url);
227
228
  const progress: LoadingProgressArgs = {
@@ -73,7 +73,7 @@ export class EngineLoadingView implements ILoadingViewHandler {
73
73
  }
74
74
 
75
75
  onLoadingBegin(message?: string) {
76
- if (debug) console.log("Begin Loading")
76
+ if (debug) console.warn("Begin Loading")
77
77
  if (!this._loadingElement) {
78
78
  for (let i = 0; i < this._element.children.length; i++) {
79
79
  const el = this._element.children[i] as HTMLElement;
@@ -98,9 +98,8 @@ export class EngineLoadingView implements ILoadingViewHandler {
98
98
  }
99
99
 
100
100
  onLoadingUpdate(progress: LoadingProgressArgs | ProgressEvent | number, message?: string) {
101
- // if the element has no parent we want to add it
102
101
  if (!this._loadingElement?.parentElement) {
103
- this.onLoadingBegin(message);
102
+ return;
104
103
  }
105
104
  // console.log(callback.name, callback.progress.loaded / callback.progress.total, callback.index + "/" + callback.count);
106
105
  let total01 = 0;
@@ -163,8 +163,9 @@ export function foreachComponent(instance: Object3D, cb: ForEachComponentCallbac
163
163
  return internalForEachComponent(instance, cb, recursive);
164
164
  }
165
165
 
166
- export function* foreachComponentEnumerator<T extends IComponent>(instance: Object3D, type?: Constructor<T>, includeChildren: boolean = false): Generator<T> {
166
+ export function* foreachComponentEnumerator<T extends IComponent>(instance: Object3D, type?: Constructor<T>, includeChildren: boolean = false, maxLevel: number = 999, _currentLevel: number = 0): Generator<T> {
167
167
  if (!instance?.userData.components) return;
168
+ if (_currentLevel > maxLevel) return;
168
169
  for (const comp of instance.userData.components) {
169
170
  if (type && comp?.isComponent === true && comp instanceof type) {
170
171
  yield comp;
@@ -175,7 +176,7 @@ export function* foreachComponentEnumerator<T extends IComponent>(instance: Obje
175
176
  }
176
177
  if (includeChildren === true) {
177
178
  for (const ch of instance.children) {
178
- yield* foreachComponentEnumerator(ch, type, true);
179
+ yield* foreachComponentEnumerator(ch, type, true, maxLevel, _currentLevel + 1);
179
180
  }
180
181
  }
181
182
  }
@@ -2,7 +2,7 @@ import { Vector2 } from 'three';
2
2
  import { showBalloonMessage, showBalloonWarning } from './debug/debug';
3
3
  import { assign } from './engine_serialization_core';
4
4
  import { Context } from './engine_setup';
5
- import { Vec2 } from './engine_types';
5
+ import { IInput, Vec2 } from './engine_types';
6
6
  import { getParam } from './engine_utils';
7
7
 
8
8
  const debug = getParam("debuginput");
@@ -52,7 +52,7 @@ export enum PointerType {
52
52
  // }
53
53
  // }
54
54
 
55
- export class Input extends EventTarget {
55
+ export class Input extends EventTarget implements IInput {
56
56
 
57
57
  _doubleClickTimeThreshold = .2;
58
58
  _longPressTimeThreshold = 1;