babylon.poseEnabledController.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. module BABYLON {
  2. /**
  3. * Defines the types of pose enabled controllers that are supported
  4. */
  5. export enum PoseEnabledControllerType {
  6. /**
  7. * HTC Vive
  8. */
  9. VIVE,
  10. /**
  11. * Oculus Rift
  12. */
  13. OCULUS,
  14. /**
  15. * Windows mixed reality
  16. */
  17. WINDOWS,
  18. /**
  19. * Samsung gear VR
  20. */
  21. GEAR_VR,
  22. /**
  23. * Google Daydream
  24. */
  25. DAYDREAM,
  26. /**
  27. * Generic
  28. */
  29. GENERIC
  30. }
  31. /**
  32. * Defines the MutableGamepadButton interface for the state of a gamepad button
  33. */
  34. export interface MutableGamepadButton {
  35. /**
  36. * Value of the button/trigger
  37. */
  38. value: number;
  39. /**
  40. * If the button/trigger is currently touched
  41. */
  42. touched: boolean;
  43. /**
  44. * If the button/trigger is currently pressed
  45. */
  46. pressed: boolean;
  47. }
  48. /**
  49. * Defines the ExtendedGamepadButton interface for a gamepad button which includes state provided by a pose controller
  50. * @ignore
  51. */
  52. export interface ExtendedGamepadButton extends GamepadButton {
  53. /**
  54. * If the button/trigger is currently pressed
  55. */
  56. readonly pressed: boolean;
  57. /**
  58. * If the button/trigger is currently touched
  59. */
  60. readonly touched: boolean;
  61. /**
  62. * Value of the button/trigger
  63. */
  64. readonly value: number;
  65. }
  66. /**
  67. * Defines the PoseEnabledControllerHelper object that is used initialize a gamepad as the controller type it is specified as (eg. windows mixed reality controller)
  68. */
  69. export class PoseEnabledControllerHelper {
  70. /**
  71. * Initializes a gamepad as the controller type it is specified as (eg. windows mixed reality controller)
  72. * @param vrGamepad the gamepad to initialized
  73. * @returns a vr controller of the type the gamepad identified as
  74. */
  75. public static InitiateController(vrGamepad: any) {
  76. // Oculus Touch
  77. if (vrGamepad.id.indexOf('Oculus Touch') !== -1) {
  78. return new OculusTouchController(vrGamepad);
  79. }
  80. // Windows Mixed Reality controllers
  81. else if (vrGamepad.id.indexOf(WindowsMotionController.GAMEPAD_ID_PREFIX) === 0) {
  82. return new WindowsMotionController(vrGamepad);
  83. }
  84. // HTC Vive
  85. else if (vrGamepad.id.toLowerCase().indexOf('openvr') !== -1) {
  86. return new ViveController(vrGamepad);
  87. }
  88. // Samsung/Oculus Gear VR
  89. else if (vrGamepad.id.indexOf(GearVRController.GAMEPAD_ID_PREFIX) === 0) {
  90. return new GearVRController(vrGamepad);
  91. }
  92. // Google Daydream
  93. else if (vrGamepad.id.indexOf(DaydreamController.GAMEPAD_ID_PREFIX) === 0) {
  94. return new DaydreamController(vrGamepad);
  95. }
  96. // Generic
  97. else {
  98. return new GenericController(vrGamepad);
  99. }
  100. }
  101. }
  102. /**
  103. * Defines the PoseEnabledController object that contains state of a vr capable controller
  104. */
  105. export class PoseEnabledController extends Gamepad implements PoseControlled {
  106. // Represents device position and rotation in room space. Should only be used to help calculate babylon space values
  107. private _deviceRoomPosition = Vector3.Zero();
  108. private _deviceRoomRotationQuaternion = new Quaternion();
  109. /**
  110. * The device position in babylon space
  111. */
  112. public devicePosition = Vector3.Zero();
  113. /**
  114. * The device rotation in babylon space
  115. */
  116. public deviceRotationQuaternion = new Quaternion();
  117. /**
  118. * The scale factor of the device in babylon space
  119. */
  120. public deviceScaleFactor: number = 1;
  121. /**
  122. * (Likely devicePosition should be used instead) The device position in its room space
  123. */
  124. public position: Vector3;
  125. /**
  126. * (Likely deviceRotationQuaternion should be used instead) The device rotation in its room space
  127. */
  128. public rotationQuaternion: Quaternion;
  129. /**
  130. * The type of controller (Eg. Windows mixed reality)
  131. */
  132. public controllerType: PoseEnabledControllerType;
  133. private _calculatedPosition: Vector3;
  134. private _calculatedRotation: Quaternion;
  135. /**
  136. * The raw pose from the device
  137. */
  138. public rawPose: DevicePose; //GamepadPose;
  139. /**
  140. * Internal, the mesh attached to the controller
  141. */
  142. public _mesh: Nullable<AbstractMesh>; // a node that will be attached to this Gamepad
  143. private _poseControlledCamera: TargetCamera;
  144. private _leftHandSystemQuaternion: Quaternion = new Quaternion();
  145. /**
  146. * Internal, matrix used to convert room space to babylon space
  147. */
  148. public _deviceToWorld = Matrix.Identity();
  149. /**
  150. * Node to be used when casting a ray from the controller
  151. */
  152. public _pointingPoseNode:Nullable<AbstractMesh> = null;
  153. /**
  154. * Name of the child mesh that can be used to cast a ray from the controller
  155. */
  156. public static readonly POINTING_POSE = "POINTING_POSE";
  157. /**
  158. * Creates a new PoseEnabledController from a gamepad
  159. * @param browserGamepad the gamepad that the PoseEnabledController should be created from
  160. */
  161. constructor(browserGamepad: any) {
  162. super(browserGamepad.id, browserGamepad.index, browserGamepad);
  163. this.type = Gamepad.POSE_ENABLED;
  164. this.controllerType = PoseEnabledControllerType.GENERIC;
  165. this.position = Vector3.Zero();
  166. this.rotationQuaternion = new Quaternion();
  167. this._calculatedPosition = Vector3.Zero();
  168. this._calculatedRotation = new Quaternion();
  169. Quaternion.RotationYawPitchRollToRef(Math.PI, 0, 0, this._leftHandSystemQuaternion);
  170. }
  171. private _workingMatrix = Matrix.Identity();
  172. /**
  173. * Updates the state of the pose enbaled controller and mesh based on the current position and rotation of the controller
  174. */
  175. public update() {
  176. super.update();
  177. var pose: GamepadPose = this.browserGamepad.pose;
  178. this.updateFromDevice(pose);
  179. Vector3.TransformCoordinatesToRef(this._calculatedPosition, this._deviceToWorld, this.devicePosition)
  180. this._deviceToWorld.getRotationMatrixToRef(this._workingMatrix);
  181. Quaternion.FromRotationMatrixToRef(this._workingMatrix, this.deviceRotationQuaternion);
  182. this.deviceRotationQuaternion.multiplyInPlace(this._calculatedRotation)
  183. if (this._mesh) {
  184. this._mesh.position.copyFrom(this.devicePosition);
  185. if (this._mesh.rotationQuaternion) {
  186. this._mesh.rotationQuaternion.copyFrom(this.deviceRotationQuaternion);
  187. }
  188. }
  189. }
  190. /**
  191. * Updates the state of the pose enbaled controller based on the raw pose data from the device
  192. * @param poseData raw pose fromthe device
  193. */
  194. updateFromDevice(poseData: DevicePose) {
  195. if (poseData) {
  196. this.rawPose = poseData;
  197. if (poseData.position) {
  198. this._deviceRoomPosition.copyFromFloats(poseData.position[0], poseData.position[1], -poseData.position[2]);
  199. if (this._mesh && this._mesh.getScene().useRightHandedSystem) {
  200. this._deviceRoomPosition.z *= -1;
  201. }
  202. this._deviceRoomPosition.scaleToRef(this.deviceScaleFactor, this._calculatedPosition);
  203. this._calculatedPosition.addInPlace(this.position);
  204. }
  205. let pose = this.rawPose;
  206. if (poseData.orientation && pose.orientation) {
  207. this._deviceRoomRotationQuaternion.copyFromFloats(pose.orientation[0], pose.orientation[1], -pose.orientation[2], -pose.orientation[3]);
  208. if (this._mesh) {
  209. if (this._mesh.getScene().useRightHandedSystem) {
  210. this._deviceRoomRotationQuaternion.z *= -1;
  211. this._deviceRoomRotationQuaternion.w *= -1;
  212. } else {
  213. this._deviceRoomRotationQuaternion.multiplyToRef(this._leftHandSystemQuaternion, this._deviceRoomRotationQuaternion);
  214. }
  215. }
  216. // if the camera is set, rotate to the camera's rotation
  217. this._deviceRoomRotationQuaternion.multiplyToRef(this.rotationQuaternion, this._calculatedRotation);
  218. }
  219. }
  220. }
  221. /**
  222. * Attaches a mesh to the controller
  223. * @param mesh the mesh to be attached
  224. */
  225. public attachToMesh(mesh: AbstractMesh) {
  226. if (this._mesh) {
  227. this._mesh.parent = null;
  228. }
  229. this._mesh = mesh;
  230. if (this._poseControlledCamera) {
  231. this._mesh.parent = this._poseControlledCamera;
  232. }
  233. if (!this._mesh.rotationQuaternion) {
  234. this._mesh.rotationQuaternion = new Quaternion();
  235. }
  236. }
  237. /**
  238. * Attaches the controllers mesh to a camera
  239. * @param camera the camera the mesh should be attached to
  240. */
  241. public attachToPoseControlledCamera(camera: TargetCamera) {
  242. this._poseControlledCamera = camera;
  243. if (this._mesh) {
  244. this._mesh.parent = this._poseControlledCamera;
  245. }
  246. }
  247. /**
  248. * Disposes of the controller
  249. */
  250. public dispose() {
  251. if (this._mesh) {
  252. this._mesh.dispose();
  253. }
  254. this._mesh = null;
  255. super.dispose();
  256. }
  257. /**
  258. * The mesh that is attached to the controller
  259. */
  260. public get mesh(): Nullable<AbstractMesh> {
  261. return this._mesh;
  262. }
  263. /**
  264. * Gets the ray of the controller in the direction the controller is pointing
  265. * @param length the length the resulting ray should be
  266. * @returns a ray in the direction the controller is pointing
  267. */
  268. public getForwardRay(length = 100): Ray {
  269. if (!this.mesh) {
  270. return new Ray(Vector3.Zero(), new Vector3(0, 0, 1), length);
  271. }
  272. var m = this._pointingPoseNode ? this._pointingPoseNode.getWorldMatrix() : this.mesh.getWorldMatrix();
  273. var origin = m.getTranslation();
  274. var forward = new Vector3(0, 0, -1);
  275. var forwardWorld = Vector3.TransformNormal(forward, m);
  276. var direction = Vector3.Normalize(forwardWorld);
  277. return new Ray(origin, direction, length);
  278. }
  279. }
  280. }