babylon.oculusTouchController.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. module BABYLON {
  2. export class OculusTouchController extends WebVRController {
  3. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/oculus/';
  4. public static MODEL_LEFT_FILENAME: string = 'left.babylon';
  5. public static MODEL_RIGHT_FILENAME: string = 'right.babylon';
  6. public onSecondaryTriggerStateChangedObservable = new Observable<ExtendedGamepadButton>();
  7. public onThumbRestChangedObservable = new Observable<ExtendedGamepadButton>();
  8. constructor(vrGamepad: any) {
  9. super(vrGamepad);
  10. this.controllerType = PoseEnabledControllerType.OCULUS;
  11. }
  12. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  13. let meshName;
  14. // Hand
  15. if (this.hand === 'left') {
  16. meshName = OculusTouchController.MODEL_LEFT_FILENAME;
  17. }
  18. else { // Right is the default if no hand is specified
  19. meshName = OculusTouchController.MODEL_RIGHT_FILENAME;
  20. }
  21. SceneLoader.ImportMesh("", OculusTouchController.MODEL_BASE_URL, meshName, scene, (newMeshes) => {
  22. /*
  23. Parent Mesh name: oculus_touch_left
  24. - body
  25. - trigger
  26. - thumbstick
  27. - grip
  28. - button_y
  29. - button_x
  30. - button_enter
  31. */
  32. this._defaultModel = newMeshes[1];
  33. this.attachToMesh(this._defaultModel);
  34. if (meshLoaded) {
  35. meshLoaded(this._defaultModel);
  36. }
  37. });
  38. }
  39. // helper getters for left and right hand.
  40. public get onAButtonStateChangedObservable() {
  41. if (this.hand === 'right') {
  42. return this.onMainButtonStateChangedObservable;
  43. } else {
  44. throw new Error('No A button on left hand');
  45. }
  46. }
  47. public get onBButtonStateChangedObservable() {
  48. if (this.hand === 'right') {
  49. return this.onSecondaryButtonStateChangedObservable;
  50. } else {
  51. throw new Error('No B button on left hand');
  52. }
  53. }
  54. public get onXButtonStateChangedObservable() {
  55. if (this.hand === 'left') {
  56. return this.onMainButtonStateChangedObservable;
  57. } else {
  58. throw new Error('No X button on right hand');
  59. }
  60. }
  61. public get onYButtonStateChangedObservable() {
  62. if (this.hand === 'left') {
  63. return this.onSecondaryButtonStateChangedObservable;
  64. } else {
  65. throw new Error('No Y button on right hand');
  66. }
  67. }
  68. /*
  69. 0) thumb stick (touch, press, value = pressed (0,1)). value is in this.leftStick
  70. 1) index trigger (touch (?), press (only when value > 0.1), value 0 to 1)
  71. 2) secondary trigger (same)
  72. 3) A (right) X (left), touch, pressed = value
  73. 4) B / Y
  74. 5) thumb rest
  75. */
  76. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  77. let notifyObject = state; //{ state: state, changes: changes };
  78. let triggerDirection = this.hand === 'right' ? -1 : 1;
  79. switch (buttonIdx) {
  80. case 0:
  81. this.onPadStateChangedObservable.notifyObservers(notifyObject);
  82. return;
  83. case 1: // index trigger
  84. if (this._defaultModel) {
  85. (<AbstractMesh>(this._defaultModel.getChildren()[3])).rotation.x = -notifyObject.value * 0.20;
  86. (<AbstractMesh>(this._defaultModel.getChildren()[3])).position.y = -notifyObject.value * 0.005;
  87. (<AbstractMesh>(this._defaultModel.getChildren()[3])).position.z = -notifyObject.value * 0.005;
  88. }
  89. this.onTriggerStateChangedObservable.notifyObservers(notifyObject);
  90. return;
  91. case 2: // secondary trigger
  92. if (this._defaultModel) {
  93. (<AbstractMesh>(this._defaultModel.getChildren()[4])).position.x = triggerDirection * notifyObject.value * 0.0035;
  94. }
  95. this.onSecondaryTriggerStateChangedObservable.notifyObservers(notifyObject);
  96. return;
  97. case 3:
  98. if (this._defaultModel) {
  99. if (notifyObject.pressed) {
  100. (<AbstractMesh>(this._defaultModel.getChildren()[1])).position.y = -0.001;
  101. }
  102. else {
  103. (<AbstractMesh>(this._defaultModel.getChildren()[1])).position.y = 0;
  104. }
  105. }
  106. this.onMainButtonStateChangedObservable.notifyObservers(notifyObject);
  107. return;
  108. case 4:
  109. if (this._defaultModel) {
  110. if (notifyObject.pressed) {
  111. (<AbstractMesh>(this._defaultModel.getChildren()[2])).position.y = -0.001;
  112. }
  113. else {
  114. (<AbstractMesh>(this._defaultModel.getChildren()[2])).position.y = 0;
  115. }
  116. }
  117. this.onSecondaryButtonStateChangedObservable.notifyObservers(notifyObject);
  118. return;
  119. case 5:
  120. this.onThumbRestChangedObservable.notifyObservers(notifyObject);
  121. return;
  122. }
  123. }
  124. }
  125. }