webXROculusTouchMotionController.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import {
  2. WebXRAbstractMotionController,
  3. IMinimalMotionControllerObject,
  4. MotionControllerHandness,
  5. IMotionControllerLayoutMap
  6. } from "./webXRAbstractMotionController";
  7. import { WebXRMotionControllerManager } from './webXRMotionControllerManager';
  8. import { AbstractMesh } from '../../Meshes/abstractMesh';
  9. import { Scene } from '../../scene';
  10. import { Mesh } from '../../Meshes/mesh';
  11. import { Quaternion } from '../../Maths/math.vector';
  12. /**
  13. * The motion controller class for oculus touch (quest, rift).
  14. * This class supports legacy mapping as well the standard xr mapping
  15. */
  16. export class WebXROculusTouchMotionController extends WebXRAbstractMotionController {
  17. private _modelRootNode: AbstractMesh;
  18. /**
  19. * The base url used to load the left and right controller models
  20. */
  21. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/oculus/';
  22. /**
  23. * The name of the left controller model file
  24. */
  25. public static MODEL_LEFT_FILENAME: string = 'left.babylon';
  26. /**
  27. * The name of the right controller model file
  28. */
  29. public static MODEL_RIGHT_FILENAME: string = 'right.babylon';
  30. /**
  31. * Base Url for the Quest controller model.
  32. */
  33. public static QUEST_MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/oculusQuest/';
  34. public profileId = "oculus-touch";
  35. constructor(scene: Scene,
  36. gamepadObject: IMinimalMotionControllerObject,
  37. handness: MotionControllerHandness,
  38. legacyMapping: boolean = false,
  39. private _forceLegacyControllers: boolean = false) {
  40. super(scene, OculusTouchLayouts[handness], gamepadObject, handness);
  41. }
  42. protected _getFilenameAndPath(): { filename: string; path: string; } {
  43. let filename = "";
  44. if (this.handness === 'left') {
  45. filename = WebXROculusTouchMotionController.MODEL_LEFT_FILENAME;
  46. }
  47. else { // Right is the default if no hand is specified
  48. filename = WebXROculusTouchMotionController.MODEL_RIGHT_FILENAME;
  49. }
  50. let path = this._isQuest() ? WebXROculusTouchMotionController.QUEST_MODEL_BASE_URL : WebXROculusTouchMotionController.MODEL_BASE_URL;
  51. return {
  52. filename,
  53. path
  54. };
  55. }
  56. protected _getModelLoadingConstraints(): boolean {
  57. return true;
  58. }
  59. protected _processLoadedModel(_meshes: AbstractMesh[]): void {
  60. const isQuest = this._isQuest();
  61. const triggerDirection = this.handness === 'right' ? -1 : 1;
  62. this.getComponentIds().forEach((id) => {
  63. const comp = id && this.getComponent(id);
  64. if (comp) {
  65. comp.onButtonStateChangedObservable.add((component) => {
  66. if (!this.rootMesh || this.disableAnimation) { return; }
  67. switch (id) {
  68. case "xr-standard-trigger": // index trigger
  69. if (!isQuest) {
  70. (<AbstractMesh>(this._modelRootNode.getChildren()[3])).rotation.x = -component.value * 0.20;
  71. (<AbstractMesh>(this._modelRootNode.getChildren()[3])).position.y = -component.value * 0.005;
  72. (<AbstractMesh>(this._modelRootNode.getChildren()[3])).position.z = -component.value * 0.005;
  73. }
  74. return;
  75. case "xr-standard-squeeze": // secondary trigger
  76. if (!isQuest) {
  77. (<AbstractMesh>(this._modelRootNode.getChildren()[4])).position.x = triggerDirection * component.value * 0.0035;
  78. }
  79. return;
  80. case "xr-standard-thumbstick": // thumbstick
  81. return;
  82. case "a-button":
  83. case "x-button":
  84. if (!isQuest) {
  85. if (component.pressed) {
  86. (<AbstractMesh>(this._modelRootNode.getChildren()[1])).position.y = -0.001;
  87. }
  88. else {
  89. (<AbstractMesh>(this._modelRootNode.getChildren()[1])).position.y = 0;
  90. }
  91. }
  92. return;
  93. case "b-button":
  94. case "y-button":
  95. if (!isQuest) {
  96. if (component.pressed) {
  97. (<AbstractMesh>(this._modelRootNode.getChildren()[2])).position.y = -0.001;
  98. }
  99. else {
  100. (<AbstractMesh>(this._modelRootNode.getChildren()[2])).position.y = 0;
  101. }
  102. }
  103. return;
  104. }
  105. }, undefined, true);
  106. }
  107. });
  108. }
  109. protected _setRootMesh(meshes: AbstractMesh[]): void {
  110. this.rootMesh = new Mesh(this.profileId + " " + this.handness, this.scene);
  111. this.rootMesh.rotationQuaternion = Quaternion.FromEulerAngles(0, Math.PI, 0);
  112. meshes.forEach((mesh) => { mesh.isPickable = false; });
  113. if (this._isQuest()) {
  114. this._modelRootNode = meshes[0];
  115. } else {
  116. this._modelRootNode = meshes[1];
  117. this.rootMesh.position.y = 0.034;
  118. this.rootMesh.position.z = 0.052;
  119. }
  120. this._modelRootNode.parent = this.rootMesh;
  121. }
  122. protected _updateModel(): void {
  123. // no-op. model is updated using observables.
  124. }
  125. /**
  126. * Is this the new type of oculus touch. At the moment both have the same profile and it is impossible to differentiate
  127. * between the touch and touch 2.
  128. */
  129. private _isQuest() {
  130. // this is SADLY the only way to currently check. Until proper profiles will be available.
  131. return !!navigator.userAgent.match(/Quest/gi) && !this._forceLegacyControllers;
  132. }
  133. }
  134. // register the profile
  135. WebXRMotionControllerManager.RegisterController("oculus-touch", (xrInput: XRInputSource, scene: Scene) => {
  136. return new WebXROculusTouchMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness);
  137. });
  138. WebXRMotionControllerManager.RegisterController("oculus-touch-legacy", (xrInput: XRInputSource, scene: Scene) => {
  139. return new WebXROculusTouchMotionController(scene, <any>(xrInput.gamepad), xrInput.handedness, true);
  140. });
  141. const OculusTouchLayouts: IMotionControllerLayoutMap = {
  142. "left": {
  143. "selectComponentId": "xr-standard-trigger",
  144. "components": {
  145. "xr-standard-trigger": {
  146. "type": "trigger",
  147. "gamepadIndices": {
  148. "button": 0
  149. },
  150. "rootNodeName": "xr_standard_trigger",
  151. "visualResponses": {
  152. }
  153. },
  154. "xr-standard-squeeze": {
  155. "type": "squeeze",
  156. "gamepadIndices": {
  157. "button": 1
  158. },
  159. "rootNodeName": "xr_standard_squeeze",
  160. "visualResponses": {
  161. }
  162. },
  163. "xr-standard-thumbstick": {
  164. "type": "thumbstick",
  165. "gamepadIndices": {
  166. "button": 3,
  167. "xAxis": 2,
  168. "yAxis": 3
  169. },
  170. "rootNodeName": "xr_standard_thumbstick",
  171. "visualResponses": {
  172. }
  173. },
  174. "x-button": {
  175. "type": "button",
  176. "gamepadIndices": {
  177. "button": 4
  178. },
  179. "rootNodeName": "x_button",
  180. "visualResponses": {
  181. }
  182. },
  183. "y-button": {
  184. "type": "button",
  185. "gamepadIndices": {
  186. "button": 5
  187. },
  188. "rootNodeName": "y_button",
  189. "visualResponses": {
  190. }
  191. },
  192. "thumbrest": {
  193. "type": "button",
  194. "gamepadIndices": {
  195. "button": 6
  196. },
  197. "rootNodeName": "thumbrest",
  198. "visualResponses": {
  199. }
  200. }
  201. },
  202. "gamepadMapping": "xr-standard",
  203. "rootNodeName": "oculus-touch-v2-left",
  204. "assetPath": "left.glb"
  205. },
  206. "right": {
  207. "selectComponentId": "xr-standard-trigger",
  208. "components": {
  209. "xr-standard-trigger": {
  210. "type": "trigger",
  211. "gamepadIndices": {
  212. "button": 0
  213. },
  214. "rootNodeName": "xr_standard_trigger",
  215. "visualResponses": {
  216. }
  217. },
  218. "xr-standard-squeeze": {
  219. "type": "squeeze",
  220. "gamepadIndices": {
  221. "button": 1
  222. },
  223. "rootNodeName": "xr_standard_squeeze",
  224. "visualResponses": {
  225. }
  226. },
  227. "xr-standard-thumbstick": {
  228. "type": "thumbstick",
  229. "gamepadIndices": {
  230. "button": 3,
  231. "xAxis": 2,
  232. "yAxis": 3
  233. },
  234. "rootNodeName": "xr_standard_thumbstick",
  235. "visualResponses": {
  236. }
  237. },
  238. "a-button": {
  239. "type": "button",
  240. "gamepadIndices": {
  241. "button": 4
  242. },
  243. "rootNodeName": "a_button",
  244. "visualResponses": {
  245. }
  246. },
  247. "b-button": {
  248. "type": "button",
  249. "gamepadIndices": {
  250. "button": 5
  251. },
  252. "rootNodeName": "b_button",
  253. "visualResponses": {
  254. }
  255. },
  256. "thumbrest": {
  257. "type": "button",
  258. "gamepadIndices": {
  259. "button": 6
  260. },
  261. "rootNodeName": "thumbrest",
  262. "visualResponses": {
  263. }
  264. }
  265. },
  266. "gamepadMapping": "xr-standard",
  267. "rootNodeName": "oculus-touch-v2-right",
  268. "assetPath": "right.glb"
  269. }
  270. };