Needle Engine

Changes between version 3.31.5 and 3.32.12-alpha
Files changed (226) hide show
  1. src/engine-schemes/vrUserStateBuffer.fbs +0 -0
  2. src/engine-components/export/usdz/extensions/behavior/Actions.ts +3 -2
  3. src/engine-components/AlignmentConstraint.ts +3 -2
  4. src/engine-components/Animation.ts +4 -3
  5. src/engine-components/export/usdz/extensions/Animation.ts +5 -4
  6. src/engine-components/AnimationCurve.ts +18 -2
  7. src/engine-components/AnimationUtils.ts +4 -3
  8. src/engine-components/export/usdz/utils/animationutils.ts +5 -4
  9. src/engine-components/Animator.ts +6 -5
  10. src/engine-components/AnimatorController.ts +10 -9
  11. src/engine-components/postprocessing/Effects/Antialiasing.ts +1 -0
  12. src/engine-components/api.ts +6 -8
  13. src/engine/api.ts +21 -22
  14. src/engine-components/export/usdz/extensions/behavior/AudioExtension.ts +3 -2
  15. src/engine-components/AudioListener.ts +2 -1
  16. src/engine-components/AudioSource.ts +13 -8
  17. src/engine-components/avatar/Avatar_Brain_LookAt.ts +5 -4
  18. src/engine-components/avatar/Avatar_MouthShapes.ts +4 -3
  19. src/engine-components/avatar/AvatarBlink_Simple.ts +3 -2
  20. src/engine-components/avatar/AvatarEyeLook_Rotation.ts +5 -4
  21. src/engine-components/AvatarLoader.ts +6 -5
  22. src/engine-components/AxesHelper.ts +3 -2
  23. src/engine-components/ui/BaseUIComponent.ts +27 -25
  24. src/engine-components/BasicIKConstraint.ts +3 -2
  25. src/engine-components/export/usdz/extensions/behavior/Behaviour.ts +1 -1
  26. src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +12 -13
  27. src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts +2 -2
  28. src/engine-components/postprocessing/Effects/Bloom.ts +1 -0
  29. src/engine-components/BoxHelperComponent.ts +4 -3
  30. src/engine-components/ui/Button.ts +10 -10
  31. src/engine-components/Camera.ts +12 -13
  32. src/engine-components/CameraUtils.ts +8 -7
  33. src/engine-components/ui/Canvas.ts +38 -20
  34. src/engine-components/ui/CanvasGroup.ts +3 -3
  35. src/engine-components/CharacterController.ts +5 -4
  36. src/engine-components/postprocessing/Effects/ChromaticAberration.ts +1 -0
  37. src/engine-components/Collider.ts +6 -5
  38. src/engine-components/postprocessing/Effects/ColorAdjustments.ts +2 -1
  39. src/engine-components/Component.ts +112 -19
  40. src/engine-components/codegen/components.ts +10 -15
  41. src/engine-components/ContactShadows.ts +3 -3
  42. src/engine/debug/debug_console.ts +8 -4
  43. src/engine/debug/debug_overlay.ts +8 -7
  44. src/engine/debug/debug.ts +2 -2
  45. src/engine-components/DeleteBox.ts +1 -0
  46. src/engine-components/postprocessing/Effects/DepthOfField.ts +2 -1
  47. src/engine-components/DeviceFlag.ts +1 -1
  48. src/engine-components/DragControls.ts +939 -182
  49. src/engine-components/DropListener.ts +5 -4
  50. src/engine-components/Duplicatable.ts +73 -91
  51. src/engine/engine_addressables.ts +7 -6
  52. src/engine/engine_assetdatabase.ts +2 -1
  53. src/engine/engine_camera.ts +2 -1
  54. src/engine/engine_components.ts +7 -6
  55. src/engine/engine_context.ts +111 -69
  56. src/engine/engine_create_objects.ts +13 -1
  57. src/engine/engine_element_loading.ts +24 -11
  58. src/engine/engine_element_overlay.ts +17 -0
  59. src/engine/engine_element.ts +35 -10
  60. src/engine/engine_gameobject.ts +21 -9
  61. src/engine/engine_gizmos.ts +61 -20
  62. src/engine/engine_gltf_builtin_components.ts +11 -9
  63. src/engine/engine_gltf.ts +4 -3
  64. src/engine/engine_hot_reload.ts +2 -2
  65. src/engine/engine_input.ts +480 -185
  66. src/engine/engine_license.ts +26 -12
  67. src/engine/engine_lifecycle_api.ts +28 -4
  68. src/engine/engine_lifecycle_functions_internal.ts +2 -2
  69. src/engine/engine_lightdata.ts +4 -3
  70. src/engine/engine_loaders.ts +3 -3
  71. src/engine/engine_mainloop_utils.ts +34 -7
  72. src/engine/engine_networking_auto.ts +1 -1
  73. src/engine/engine_networking_files_default_components.ts +2 -1
  74. src/engine/engine_networking_files.ts +8 -7
  75. src/engine/engine_networking_instantiate.ts +17 -12
  76. src/engine/engine_networking_peer.ts +2 -1
  77. src/engine/engine_networking_streams.ts +8 -7
  78. src/engine/engine_networking.ts +12 -8
  79. src/engine/engine_physics_rapier.ts +57 -44
  80. src/engine/engine_physics.ts +23 -18
  81. src/engine/engine_playerview.ts +2 -1
  82. src/engine/engine_scenelighting.ts +5 -4
  83. src/engine/engine_scenetools.ts +9 -8
  84. src/engine/engine_serialization_builtin_serializer.ts +7 -6
  85. src/engine/engine_serialization_core.ts +8 -7
  86. src/engine/engine_serialization.ts +4 -5
  87. src/engine/engine_shaders.ts +4 -3
  88. src/engine/engine_texture.ts +2 -1
  89. src/engine/engine_three_utils.ts +18 -4
  90. src/engine/engine_time.ts +2 -1
  91. src/engine/engine_types.ts +25 -4
  92. src/engine/engine_util_decorator.ts +3 -2
  93. src/engine/engine_utils_screenshot.ts +2 -1
  94. src/engine/engine_utils.ts +71 -6
  95. src/engine/engine.ts +3 -3
  96. src/engine-components/ui/EventSystem.ts +237 -178
  97. src/engine-components/EventTrigger.ts +2 -2
  98. src/engine/extensions/EXT_texture_exr.ts +3 -2
  99. src/engine/extensions/extension_utils.ts +2 -1
  100. src/engine-components/export/usdz/Extension.ts +2 -1
  101. src/engine/extensions/extensions.ts +12 -11
  102. src/engine-components/js-extensions/ExtensionUtils.ts +1 -0
  103. src/engine-components/FlyControls.ts +2 -1
  104. src/engine-components/Fog.ts +2 -1
  105. src/engine-components/Gizmos.ts +5 -4
  106. src/engine-components/export/gltf/GltfExport.ts +6 -6
  107. src/engine-components/ui/Graphic.ts +8 -7
  108. src/engine-components/GridHelper.ts +4 -3
  109. src/engine-components/GroundProjection.ts +11 -5
  110. src/engine-components/ui/Image.ts +2 -1
  111. src/engine-components/export/usdz/index.ts +3 -3
  112. src/engine-components/postprocessing/index.ts +2 -2
  113. src/engine-components/timeline/index.ts +2 -2
  114. src/engine-components/webxr/index.ts +2 -3
  115. src/engine/extensions/index.ts +2 -2
  116. src/engine-components/ui/InputField.ts +4 -4
  117. src/engine-components/Interactable.ts +6 -14
  118. src/engine-components/Joints.ts +1 -0
  119. src/engine-components/ui/Layout.ts +3 -3
  120. src/engine-components/Light.ts +10 -13
  121. src/engine-components/LODGroup.ts +5 -4
  122. src/engine-components/debug/LogStats.ts +1 -1
  123. src/engine-components/utils/LookAt.ts +4 -4
  124. src/engine-components/LookAtConstraint.ts +3 -2
  125. src/engine/extensions/NEEDLE_animator_controller_model.ts +3 -2
  126. src/engine/extensions/NEEDLE_components.ts +7 -6
  127. src/engine/extensions/NEEDLE_gameobject_data.ts +3 -3
  128. src/engine/extensions/NEEDLE_lighting_settings.ts +7 -6
  129. src/engine/extensions/NEEDLE_lightmaps.ts +8 -7
  130. src/engine/extensions/NEEDLE_persistent_assets.ts +3 -2
  131. src/engine/extensions/NEEDLE_progressive.ts +5 -3
  132. src/engine/extensions/NEEDLE_render_objects.ts +18 -18
  133. src/engine/extensions/NEEDLE_techniques_webgl.ts +8 -5
  134. src/needle-engine.ts +0 -3
  135. src/engine-components/NestedGltf.ts +4 -4
  136. src/engine-components/Networking.ts +1 -1
  137. src/engine-components/js-extensions/Object3D.ts +11 -12
  138. src/engine-components/OffsetConstraint.ts +4 -3
  139. src/engine-components/utils/OpenURL.ts +8 -40
  140. src/engine-components/OrbitControls.ts +15 -15
  141. src/engine-components/ui/Outline.ts +3 -2
  142. src/engine-components/ParticleSystem.ts +38 -33
  143. src/engine-components/ParticleSystemModules.ts +66 -17
  144. src/engine-components/ParticleSystemSubEmitter.ts +4 -3
  145. src/engine-components/postprocessing/Effects/Pixelation.ts +4 -3
  146. src/engine-components/timeline/PlayableDirector.ts +10 -9
  147. src/engine-components/PlayerColor.ts +19 -14
  148. src/engine-components-experimental/networking/PlayerSync.ts +113 -26
  149. src/engine-components/ui/PointerEvents.ts +118 -30
  150. src/engine-components/postprocessing/PostProcessingEffect.ts +5 -4
  151. src/engine-components/postprocessing/PostProcessingHandler.ts +5 -4
  152. src/engine-components-experimental/Presentation.ts +1 -1
  153. src/engine-components/ui/Raycaster.ts +27 -8
  154. src/engine-components/ui/RaycastUtils.ts +2 -1
  155. src/engine-components/ui/RectTransform.ts +6 -5
  156. src/engine-components/ReflectionProbe.ts +3 -2
  157. src/engine/codegen/register_types.ts +17 -28
  158. src/engine-components/Renderer.ts +33 -38
  159. src/engine-components/RendererLightmap.ts +3 -2
  160. src/engine-components/js-extensions/RGBAColor.ts +2 -1
  161. src/engine-components/RigidBody.ts +7 -6
  162. src/engine-components/SceneSwitcher.ts +13 -12
  163. src/engine-schemes/schemes.ts +2 -1
  164. src/engine-components/ScreenCapture.ts +9 -8
  165. src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.ts +1 -0
  166. src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.ts +3 -2
  167. src/engine-components/ShadowCatcher.ts +4 -3
  168. src/engine-components/timeline/SignalAsset.ts +2 -2
  169. src/engine-components/Skybox.ts +10 -9
  170. src/engine-components/SmoothFollow.ts +5 -4
  171. src/engine-components/ui/SpatialHtml.ts +1 -0
  172. src/engine-components/SpatialTrigger.ts +4 -3
  173. src/engine-components/SpectatorCamera.ts +23 -33
  174. src/engine-components/SpriteRenderer.ts +4 -3
  175. src/engine-components/SyncedCamera.ts +13 -13
  176. src/engine-components/SyncedRoom.ts +2 -2
  177. src/engine-components/SyncedTransform.ts +25 -7
  178. src/engine/tests/test_utils.ts +1 -1
  179. src/engine-components/TestRunner.ts +6 -5
  180. src/engine-components/ui/Text.ts +9 -8
  181. src/engine-components/export/usdz/ThreeUSDZExporter.ts +33 -32
  182. src/engine-components/postprocessing/Effects/TiltShiftEffect.ts +4 -3
  183. src/engine-components/timeline/TimelineTracks.ts +60 -22
  184. src/engine-components/postprocessing/Effects/Tonemapping.ts +1 -0
  185. src/engine-components/TransformGizmo.ts +6 -5
  186. src/engine/extensions/usage_tracker.ts +2 -1
  187. src/engine-components/export/usdz/USDZExporter.ts +21 -92
  188. src/engine-components/export/usdz/extensions/USDZText.ts +6 -5
  189. src/engine-components/export/usdz/extensions/USDZUI.ts +7 -6
  190. src/engine-components/ui/Utils.ts +4 -2
  191. src/engine-components/js-extensions/Vector.ts +2 -1
  192. src/engine-components/VideoPlayer.ts +7 -6
  193. src/engine-components/postprocessing/Effects/Vignette.ts +3 -2
  194. src/engine-components/Voip.ts +6 -5
  195. src/engine-components/postprocessing/Volume.ts +7 -6
  196. src/engine-schemes/vr-user-state-buffer.ts +37 -30
  197. src/engine-components/webxr/WebARCameraBackground.ts +46 -53
  198. src/engine-components/webxr/WebARSessionRoot.ts +400 -29
  199. src/engine-components/webxr/WebXR.ts +212 -672
  200. src/engine-components/webxr/WebXRAvatar.ts +10 -300
  201. src/engine-components/webxr/WebXRController.ts +0 -1168
  202. src/engine-components/webxr/WebXRGrabRendering.ts +0 -151
  203. src/engine-components/webxr/WebXRImageTracking.ts +69 -77
  204. src/engine-components/webxr/WebXRPlaneTracking.ts +56 -49
  205. src/engine-components/webxr/WebXRRig.ts +45 -8
  206. src/engine-components/webxr/WebXRSync.ts +0 -463
  207. src/engine-components/XRFlag.ts +0 -139
  208. src/engine-schemes/README.md +2 -0
  209. src/engine-components/webxr/Avatar.ts +221 -0
  210. src/engine/engine_xr.ts +2 -0
  211. src/engine/xr/index.ts +5 -0
  212. src/engine/xr/internal.ts +35 -0
  213. src/engine/xr/NeedleXRController.ts +639 -0
  214. src/engine/xr/NeedleXRSession.ts +1261 -0
  215. src/engine/xr/NeedleXRSync.ts +221 -0
  216. src/engine/xr/SceneTransition.ts +79 -0
  217. src/engine-components/webxr/TeleportTarget.ts +9 -0
  218. src/engine/xr/TempXRContext.ts +183 -0
  219. src/engine-components/webxr/types.ts +4 -0
  220. src/engine/xr/utils.ts +40 -0
  221. src/engine-components/webxr/WebXRButtons.ts +317 -0
  222. src/engine-components/webxr/controllers/XRControllerFollow.ts +67 -0
  223. src/engine-components/webxr/controllers/XRControllerModel.ts +255 -0
  224. src/engine-components/webxr/controllers/XRControllerMovement.ts +328 -0
  225. src/engine-components/webxr/XRFlag.ts +143 -0
  226. src/engine/xr/XRRig.ts +9 -0
src/engine-schemes/vrUserStateBuffer.fbs CHANGED
File without changes
src/engine-components/export/usdz/extensions/behavior/Actions.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { Object3D, Matrix4, Material, BufferGeometry } from "three";
1
+ import { BufferGeometry,Material, Matrix4, Object3D } from "three";
2
+
3
+ import { USDDocument,USDObject } from "../../ThreeUSDZExporter.js";
2
4
  import { ActionBuilder, ActionModel } from "./BehavioursBuilder.js";
3
- import { USDObject, USDDocument } from "../../ThreeUSDZExporter.js";
4
5
 
5
6
  export abstract class DocumentAction {
6
7
 
src/engine-components/AlignmentConstraint.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { Behaviour, GameObject } from "./Component.js";
2
- import * as utils from "./../engine/engine_three_utils.js";
3
1
  import { Vector3 } from "three";
2
+
4
3
  import { serializable } from "../engine/engine_serialization_decorator.js";
4
+ import * as utils from "./../engine/engine_three_utils.js";
5
+ import { Behaviour, GameObject } from "./Component.js";
5
6
 
6
7
  export class AlignmentConstraint extends Behaviour {
7
8
 
src/engine-components/Animation.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { Behaviour } from "./Component.js";
2
1
  import { AnimationAction, AnimationClip, AnimationMixer, LoopOnce, LoopRepeat } from "three";
3
- import { MixerEvent } from "./Animator.js";
2
+
3
+ import { Mathf } from "../engine/engine_math.js";
4
4
  import { serializable } from "../engine/engine_serialization_decorator.js";
5
- import { Mathf } from "../engine/engine_math.js";
6
5
  import type { Vec2 } from "../engine/engine_types.js";
7
6
  import { getParam } from "../engine/engine_utils.js";
7
+ import { MixerEvent } from "./Animator.js";
8
+ import { Behaviour } from "./Component.js";
8
9
 
9
10
  const debug = getParam("debuganimation");
10
11
 
src/engine-components/export/usdz/extensions/Animation.ts CHANGED
@@ -1,9 +1,10 @@
1
+ import { AnimationClip, Bone,Interpolant, KeyframeTrack, Matrix4, Object3D, PropertyBinding, Quaternion, Vector3 } from "three";
2
+
3
+ import { getParam } from "../../../../engine/engine_utils.js";
4
+ import { Animator } from "../../../Animator.js";
1
5
  import { GameObject } from "../../../Component.js";
2
- import { getParam } from "../../../../engine/engine_utils.js";
3
- import { USDObject, buildMatrix, findStructuralNodesInBoneHierarchy, usdNumberFormatting as fn, getPathToSkeleton } from "../ThreeUSDZExporter.js";
4
6
  import type { IUSDExporterExtension } from "../Extension.js";
5
- import { Object3D, Matrix4, Vector3, Quaternion, Interpolant, AnimationClip, KeyframeTrack, PropertyBinding, Bone } from "three";
6
- import { Animator } from "../../../Animator.js";
7
+ import { buildMatrix, findStructuralNodesInBoneHierarchy, getPathToSkeleton,usdNumberFormatting as fn, USDObject } from "../ThreeUSDZExporter.js";
7
8
 
8
9
  const debug = getParam("debugusdzanimation");
9
10
  const debugSerialization = getParam("debugusdzanimationserialization");
src/engine-components/AnimationCurve.ts CHANGED
@@ -23,6 +23,22 @@
23
23
  @serializable(Keyframe)
24
24
  keys!: Array<Keyframe>;
25
25
 
26
+ clone() {
27
+ const curve = new AnimationCurve();
28
+ curve.keys = this.keys?.map(k => {
29
+ const key = new Keyframe();
30
+ key.time = k.time;
31
+ key.value = k.value;
32
+ key.inTangent = k.inTangent;
33
+ key.inWeight = k.inWeight;
34
+ key.outTangent = k.outTangent;
35
+ key.outWeight = k.outWeight;
36
+ key.weightedMode = k.weightedMode;
37
+ return key;
38
+ }) || [];
39
+ return curve;
40
+ }
41
+
26
42
  get duration(): number {
27
43
  if (!this.keys || this.keys.length == 0) return 0;
28
44
  return this.keys[this.keys.length - 1].time;
@@ -38,9 +54,9 @@
38
54
  for (let i = 0; i < this.keys.length; i++) {
39
55
  const kf = this.keys[i];
40
56
  if (kf.time <= time) {
41
- const hasNextKeyframe = i+1 < this.keys.length;
57
+ const hasNextKeyframe = i + 1 < this.keys.length;
42
58
  if (hasNextKeyframe) {
43
- const nextKf = this.keys[i+1];
59
+ const nextKf = this.keys[i + 1];
44
60
  // if the next
45
61
  if (nextKf.time < time) continue;
46
62
  // tangents are set to Infinity if interpolation is set to constant - in that case we should always return the floored value
src/engine-components/AnimationUtils.ts CHANGED
@@ -1,11 +1,12 @@
1
+ import { Object3D } from "three";
1
2
  import type { GLTF } from "three/examples/jsm/loaders/GLTFLoader.js";
3
+
4
+ import { addNewComponent } from "../engine/engine_components.js";
2
5
  import { ContextEvent, ContextRegistry } from "../engine/engine_context_registry.js";
3
- import { addNewComponent } from "../engine/engine_components.js";
6
+ import { Animation } from "./Animation.js";
4
7
  import { Animator } from "./Animator.js";
5
- import { Animation } from "./Animation.js";
6
8
  import { GameObject } from "./Component.js";
7
9
  import { PlayableDirector } from "./timeline/PlayableDirector.js";
8
- import { Object3D } from "three";
9
10
 
10
11
 
11
12
  const $objectAnimationKey = Symbol("objectIsAnimatedData");
src/engine-components/export/usdz/utils/animationutils.ts CHANGED
@@ -1,9 +1,10 @@
1
+ import { AnimationClip,Object3D } from "three";
2
+
3
+ import { getParam } from "../../../../engine/engine_utils.js";
4
+ import { Animation } from "../../../Animation.js";
1
5
  import { Animator } from "../../../Animator.js";
2
- import { Animation } from "../../../Animation.js";
3
- import { Object3D, AnimationClip } from "three";
6
+ import { Behaviour, GameObject } from "../../../Component.js";
4
7
  import { AnimationExtension } from "../extensions/Animation.js";
5
- import { Behaviour, GameObject } from "../../../Component.js";
6
- import { getParam } from "../../../../engine/engine_utils.js";
7
8
  import { PlayAnimationOnClick } from "../extensions/behavior/BehaviourComponents.js";
8
9
 
9
10
  const debug = getParam("debugusdz");
src/engine-components/Animator.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { Behaviour } from "./Component.js";
2
- import type { AnimationActionLoopStyles, AnimationAction, AnimationMixer } from "three";
1
+ import type { AnimationAction, AnimationActionLoopStyles, AnimationMixer } from "three";
2
+
3
+ import { Mathf } from "../engine/engine_math.js";
4
+ import { serializable } from "../engine/engine_serialization_decorator.js";
3
5
  import { getParam } from "../engine/engine_utils.js";
4
6
  import type { AnimatorControllerModel } from "../engine/extensions/NEEDLE_animator_controller_model.js";
7
+ import { getObjectAnimated } from "./AnimationUtils.js";
5
8
  import { AnimatorController } from "./AnimatorController.js";
6
- import { serializable } from "../engine/engine_serialization_decorator.js";
7
- import { Mathf } from "../engine/engine_math.js";
8
- import { getObjectAnimated } from "./AnimationUtils.js";
9
+ import { Behaviour } from "./Component.js";
9
10
 
10
11
  const debug = getParam("debuganimator");
11
12
 
src/engine-components/AnimatorController.ts CHANGED
@@ -1,15 +1,16 @@
1
- import { Animator } from "./Animator.js";
2
- import type { AnimatorControllerModel, Condition, State, Transition } from "../engine/extensions/NEEDLE_animator_controller_model.js";
3
- import { AnimatorConditionMode, AnimatorControllerParameterType, AnimatorStateInfo, createMotion, StateMachineBehaviour } from "../engine/extensions/NEEDLE_animator_controller_model.js";
4
1
  import { AnimationAction, AnimationClip, AnimationMixer, AxesHelper, Euler, KeyframeTrack, LoopOnce, Object3D, Quaternion, Vector3 } from "three";
5
- import { deepClone, getParam } from "../engine/engine_utils.js";
2
+
3
+ import { isDevEnvironment } from "../engine/debug/index.js";
4
+ import { Mathf } from "../engine/engine_math.js";
5
+ import { InstantiateIdProvider } from "../engine/engine_networking_instantiate.js";
6
+ import { assign,SerializationContext, TypeSerializer } from "../engine/engine_serialization_core.js";
6
7
  import { Context } from "../engine/engine_setup.js";
8
+ import { isAnimationAction } from "../engine/engine_three_utils.js";
7
9
  import { TypeStore } from "../engine/engine_typestore.js";
8
- import { SerializationContext, TypeSerializer, assign } from "../engine/engine_serialization_core.js";
9
- import { Mathf } from "../engine/engine_math.js";
10
- import { isAnimationAction } from "../engine/engine_three_utils.js";
11
- import { isDevEnvironment } from "../engine/debug/index.js";
12
- import { InstantiateIdProvider } from "../engine/engine_networking_instantiate.js";
10
+ import { deepClone, getParam } from "../engine/engine_utils.js";
11
+ import type { AnimatorControllerModel, Condition, State, Transition } from "../engine/extensions/NEEDLE_animator_controller_model.js";
12
+ import { AnimatorConditionMode, AnimatorControllerParameterType, AnimatorStateInfo, createMotion, StateMachineBehaviour } from "../engine/extensions/NEEDLE_animator_controller_model.js";
13
+ import { Animator } from "./Animator.js";
13
14
 
14
15
  const debug = getParam("debuganimatorcontroller");
15
16
  const debugRootMotion = getParam("debugrootmotion");
src/engine-components/postprocessing/Effects/Antialiasing.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EdgeDetectionMode, SMAAEffect, SMAAPreset } from "postprocessing";
2
+
2
3
  import { serializable } from "../../../engine/engine_serialization.js";
3
4
  import { type EffectProviderResult, PostProcessingEffect } from "../PostProcessingEffect.js";
4
5
  import { VolumeParameter } from "../VolumeParameter.js";
src/engine-components/api.ts CHANGED
@@ -1,20 +1,18 @@
1
+ export * from "./codegen/components.js";
1
2
  export { Behaviour, Component, GameObject } from "./Component.js"
2
- export * from "./codegen/components.js";
3
3
 
4
4
  // We dont want to export everything in the extensions
5
+ export { ClearFlags } from "./Camera.js"
6
+ export * from "./export/index.js"
7
+ export * from "./js-extensions/Object3D.js";
5
8
  export * from "./js-extensions/RGBAColor.js";
6
- export * from "./js-extensions/Object3D.js";
7
- export * from "./XRFlag.js"
8
-
9
- export * from "./export/index.js"
10
9
  export * from "./postprocessing/index.js"
10
+ export { type ISceneEventListener } from "./SceneSwitcher.js";
11
11
  export * from "./timeline/index.js"
12
12
  export * from "./ui/index.js"
13
13
  export * from "./webxr/index.js"
14
+ export * from "./webxr/XRFlag.js"
14
15
 
15
- export { ClearFlags } from "./Camera.js"
16
- export { type ISceneEventListener } from "./SceneSwitcher.js";
17
-
18
16
  import "./CameraUtils.js"
19
17
  import "./AnimationUtils.js"
20
18
 
src/engine/api.ts CHANGED
@@ -1,42 +1,42 @@
1
1
 
2
- export * from "./extensions/index.js";
2
+ export * from "./debug/index.js";
3
3
  export * from "./engine_addressables.js";
4
4
  export * from "./engine_application.js";
5
5
  export * from "./engine_assetdatabase.js";
6
- export * from "./engine_create_objects.js";
7
- export * from "./engine_components_internal.js";
8
6
  export * from "./engine_components.js";
9
7
  export * from "./engine_components_internal.js";
8
+ export * from "./engine_components_internal.js";
9
+ export * from "./engine_constants.js";
10
+ export * from "./engine_context.js";
10
11
  export * from "./engine_context_registry.js";
11
- export * from "./engine_context.js";
12
12
  export * from "./engine_coroutine.js"
13
- export * from "./engine_constants.js";
14
- export * from "./debug/index.js";
13
+ export * from "./engine_create_objects.js";
15
14
  export * from "./engine_element.js";
15
+ export * from "./engine_element_attributes.js";
16
16
  export * from "./engine_element_loading.js";
17
- export * from "./engine_element_attributes.js";
17
+ export * from "./engine_gameobject.js";
18
18
  export { Gizmos } from "./engine_gizmos.js"
19
19
  export * from "./engine_gltf.js";
20
20
  export * from "./engine_hot_reload.js";
21
- export * from "./engine_gameobject.js";
21
+ export * from "./engine_input.js";
22
+ export { InstancingUtil } from "./engine_instancing.js";
23
+ export { hasIndieLicense,hasProLicense } from "./engine_license.js";
24
+ export * from "./engine_lifecycle_api.js";
25
+ export * from "./engine_math.js";
22
26
  export * from "./engine_networking.js";
23
- export * from "./engine_networking_types.js";
24
27
  export { syncField } from "./engine_networking_auto.js";
25
28
  export * from "./engine_networking_files.js";
26
29
  export * from "./engine_networking_instantiate.js";
30
+ export * from "./engine_networking_peer.js";
27
31
  export * from "./engine_networking_streams.js";
32
+ export * from "./engine_networking_types.js";
28
33
  export * from "./engine_networking_utils.js";
29
- export * from "./engine_networking_peer.js";
30
34
  export * from "./engine_patcher.js";
31
- export * from "./engine_playerview.js";
32
35
  export * from "./engine_physics.js";
33
36
  export * from "./engine_physics.types.js";
34
37
  export * from "./engine_physics_rapier.js";
38
+ export * from "./engine_playerview.js";
35
39
  export * from "./engine_scenelighting.js";
36
- export * from "./engine_input.js";
37
- export * from "./engine_lifecycle_api.js";
38
- export * from "./engine_math.js";
39
- export * from "./js-extensions/index.js";
40
40
  export * from "./engine_scenetools.js";
41
41
  export * from "./engine_serialization.js";
42
42
  export { type ISerializable } from "./engine_serialization_core.js";
@@ -44,12 +44,11 @@
44
44
  export * from "./engine_three_utils.js";
45
45
  export * from "./engine_time.js";
46
46
  export * from "./engine_types.js";
47
+ export { registerType,TypeStore } from "./engine_typestore.js";
48
+ export { prefix,validate } from "./engine_util_decorator.js";
49
+ export * from "./engine_utils.js";
47
50
  export * from "./engine_utils_screenshot.js";
48
51
  export * from "./engine_web_api.js";
49
- export * from "./engine_utils.js";
50
-
51
- export { TypeStore, registerType } from "./engine_typestore.js";
52
-
53
- export { InstancingUtil } from "./engine_instancing.js";
54
- export { validate, prefix } from "./engine_util_decorator.js";
55
- export { hasProLicense, hasIndieLicense } from "./engine_license.js";
52
+ export * from "./engine_xr.js";
53
+ export * from "./extensions/index.js";
54
+ export * from "./js-extensions/index.js";
src/engine-components/export/usdz/extensions/behavior/AudioExtension.ts CHANGED
@@ -1,8 +1,9 @@
1
+ import { Object3D } from "three";
2
+
3
+ import { AudioSource } from "../../../../AudioSource.js";
1
4
  import { GameObject } from "../../../../Component.js";
2
5
  import type { IUSDExporterExtension } from "../../Extension.js";
3
6
  import { USDObject, USDWriter, USDZExporterContext } from "../../ThreeUSDZExporter.js";
4
- import { Object3D } from "three";
5
- import { AudioSource } from "../../../../AudioSource.js";
6
7
 
7
8
  export class AudioExtension implements IUSDExporterExtension {
8
9
 
src/engine-components/AudioListener.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { Behaviour, GameObject } from "./Component.js";
2
1
  import { AudioListener as ThreeAudioListener } from "three";
2
+
3
3
  import { AudioSource } from "./AudioSource.js";
4
4
  import { Camera } from "./Camera.js";
5
+ import { Behaviour, GameObject } from "./Component.js";
5
6
 
6
7
 
7
8
  export class AudioListener extends Behaviour {
src/engine-components/AudioSource.ts CHANGED
@@ -1,11 +1,12 @@
1
- import { Behaviour, GameObject } from "./Component.js";
1
+ import { Audio, AudioContext, AudioLoader, PositionalAudio } from "three";
2
2
  import { PositionalAudioHelper } from 'three/examples/jsm/helpers/PositionalAudioHelper.js';
3
+
4
+ import { isDevEnvironment } from "../engine/debug/index.js";
5
+ import { ApplicationEvents } from "../engine/engine_application.js";
6
+ import { serializable } from "../engine/engine_serialization_decorator.js";
7
+ import * as utils from "../engine/engine_utils.js";
3
8
  import { AudioListener } from "./AudioListener.js";
4
- import * as utils from "../engine/engine_utils.js";
5
- import { serializable } from "../engine/engine_serialization_decorator.js";
6
- import { ApplicationEvents } from "../engine/engine_application.js";
7
- import { Audio, AudioContext, AudioLoader, PositionalAudio } from "three";
8
- import { isDevEnvironment } from "../engine/debug/index.js";
9
+ import { Behaviour, GameObject } from "./Component.js";
9
10
 
10
11
 
11
12
  const debug = utils.getParam("debugaudio");
@@ -140,7 +141,7 @@
140
141
  if (!listener && this.context.mainCamera) listener = GameObject.addNewComponent(this.context.mainCamera, AudioListener);
141
142
  if (listener?.listener) {
142
143
  this.sound = new PositionalAudio(listener.listener);
143
- this.gameObject.add(this.sound);
144
+ this.gameObject?.add(this.sound);
144
145
  }
145
146
  else if (debug) console.warn("No audio listener found in scene - can not play audio");
146
147
  }
@@ -157,6 +158,9 @@
157
158
  }
158
159
 
159
160
  onEnable(): void {
161
+ if (this.sound)
162
+ this.gameObject.add(this.sound);
163
+
160
164
  if (!AudioSource.userInteractionRegistered) {
161
165
  AudioSource.registerWaitForAllowAudio(() => {
162
166
  if (this.enabled && !this.destroyed && this.shouldPlay)
@@ -328,6 +332,7 @@
328
332
  if (this.sound && !this.sound.isPlaying) {
329
333
  const muted = this.context.application.muted;
330
334
  if (muted) this.sound.setVolume(0);
335
+ this.gameObject?.add(this.sound);
331
336
 
332
337
  if (this.clip instanceof MediaStream) {
333
338
 
@@ -411,7 +416,7 @@
411
416
  this._hasEnded = true;
412
417
  if (debug)
413
418
  console.log("Audio clip ended", this.clip);
414
- this.sound.dispatchEvent({ type: 'ended', target: this });
419
+ this.dispatchEvent(new CustomEvent("ended", { detail: this }));
415
420
  }
416
421
 
417
422
  // this.gameObject.position.x = Math.sin(time.time) * 2;
src/engine-components/avatar/Avatar_Brain_LookAt.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import * as THREE from "three";
2
+
3
+ import { OwnershipModel } from "../../engine/engine_networking.js";
4
+ import type { IModel } from "../../engine/engine_networking_types.js";
5
+ import { Context } from "../../engine/engine_setup.js";
6
+ import * as utils from "../../engine/engine_three_utils.js";
2
7
  import { TypeStore } from "../../engine/engine_typestore.js";
3
8
  import { Behaviour, GameObject } from "../Component.js";
4
9
  import { AvatarMarker } from "../webxr/WebXRAvatar.js";
5
- import * as utils from "../../engine/engine_three_utils.js";
6
- import { OwnershipModel } from "../../engine/engine_networking.js";
7
- import { Context } from "../../engine/engine_setup.js";
8
- import type { IModel } from "../../engine/engine_networking_types.js";
9
10
 
10
11
  export class Avatar_POI {
11
12
 
src/engine-components/avatar/Avatar_MouthShapes.ts CHANGED
@@ -1,9 +1,10 @@
1
+ import { Object3D } from "three";
2
+
3
+ import { serializable } from "../../engine/engine_serialization_decorator.js";
4
+ import * as utils from "../../engine/engine_utils.js";
1
5
  import { Behaviour, GameObject } from "../Component.js";
2
6
  import { Voip } from "../Voip.js";
3
7
  import { AvatarMarker } from "../webxr/WebXRAvatar.js";
4
- import * as utils from "../../engine/engine_utils.js";
5
- import { Object3D } from "three";
6
- import { serializable } from "../../engine/engine_serialization_decorator.js";
7
8
 
8
9
  const debug = utils.getParam("debugmouth");
9
10
 
src/engine-components/avatar/AvatarBlink_Simple.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { Object3D } from "three";
2
+
3
+ import { serializable } from "../../engine/engine_serialization_decorator.js";
2
4
  import { Behaviour, GameObject } from "../Component.js";
3
- import { XRFlag, XRState } from "../XRFlag.js";
4
- import { serializable } from "../../engine/engine_serialization_decorator.js";
5
+ import { XRFlag, XRState } from "../webxr/XRFlag.js";
5
6
 
6
7
 
7
8
  export class AvatarBlink_Simple extends Behaviour {
src/engine-components/avatar/AvatarEyeLook_Rotation.ts CHANGED
@@ -1,10 +1,11 @@
1
- import { Behaviour, GameObject } from "../Component.js";
2
- import * as utils from "../../engine/engine_three_utils.js"
3
1
  import * as THREE from "three";
4
- import { Avatar_Brain_LookAt } from "./Avatar_Brain_LookAt.js";
5
- import { serializable } from "../../engine/engine_serialization_decorator.js";
6
2
  import { Object3D } from "three";
7
3
 
4
+ import { serializable } from "../../engine/engine_serialization_decorator.js";
5
+ import * as utils from "../../engine/engine_three_utils.js"
6
+ import { Behaviour, GameObject } from "../Component.js";
7
+ import { Avatar_Brain_LookAt } from "./Avatar_Brain_LookAt.js";
8
+
8
9
  export class AvatarEyeLook_Rotation extends Behaviour {
9
10
 
10
11
  @serializable(Object3D)
src/engine-components/AvatarLoader.ts CHANGED
@@ -1,12 +1,13 @@
1
+ import { Box3, Object3D, Vector3 } from "three";
1
2
  import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
2
- import * as utils from "../engine/engine_utils.js"
3
+
4
+ import { InstantiateOptions } from "../engine/engine_gameobject.js";
5
+ import { getLoader } from "../engine/engine_gltf.js";
3
6
  import * as loaders from "../engine/engine_loaders.js"
4
7
  import { Context } from "../engine/engine_setup.js";
8
+ import * as utils from "../engine/engine_utils.js"
9
+ import { download_file } from "../engine/engine_web_api.js";
5
10
  import { GameObject } from "./Component.js";
6
- import { download_file } from "../engine/engine_web_api.js";
7
- import { getLoader } from "../engine/engine_gltf.js";
8
- import { InstantiateOptions } from "../engine/engine_gameobject.js";
9
- import { Box3, Object3D, Vector3 } from "three";
10
11
 
11
12
  const debug = utils.getParam("debugavatar");
12
13
 
src/engine-components/AxesHelper.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { Behaviour } from "./Component.js";
1
+ import { AxesHelper as _AxesHelper } from "three";
2
+
2
3
  import * as params from "../engine/engine_default_parameters.js";
3
4
  import { serializable } from "../engine/engine_serialization_decorator.js";
4
- import { AxesHelper as _AxesHelper } from "three";
5
+ import { Behaviour } from "./Component.js";
5
6
 
6
7
  export class AxesHelper extends Behaviour {
7
8
  @serializable()
src/engine-components/ui/BaseUIComponent.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  // import { Canvas } from './Canvas.js';
2
+ import { AxesHelper, Object3D } from 'three';
2
3
  import * as ThreeMeshUI from 'three-mesh-ui';
4
+
5
+ import { showGizmos } from '../../engine/engine_default_parameters.js';
6
+ import { getParam } from '../../engine/engine_utils.js';
3
7
  import { Behaviour, GameObject } from "../Component.js";
4
8
  import { EventSystem } from "./EventSystem.js";
5
- import { showGizmos } from '../../engine/engine_default_parameters.js';
6
- import { AxesHelper, Object3D } from 'three';
7
9
  import type { ICanvas } from './Interfaces.js';
8
- import { getParam } from '../../engine/engine_utils.js';
9
10
  export const includesDir = "./include";
10
11
 
11
12
  const debug = getParam("debugshadowcomponents");
@@ -24,22 +25,38 @@
24
25
 
25
26
  export const $shadowDomOwner = Symbol("shadowDomOwner");
26
27
 
28
+ /** Derive from this class if you want to implement your own UI components
29
+ * It provides utility methods and simplifies managing the underlying three-mesh-ui hierarchy
30
+ */
27
31
  export class BaseUIComponent extends Behaviour {
28
32
 
33
+ /** Is this object on the root of the UI hierarchy ? */
29
34
  isRoot() { return this.Root?.gameObject === this.gameObject; }
30
35
 
36
+ /** Access the parent canvas component */
31
37
  get canvas() {
32
38
  const cv = this.Root as any as ICanvas;
33
39
  if (cv?.isCanvas) return cv;
34
40
  return null;
35
41
  }
42
+ /** @deprecated use `canvas` */
43
+ protected get Canvas() {
44
+ return this.canvas;
45
+ }
36
46
 
47
+ /** Mark the UI dirty which will trigger an THREE-Mesh-UI update */
37
48
  markDirty() {
38
49
  EventSystem.markUIDirty(this.context);
39
50
  }
40
51
 
41
- shadowComponent: ThreeMeshUI.Block | null = null;
52
+ /** the underlying three-mesh-ui */
53
+ get shadowComponent() { return this._shadowComponent }
54
+ private set shadowComponent(val: Object3D | null) {
55
+ this._shadowComponent = val;
56
+ }
42
57
 
58
+ private _shadowComponent: Object3D | null = null;
59
+
43
60
  private _controlsChildLayout = true;
44
61
  get controlsChildLayout(): boolean { return this._controlsChildLayout; }
45
62
  set controlsChildLayout(val: boolean) {
@@ -58,11 +75,6 @@
58
75
  return this._root;
59
76
  }
60
77
 
61
- // TODO: rename to canvas
62
- protected get Canvas() {
63
- return this.canvas;
64
- }
65
-
66
78
  // private _intermediate?: Object3D;
67
79
  protected _parentComponent?: BaseUIComponent | null = undefined;
68
80
 
@@ -77,7 +89,10 @@
77
89
  super.onEnable();
78
90
  }
79
91
 
80
- //@ts-ignore
92
+ /** Add a three-mesh-ui object to the UI hierarchy
93
+ * @param container the three-mesh-ui object to add
94
+ * @param parent the parent component to add the object to
95
+ */
81
96
  protected addShadowComponent(container: any, parent?: BaseUIComponent) {
82
97
 
83
98
  this.removeShadowComponent();
@@ -134,21 +149,7 @@
134
149
  if(debug) console.log(this.shadowComponent)
135
150
  }
136
151
 
137
-
138
- set(_state: object) {
139
- // if (!this.shadowComponent) return;
140
- // this.traverseOwnedShadowComponents(this.shadowComponent, this, o => {
141
- // for (const ch of o.children) {
142
- // console.log(this, ch);
143
- // if (ch.isUI && typeof ch.set === "function") {
144
- // // ch.set(state);
145
- // // ch.update(true, true, true);
146
- // }
147
- // }
148
- // })
149
- }
150
-
151
- protected setShadowComponentOwner(current: Object3D | null | undefined) {
152
+ protected setShadowComponentOwner(current: ThreeMeshUI.MeshUIBaseElement | Object3D | null | undefined) {
152
153
  if (!current) return;
153
154
  // TODO: only traverse our own hierarchy, we can stop if we find another owner
154
155
  if (current[$shadowDomOwner] === undefined || current[$shadowDomOwner] === this) {
@@ -171,6 +172,7 @@
171
172
  }
172
173
  }
173
174
 
175
+ /** Remove the underlying UI object from the hierarchy */
174
176
  protected removeShadowComponent() {
175
177
  if (this.shadowComponent) {
176
178
  this.shadowComponent.removeFromParent();
src/engine-components/BasicIKConstraint.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { Behaviour, GameObject } from "./Component.js";
2
- import * as utils from "./../engine/engine_three_utils.js";
3
1
  import { Vector3 } from "three";
4
2
 
3
+ import * as utils from "./../engine/engine_three_utils.js";
4
+ import { Behaviour, GameObject } from "./Component.js";
5
+
5
6
  export class BasicIKConstraint extends Behaviour {
6
7
 
7
8
  private from!: GameObject;
src/engine-components/export/usdz/extensions/behavior/Behaviour.ts CHANGED
@@ -1,8 +1,8 @@
1
+ import { getParam } from "../../../../../engine/engine_utils.js";
1
2
  import { GameObject } from "../../../../Component.js";
2
3
  import type { IUSDExporterExtension } from "../../Extension.js";
3
4
  import { USDObject, USDWriter, USDZExporterContext } from "../../ThreeUSDZExporter.js";
4
5
  import { BehaviorModel } from "./BehavioursBuilder.js";
5
- import { getParam } from "../../../../../engine/engine_utils.js";
6
6
 
7
7
  const debug = getParam("debugusdz");
8
8
 
src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts CHANGED
@@ -1,21 +1,20 @@
1
+ import { Group,Material, Mesh, Object3D, Quaternion, Vector3 } from "three";
2
+
3
+ import { isDevEnvironment, showBalloonWarning } from "../../../../../engine/debug/index.js";
4
+ import { serializable } from "../../../../../engine/engine_serialization_decorator.js";
5
+ import { getWorldPosition, getWorldQuaternion, getWorldScale, setWorldPosition, setWorldQuaternion, setWorldScale } from "../../../../../engine/engine_three_utils.js";
6
+ import type { State } from "../../../../../engine/extensions/NEEDLE_animator_controller_model.js";
7
+ import { NEEDLE_progressive } from "../../../../../engine/extensions/NEEDLE_progressive.js";
8
+ import { Animator } from "../../../../Animator.js";
9
+ import { AudioSource } from "../../../../AudioSource.js";
1
10
  import { Behaviour, GameObject } from "../../../../Component.js";
2
- import { Animator } from "../../../../Animator.js";
3
11
  import { Renderer } from "../../../../Renderer.js";
4
- import { serializable } from "../../../../../engine/engine_serialization_decorator.js";
5
12
  import type { IPointerClickHandler, PointerEventData } from "../../../../ui/PointerEvents.js";
13
+ import { ObjectRaycaster,Raycaster } from "../../../../ui/Raycaster.js";
14
+ import { USDDocument, USDObject, USDZExporterContext } from "../../ThreeUSDZExporter.js";
6
15
  import { AnimationExtension, RegisteredAnimationInfo, type UsdzAnimation } from "../Animation.js";
7
- import { getWorldPosition, getWorldQuaternion, getWorldScale, setWorldPosition, setWorldQuaternion, setWorldScale } from "../../../../../engine/engine_three_utils.js";
8
-
9
- import { Object3D, Material, Vector3, Quaternion, Mesh, Group } from "three";
10
- import { USDDocument, USDObject, USDZExporterContext } from "../../ThreeUSDZExporter.js";
11
-
12
16
  import type { BehaviorExtension, UsdzBehaviour } from "./Behaviour.js";
13
- import { ActionBuilder, ActionModel, AuralMode, BehaviorModel, type IBehaviorElement, MotionType, PlayAction, Space, TriggerBuilder, GroupActionModel, MultiplePerformOperation } from "./BehavioursBuilder.js";
14
- import { AudioSource } from "../../../../AudioSource.js";
15
- import { NEEDLE_progressive } from "../../../../../engine/extensions/NEEDLE_progressive.js";
16
- import { isDevEnvironment, showBalloonWarning } from "../../../../../engine/debug/index.js";
17
- import { Raycaster, ObjectRaycaster } from "../../../../ui/Raycaster.js";
18
- import type { State } from "../../../../../engine/extensions/NEEDLE_animator_controller_model.js";
17
+ import { ActionBuilder, ActionModel, AuralMode, BehaviorModel, GroupActionModel, type IBehaviorElement, MotionType, MultiplePerformOperation,PlayAction, Space, TriggerBuilder } from "./BehavioursBuilder.js";
19
18
 
20
19
  function ensureRaycaster(obj: GameObject) {
21
20
  if (!obj) return;
src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Object3D } from "three";
2
- import { USDDocument, USDObject, USDWriter, makeNameSafeForUSD } from "../../ThreeUSDZExporter.js";
3
2
 
3
+ import { getParam } from "../../../../../engine/engine_utils.js";
4
+ import { makeNameSafeForUSD,USDDocument, USDObject, USDWriter } from "../../ThreeUSDZExporter.js";
4
5
  import { BehaviorExtension } from "./Behaviour.js";
5
- import { getParam } from "../../../../../engine/engine_utils.js";
6
6
 
7
7
  const debug = getParam("debugusdz");
8
8
 
src/engine-components/postprocessing/Effects/Bloom.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { BlendFunction, BloomEffect, SelectiveBloomEffect } from "postprocessing";
2
+
2
3
  import { serializable } from "../../../engine/engine_serialization.js";
3
4
  import { PostProcessingEffect } from "../PostProcessingEffect.js";
4
5
  import { VolumeParameter } from "../VolumeParameter.js";
src/engine-components/BoxHelperComponent.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { Behaviour } from "./Component.js";
2
- import { getParam } from "../engine/engine_utils.js";
1
+ import { Box3, Color, type ColorRepresentation, LineSegments, Object3D, Vector3 } from "three";
2
+
3
3
  import { CreateWireCube, Gizmos } from "../engine/engine_gizmos.js";
4
4
  import { getWorldPosition, getWorldScale } from "../engine/engine_three_utils.js";
5
- import { Box3, Color, type ColorRepresentation, LineSegments, Object3D, Vector3 } from "three";
5
+ import { getParam } from "../engine/engine_utils.js";
6
+ import { Behaviour } from "./Component.js";
6
7
 
7
8
  const gizmos = getParam("gizmos");
8
9
  const debug = getParam("debugboxhelper");
src/engine-components/ui/Button.ts CHANGED
@@ -1,15 +1,15 @@
1
+ import { showBalloonMessage } from "../../engine/debug/index.js";
2
+ import { Gizmos } from "../../engine/engine_gizmos.js";
3
+ import { PointerType } from "../../engine/engine_input.js";
4
+ import { serializable } from "../../engine/engine_serialization_decorator.js";
5
+ import { getParam } from "../../engine/engine_utils.js";
6
+ import { Animator } from "../Animator.js";
1
7
  import { Behaviour, GameObject } from "../Component.js";
2
8
  import { EventList } from "../EventList.js";
9
+ import { RGBAColor } from "../js-extensions/RGBAColor.js";
10
+ import { Image } from "./Image.js";
3
11
  import type { IPointerClickHandler, IPointerEnterHandler, IPointerEventHandler, IPointerExitHandler, PointerEventData } from "./PointerEvents.js";
4
- import { Image } from "./Image.js";
5
- import { RGBAColor } from "../js-extensions/RGBAColor.js";
6
- import { serializable } from "../../engine/engine_serialization_decorator.js";
7
- import { Animator } from "../Animator.js";
8
- import { getParam } from "../../engine/engine_utils.js";
9
- import { showBalloonMessage } from "../../engine/debug/index.js";
10
12
  import { GraphicRaycaster, ObjectRaycaster, Raycaster } from "./Raycaster.js";
11
- import { PointerType } from "../../engine/engine_input.js";
12
- import { Gizmos } from "../../engine/engine_gizmos.js";
13
13
 
14
14
  const debug = getParam("debugbutton");
15
15
 
@@ -120,10 +120,10 @@
120
120
  }
121
121
 
122
122
  onPointerClick(args: PointerEventData) {
123
- if (!this.interactable || args.pointerId !== 0) return;
123
+ if (!this.interactable) return;
124
124
 
125
+ if (args.button !== 0 && args.event.pointerType === PointerType.Mouse) return;
125
126
  // Button clicks should only run with left mouse button while using mouse
126
- if(args.pointerId !== 0 && this.context.input.getIsMouse(args.pointerId)) return;
127
127
  if (debug) {
128
128
  console.warn("Button Click", this.onClick);
129
129
  showBalloonMessage("CLICKED button " + this.name + " at " + this.context.time.frameCount);
src/engine-components/Camera.ts CHANGED
@@ -1,17 +1,17 @@
1
+ import { EquirectangularReflectionMapping, OrthographicCamera, PerspectiveCamera, Ray, SRGBColorSpace, Vector3 } from "three";
2
+ import { Texture } from "three";
3
+
4
+ import { isDevEnvironment, showBalloonMessage, showBalloonWarning } from "../engine/debug/index.js";
5
+ import { Gizmos } from "../engine/engine_gizmos.js";
6
+ import { serializable } from "../engine/engine_serialization_decorator.js";
7
+ import { Context } from "../engine/engine_setup.js";
8
+ import { RenderTexture } from "../engine/engine_texture.js";
9
+ import { getWorldPosition } from "../engine/engine_three_utils.js";
10
+ import type { ICamera } from "../engine/engine_types.js"
11
+ import { getParam } from "../engine/engine_utils.js";
1
12
  import { Behaviour, GameObject } from "./Component.js";
2
- import { getParam } from "../engine/engine_utils.js";
3
- import { serializable } from "../engine/engine_serialization_decorator.js";
4
13
  import { RGBAColor } from "./js-extensions/RGBAColor.js";
5
- import { Context, XRSessionMode } from "../engine/engine_setup.js";
6
- import type { ICamera } from "../engine/engine_types.js"
7
- import { isDevEnvironment, showBalloonMessage, showBalloonWarning } from "../engine/debug/index.js";
8
- import { getWorldPosition } from "../engine/engine_three_utils.js";
9
- import { Gizmos } from "../engine/engine_gizmos.js";
10
-
11
- import { EquirectangularReflectionMapping, OrthographicCamera, PerspectiveCamera, Ray, SRGBColorSpace, Vector3 } from "three";
12
14
  import { OrbitControls } from "./OrbitControls.js";
13
- import { RenderTexture } from "../engine/engine_texture.js";
14
- import { Texture } from "three";
15
15
 
16
16
  export enum ClearFlags {
17
17
  Skybox = 1,
@@ -350,7 +350,6 @@
350
350
  if (this._backgroundBlurriness !== undefined)
351
351
  this.context.scene.backgroundBlurriness = this._backgroundBlurriness;
352
352
  if (this._backgroundIntensity !== undefined)
353
- //@ts-ignore
354
353
  this.context.scene.backgroundIntensity = this._backgroundIntensity;
355
354
 
356
355
  break;
@@ -392,7 +391,7 @@
392
391
  if (debug)
393
392
  showBalloonMessage("Environment blend mode: " + environmentBlendMode + " on " + navigator.userAgent);
394
393
  let transparent = environmentBlendMode === 'additive' || environmentBlendMode === 'alpha-blend';
395
- if (context.xrSessionMode === XRSessionMode.ImmersiveAR) {
394
+ if (context.isInAR) {
396
395
  if (environmentBlendMode === "opaque") {
397
396
  // workaround for Quest 2 returning opaque when it should be alpha-blend
398
397
  // check user agent if this is the Quest browser and return true if so
src/engine-components/CameraUtils.ts CHANGED
@@ -1,14 +1,15 @@
1
- import { OrbitControls } from "./OrbitControls.js";
1
+ import { Object3D } from "three";
2
+
3
+ import { getCameraController } from "../engine/engine_camera.js";
2
4
  import { addNewComponent, getOrAddComponent } from "../engine/engine_components.js";
3
- import { Object3D } from "three";
4
- import type { ICamera, IContext } from "../engine/engine_types.js";
5
- import { RGBAColor } from "./js-extensions/RGBAColor.js";
5
+ import { Context } from "../engine/engine_context.js";
6
6
  import { ContextEvent, ContextRegistry } from "../engine/engine_context_registry.js";
7
- import { getCameraController } from "../engine/engine_camera.js";
8
- import { Camera, ClearFlags } from "./Camera.js";
9
7
  import { NeedleEngineHTMLElement } from "../engine/engine_element.js";
8
+ import type { ICamera, IContext } from "../engine/engine_types.js";
10
9
  import { getParam } from "../engine/engine_utils.js";
11
- import { Context } from "../engine/engine_context.js";
10
+ import { Camera, ClearFlags } from "./Camera.js";
11
+ import { RGBAColor } from "./js-extensions/RGBAColor.js";
12
+ import { OrbitControls } from "./OrbitControls.js";
12
13
 
13
14
  const debug = getParam("debugmissingcamera");
14
15
 
src/engine-components/ui/Canvas.ts CHANGED
@@ -1,17 +1,19 @@
1
- import { updateRenderSettings as updateRenderSettingsRecursive } from "./Utils.js";
1
+ import { Matrix4, Object3D } from "three";
2
+ import * as ThreeMeshUI from 'three-mesh-ui'
3
+
4
+ import { Mathf } from "../../engine/engine_math.js";
2
5
  import { serializable } from "../../engine/engine_serialization_decorator.js";
3
6
  import { FrameEvent } from "../../engine/engine_setup.js";
7
+ import { getParam } from "../../engine/engine_utils.js";
8
+ import { NeedleXREventArgs } from "../../engine/xr/index.js";
9
+ import { Camera } from "../Camera.js";
10
+ import { GameObject } from "../Component.js";
4
11
  import { BaseUIComponent, UIRootComponent } from "./BaseUIComponent.js";
5
- import { GameObject } from "../Component.js";
6
- import { Matrix4, Object3D } from "three";
7
- import { RectTransform } from "./RectTransform.js";
12
+ import { EventSystem } from "./EventSystem.js";
8
13
  import type { ICanvas, ICanvasEventReceiver, ILayoutGroup, IRectTransform } from "./Interfaces.js";
9
- import { Camera } from "../Camera.js";
10
- import { EventSystem } from "./EventSystem.js";
11
- import * as ThreeMeshUI from 'three-mesh-ui'
12
- import { getParam } from "../../engine/engine_utils.js";
13
14
  import { LayoutGroup } from "./Layout.js";
14
- import { Mathf } from "../../engine/engine_math.js";
15
+ import { RectTransform } from "./RectTransform.js";
16
+ import { updateRenderSettings as updateRenderSettingsRecursive } from "./Utils.js";
15
17
 
16
18
  export enum RenderMode {
17
19
  ScreenSpaceOverlay = 0,
@@ -200,19 +202,30 @@
200
202
  }
201
203
  }
202
204
 
205
+ onEnterXR(args: NeedleXREventArgs): void {
206
+ if (this.screenspace) {
207
+ if (args.xr.isVR || args.xr.isPassThrough) {
208
+ this.gameObject.visible = false;
209
+ }
210
+ }
211
+ }
212
+ onLeaveXR(args: NeedleXREventArgs): void {
213
+ if (this.screenspace) {
214
+ if (args.xr.isVR || args.xr.isPassThrough) {
215
+ this.gameObject.visible = true;
216
+ }
217
+ }
218
+ }
219
+
203
220
  onBeforeRenderRoutine = () => {
204
- if (this.context.isInVR) {
205
- this.onUpdateRenderMode();
206
- this.handleLayoutUpdates();
207
- // TODO TMUI @swingingtom - For VR this is so we don't have text clipping
208
- this.shadowComponent?.updateMatrixWorld(true);
209
- this.shadowComponent?.updateWorldMatrix(true, true);
210
- this.invokeBeforeRenderEvents();
211
- EventSystem.ensureUpdateMeshUI(ThreeMeshUI, this.context, true);
221
+ this.previousParent = this.gameObject.parent;
222
+ if ((this.context.xr?.isVR || this.context.xr?.isPassThrough) && this.screenspace) {
223
+ // see https://linear.app/needle/issue/NE-4114
224
+ this.gameObject.visible = false;
225
+ this.gameObject.removeFromParent();
212
226
  return;
213
227
  }
214
228
 
215
- this.previousParent = this.gameObject.parent;
216
229
  // console.log(this.previousParent?.name + "/" + this.gameObject.name);
217
230
 
218
231
  if (this.renderOnTop || this.screenspace) {
@@ -231,7 +244,12 @@
231
244
  }
232
245
 
233
246
  onAfterRenderRoutine = () => {
234
- if(this.context.isInVR) return;
247
+ if ((this.context.xr?.isVR || this.context.xr?.isPassThrough) && this.screenspace) {
248
+ this.previousParent?.add(this.gameObject);
249
+ // this is currently causing an error during XR (https://linear.app/needle/issue/NE-4114)
250
+ // this.gameObject.visible = true;
251
+ return;
252
+ }
235
253
  if ((this.screenspace || this.renderOnTop) && this.previousParent && this.context.mainCamera) {
236
254
  if (this.screenspace) {
237
255
  const camObj = this.context.mainCamera;
@@ -276,7 +294,7 @@
276
294
  for (const ch of this._rectTransforms) {
277
295
  if (matrixWorldChanged) ch.markDirty();
278
296
  let layout = this._layoutGroups.get(ch.gameObject);
279
- if(ch.isDirty && !layout){
297
+ if (ch.isDirty && !layout) {
280
298
  layout = ch.gameObject.getComponentInParent(LayoutGroup) as LayoutGroup;
281
299
  }
282
300
  if (ch.isDirty || layout?.isDirty) {
src/engine-components/ui/CanvasGroup.ts CHANGED
@@ -1,9 +1,9 @@
1
- import { Graphic } from "./Graphic.js";
1
+ import { serializable } from "../../engine/engine_serialization_decorator.js";
2
2
  import { FrameEvent } from "../../engine/engine_setup.js";
3
3
  import { Behaviour, GameObject } from "../Component.js";
4
+ import { BaseUIComponent } from "./BaseUIComponent.js";
5
+ import { Graphic } from "./Graphic.js";
4
6
  import { type ICanvasGroup, type IHasAlphaFactor } from "./Interfaces.js";
5
- import { serializable } from "../../engine/engine_serialization_decorator.js";
6
- import { BaseUIComponent } from "./BaseUIComponent.js";
7
7
 
8
8
 
9
9
  export class CanvasGroup extends Behaviour implements ICanvasGroup {
src/engine-components/CharacterController.ts CHANGED
@@ -1,14 +1,15 @@
1
1
  import { Quaternion, Ray, Vector2, Vector3 } from "three";
2
+
2
3
  import { Mathf } from "../engine/engine_math.js";
4
+ import { RaycastOptions } from "../engine/engine_physics.js";
3
5
  import { serializable } from "../engine/engine_serialization.js";
6
+ import { getWorldPosition } from "../engine/engine_three_utils.js";
4
7
  import { Collision } from "../engine/engine_types.js";
8
+ import { getParam } from "../engine/engine_utils.js";
9
+ import { Animator } from "./Animator.js"
5
10
  import { CapsuleCollider } from "./Collider.js";
6
11
  import { Behaviour, GameObject } from "./Component.js";
7
12
  import { Rigidbody } from "./RigidBody.js";
8
- import { Animator } from "./Animator.js"
9
- import { RaycastOptions } from "../engine/engine_physics.js";
10
- import { getWorldPosition } from "../engine/engine_three_utils.js";
11
- import { getParam } from "../engine/engine_utils.js";
12
13
 
13
14
  const debug = getParam("debugcharactercontroller");
14
15
 
src/engine-components/postprocessing/Effects/ChromaticAberration.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ChromaticAberrationEffect } from "postprocessing";
2
2
  import { Vector2 } from "three";
3
+
3
4
  import { serializable } from "../../../engine/engine_serialization.js";
4
5
  import { type EffectProviderResult, PostProcessingEffect } from "../PostProcessingEffect.js";
5
6
  import { VolumeParameter } from "../VolumeParameter.js";
src/engine-components/Collider.ts CHANGED
@@ -1,13 +1,14 @@
1
- import { Behaviour } from "./Component.js";
2
- import { Rigidbody } from "./RigidBody.js";
1
+ import { Group, Mesh, Vector3 } from "three"
2
+
3
+ import type { PhysicsMaterial } from "../engine/engine_physics.types.js";
3
4
  import { serializable } from "../engine/engine_serialization_decorator.js";
4
- import { Group, Mesh, Vector3 } from "three"
5
+ import { getWorldScale } from "../engine/engine_three_utils.js";
5
6
  // import { IColliderProvider, registerColliderProvider } from "../engine/engine_physics.js";
6
7
  import type { IBoxCollider, ICollider, ISphereCollider } from "../engine/engine_types.js";
7
- import { getWorldScale } from "../engine/engine_three_utils.js";
8
- import type { PhysicsMaterial } from "../engine/engine_physics.types.js";
9
8
  import { validate } from "../engine/engine_util_decorator.js";
10
9
  import { unwatchWrite, watchWrite } from "../engine/engine_utils.js";
10
+ import { Behaviour } from "./Component.js";
11
+ import { Rigidbody } from "./RigidBody.js";
11
12
 
12
13
 
13
14
  export class Collider extends Behaviour implements ICollider {
src/engine-components/postprocessing/Effects/ColorAdjustments.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { BrightnessContrastEffect, HueSaturationEffect } from "postprocessing";
2
+ import { LinearToneMapping, NoToneMapping } from "three";
3
+
2
4
  import { serializable } from "../../../engine/engine_serialization.js";
3
5
  import { type EffectProviderResult, PostProcessingEffect } from "../PostProcessingEffect.js";
4
6
  import { VolumeParameter } from "../VolumeParameter.js";
5
7
  import { registerCustomEffectType } from "../VolumeProfile.js";
6
- import { LinearToneMapping, NoToneMapping } from "three";
7
8
 
8
9
 
9
10
  export class ColorAdjustments extends PostProcessingEffect {
src/engine-components/Component.ts CHANGED
@@ -1,15 +1,17 @@
1
- import { Mathf } from "../engine/engine_math.js";
2
- import * as threeutils from "../engine/engine_three_utils.js";
1
+ import { Euler, Object3D, Quaternion, Scene, Vector3 } from "three";
2
+
3
+ import { isDevEnvironment } from "../engine/debug/index.js";
4
+ import { addNewComponent, destroyComponentInstance, findObjectOfType, findObjectsOfType, getComponent, getComponentInChildren, getComponentInParent, getComponents, getComponentsInChildren, getComponentsInParent, getOrAddComponent, moveComponentInstance, removeComponent } from "../engine/engine_components.js";
3
5
  import { activeInHierarchyFieldName } from "../engine/engine_constants.js";
4
- import { Context, FrameEvent } from "../engine/engine_setup.js";
6
+ import { destroy, findByGuid, foreachComponent, HideFlags, IInstantiateOptions, instantiate, isActiveInHierarchy, isActiveSelf, isDestroyed, isUsingInstancing, markAsInstancedRendered, setActive } from "../engine/engine_gameobject.js";
5
7
  import * as main from "../engine/engine_mainloop_utils.js";
6
8
  import { syncDestroy, syncInstantiate } from "../engine/engine_networking_instantiate.js";
7
- import type { ConstructorConcrete, SourceIdentifier, IComponent, IGameObject, Constructor, GuidsMap, Collision, ICollider } from "../engine/engine_types.js";
8
- import { addNewComponent, destroyComponentInstance, findObjectOfType, findObjectsOfType, getComponent, getComponentInChildren, getComponentInParent, getComponents, getComponentsInChildren, getComponentsInParent, getOrAddComponent, moveComponentInstance, removeComponent } from "../engine/engine_components.js";
9
- import { findByGuid, destroy, InstantiateOptions, instantiate, HideFlags, foreachComponent, markAsInstancedRendered, isActiveInHierarchy, isActiveSelf, isUsingInstancing, setActive, isDestroyed, IInstantiateOptions } from "../engine/engine_gameobject.js";
9
+ import { Context, FrameEvent } from "../engine/engine_setup.js";
10
+ import * as threeutils from "../engine/engine_three_utils.js";
11
+ import type { Collision, Constructor, ConstructorConcrete, GuidsMap, ICollider, IComponent, IGameObject, SourceIdentifier } from "../engine/engine_types.js";
12
+ import { INeedleXRSessionEventReceiver, NeedleXRControllerEventArgs, NeedleXREventArgs } from "../engine/engine_xr.js";
13
+ import { IPointerEventHandler, PointerEventData } from "./ui/PointerEvents.js";
10
14
 
11
- import { Euler, Object3D, Quaternion, Scene, Vector3 } from "three";
12
- import { showBalloonWarning, isDevEnvironment } from "../engine/debug/index.js";
13
15
 
14
16
  // export interface ISerializationCallbackReceiver {
15
17
  // onBeforeSerialize?(): object | void;
@@ -123,7 +125,7 @@
123
125
  main.addScriptToArrays(comp, context!);
124
126
  if (comp.__internalDidAwakeAndStart) return;
125
127
  if (context!.new_script_start.includes(comp) === false) {
126
- context!.new_script_start.push(comp as Behaviour);
128
+ context!.new_script_start.push(comp as Component);
127
129
  }
128
130
  }, true);
129
131
  }
@@ -254,7 +256,7 @@
254
256
  return getComponentsInParent(go, typeName, arr);
255
257
  }
256
258
 
257
- public static getAllComponents(go: IGameObject | Object3D): Behaviour[] {
259
+ public static getAllComponents(go: IGameObject | Object3D): Component[] {
258
260
  const componentsList = go.userData?.components;
259
261
  const newList = [...componentsList];
260
262
  return newList;
@@ -294,7 +296,7 @@
294
296
  abstract set worldQuaternion(val: Quaternion);
295
297
  abstract get worldQuaternion(): Quaternion;
296
298
  abstract set worldRotation(val: Vector3);
297
- abstract get worldRotation(): Vector3;
299
+ abstract get worldRotation(): Vector3;
298
300
  abstract set worldScale(val: Vector3);
299
301
  abstract get worldScale(): Vector3;
300
302
 
@@ -305,17 +307,28 @@
305
307
 
306
308
 
307
309
 
308
- export class Component implements IComponent, EventTarget {
310
+ /** Needle Engine component base class. Derive from this component to implement your own using the provided lifecycle methods. Components can be added to threejs objects using `GameObject.addComponent`.
311
+ *
312
+ * The most common lifecycle methods are `awake`, `start`, `onEanble`, `onDisable` `update` and `onDestroy`.
313
+ * XR specific callbacks include `onEnterXR`, `onLeaveXR`, `onUpdateXR`, `onControllerAdded` and `onControllerRemoved`.
314
+ * To receive pointer events implement `onPointerDown`, `onPointerUp`, `onPointerEnter`, `onPointerExit` and `onPointerMove`.
315
+ */
316
+ export abstract class Component implements IComponent, EventTarget,
317
+ Partial<INeedleXRSessionEventReceiver>,
318
+ Partial<IPointerEventHandler>
319
+ {
309
320
 
310
321
  get isComponent(): boolean { return true; }
311
322
 
312
323
  private __context: Context | undefined;
324
+ /** Use the context to get access to many Needle Engine features and use physics, timing, access the camera or scene */
313
325
  get context(): Context {
314
326
  return this.__context ?? Context.Current;
315
327
  }
316
328
  set context(context: Context) {
317
329
  this.__context = context;
318
330
  }
331
+ /** shorthand for `this.context.scene` */
319
332
  get scene(): Scene { return this.context.scene; }
320
333
 
321
334
  get layer(): number {
@@ -355,7 +368,7 @@
355
368
  return this.gameObject?.userData.hideFlags;
356
369
  }
357
370
 
358
-
371
+ /** @returns true if the object is enabled and active in the hierarchy */
359
372
  get activeAndEnabled(): boolean {
360
373
  if (this.destroyed) return false;
361
374
  if (this.__isEnabled === false) return false;
@@ -385,19 +398,27 @@
385
398
  this.gameObject[activeInHierarchyFieldName] = val;
386
399
  }
387
400
 
401
+ /** the object this component is attached to. Note that this is a threejs Object3D with some additional features */
388
402
  gameObject!: GameObject;
403
+ /** the unique identifier for this component */
389
404
  guid: string = "invalid";
405
+ /** holds the source identifier this object was created with/from (e.g. if it was part of a glTF file the sourceId holds the url to the glTF) */
390
406
  sourceId?: SourceIdentifier;
391
407
  // transform: Object3D = nullObject;
392
408
 
393
409
  /** called on a component with a map of old to new guids (e.g. when instantiate generated new guids and e.g. timeline track bindings needs to remape them) */
394
410
  resolveGuids?(guidsMap: GuidsMap): void;
395
411
 
396
- /** called once when the component becomes active for the first time */
412
+ /** called once when the component becomes active for the first time (once per component)
413
+ * This is the first callback to be called */
397
414
  awake() { }
398
- /** called every time when the component gets enabled (this is invoked after awake and before start) */
415
+ /** called every time when the component gets enabled (this is invoked after awake and before start)
416
+ * or when it becomes active in the hierarchy (e.g. if a parent object or this.gameObject gets set to visible)
417
+ */
399
418
  onEnable() { }
419
+ /** called every time the component gets disabled or if a parent object (or this.gameObject) gets set to invisible */
400
420
  onDisable() { }
421
+ /** Called when the component gets destroyed */
401
422
  onDestroy() {
402
423
  this.__destroyed = true;
403
424
  }
@@ -409,11 +430,17 @@
409
430
  /** Called for all scripts when the context gets paused or unpaused */
410
431
  onPausedChanged?(isPaused: boolean, wasPaused: boolean): void;
411
432
 
433
+ /** called at the beginning of a frame (once per component) */
412
434
  start?(): void;
435
+ /** first callback in a frame (called every frame when implemented) */
413
436
  earlyUpdate?(): void;
437
+ /** regular callback in a frame (called every frame when implemented) */
414
438
  update?(): void;
439
+ /** late callback in a frame (called every frame when implemented) */
415
440
  lateUpdate?(): void;
441
+ /** called before the scene gets rendered in the main update loop */
416
442
  onBeforeRender?(frame: XRFrame | null): void;
443
+ /** called after the scene was rendered */
417
444
  onAfterRender?(): void;
418
445
 
419
446
  onCollisionEnter?(col: Collision);
@@ -424,18 +451,79 @@
424
451
  onTriggerStay?(col: ICollider);
425
452
  onTriggerExit?(col: ICollider);
426
453
 
454
+
455
+ /** Optional callback, you can implement this to only get callbacks for VR or AR sessions if necessary.
456
+ * @returns true if the mode is supported (if false the mode is not supported by this ciomponent and it will not receive XR callbacks for this mode)
457
+ */
458
+ supportsXR?(mode: XRSessionMode): boolean;
459
+ /** Called before the XR session is requested. Use this callback if you want to modify the session init features */
460
+ onBeforeXR?(mode: XRSessionMode, args: XRSessionInit): void;
461
+ /** Callback when this component joins a xr session (or becomes active in a running XR session) */
462
+ onEnterXR?(args: NeedleXREventArgs): void;
463
+ /** Callback when a xr session updates (while it is still active in XR session) */
464
+ onUpdateXR?(args: NeedleXREventArgs): void;
465
+ /** Callback when this component exists a xr session (or when it becomes inactive in a running XR session) */
466
+ onLeaveXR?(args: NeedleXREventArgs): void;
467
+ /** Callback when a controller is connected/added while in a XR session
468
+ * OR when the component joins a running XR session that has already connected controllers
469
+ * OR when the component becomes active during a running XR session that has already connected controllers */
470
+ onXRControllerAdded?(args: NeedleXRControllerEventArgs): void;
471
+ /** callback when a controller is removed while in a XR session
472
+ * OR when the component becomes inactive during a running XR session
473
+ */
474
+ onXRControllerRemoved?(args: NeedleXRControllerEventArgs): void;
475
+
476
+
477
+ /* IPointerEventReceiver */
478
+ /* @inheritdoc */
479
+ onPointerEnter?(args: PointerEventData);
480
+ onPointerMove?(args: PointerEventData);
481
+ onPointerExit?(args: PointerEventData);
482
+ onPointerDown?(args: PointerEventData);
483
+ onPointerUp?(args: PointerEventData);
484
+ onPointerClick?(args: PointerEventData);
485
+
486
+
487
+ /** starts a coroutine (javascript generator function)
488
+ * `yield` will wait for the next frame:
489
+ * - Use `yield WaitForSeconds(1)` to wait for 1 second.
490
+ * - Use `yield WaitForFrames(10)` to wait for 10 frames.
491
+ * - Use `yield new Promise(...)` to wait for a promise to resolve.
492
+ * @param routine generator function to start
493
+ * @param evt event to register the coroutine for (default: FrameEvent.Update). Note that all coroutine FrameEvent callbacks are invoked after the matching regular component callbacks. For example `FrameEvent.Update` will be called after regular component `update()` methods)
494
+ * @returns the generator function (use it to stop the coroutine with `stopCoroutine`)
495
+ * @example
496
+ * ```ts
497
+ * onEnable() { this.startCoroutine(this.myCoroutine()); }
498
+ * private *myCoroutine() {
499
+ * while(this.activeAndEnabled) {
500
+ * console.log("Hello World", this.context.time.frame);
501
+ * // wait for 5 frames
502
+ * for(let i = 0; i < 5; i++) yield;
503
+ * }
504
+ * }
505
+ * ```
506
+ */
427
507
  startCoroutine(routine: Generator, evt: FrameEvent = FrameEvent.Update): Generator {
428
508
  return this.context.registerCoroutineUpdate(this, routine, evt);
429
509
  }
430
-
510
+ /**
511
+ * Stop a coroutine that was previously started with `startCoroutine`
512
+ * @param routine the routine to be stopped
513
+ * @param evt the frame event to unregister the routine from (default: FrameEvent.Update)
514
+ */
431
515
  stopCoroutine(routine: Generator, evt: FrameEvent = FrameEvent.Update): void {
432
516
  this.context.unregisterCoroutineUpdate(routine, evt);
433
517
  }
434
518
 
519
+ /** @returns true if this component was destroyed (`this.destroy()`) or the whole object this component was part of */
435
520
  public get destroyed(): boolean {
436
521
  return this.__destroyed;
437
522
  }
438
523
 
524
+ /**
525
+ * Destroys this component (and removes it from the object)
526
+ */
439
527
  public destroy() {
440
528
  if (this.__destroyed) return;
441
529
  this.__internalDestroy();
@@ -464,7 +552,11 @@
464
552
 
465
553
  /** @internal */
466
554
  constructor() {
467
- this.__internalNewInstanceCreated();
555
+ this.__didAwake = false;
556
+ this.__didStart = false;
557
+ this.__didEnable = false;
558
+ this.__isEnabled = undefined;
559
+ this.__destroyed = false;
468
560
  }
469
561
 
470
562
 
@@ -666,5 +758,6 @@
666
758
  }
667
759
  }
668
760
 
669
- export class Behaviour extends Component {
670
- }
761
+ // For legacy reasons we need to export this as well
762
+ // (and we don't use extend to inherit the component docs)
763
+ export { Component as Behaviour };
src/engine-components/codegen/components.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable */
1
2
  // Export types
2
3
  export class __Ignore {}
3
4
  export { ActionBuilder } from "../export/usdz/extensions/behavior/BehavioursBuilder.js";
@@ -11,11 +12,11 @@
11
12
  export { Animator } from "../Animator.js";
12
13
  export { AnimatorController } from "../AnimatorController.js";
13
14
  export { Antialiasing } from "../postprocessing/Effects/Antialiasing.js";
14
- export { AttachedObject } from "../webxr/WebXRController.js";
15
15
  export { AudioExtension } from "../export/usdz/extensions/behavior/AudioExtension.js";
16
16
  export { AudioListener } from "../AudioListener.js";
17
17
  export { AudioSource } from "../AudioSource.js";
18
18
  export { AudioTrackHandler } from "../timeline/TimelineTracks.js";
19
+ export { Avatar } from "../webxr/Avatar.js";
19
20
  export { Avatar_Brain_LookAt } from "../avatar/Avatar_Brain_LookAt.js";
20
21
  export { Avatar_MouthShapes } from "../avatar/Avatar_MouthShapes.js";
21
22
  export { Avatar_MustacheShake } from "../avatar/Avatar_MustacheShake.js";
@@ -30,7 +31,6 @@
30
31
  export { BasicIKConstraint } from "../BasicIKConstraint.js";
31
32
  export { BehaviorExtension } from "../export/usdz/extensions/behavior/Behaviour.js";
32
33
  export { BehaviorModel } from "../export/usdz/extensions/behavior/BehavioursBuilder.js";
33
- export { Behaviour } from "../Component.js";
34
34
  export { Bloom } from "../postprocessing/Effects/Bloom.js";
35
35
  export { BoxCollider } from "../Collider.js";
36
36
  export { BoxGizmo } from "../Gizmos.js";
@@ -51,7 +51,6 @@
51
51
  export { ColorAdjustments } from "../postprocessing/Effects/ColorAdjustments.js";
52
52
  export { ColorBySpeedModule } from "../ParticleSystemModules.js";
53
53
  export { ColorOverLifetimeModule } from "../ParticleSystemModules.js";
54
- export { Component } from "../Component.js";
55
54
  export { ContactShadows } from "../ContactShadows.js";
56
55
  export { ControlTrackHandler } from "../timeline/TimelineTracks.js";
57
56
  export { CustomBranding } from "../export/usdz/USDZExporter.js";
@@ -88,7 +87,6 @@
88
87
  export { Image } from "../ui/Image.js";
89
88
  export { InheritVelocityModule } from "../ParticleSystemModules.js";
90
89
  export { InputField } from "../ui/InputField.js";
91
- export { Interactable } from "../Interactable.js";
92
90
  export { Light } from "../Light.js";
93
91
  export { LimitVelocityOverLifetimeModule } from "../ParticleSystemModules.js";
94
92
  export { LODGroup } from "../LODGroup.js";
@@ -102,6 +100,7 @@
102
100
  export { MeshRenderer } from "../Renderer.js";
103
101
  export { MinMaxCurve } from "../ParticleSystemModules.js";
104
102
  export { MinMaxGradient } from "../ParticleSystemModules.js";
103
+ export { NeedleWebXRHtmlElement } from "../webxr/WebXRButtons.js";
105
104
  export { NestedGltf } from "../NestedGltf.js";
106
105
  export { Networking } from "../Networking.js";
107
106
  export { NoiseModule } from "../ParticleSystemModules.js";
@@ -125,7 +124,6 @@
125
124
  export { PreliminaryAction } from "../export/usdz/extensions/behavior/BehaviourComponents.js";
126
125
  export { PreliminaryTrigger } from "../export/usdz/extensions/behavior/BehaviourComponents.js";
127
126
  export { RawImage } from "../ui/Image.js";
128
- export { Raycaster } from "../ui/Raycaster.js";
129
127
  export { Rect } from "../ui/RectTransform.js";
130
128
  export { RectTransform } from "../ui/RectTransform.js";
131
129
  export { ReflectionProbe } from "../ReflectionProbe.js";
@@ -153,6 +151,7 @@
153
151
  export { SizeOverLifetimeModule } from "../ParticleSystemModules.js";
154
152
  export { SkinnedMeshRenderer } from "../Renderer.js";
155
153
  export { SmoothFollow } from "../SmoothFollow.js";
154
+ export { SpatialGrabRaycaster } from "../ui/Raycaster.js";
156
155
  export { SpatialHtml } from "../ui/SpatialHtml.js";
157
156
  export { SpatialTrigger } from "../SpatialTrigger.js";
158
157
  export { SpatialTriggerReceiver } from "../SpatialTrigger.js";
@@ -167,7 +166,7 @@
167
166
  export { SyncedRoom } from "../SyncedRoom.js";
168
167
  export { SyncedTransform } from "../SyncedTransform.js";
169
168
  export { TapGestureTrigger } from "../export/usdz/extensions/behavior/BehaviourComponents.js";
170
- export { TeleportTarget } from "../webxr/WebXRController.js";
169
+ export { TeleportTarget } from "../webxr/TeleportTarget.js";
171
170
  export { TestRunner } from "../TestRunner.js";
172
171
  export { TestSimulateUserData } from "../TestRunner.js";
173
172
  export { Text } from "../ui/Text.js";
@@ -197,20 +196,16 @@
197
196
  export { Volume } from "../postprocessing/Volume.js";
198
197
  export { VolumeParameter } from "../postprocessing/VolumeParameter.js";
199
198
  export { VolumeProfile } from "../postprocessing/VolumeProfile.js";
200
- export { VRUserState } from "../webxr/WebXRSync.js";
201
- export { WebAR } from "../webxr/WebXR.js";
202
199
  export { WebARCameraBackground } from "../webxr/WebARCameraBackground.js";
203
200
  export { WebARSessionRoot } from "../webxr/WebARSessionRoot.js";
204
201
  export { WebXR } from "../webxr/WebXR.js";
205
- export { WebXRAvatar } from "../webxr/WebXRAvatar.js";
206
- export { WebXRController } from "../webxr/WebXRController.js";
207
202
  export { WebXRImageTracking } from "../webxr/WebXRImageTracking.js";
208
203
  export { WebXRImageTrackingModel } from "../webxr/WebXRImageTracking.js";
209
204
  export { WebXRPlaneTracking } from "../webxr/WebXRPlaneTracking.js";
210
- export { WebXRSync } from "../webxr/WebXRSync.js";
211
205
  export { WebXRTrackedImage } from "../webxr/WebXRImageTracking.js";
212
- export { XRFlag } from "../XRFlag.js";
213
- export { XRGrabModel } from "../webxr/WebXRGrabRendering.js";
214
- export { XRGrabRendering } from "../webxr/WebXRGrabRendering.js";
206
+ export { XRControllerFollow } from "../webxr/controllers/XRControllerFollow.js";
207
+ export { XRControllerModel } from "../webxr/controllers/XRControllerModel.js";
208
+ export { XRControllerMovement } from "../webxr/controllers/XRControllerMovement.js";
209
+ export { XRFlag } from "../webxr/XRFlag.js";
215
210
  export { XRRig } from "../webxr/WebXRRig.js";
216
- export { XRState } from "../XRFlag.js";
211
+ export { XRState } from "../webxr/XRFlag.js";
src/engine-components/ContactShadows.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { Behaviour } from "./Component.js";
2
- import { serializable } from "../engine/engine_serialization_decorator.js";
3
-
4
1
  import { CustomBlending, DoubleSide, Group, Matrix4, MaxEquation, Mesh, MeshBasicMaterial, MeshDepthMaterial, MinEquation, OrthographicCamera, PlaneGeometry, ShaderMaterial, WebGLRenderTarget } from "three";
5
2
  import { HorizontalBlurShader } from 'three/examples/jsm/shaders/HorizontalBlurShader.js';
6
3
  import { VerticalBlurShader } from 'three/examples/jsm/shaders/VerticalBlurShader.js';
4
+
5
+ import { serializable } from "../engine/engine_serialization_decorator.js";
7
6
  import { getParam } from "../engine/engine_utils.js"
8
7
  import { setCustomVisibility } from "../engine/js-extensions/Layers.js";
8
+ import { Behaviour } from "./Component.js";
9
9
 
10
10
  const debug = getParam("debugcontactshadows");
11
11
 
src/engine/debug/debug_console.ts CHANGED
@@ -1,6 +1,7 @@
1
- import { getErrorCount } from "./debug_overlay.js";
2
- import { getParam, isMobileDevice } from "../engine_utils.js";
3
1
  import { isLocalNetwork } from "../engine_networking_utils.js";
2
+ import { getParam, isMobileDevice, isQuest } from "../engine_utils.js";
3
+ import { isDevEnvironment } from "./debug.js";
4
+ import { getErrorCount, makeErrorsVisibleForDevelopment } from "./debug_overlay.js";
4
5
 
5
6
  let consoleInstance: any = null;
6
7
  let consoleHtmlElement: HTMLElement | null = null;
@@ -22,8 +23,11 @@
22
23
  currentUrl.searchParams.set("console", "1");
23
24
  console.log("🌵 Tip: You can add the \"?console\" query parameter to the url to show the debug console (on mobile it will automatically open in the bottom right corner when your get errors during development)", "\nOpen this page console: " + currentUrl.toString());
24
25
  }
25
- const isMobile = isMobileDevice();
26
+ const isMobile = isMobileDevice() || (isQuest() && isDevEnvironment());
26
27
  if (isMobile) {
28
+ // we need to invoke this here - otherwise we will miss errors that happen after the console is loaded
29
+ // and calling the method from the root needle-engine.ts import is evaluated later (if we import the method from the toplevel file and then invoke it)
30
+ makeErrorsVisibleForDevelopment();
27
31
  beginWatchingLogs();
28
32
  createConsole(true);
29
33
  if (isMobile) {
@@ -191,7 +195,7 @@
191
195
  }
192
196
  `;
193
197
  consoleHtmlElement?.prepend(styles);
194
- if (startHidden === true)
198
+ if (startHidden === true && getErrorCount() <= 0)
195
199
  hideDebugConsole();
196
200
  console.log("🌵 Debug console has loaded");
197
201
  }
src/engine/debug/debug_overlay.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import { ContextRegistry } from "../engine_context_registry.js";
2
+ import { isLocalNetwork } from "../engine_networking_utils.js";
1
3
  import { getParam } from "../engine_utils.js";
2
- import { isLocalNetwork } from "../engine_networking_utils.js";
3
- import { ContextRegistry } from "../engine_context_registry.js";
4
4
 
5
5
  const debug = getParam("debugdebug");
6
6
  let hide = false;
@@ -15,7 +15,7 @@
15
15
  }
16
16
 
17
17
  export function getErrorCount() {
18
- return errorCount;
18
+ return _errorCount;
19
19
  }
20
20
 
21
21
  const originalConsoleError = console.error;
@@ -37,9 +37,10 @@
37
37
  if (hide) return;
38
38
  const isLocal = isLocalNetwork();
39
39
  if (debug) console.log("Is this a local network?", isLocal);
40
- if (isLocal) {
40
+ if (isLocal)
41
+ {
41
42
  if (debug)
42
- console.log(window.location.hostname);
43
+ console.warn("Patch console", window.location.hostname);
43
44
  console.error = patchedConsoleError;
44
45
  window.addEventListener("error", (event) => {
45
46
  if (hide) return;
@@ -66,10 +67,10 @@
66
67
  }
67
68
 
68
69
 
69
- let errorCount = 0;
70
+ let _errorCount = 0;
70
71
 
71
72
  function onReceivedError() {
72
- errorCount += 1;
73
+ _errorCount += 1;
73
74
  }
74
75
 
75
76
  function onParseError(args: Array<any>) {
src/engine/debug/debug.ts CHANGED
@@ -1,6 +1,6 @@
1
+ import { isLocalNetwork } from "../engine_networking_utils.js";
2
+ import { showDebugConsole } from "./debug_console.js";
1
3
  import { addLog, LogType, setAllowOverlayMessages } from "./debug_overlay.js";
2
- import { showDebugConsole } from "./debug_console.js";
3
- import { isLocalNetwork } from "../engine_networking_utils.js";
4
4
 
5
5
  export { showDebugConsole }
6
6
  export { LogType, setAllowOverlayMessages };
src/engine-components/DeleteBox.ts CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  import * as THREE from "three";
3
+
3
4
  import { syncDestroy } from "../engine/engine_networking_instantiate.js";
4
5
  import { getParam } from "../engine/engine_utils.js";
5
6
  import { BoxHelperComponent } from "./BoxHelperComponent.js";
src/engine-components/postprocessing/Effects/DepthOfField.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { DepthOfFieldEffect } from "postprocessing";
2
+
3
+ import { Mathf } from "../../../engine/engine_math.js";
2
4
  import { serializable } from "../../../engine/engine_serialization.js";
3
- import { Mathf } from "../../../engine/engine_math.js";
4
5
  import { getParam, isMobileDevice } from "../../../engine/engine_utils.js";
5
6
  import { PostProcessingEffect } from "../PostProcessingEffect.js";
6
7
  import { VolumeParameter } from "../VolumeParameter.js";
src/engine-components/DeviceFlag.ts CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
+ import { serializable } from "../engine/engine_serialization_decorator.js";
2
3
  import { isMobileDevice } from "../engine/engine_utils.js";
3
- import { serializable } from "../engine/engine_serialization_decorator.js";
4
4
  import { Behaviour, GameObject } from "./Component.js";
5
5
 
6
6
 
src/engine-components/DragControls.ts CHANGED
@@ -1,104 +1,126 @@
1
- import { GameObject } from "./Component.js";
2
- import { SyncedTransform } from "./SyncedTransform.js";
3
- import type { IPointerDownHandler, IPointerEnterHandler, IPointerEventHandler, IPointerExitHandler, IPointerUpHandler, PointerEventData } from "./ui/PointerEvents.js";
1
+ import { AxesHelper, Box3, BufferGeometry, Camera, Color, Event, Line, LineBasicMaterial, Matrix3, Matrix4, Mesh, MeshBasicMaterial, Object3D, Plane, PlaneHelper, Quaternion, Ray, Raycaster, SphereGeometry, Vector3 } from "three";
2
+
3
+ import { Gizmos } from "../engine/engine_gizmos.js";
4
+ import { InstancingUtil } from "../engine/engine_instancing.js";
5
+ import { Mathf } from "../engine/engine_math.js";
6
+ import { RaycastOptions } from "../engine/engine_physics.js";
7
+ import { serializable } from "../engine/engine_serialization_decorator.js";
4
8
  import { Context } from "../engine/engine_setup.js";
5
- import { Interactable, UsageMarker } from "./Interactable.js";
6
- import { Rigidbody } from "./RigidBody.js";
7
- import { WebXR } from "./webxr/WebXR.js";
9
+ import { getWorldPosition, setWorldPosition } from "../engine/engine_three_utils.js";
10
+ import { IGameObject } from "../engine/engine_types.js";
11
+ import { getParam } from "../engine/engine_utils.js";
12
+ import { NeedleXRSession } from "../engine/engine_xr.js";
8
13
  import { Avatar_POI } from "./avatar/Avatar_Brain_LookAt.js";
9
- import { RaycastOptions } from "../engine/engine_physics.js";
10
- import { getWorldPosition, setWorldPosition } from "../engine/engine_three_utils.js";
11
- import type { KeyCode } from "../engine/engine_input.js";
12
- import { nameofFactory } from "../engine/engine_utils.js";
13
- import { InstancingUtil } from "../engine/engine_instancing.js";
14
+ import { Behaviour, GameObject } from "./Component.js";
15
+ import { UsageMarker } from "./Interactable.js";
14
16
  import { OrbitControls } from "./OrbitControls.js";
15
- import { BufferGeometry, Camera, Color, Line, LineBasicMaterial, Matrix4, Mesh, MeshBasicMaterial, Object3D, Plane, Ray, Raycaster, SphereGeometry, Vector2, Vector3 } from "three";
17
+ import { Rigidbody } from "./RigidBody.js";
18
+ import { SyncedTransform } from "./SyncedTransform.js";
19
+ import type { IPointerEventHandler, PointerEventData } from "./ui/PointerEvents.js";
16
20
  import { ObjectRaycaster } from "./ui/Raycaster.js";
17
- import { serializable } from "../engine/engine_serialization_decorator.js";
18
21
 
19
- const debug = false;
22
+ const debug = getParam("debugdrag");
20
23
 
21
- export enum DragEvents {
22
- SelectStart = "selectstart",
23
- SelectEnd = "selectend",
24
+ export enum DragMode {
25
+ /** Object stays at the same horizontal plane as it started. Commonly used for objects on the floor */
26
+ XZPlane = 0,
27
+ /** Object is dragged as if it was attached to the pointer. In 2D, that means it's dragged along the camera screen plane. In XR, it's dragged by the controller/hand. */
28
+ Attached = 1,
29
+ /** Object is dragged along the initial raycast hit normal. */
30
+ HitNormal = 2,
31
+ /** Combination of XZ and Screen based on the viewing angle. Low angles result in Screen dragging and higher angles in XZ dragging. */
32
+ DynamicViewAngle = 3,
33
+ /** The drag plane is adjusted dynamically while dragging. */
34
+ SnapToSurfaces = 4,
24
35
  }
25
36
 
26
- interface SelectArgs {
27
- selected: Object3D;
28
- attached: Object3D | GameObject | null;
29
- }
37
+ export class DragControls extends Behaviour implements IPointerEventHandler {
30
38
 
39
+ // dragPlane (floor, object, view)
40
+ // snap to surface (snap orientation?)
41
+ // two-handed drag (scale, rotate, move)
42
+ // keep upright (no tilt)
31
43
 
32
- export interface IDragEventListener {
33
- onDragStart?();
34
- onDragEnd?();
35
- }
44
+ /** How and where the object is dragged along. */
45
+ @serializable()
46
+ public dragMode: DragMode = DragMode.DynamicViewAngle;
36
47
 
37
- export class DragControls extends Interactable implements IPointerEventHandler {
48
+ /** Snap dragged objects to a XYZ grid – 0 means: no snapping. */
49
+ @serializable()
50
+ public snapGridResolution: number = 0.0;
51
+
52
+ /** Keep the original rotation of the dragged object. */
53
+ @serializable()
54
+ public keepRotation: boolean = true;
55
+
56
+ /** How and where the object is dragged along while dragging in XR. */
57
+ @serializable()
58
+ public xrDragMode: DragMode = DragMode.Attached;
38
59
 
39
- private static _active: number = 0;
40
- public static get HasAnySelected(): boolean { return this._active > 0; }
60
+ /** Keep the original rotation of the dragged object while dragging in XR. */
61
+ @serializable()
62
+ public xrKeepRotation: boolean = false;
41
63
 
42
- /** Show's drag gizmos when enabled */
64
+ /** Accelerate dragging objects closer / further away when in XR */
43
65
  @serializable()
44
- public showGizmo: boolean = true;
66
+ public xrDistanceDragFactor: number = 1;
45
67
 
46
- /** When enabled DragControls will drag vertically when the object is viewed from a low angle */
68
+ /** When enabled, draws a line from the dragged object downwards to the next raycast hit. */
47
69
  @serializable()
48
- public useViewAngle: boolean = true;
70
+ public showGizmo: boolean = false;
49
71
 
50
- public transformSelf: boolean = true;
51
- // public transformGroup: boolean = true;
52
- // public targets: Object3D[] | null = null;
72
+ // future:
73
+ // constraints?
53
74
 
54
- // private controls: Control | null = null;
75
+ public static get HasAnySelected(): boolean { return this._active > 0; }
76
+ private static _active: number = 0;
77
+
78
+ /** The object to be dragged – we pass this to handlers when they are created */
79
+ private targetObject: GameObject | null = null;
55
80
  private orbit: OrbitControls | null = null;
81
+ private _dragHelper: LegacyDragVisualsHelper | null = null;
82
+ private static lastHovered: Object3D;
83
+ private _draggingRigidbodies: Rigidbody[] = [];
84
+ private _potentialDragStartEvt: PointerEventData | null = null;
85
+ private _dragHandlers: Map<Object3D, IDragHandler> = new Map();
86
+ private _totalMovement: Vector3 = new Vector3();
87
+ /** A marker is attached to components that are currently interacted with, to e.g. prevent them from being deleted. */
88
+ private _marker: UsageMarker | null = null;
89
+ private _isDragging: boolean = false;
90
+ private _didDrag: boolean = false;
56
91
 
57
- private selectStartEventListener: ((controls: DragControls, args: SelectArgs) => void)[] = [];
58
- private selectEndEventListener: Array<Function> = [];
59
- private _dragHelper: DragHelper | null = null;
60
-
61
- constructor() {
62
- super();
63
- this.selectStartEventListener = [];
64
- this.selectEndEventListener = [];
65
- this._dragDelta = new Vector2();
92
+ setTargetObject(obj: Object3D | null) {
93
+ this.targetObject = obj as GameObject;
94
+ for (const handler of this._dragHandlers.values()) {
95
+ handler.setTargetObject(obj);
96
+ }
66
97
  }
67
98
 
68
-
69
- // TODO: Update DragEventListener code
70
- addDragEventListener(type: DragEvents, cb: (ctrls: DragControls, args: SelectArgs) => void | Function) {
71
- switch (type) {
72
- case DragEvents.SelectStart:
73
- this.selectStartEventListener.push(cb);
74
- break;
75
- case DragEvents.SelectEnd:
76
- this.selectEndEventListener.push(cb);
77
- break;
78
- }
99
+ awake() {
100
+ // initialize all data that may be cloned incorrectly otherwise
101
+ this._potentialDragStartEvt = null;
102
+ this._dragHandlers = new Map();
103
+ this._totalMovement = new Vector3();
104
+ this._marker = null;
105
+ this._isDragging = false;
106
+ this._didDrag = false;
107
+ this._dragHelper = null;
108
+ this._draggingRigidbodies = [];
79
109
  }
80
110
 
81
-
82
-
83
111
  start() {
84
112
  this.orbit = GameObject.findObjectOfType(OrbitControls, this.context);
85
- if (!this.gameObject.getComponentInParent(ObjectRaycaster)) {
113
+ if (!this.gameObject.getComponentInParent(ObjectRaycaster))
86
114
  this.gameObject.addNewComponent(ObjectRaycaster);
87
- }
88
115
  }
89
116
 
90
- private static lastHovered: Object3D;
91
- private _draggingRigidbodies: Rigidbody[] = [];
92
-
93
117
  private allowEdit(_obj: Object3D | null = null) {
94
118
  return this.context.connection.allowEditing;
95
119
  }
96
120
 
97
121
  onPointerEnter(evt: PointerEventData) {
98
122
  if (!this.allowEdit(this.gameObject)) return;
99
- if (WebXR.IsInWebXR) return;
100
- // const interactable = GameObject.getComponentInParent(evt.object, Interactable);
101
- // if (!interactable) return;
123
+ if (evt.mode !== "screen") return;
102
124
  const dc = GameObject.getComponentInParent(evt.object, DragControls);
103
125
  if (!dc || dc !== this) return;
104
126
  DragControls.lastHovered = evt.object;
@@ -107,83 +129,121 @@
107
129
 
108
130
  onPointerExit(evt: PointerEventData) {
109
131
  if (!this.allowEdit(this.gameObject)) return;
110
- if (WebXR.IsInWebXR) return;
132
+ if (evt.mode !== "screen") return;
111
133
  if (DragControls.lastHovered !== evt.object) return;
112
- // const interactable = GameObject.getComponentInParent(evt.object, Interactable);
113
- // if (!interactable) return;
114
134
  this.context.domElement.style.cursor = 'auto';
115
135
  }
116
136
 
117
- private _waitingForDragStart: PointerEventData | null = null;
118
-
119
137
  onPointerDown(args: PointerEventData) {
120
138
  if (!this.allowEdit(this.gameObject)) return;
121
- if (WebXR.IsInWebXR) return;
122
- DragControls._active += 1;
123
- this._dragDelta.set(0, 0);
124
- this._didDrag = false;
125
- // Clone to not modify the original event (and this event is used in the actual onDragStart method)
126
- this._waitingForDragStart = args.clone();
127
- args.stopPropagation();
128
- // disabling pointer controls here already, otherwise we get a few frames of movement event in orbit controls and this will rotate the camera sligthly AFTER drag controls dragging ends.
129
- if (this.orbit) this.orbit.enabled = false;
139
+ if (args.used) return;
140
+ DragControls.lastHovered = args.object;
141
+
142
+ if (args.button === 0) {
143
+ if (this._dragHandlers.size === 0) {
144
+ this._didDrag = false;
145
+ this._totalMovement.set(0, 0, 0);
146
+ this._potentialDragStartEvt = args;
147
+ }
148
+
149
+ DragControls._active += 1;
150
+
151
+ const newDragHandler = new DragPointerHandler(this, this.targetObject || this.gameObject);
152
+ this._dragHandlers.set(args.event.space, newDragHandler);
153
+
154
+ // We need to turn off OrbitControls immediately, otherwise they still get data for a short moment
155
+ // and they don't properly handle being disabled while already processing data (smoothing happens when enabling again)
156
+ if (this.orbit) this.orbit.enabled = false;
157
+
158
+ newDragHandler.onDragStart(args);
159
+
160
+ if (this._dragHandlers.size === 2) {
161
+ const iterator = this._dragHandlers.values();
162
+ const a = iterator.next().value;
163
+ const b = iterator.next().value;
164
+ const mtHandler = new MultiTouchDragHandler(this, this.targetObject || this.gameObject, a, b);
165
+ this._dragHandlers.set(this.gameObject, mtHandler);
166
+
167
+ mtHandler.onDragStart(args);
168
+ }
169
+
170
+ args.use();
171
+ }
130
172
  }
131
173
 
132
174
  onPointerMove(args: PointerEventData) {
133
- if(this._isDragging || this._waitingForDragStart !== null) args.use();
175
+ if (this._isDragging || this._potentialDragStartEvt !== null) args.use();
134
176
  }
135
177
 
136
178
  onPointerUp(args: PointerEventData) {
137
- this._waitingForDragStart = null;
179
+
180
+ if(debug) Gizmos.DrawLabel(args.point ?? this.gameObject.worldPosition, "POINTERUP:" + args.pointerId + ", " + args.button, .03, 3);
181
+
138
182
  if (!this.allowEdit(this.gameObject)) return;
139
- if (DragControls._active > 0)
140
- DragControls._active -= 1;
141
- if (WebXR.IsInWebXR) return;
142
- this.onDragEnd(args);
143
- args.stopPropagation();
144
- if (this.orbit) this.orbit.enabled = true;
183
+ if (args.button !== 0) return;
184
+ this._potentialDragStartEvt = null;
185
+
186
+ const handler = this._dragHandlers.get(args.event.space);
187
+ const mtHandler = this._dragHandlers.get(this.gameObject) as MultiTouchDragHandler;
188
+ if (mtHandler && (mtHandler.handlerA === handler || mtHandler.handlerB === handler)) {
189
+ // any of the two handlers has been released, so we can remove the multi-touch handler
190
+ this._dragHandlers.delete(this.gameObject);
191
+ mtHandler.onDragEnd(args);
192
+ }
193
+
194
+ if (handler) {
195
+ if (DragControls._active > 0)
196
+ DragControls._active -= 1;
197
+
198
+ if (handler.onDragEnd) handler.onDragEnd(args);
199
+ this._dragHandlers.delete(args.event.space);
200
+
201
+ if (this._dragHandlers.size === 0) {
202
+ this.onLastDragEnd(args);
203
+ }
204
+ args.use();
205
+ }
206
+
207
+ if (DragControls._active === 0) {
208
+ if (this.orbit) this.orbit.enabled = true;
209
+ }
145
210
  }
146
211
 
147
-
148
212
  update(): void {
149
- if (WebXR.IsInWebXR) return;
150
213
 
214
+ for (const handler of this._dragHandlers.values()) {
215
+ if (handler.collectMovementInfo) handler.collectMovementInfo();
216
+ // TODO this doesn't make sense, we should instead just use the max here
217
+ // or even better, each handler can decide on their own how to handle this
218
+ if (handler.getTotalMovement) this._totalMovement.add(handler.getTotalMovement());
219
+ }
220
+
151
221
  // drag start only after having dragged for some pixels
152
- if (this._waitingForDragStart) {
222
+ if (this._potentialDragStartEvt) {
153
223
  if (!this._didDrag) {
154
- // this is so we can e.g. process clicks without having a drag change the position
155
- // e.g. a click to rotate the object
156
- const delta = this.context.input.getPointerPositionDelta(0);
157
- if (delta)
158
- this._dragDelta.add(delta);
159
- if (this._dragDelta.length() > 2)
224
+ // this is so we can e.g. process clicks without having a drag change the position, e.g. a click to call a method.
225
+ // TODO probably needs to be treated differently for spatial (3D motion) and screen (2D pixel motion) drags
226
+ if (this._totalMovement.length() > 0.0003)
160
227
  this._didDrag = true;
161
228
  else return;
162
229
  }
163
- const args = this._waitingForDragStart;
164
- this._waitingForDragStart = null;
165
- this.onDragStart(args);
230
+ const args = this._potentialDragStartEvt;
231
+ this._potentialDragStartEvt = null;
232
+ this.onFirstDragStart(args);
166
233
  }
167
234
 
168
- if (this._dragHelper && this._dragHelper.hasSelected) {
169
- this.onUpdateDrag();
170
- }
171
-
172
- if (this._dragHelper?.hasSelected === false || (this._activePointerId !== undefined && this.context.input.getPointerPressed(this._activePointerId) === false)) {
173
- this.onDragEnd(null);
174
- }
235
+ for (const handler of this._dragHandlers.values())
236
+ if (handler.onDragUpdate) handler.onDragUpdate(this._dragHandlers.size);
237
+
238
+ if (this._dragHelper && this._dragHelper.hasSelected)
239
+ this.onAnyDragUpdate();
175
240
  }
176
241
 
177
- private _isDragging: boolean = false;
178
- private _marker: UsageMarker | null = null;
179
- private _dragDelta!: Vector2;
180
- private _didDrag: boolean = false;
181
- private _activePointerId?: number;
182
-
183
- private onDragStart(evt: PointerEventData) {
242
+ /** Called when the first pointer starts dragging on this object. Not called for subsequent pointers on the same object. */
243
+ private onFirstDragStart(evt: PointerEventData) {
184
244
  if (!this._dragHelper) {
185
245
  if (this.context.mainCamera)
186
- this._dragHelper = new DragHelper(this.context.mainCamera);
246
+ this._dragHelper = new LegacyDragVisualsHelper(this.context.mainCamera);
187
247
  else
188
248
  return;
189
249
  }
@@ -192,46 +252,17 @@
192
252
  const dc = GameObject.getComponentInParent(evt.object, DragControls);
193
253
  if (!dc || dc !== this) return;
194
254
 
255
+ const object = this.targetObject || this.gameObject;
195
256
 
196
- let object: Object3D = evt.object;
257
+ if (!object) return;
197
258
 
198
- if (this.transformSelf) {
199
- object = this.gameObject;
200
- }
201
-
202
- // raise event
203
- const args: { selected: Object3D, attached: Object3D | null } = { selected: object, attached: object };
204
- for (const listener of this.selectStartEventListener) {
205
- listener(this, args);
206
- }
207
-
208
- this._activePointerId = evt.pointerId;
209
-
210
- if (!args.attached) return;
211
- if (args.attached !== object) {
212
- // // if duplicatable changes the object being dragged
213
- // // should it also change the active drag controls (e.g. if it has a own one)
214
- // const drag = GameObject.getComponentInParent(args.attached, DragControls);
215
- // if (drag && drag !== this) {
216
- // // incredibly ugly code to pass the drag controls event to another drag controls instance
217
- // // This is necessary since we dont call the onPointerUp events anymore for all objects
218
- // // that have previously received the onPointerDown event.
219
- // // NOTE: added the EventSystem.raisedPointerDownEvents array again because of this uglyness here. The code was originally removed in 757fc5e5bafd02aa13d6cd35dd5e8729c841465a and now we're adding it in 8ce886d8344d1abd5ebb89ae3e1fb8d6d47293da
220
- // this.onDragEnd(null);
221
- // drag.onPointerDown(evt);
222
- // evt.object = args.attached;
223
- // drag.onDragStart(evt);
224
- // return;
225
- // }
226
- }
227
- object = args.attached;
228
259
  this._isDragging = true;
229
260
  this._dragHelper.setSelected(object, this.context);
230
261
  if (this.orbit) this.orbit.enabled = false;
231
262
 
232
263
  const sync = GameObject.getComponentInChildren(object, SyncedTransform);
233
- if (debug)
234
- console.log("DRAG START", sync, object);
264
+ if (debug) console.log("DRAG START", sync, object);
265
+
235
266
  if (sync) {
236
267
  sync.fastMode = true;
237
268
  sync?.requestOwnership();
@@ -239,30 +270,31 @@
239
270
 
240
271
  this._marker = GameObject.addNewComponent(object, UsageMarker);
241
272
 
242
- // console.log(object, this._marker);
243
-
244
273
  this._draggingRigidbodies.length = 0;
245
274
  const rbs = GameObject.getComponentsInChildren(object, Rigidbody);
246
275
  if (rbs)
247
276
  this._draggingRigidbodies.push(...rbs);
248
-
249
- const l = nameofFactory<IDragEventListener>();
250
- GameObject.invokeOnChildren(this._dragHelper.selected, l("onDragStart"));
251
277
  }
252
278
 
253
- private onUpdateDrag() {
279
+ /** Called each frame as long as any pointer is dragging this object. */
280
+ private onAnyDragUpdate() {
254
281
  if (!this._dragHelper) return;
255
282
  this._dragHelper.showGizmo = this.showGizmo;
256
- this._dragHelper.useViewAngle = this.useViewAngle;
257
283
 
258
284
  this._dragHelper.onUpdate(this.context);
259
285
  for (const rb of this._draggingRigidbodies) {
260
286
  rb.wakeUp();
261
287
  rb.resetVelocities();
288
+ rb.resetForcesAndTorques();
262
289
  }
290
+
291
+ const object = this.targetObject || this.gameObject;
292
+
293
+ InstancingUtil.markDirty(object);
263
294
  }
264
295
 
265
- private onDragEnd(evt: PointerEventData | null) {
296
+ /** Called when the last pointer has been removed from this object. */
297
+ private onLastDragEnd(evt: PointerEventData | null) {
266
298
  if (!this || !this._isDragging) return;
267
299
  this._isDragging = false;
268
300
  if (!this._dragHelper) return;
@@ -271,8 +303,7 @@
271
303
  }
272
304
  this._draggingRigidbodies.length = 0;
273
305
  const selected = this._dragHelper.selected;
274
- if (debug)
275
- console.log("DRAG END", selected, selected?.visible)
306
+ if (debug) console.log("DRAG END", selected, selected?.visible)
276
307
  this._dragHelper.setSelected(null, this.context);
277
308
  if (this.orbit) this.orbit.enabled = true;
278
309
  if (evt?.object) {
@@ -282,23 +313,751 @@
282
313
  // sync?.requestOwnership();
283
314
  }
284
315
  }
285
- if (this._marker) {
316
+ if (this._marker)
286
317
  this._marker.destroy();
318
+ }
319
+ }
320
+
321
+ /** Handles two touch points affecting one object. Allows movement, scale and rotation of objects. */
322
+ class MultiTouchDragHandler implements IDragHandler {
323
+
324
+ handlerA: DragPointerHandler;
325
+ handlerB: DragPointerHandler;
326
+
327
+ private context: Context;
328
+ private settings: DragControls;
329
+ private gameObject: GameObject;
330
+ private _handlerAAttachmentPoint: Vector3 = new Vector3();
331
+ private _handlerBAttachmentPoint: Vector3 = new Vector3();
332
+
333
+ private _followObject: GameObject;
334
+ private _manipulatorObject: GameObject;
335
+ private _deviceMode!: XRTargetRayMode;
336
+ private _followObjectStartWorldQuaternion: Quaternion = new Quaternion();
337
+
338
+ constructor(dragControls: DragControls, gameObject: GameObject, pointerA: DragPointerHandler, pointerB: DragPointerHandler) {
339
+ this.context = dragControls.context;
340
+ this.settings = dragControls;
341
+ this.gameObject = gameObject;
342
+ this.handlerA = pointerA;
343
+ this.handlerB = pointerB;
344
+
345
+ this._followObject = new Object3D() as GameObject;
346
+ this._manipulatorObject = new Object3D() as GameObject;
347
+
348
+ this.context.scene.add(this._manipulatorObject);
349
+
350
+ const rig = NeedleXRSession.active?.rig?.gameObject;
351
+
352
+ if (!this.handlerA || !this.handlerB || !this.handlerA.hitPointInLocalSpace || !this.handlerB.hitPointInLocalSpace) {
353
+ console.error("Invalid: MultiTouchDragHandler needs two valid DragPointerHandlers with hitPointInLocalSpace set.");
354
+ return;
287
355
  }
288
- // raise event
289
- for (const listener of this.selectEndEventListener) {
290
- listener(this);
356
+
357
+ this._tempVec1.copy(this.handlerA.hitPointInLocalSpace);
358
+ this._tempVec2.copy(this.handlerB.hitPointInLocalSpace);
359
+ this.gameObject.localToWorld(this._tempVec1);
360
+ this.gameObject.localToWorld(this._tempVec2);
361
+ if (rig) {
362
+ rig.worldToLocal(this._tempVec1);
363
+ rig.worldToLocal(this._tempVec2);
291
364
  }
365
+ this._initialDistance = this._tempVec1.distanceTo(this._tempVec2);
366
+
367
+ if (this._initialDistance < 0.02) {
368
+ if (debug) {
369
+ console.log("Finding alternative drag attachment points since initial distance is too low: " + this._initialDistance.toFixed(2));
370
+ }
371
+ // We want two reasonable pointer attachment points here.
372
+ // But if the hitPointInLocalSpace are very close to each other, we instead fall back to controller positions.
373
+ this.handlerA.followObject.parent!.getWorldPosition(this._tempVec1);
374
+ this.handlerB.followObject.parent!.getWorldPosition(this._tempVec2);
375
+ this._handlerAAttachmentPoint.copy(this._tempVec1);
376
+ this._handlerBAttachmentPoint.copy(this._tempVec2);
377
+ this.gameObject.worldToLocal(this._handlerAAttachmentPoint);
378
+ this.gameObject.worldToLocal(this._handlerBAttachmentPoint);
379
+ this._initialDistance = this._tempVec1.distanceTo(this._tempVec2);
292
380
 
293
- const l = nameofFactory<IDragEventListener>();
294
- GameObject.invokeOnChildren(selected, l("onDragEnd"));
381
+ if (this._initialDistance < 0.001) {
382
+ console.warn("Not supported right now – controller drag points for multitouch are too close!");
383
+ this._initialDistance = 1;
384
+ }
385
+ }
386
+ else {
387
+ this._handlerAAttachmentPoint.copy(this.handlerA.hitPointInLocalSpace);
388
+ this._handlerBAttachmentPoint.copy(this.handlerB.hitPointInLocalSpace);
389
+ }
390
+
391
+ this._tempVec3.lerpVectors(this._tempVec1, this._tempVec2, 0.5);
392
+ this._initialScale.copy(gameObject.scale);
393
+
394
+ if (debug) {
395
+ this._followObject.add(new AxesHelper(2));
396
+ this._manipulatorObject.add(new AxesHelper(5));
397
+
398
+ const formatVec = (v: Vector3) => `${v.x.toFixed(2)}, ${v.y.toFixed(2)}, ${v.z.toFixed(2)}`;
399
+ Gizmos.DrawLine(this._tempVec1, this._tempVec2, 0x00ffff, 0, false);
400
+ Gizmos.DrawLabel(this._tempVec3, "A:B " + this._initialDistance.toFixed(2) + "\n" + formatVec(this._tempVec1) + "\n" + formatVec(this._tempVec2), 0.03, 5);
401
+ }
295
402
  }
403
+
404
+ onDragStart(_args: PointerEventData): void {
405
+ // align _followObject with the object we want to drag
406
+ this.gameObject.add(this._followObject);
407
+ this._followObject.matrixAutoUpdate = false;
408
+ this._followObject.matrix.identity();
409
+ this._deviceMode = _args.mode;
410
+ this._followObjectStartWorldQuaternion.copy(this._followObject.worldQuaternion);
411
+
412
+ // align _manipulatorObject in the same way it would if this was a drag update
413
+ this.alignManipulator();
414
+
415
+ // and then parent it to the space object so it follows along.
416
+ this._manipulatorObject.attach(this._followObject);
417
+
418
+ // store offsets in local space
419
+ this._manipulatorPosOffset.copy(this._followObject.position);
420
+ this._manipulatorRotOffset.copy(this._followObject.quaternion);
421
+ this._manipulatorScaleOffset.copy(this._followObject.scale);
422
+ }
423
+
424
+ onDragEnd(_args: PointerEventData): void {
425
+ if (!this.handlerA || !this.handlerB) {
426
+ console.error("onDragEnd called on MultiTouchDragHandler without valid handlers. This is likely a bug.");
427
+ return;
428
+ }
429
+
430
+ // we want to initialize the drag points for these handlers again.
431
+ // one of them will be removed, but we don't know here which one
432
+ this.handlerA.recenter();
433
+ this.handlerB.recenter();
434
+
435
+ // destroy helper objects
436
+ this._manipulatorObject.removeFromParent();
437
+ this._followObject.removeFromParent();
438
+ this._manipulatorObject.destroy();
439
+ this._followObject.destroy();
440
+ }
441
+
442
+ private _manipulatorPosOffset: Vector3 = new Vector3();
443
+ private _manipulatorRotOffset: Quaternion = new Quaternion();
444
+ private _manipulatorScaleOffset: Vector3 = new Vector3();
445
+
446
+ private _tempVec1: Vector3 = new Vector3();
447
+ private _tempVec2: Vector3 = new Vector3();
448
+ private _tempVec3: Vector3 = new Vector3();
449
+ private tempLookMatrix: Matrix4 = new Matrix4();
450
+ private _initialScale: Vector3 = new Vector3();
451
+ private _initialDistance: number = 0;
452
+
453
+ private alignManipulator() {
454
+ this._tempVec1.copy(this._handlerAAttachmentPoint);
455
+ this._tempVec2.copy(this._handlerBAttachmentPoint);
456
+ this.handlerA.followObject.localToWorld(this._tempVec1);
457
+ this.handlerB.followObject.localToWorld(this._tempVec2);
458
+ this._tempVec3.lerpVectors(this._tempVec1, this._tempVec2, 0.5);
459
+
460
+ this._manipulatorObject.position.copy(this._tempVec3);
461
+
462
+ // - lookAt the second point on handlerB
463
+ const camera = this.context.mainCamera;
464
+ this.tempLookMatrix.lookAt(this._tempVec3, this._tempVec2, (camera as any as IGameObject).worldUp);
465
+ this._manipulatorObject.quaternion.setFromRotationMatrix(this.tempLookMatrix);
466
+
467
+ // - scale based on the distance between the two points
468
+ const dist = this._tempVec1.distanceTo(this._tempVec2);
469
+ this._manipulatorObject.scale.copy(this._initialScale).multiplyScalar(dist / this._initialDistance);
470
+
471
+ this._manipulatorObject.updateMatrix();
472
+ this._manipulatorObject.updateMatrixWorld(true);
473
+
474
+ if (debug) {
475
+ Gizmos.DrawLabel(this._tempVec3.clone().add(new Vector3(0,0.2,0)), "A:B " + dist.toFixed(2), 0.03);
476
+ Gizmos.DrawLine(this._tempVec1, this._tempVec2, 0x00ff00, 0, false);
477
+
478
+ // const wp = this._manipulatorObject.worldPosition;
479
+ // Gizmos.DrawWireSphere(wp, this._initialScale.length() * dist / this._initialDistance, 0x00ff00, 0, false);
480
+ }
481
+ }
482
+
483
+ onDragUpdate() {
484
+ // At this point we've run both the other handlers, but their effects have been suppressed because they can't handle
485
+ // two events at the same time. They're basically providing us with two Object3D's and we can combine these here
486
+ // into a reasonable two-handed translation/rotation/scale.
487
+ // One approach:
488
+ // - position our control object on the center between the two pointer control objects
489
+
490
+ // TODO close grab needs to be handled differently because there we don't have a hit point -
491
+ // Hit point is just the center of the object
492
+ // So probably we should fix that close grab has a better hit point approximation (point on bounds?)
493
+
494
+ this.alignManipulator();
495
+
496
+ // apply (smoothed) to the gameObject
497
+ const lerpStrength = 30;
498
+ const lerpFactor = 1.0;
499
+
500
+ this._followObject.position.copy(this._manipulatorPosOffset);
501
+ this._followObject.quaternion.copy(this._manipulatorRotOffset);
502
+ this._followObject.scale.copy(this._manipulatorScaleOffset);
503
+
504
+ const draggedObject = this.gameObject;
505
+ const targetObject = this._followObject;
506
+
507
+ targetObject.updateMatrix();
508
+ targetObject.updateMatrixWorld(true);
509
+
510
+ const isSpatialInput = this._deviceMode === "tracked-pointer";
511
+ const keepRotation = isSpatialInput ? this.settings.xrKeepRotation : this.settings.keepRotation;
512
+
513
+ // TODO refactor to a common place
514
+ // apply constraints (position grid snap, rotation, ...)
515
+ if (this.settings.snapGridResolution > 0) {
516
+ const wp = this._followObject.worldPosition;
517
+ const snap = this.settings.snapGridResolution;
518
+ wp.x = Math.round(wp.x / snap) * snap;
519
+ wp.y = Math.round(wp.y / snap) * snap;
520
+ wp.z = Math.round(wp.z / snap) * snap;
521
+ this._followObject.worldPosition = wp;
522
+ this._followObject.updateMatrix();
523
+ }
524
+ if (keepRotation) {
525
+ this._followObject.worldQuaternion = this._followObjectStartWorldQuaternion;
526
+ this._followObject.updateMatrix();
527
+ }
528
+
529
+ // TODO refactor to a common place
530
+ // TODO should use unscaled time here // some test for lerp speed depending on distance
531
+ const t = Mathf.clamp01(this.context.time.deltaTime * lerpStrength * lerpFactor);// / (currentDist - 1 + 0.01));
532
+
533
+ const wp = draggedObject.worldPosition;
534
+ wp.lerp(targetObject.worldPosition, t);
535
+ draggedObject.worldPosition = wp;
536
+
537
+ const rot = draggedObject.worldQuaternion;
538
+ rot.slerp(targetObject.worldQuaternion, t);
539
+ draggedObject.worldQuaternion = rot;
540
+
541
+ const scl = draggedObject.worldScale;
542
+ scl.lerp(targetObject.worldScale, t);
543
+ draggedObject.worldScale = scl;
544
+ }
545
+
546
+ setTargetObject(obj: Object3D | null): void {
547
+ this.gameObject = obj as GameObject;
548
+ }
296
549
  }
297
550
 
551
+ /** Common interface for pointer handlers (single touch and multi touch) */
552
+ interface IDragHandler {
553
+ /** Used to determine if a drag has happened for this handler */
554
+ getTotalMovement?(): Vector3;
555
+ /** Target object can change mid-flight (e.g. in Duplicatable), handlers should react properly to that */
556
+ setTargetObject(obj: Object3D | null): void;
557
+
558
+ /** Prewarms the drag – can already move internal points around here but should not move the object itself */
559
+ collectMovementInfo?(): void;
560
+ onDragStart?(args: PointerEventData): void;
561
+ onDragEnd?(args: PointerEventData): void;
562
+ /** The target object is moved around */
563
+ onDragUpdate?(numberOfPointers: number): void;
564
+ }
298
565
 
566
+ /** Handles a single pointer on an object. DragPointerHandlers are created on pointer enter,
567
+ * help with determining if there's actually a drag, and then perform operations based on spatial pointer data.
568
+ */
569
+ class DragPointerHandler implements IDragHandler {
299
570
 
300
- class DragHelper {
571
+ /** Absolute movement of the pointer. Used for determining if a motion/drag is happening.
572
+ * This is in world units, so very small for screens (near-plane space change) */
573
+ getTotalMovement(): Vector3 { return this._totalMovement; }
574
+ get followObject(): GameObject { return this._followObject; }
575
+ get hitPointInLocalSpace(): Vector3 { return this._hitPointInLocalSpace; }
301
576
 
577
+ private context: Context;
578
+ private gameObject: GameObject;
579
+ private settings: DragControls;
580
+ private _lastRig: IGameObject | undefined = undefined;
581
+
582
+ /** This object is placed at the pivot of the dragged object, and parented to the control space. */
583
+ private _followObject: GameObject;
584
+ private _totalMovement: Vector3 = new Vector3();
585
+ /** Motion along the pointer ray. On screens this doesn't change. In XR it can be used to determine how much
586
+ * effort someone is putting into moving an object closer or further away. */
587
+ private _totalMovementAlongRayDirection: number = 0;
588
+ /** Distance between _followObject and its parent at grab start, in local space */
589
+ private _grabStartDistance: number = 0;
590
+ private _deviceMode!: XRTargetRayMode;
591
+ private _followObjectStartPosition: Vector3 = new Vector3();
592
+ private _followObjectStartQuaternion: Quaternion = new Quaternion();
593
+ private _followObjectStartWorldQuaternion: Quaternion = new Quaternion();
594
+ private _lastDragPosRigSpace: Vector3 | undefined;
595
+ private _tempVec: Vector3 = new Vector3();
596
+ private _tempMat: Matrix4 = new Matrix4();
597
+
598
+ private _hitPointInLocalSpace: Vector3 = new Vector3();
599
+ private _hitNormalInLocalSpace: Vector3 = new Vector3();
600
+ private _bottomCenter = new Vector3();
601
+ private _backCenter = new Vector3();
602
+ private _backBottomCenter = new Vector3();
603
+ private _bounds = new Box3();
604
+ private _dragPlane = new Plane(new Vector3(0, 1, 0));
605
+ private _draggedOverObject: Object3D | null = null;
606
+ private _draggedOverObjectLastSetUp: Object3D | null = null;
607
+ private _draggedOverObjectLastNormal: Vector3 = new Vector3();
608
+ private _draggedOverObjectDuration: number = 0;
609
+
610
+ /** Allows overriding which object is dragged while a drag is already ongoing. Used for example by Duplicatable */
611
+ setTargetObject(obj: Object3D | null) {
612
+ this.gameObject = obj as GameObject;
613
+ }
614
+
615
+ constructor(dragControls: DragControls, gameObject: GameObject) {
616
+ this.settings = dragControls;
617
+ this.context = dragControls.context;
618
+ this.gameObject = gameObject;
619
+ this._followObject = new Object3D() as GameObject;
620
+ }
621
+
622
+ recenter() {
623
+ if (!this._followObject.parent) {
624
+ console.warn("Error: space follow object doesn't have parent but recenter() is called. This is likely a bug");
625
+ return;
626
+ }
627
+
628
+ const p = this._followObject.parent as GameObject;
629
+
630
+ this.gameObject.add(this._followObject);
631
+ this._followObject.matrixAutoUpdate = false;
632
+
633
+ this._followObject.position.set(0, 0, 0);
634
+ this._followObject.quaternion.set(0, 0, 0, 1);
635
+ this._followObject.scale.set(1, 1, 1);
636
+
637
+ this._followObject.updateMatrix();
638
+ this._followObject.updateMatrixWorld(true);
639
+
640
+ p.attach(this._followObject);
641
+
642
+ this._followObjectStartPosition.copy(this._followObject.position);
643
+ this._followObjectStartQuaternion.copy(this._followObject.quaternion);
644
+ this._followObjectStartWorldQuaternion.copy(this._followObject.worldQuaternion);
645
+
646
+ this._followObject.updateMatrix();
647
+ this._followObject.updateMatrixWorld(true);
648
+
649
+ const hitPointWP = this._hitPointInLocalSpace.clone();
650
+ this.gameObject.localToWorld(hitPointWP);
651
+ this._grabStartDistance = hitPointWP.distanceTo(p.worldPosition);
652
+ const rig = NeedleXRSession.active?.rig?.gameObject;
653
+ const rigScale = rig?.worldScale.x || 1;
654
+ this._grabStartDistance /= rigScale;
655
+
656
+ this._totalMovementAlongRayDirection = 0;
657
+ this._lastDragPosRigSpace = undefined;
658
+
659
+ if (debug)
660
+ {
661
+ Gizmos.DrawLine(hitPointWP, p.worldPosition, 0x00ff00, 0.5, false);
662
+ Gizmos.DrawLabel(p.worldPosition.add(new Vector3(0,0.1,0)), this._grabStartDistance.toFixed(2), 0.03, 0.5);
663
+ }
664
+ }
665
+
666
+ onDragStart(args: PointerEventData) {
667
+
668
+ args.event.space.add(this._followObject);
669
+
670
+ // prepare for drag, we will start dragging after an object has been dragged for a few centimeters
671
+ this._lastDragPosRigSpace = undefined;
672
+
673
+ if (args.point && args.normal) {
674
+ this._hitPointInLocalSpace.copy(args.point);
675
+ this.gameObject.worldToLocal(this._hitPointInLocalSpace);
676
+