WebXRControllerMovement.d.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import type { WebXRSessionManager } from "../webXRSessionManager";
  2. import type { Nullable } from "../../types";
  3. import type { WebXRInput } from "../webXRInput";
  4. import type { WebXRInputSource } from "../webXRInputSource";
  5. import type { IWebXRMotionControllerAxesValue, IWebXRMotionControllerComponentChangesValues } from "../motionController/webXRControllerComponent";
  6. import { WebXRControllerComponent } from "../motionController/webXRControllerComponent";
  7. import { Quaternion } from "../../Maths/math.vector";
  8. import { WebXRAbstractFeature } from "./WebXRAbstractFeature";
  9. import type { MotionControllerComponentType } from "../motionController/webXRAbstractMotionController";
  10. /**
  11. * The options container for the controller movement module
  12. */
  13. export interface IWebXRControllerMovementOptions {
  14. /**
  15. * Override default behaviour and provide your own movement controls
  16. */
  17. customRegistrationConfigurations?: WebXRControllerMovementRegistrationConfiguration[];
  18. /**
  19. * Is movement enabled
  20. */
  21. movementEnabled?: boolean;
  22. /**
  23. * Camera direction follows view pose and movement by default will move independently of the viewer's pose.
  24. */
  25. movementOrientationFollowsViewerPose: boolean;
  26. /**
  27. * Movement speed factor (default is 1.0)
  28. */
  29. movementSpeed?: number;
  30. /**
  31. * Minimum threshold the controller's thumbstick/touchpad must pass before being recognized for movement (avoids jitter/unintentional movement)
  32. */
  33. movementThreshold?: number;
  34. /**
  35. * Is rotation enabled
  36. */
  37. rotationEnabled?: boolean;
  38. /**
  39. * Minimum threshold the controller's thumstick/touchpad must pass before being recognized for rotation (avoids jitter/unintentional rotation)
  40. */
  41. rotationThreshold?: number;
  42. /**
  43. * Movement speed factor (default is 1.0)
  44. */
  45. rotationSpeed?: number;
  46. /**
  47. * Babylon XR Input class for controller
  48. */
  49. xrInput: WebXRInput;
  50. }
  51. /**
  52. * Feature context is used in handlers and on each XR frame to control the camera movement/direction.
  53. */
  54. export type WebXRControllerMovementFeatureContext = {
  55. movementEnabled: boolean;
  56. movementOrientationFollowsViewerPose: boolean;
  57. movementSpeed: number;
  58. movementThreshold: number;
  59. rotationEnabled: boolean;
  60. rotationSpeed: number;
  61. rotationThreshold: number;
  62. };
  63. /**
  64. * Current state of Movements shared across components and handlers.
  65. */
  66. export type WebXRControllerMovementState = {
  67. moveX: number;
  68. moveY: number;
  69. rotateX: number;
  70. rotateY: number;
  71. };
  72. /**
  73. * Button of Axis Handler must be specified.
  74. */
  75. export type WebXRControllerMovementRegistrationConfiguration = {
  76. /**
  77. * handlers are filtered to these types only
  78. */
  79. allowedComponentTypes?: MotionControllerComponentType[];
  80. /**
  81. * For registering movement to specific hand only. Useful if your app has a "main hand" and "off hand" for determining the functionality of a controller.
  82. */
  83. forceHandedness?: XRHandedness;
  84. /**
  85. * For main component only (useful for buttons and may not trigger axis changes).
  86. */
  87. mainComponentOnly?: boolean;
  88. /**
  89. * Additional predicate to apply to controllers to restrict a handler being added.
  90. */
  91. componentSelectionPredicate?: (xrController: WebXRInputSource) => Nullable<WebXRControllerComponent>;
  92. } & ({
  93. /**
  94. * Called when axis changes occur.
  95. */
  96. axisChangedHandler: (axes: IWebXRMotionControllerAxesValue, movementState: WebXRControllerMovementState, featureContext: WebXRControllerMovementFeatureContext, xrInput: WebXRInput) => void;
  97. } | {
  98. /**
  99. * Called when the button state changes.
  100. */
  101. buttonChangedhandler: (pressed: IWebXRMotionControllerComponentChangesValues<boolean>, movementState: WebXRControllerMovementState, featureContext: WebXRControllerMovementFeatureContext, xrInput: WebXRInput) => void;
  102. });
  103. /**
  104. * This is a movement feature to be used with WebXR-enabled motion controllers.
  105. * When enabled and attached, the feature will allow a user to move around and rotate in the scene using
  106. * the input of the attached controllers.
  107. */
  108. export declare class WebXRControllerMovement extends WebXRAbstractFeature {
  109. private _controllers;
  110. private _currentRegistrationConfigurations;
  111. private _featureContext;
  112. private _movementDirection;
  113. private _movementState;
  114. private _xrInput;
  115. private _tmpRotationMatrix;
  116. private _tmpTranslationDirection;
  117. private _tmpMovementTranslation;
  118. private _tempCacheQuaternion;
  119. /**
  120. * The module's name
  121. */
  122. static readonly Name = "xr-controller-movement";
  123. /**
  124. * Standard controller configurations.
  125. */
  126. static readonly REGISTRATIONS: {
  127. [key: string]: WebXRControllerMovementRegistrationConfiguration[];
  128. };
  129. /**
  130. * The (Babylon) version of this module.
  131. * This is an integer representing the implementation version.
  132. * This number does not correspond to the webxr specs version
  133. */
  134. static readonly Version = 1;
  135. /**
  136. * Current movement direction. Will be null before XR Frames have been processed.
  137. */
  138. get movementDirection(): Quaternion;
  139. /**
  140. * Is movement enabled
  141. */
  142. get movementEnabled(): boolean;
  143. /**
  144. * Sets whether movement is enabled or not
  145. * @param enabled is movement enabled
  146. */
  147. set movementEnabled(enabled: boolean);
  148. /**
  149. * If movement follows viewer pose
  150. */
  151. get movementOrientationFollowsViewerPose(): boolean;
  152. /**
  153. * Sets whether movement follows viewer pose
  154. * @param followsPose is movement should follow viewer pose
  155. */
  156. set movementOrientationFollowsViewerPose(followsPose: boolean);
  157. /**
  158. * Gets movement speed
  159. */
  160. get movementSpeed(): number;
  161. /**
  162. * Sets movement speed
  163. * @param movementSpeed movement speed
  164. */
  165. set movementSpeed(movementSpeed: number);
  166. /**
  167. * Gets minimum threshold the controller's thumbstick/touchpad must pass before being recognized for movement (avoids jitter/unintentional movement)
  168. */
  169. get movementThreshold(): number;
  170. /**
  171. * Sets minimum threshold the controller's thumbstick/touchpad must pass before being recognized for movement (avoids jitter/unintentional movement)
  172. * @param movementThreshold new threshold
  173. */
  174. set movementThreshold(movementThreshold: number);
  175. /**
  176. * Is rotation enabled
  177. */
  178. get rotationEnabled(): boolean;
  179. /**
  180. * Sets whether rotation is enabled or not
  181. * @param enabled is rotation enabled
  182. */
  183. set rotationEnabled(enabled: boolean);
  184. /**
  185. * Gets rotation speed factor
  186. */
  187. get rotationSpeed(): number;
  188. /**
  189. * Sets rotation speed factor (1.0 is default)
  190. * @param rotationSpeed new rotation speed factor
  191. */
  192. set rotationSpeed(rotationSpeed: number);
  193. /**
  194. * Gets minimum threshold the controller's thumbstick/touchpad must pass before being recognized for rotation (avoids jitter/unintentional rotation)
  195. */
  196. get rotationThreshold(): number;
  197. /**
  198. * Sets minimum threshold the controller's thumbstick/touchpad must pass before being recognized for rotation (avoids jitter/unintentional rotation)
  199. * @param threshold new threshold
  200. */
  201. set rotationThreshold(threshold: number);
  202. /**
  203. * constructs a new movement controller system
  204. * @param _xrSessionManager an instance of WebXRSessionManager
  205. * @param options configuration object for this feature
  206. */
  207. constructor(_xrSessionManager: WebXRSessionManager, options: IWebXRControllerMovementOptions);
  208. attach(): boolean;
  209. detach(): boolean;
  210. /**
  211. * Occurs on every XR frame.
  212. * @param _xrFrame
  213. */
  214. protected _onXRFrame(_xrFrame: XRFrame): void;
  215. private _attachController;
  216. private _detachController;
  217. }