webXRInputSource.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { Observable } from "../Misc/observable";
  2. import { AbstractMesh } from "../Meshes/abstractMesh";
  3. import { Quaternion, Vector3 } from "../Maths/math.vector";
  4. import { Ray } from "../Culling/ray";
  5. import { Scene } from "../scene";
  6. import { WebXRAbstractMotionController } from "./motionController/webXRAbstractMotionController";
  7. import { WebXRMotionControllerManager } from "./motionController/webXRMotionControllerManager";
  8. import { Tools } from "../Misc/tools";
  9. let idCount = 0;
  10. /**
  11. * Configuration options for the WebXR controller creation
  12. */
  13. export interface IWebXRControllerOptions {
  14. /**
  15. * Should the controller mesh be animated when a user interacts with it
  16. * The pressed buttons / thumbstick and touchpad animations will be disabled
  17. */
  18. disableMotionControllerAnimation?: boolean;
  19. /**
  20. * Do not load the controller mesh, in case a different mesh needs to be loaded.
  21. */
  22. doNotLoadControllerMesh?: boolean;
  23. /**
  24. * Force a specific controller type for this controller.
  25. * This can be used when creating your own profile or when testing different controllers
  26. */
  27. forceControllerProfile?: string;
  28. /**
  29. * Defines a rendering group ID for meshes that will be loaded.
  30. * This is for the default controllers only.
  31. */
  32. renderingGroupId?: number;
  33. }
  34. /**
  35. * Represents an XR controller
  36. */
  37. export class WebXRInputSource {
  38. private _tmpVector = new Vector3();
  39. private _uniqueId: string;
  40. private _disposed = false;
  41. /**
  42. * Represents the part of the controller that is held. This may not exist if the controller is the head mounted display itself, if thats the case only the pointer from the head will be availible
  43. */
  44. public grip?: AbstractMesh;
  45. /**
  46. * If available, this is the gamepad object related to this controller.
  47. * Using this object it is possible to get click events and trackpad changes of the
  48. * webxr controller that is currently being used.
  49. */
  50. public motionController?: WebXRAbstractMotionController;
  51. /**
  52. * Event that fires when the controller is removed/disposed.
  53. * The object provided as event data is this controller, after associated assets were disposed.
  54. * uniqueId is still available.
  55. */
  56. public onDisposeObservable = new Observable<WebXRInputSource>();
  57. /**
  58. * Will be triggered when the mesh associated with the motion controller is done loading.
  59. * It is also possible that this will never trigger (!) if no mesh was loaded, or if the developer decides to load a different mesh
  60. * A shortened version of controller -> motion controller -> on mesh loaded.
  61. */
  62. public onMeshLoadedObservable = new Observable<AbstractMesh>();
  63. /**
  64. * Observers registered here will trigger when a motion controller profile was assigned to this xr controller
  65. */
  66. public onMotionControllerInitObservable = new Observable<WebXRAbstractMotionController>();
  67. /**
  68. * Pointer which can be used to select objects or attach a visible laser to
  69. */
  70. public pointer: AbstractMesh;
  71. /**
  72. * Creates the input source object
  73. * @see https://doc.babylonjs.com/how_to/webxr_controllers_support
  74. * @param _scene the scene which the controller should be associated to
  75. * @param inputSource the underlying input source for the controller
  76. * @param _options options for this controller creation
  77. */
  78. constructor(
  79. private _scene: Scene,
  80. /** The underlying input source for the controller */
  81. public inputSource: XRInputSource,
  82. private _options: IWebXRControllerOptions = {}
  83. ) {
  84. this._uniqueId = `controller-${idCount++}-${inputSource.targetRayMode}-${inputSource.handedness}`;
  85. this.pointer = new AbstractMesh(`${this._uniqueId}-pointer`, _scene);
  86. this.pointer.rotationQuaternion = new Quaternion();
  87. if (this.inputSource.gripSpace) {
  88. this.grip = new AbstractMesh(`${this._uniqueId}-grip`, this._scene);
  89. this.grip.rotationQuaternion = new Quaternion();
  90. }
  91. this._tmpVector.set(0, 0, this._scene.useRightHandedSystem ? -1.0 : 1.0);
  92. // for now only load motion controllers if gamepad object available
  93. if (this.inputSource.gamepad) {
  94. WebXRMotionControllerManager.GetMotionControllerWithXRInput(inputSource, _scene, this._options.forceControllerProfile).then(
  95. (motionController) => {
  96. this.motionController = motionController;
  97. this.onMotionControllerInitObservable.notifyObservers(motionController);
  98. // should the model be loaded?
  99. if (!this._options.doNotLoadControllerMesh) {
  100. this.motionController.loadModel().then((success) => {
  101. if (success && this.motionController && this.motionController.rootMesh) {
  102. if (this._options.renderingGroupId) {
  103. // anything other than 0?
  104. this.motionController.rootMesh.renderingGroupId = this._options.renderingGroupId;
  105. this.motionController.rootMesh.getChildMeshes(false).forEach((mesh) => (mesh.renderingGroupId = this._options.renderingGroupId!));
  106. }
  107. this.onMeshLoadedObservable.notifyObservers(this.motionController.rootMesh);
  108. this.motionController.rootMesh.parent = this.grip || this.pointer;
  109. this.motionController.disableAnimation = !!this._options.disableMotionControllerAnimation;
  110. }
  111. // make sure to dispose is the controller is already disposed
  112. if (this._disposed) {
  113. this.motionController?.dispose();
  114. }
  115. });
  116. }
  117. },
  118. () => {
  119. Tools.Warn(`Could not find a matching motion controller for the registered input source`);
  120. }
  121. );
  122. }
  123. }
  124. /**
  125. * Get this controllers unique id
  126. */
  127. public get uniqueId() {
  128. return this._uniqueId;
  129. }
  130. /**
  131. * Disposes of the object
  132. */
  133. public dispose() {
  134. if (this.grip) {
  135. this.grip.dispose();
  136. }
  137. if (this.motionController) {
  138. this.motionController.dispose();
  139. }
  140. this.pointer.dispose();
  141. this.onMotionControllerInitObservable.clear();
  142. this.onMeshLoadedObservable.clear();
  143. this.onDisposeObservable.notifyObservers(this);
  144. this.onDisposeObservable.clear();
  145. this._disposed = true;
  146. }
  147. /**
  148. * Gets a world space ray coming from the pointer or grip
  149. * @param result the resulting ray
  150. * @param gripIfAvailable use the grip mesh instead of the pointer, if available
  151. */
  152. public getWorldPointerRayToRef(result: Ray, gripIfAvailable: boolean = false) {
  153. const object = gripIfAvailable && this.grip ? this.grip : this.pointer;
  154. Vector3.TransformNormalToRef(this._tmpVector, object.getWorldMatrix(), result.direction);
  155. result.direction.normalize();
  156. result.origin.copyFrom(object.absolutePosition);
  157. result.length = 1000;
  158. }
  159. /**
  160. * Updates the controller pose based on the given XRFrame
  161. * @param xrFrame xr frame to update the pose with
  162. * @param referenceSpace reference space to use
  163. */
  164. public updateFromXRFrame(xrFrame: XRFrame, referenceSpace: XRReferenceSpace) {
  165. let pose = xrFrame.getPose(this.inputSource.targetRaySpace, referenceSpace);
  166. // Update the pointer mesh
  167. if (pose) {
  168. const pos = pose.transform.position;
  169. this.pointer.position.set(pos.x, pos.y, pos.z);
  170. const orientation = pose.transform.orientation;
  171. this.pointer.rotationQuaternion!.set(orientation.x, orientation.y, orientation.z, orientation.w);
  172. if (!this._scene.useRightHandedSystem) {
  173. this.pointer.position.z *= -1;
  174. this.pointer.rotationQuaternion!.z *= -1;
  175. this.pointer.rotationQuaternion!.w *= -1;
  176. }
  177. }
  178. // Update the grip mesh if it exists
  179. if (this.inputSource.gripSpace && this.grip) {
  180. let pose = xrFrame.getPose(this.inputSource.gripSpace, referenceSpace);
  181. if (pose) {
  182. const pos = pose.transform.position;
  183. const orientation = pose.transform.orientation;
  184. this.grip.position.set(pos.x, pos.y, pos.z);
  185. this.grip.rotationQuaternion!.set(orientation.x, orientation.y, orientation.z, orientation.w);
  186. if (!this._scene.useRightHandedSystem) {
  187. this.grip.position.z *= -1;
  188. this.grip.rotationQuaternion!.z *= -1;
  189. this.grip.rotationQuaternion!.w *= -1;
  190. }
  191. }
  192. }
  193. if (this.motionController) {
  194. // either update buttons only or also position, if in gamepad mode
  195. this.motionController.updateFromXRFrame(xrFrame);
  196. }
  197. }
  198. }