babylon.poseEnabledController.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. * @hidden
  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 or Oculus Go
  89. else if (vrGamepad.id.indexOf(GearVRController.GAMEPAD_ID_PREFIX) === 0 || vrGamepad.id.indexOf('Oculus Go') !== -1) {
  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. protected _calculatedPosition: Vector3;
  134. private _calculatedRotation: Quaternion;
  135. /**
  136. * The raw pose from the device
  137. */
  138. public rawPose: DevicePose; //GamepadPose;
  139. // Used to convert 6dof controllers to 3dof
  140. private _trackPosition = true;
  141. private _maxRotationDistFromHeadset = Math.PI / 5;
  142. private _draggedRoomRotation = 0;
  143. /**
  144. * @hidden
  145. */
  146. public _disableTrackPosition(fixedPosition: Vector3) {
  147. if (this._trackPosition) {
  148. this._calculatedPosition.copyFrom(fixedPosition);
  149. this._trackPosition = false;
  150. }
  151. }
  152. /**
  153. * Internal, the mesh attached to the controller
  154. * @hidden
  155. */
  156. public _mesh: Nullable<AbstractMesh>; // a node that will be attached to this Gamepad
  157. private _poseControlledCamera: TargetCamera;
  158. private _leftHandSystemQuaternion: Quaternion = new Quaternion();
  159. /**
  160. * Internal, matrix used to convert room space to babylon space
  161. * @hidden
  162. */
  163. public _deviceToWorld = Matrix.Identity();
  164. /**
  165. * Node to be used when casting a ray from the controller
  166. * @hidden
  167. */
  168. public _pointingPoseNode: Nullable<TransformNode> = null;
  169. /**
  170. * Name of the child mesh that can be used to cast a ray from the controller
  171. */
  172. public static readonly POINTING_POSE = "POINTING_POSE";
  173. /**
  174. * Creates a new PoseEnabledController from a gamepad
  175. * @param browserGamepad the gamepad that the PoseEnabledController should be created from
  176. */
  177. constructor(browserGamepad: any) {
  178. super(browserGamepad.id, browserGamepad.index, browserGamepad);
  179. this.type = Gamepad.POSE_ENABLED;
  180. this.controllerType = PoseEnabledControllerType.GENERIC;
  181. this.position = Vector3.Zero();
  182. this.rotationQuaternion = new Quaternion();
  183. this._calculatedPosition = Vector3.Zero();
  184. this._calculatedRotation = new Quaternion();
  185. Quaternion.RotationYawPitchRollToRef(Math.PI, 0, 0, this._leftHandSystemQuaternion);
  186. }
  187. private _workingMatrix = Matrix.Identity();
  188. /**
  189. * Updates the state of the pose enbaled controller and mesh based on the current position and rotation of the controller
  190. */
  191. public update() {
  192. super.update();
  193. this._updatePoseAndMesh();
  194. }
  195. /**
  196. * Updates only the pose device and mesh without doing any button event checking
  197. */
  198. protected _updatePoseAndMesh() {
  199. var pose: GamepadPose = this.browserGamepad.pose;
  200. this.updateFromDevice(pose);
  201. if (!this._trackPosition && BABYLON.Engine.LastCreatedScene && BABYLON.Engine.LastCreatedScene.activeCamera && (<WebVRFreeCamera>BABYLON.Engine.LastCreatedScene.activeCamera).devicePosition) {
  202. var camera = <WebVRFreeCamera>BABYLON.Engine.LastCreatedScene.activeCamera;
  203. camera._computeDevicePosition();
  204. this._deviceToWorld.setTranslation(camera.devicePosition);
  205. if (camera.deviceRotationQuaternion) {
  206. var camera = camera;
  207. camera._deviceRoomRotationQuaternion.toEulerAnglesToRef(BABYLON.Tmp.Vector3[0]);
  208. // Find the radian distance away that the headset is from the controllers rotation
  209. var distanceAway = Math.atan2(Math.sin(BABYLON.Tmp.Vector3[0].y - this._draggedRoomRotation), Math.cos(BABYLON.Tmp.Vector3[0].y - this._draggedRoomRotation));
  210. if (Math.abs(distanceAway) > this._maxRotationDistFromHeadset) {
  211. // Only rotate enouph to be within the _maxRotationDistFromHeadset
  212. var rotationAmount = distanceAway - (distanceAway < 0 ? -this._maxRotationDistFromHeadset : this._maxRotationDistFromHeadset);
  213. this._draggedRoomRotation += rotationAmount;
  214. // Rotate controller around headset
  215. var sin = Math.sin(-rotationAmount);
  216. var cos = Math.cos(-rotationAmount);
  217. this._calculatedPosition.x = this._calculatedPosition.x * cos - this._calculatedPosition.z * sin;
  218. this._calculatedPosition.z = this._calculatedPosition.x * sin + this._calculatedPosition.z * cos;
  219. }
  220. }
  221. }
  222. Vector3.TransformCoordinatesToRef(this._calculatedPosition, this._deviceToWorld, this.devicePosition);
  223. this._deviceToWorld.getRotationMatrixToRef(this._workingMatrix);
  224. Quaternion.FromRotationMatrixToRef(this._workingMatrix, this.deviceRotationQuaternion);
  225. this.deviceRotationQuaternion.multiplyInPlace(this._calculatedRotation);
  226. if (this._mesh) {
  227. this._mesh.position.copyFrom(this.devicePosition);
  228. if (this._mesh.rotationQuaternion) {
  229. this._mesh.rotationQuaternion.copyFrom(this.deviceRotationQuaternion);
  230. }
  231. }
  232. }
  233. /**
  234. * Updates the state of the pose enbaled controller based on the raw pose data from the device
  235. * @param poseData raw pose fromthe device
  236. */
  237. updateFromDevice(poseData: DevicePose) {
  238. if (poseData) {
  239. this.rawPose = poseData;
  240. if (poseData.position) {
  241. this._deviceRoomPosition.copyFromFloats(poseData.position[0], poseData.position[1], -poseData.position[2]);
  242. if (this._mesh && this._mesh.getScene().useRightHandedSystem) {
  243. this._deviceRoomPosition.z *= -1;
  244. }
  245. if (this._trackPosition) {
  246. this._deviceRoomPosition.scaleToRef(this.deviceScaleFactor, this._calculatedPosition);
  247. }
  248. this._calculatedPosition.addInPlace(this.position);
  249. }
  250. let pose = this.rawPose;
  251. if (poseData.orientation && pose.orientation) {
  252. this._deviceRoomRotationQuaternion.copyFromFloats(pose.orientation[0], pose.orientation[1], -pose.orientation[2], -pose.orientation[3]);
  253. if (this._mesh) {
  254. if (this._mesh.getScene().useRightHandedSystem) {
  255. this._deviceRoomRotationQuaternion.z *= -1;
  256. this._deviceRoomRotationQuaternion.w *= -1;
  257. } else {
  258. this._deviceRoomRotationQuaternion.multiplyToRef(this._leftHandSystemQuaternion, this._deviceRoomRotationQuaternion);
  259. }
  260. }
  261. // if the camera is set, rotate to the camera's rotation
  262. this._deviceRoomRotationQuaternion.multiplyToRef(this.rotationQuaternion, this._calculatedRotation);
  263. }
  264. }
  265. }
  266. /**
  267. * @hidden
  268. */
  269. public _meshAttachedObservable = new Observable<AbstractMesh>();
  270. /**
  271. * Attaches a mesh to the controller
  272. * @param mesh the mesh to be attached
  273. */
  274. public attachToMesh(mesh: AbstractMesh) {
  275. if (this._mesh) {
  276. this._mesh.parent = null;
  277. }
  278. this._mesh = mesh;
  279. if (this._poseControlledCamera) {
  280. this._mesh.parent = this._poseControlledCamera;
  281. }
  282. if (!this._mesh.rotationQuaternion) {
  283. this._mesh.rotationQuaternion = new Quaternion();
  284. }
  285. // Sync controller mesh and pointing pose node's state with controller, this is done to avoid a frame where position is 0,0,0 when attaching mesh
  286. this._updatePoseAndMesh();
  287. if (this._pointingPoseNode) {
  288. var parents = [];
  289. var obj: Node = this._pointingPoseNode;
  290. while (obj.parent) {
  291. parents.push(obj.parent);
  292. obj = obj.parent;
  293. }
  294. parents.reverse().forEach((p) => { p.computeWorldMatrix(true); });
  295. }
  296. this._meshAttachedObservable.notifyObservers(mesh);
  297. }
  298. /**
  299. * Attaches the controllers mesh to a camera
  300. * @param camera the camera the mesh should be attached to
  301. */
  302. public attachToPoseControlledCamera(camera: TargetCamera) {
  303. this._poseControlledCamera = camera;
  304. if (this._mesh) {
  305. this._mesh.parent = this._poseControlledCamera;
  306. }
  307. }
  308. /**
  309. * Disposes of the controller
  310. */
  311. public dispose() {
  312. if (this._mesh) {
  313. this._mesh.dispose();
  314. }
  315. this._mesh = null;
  316. super.dispose();
  317. }
  318. /**
  319. * The mesh that is attached to the controller
  320. */
  321. public get mesh(): Nullable<AbstractMesh> {
  322. return this._mesh;
  323. }
  324. /**
  325. * Gets the ray of the controller in the direction the controller is pointing
  326. * @param length the length the resulting ray should be
  327. * @returns a ray in the direction the controller is pointing
  328. */
  329. public getForwardRay(length = 100): Ray {
  330. if (!this.mesh) {
  331. return new Ray(Vector3.Zero(), new Vector3(0, 0, 1), length);
  332. }
  333. var m = this._pointingPoseNode ? this._pointingPoseNode.getWorldMatrix() : this.mesh.getWorldMatrix();
  334. var origin = m.getTranslation();
  335. var forward = new Vector3(0, 0, -1);
  336. var forwardWorld = Vector3.TransformNormal(forward, m);
  337. var direction = Vector3.Normalize(forwardWorld);
  338. return new Ray(origin, direction, length);
  339. }
  340. }
  341. }