webVRController.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Observable } from "Tools";
  2. import { Scene } from "scene";
  3. import {PoseEnabledController, StickValues, ExtendedGamepadButton, GamepadButtonChanges, MutableGamepadButton} from "Gamepad"
  4. import { AbstractMesh } from "Mesh";
  5. import { _TimeToken } from "Instrumentation";
  6. import { _DepthCullingState, _StencilState, _AlphaState } from "States";
  7. /**
  8. * Defines the WebVRController object that represents controllers tracked in 3D space
  9. */
  10. export abstract class WebVRController extends PoseEnabledController {
  11. /**
  12. * Internal, the default controller model for the controller
  13. */
  14. protected _defaultModel: AbstractMesh;
  15. // Observables
  16. /**
  17. * Fired when the trigger state has changed
  18. */
  19. public onTriggerStateChangedObservable = new Observable<ExtendedGamepadButton>();
  20. /**
  21. * Fired when the main button state has changed
  22. */
  23. public onMainButtonStateChangedObservable = new Observable<ExtendedGamepadButton>();
  24. /**
  25. * Fired when the secondary button state has changed
  26. */
  27. public onSecondaryButtonStateChangedObservable = new Observable<ExtendedGamepadButton>();
  28. /**
  29. * Fired when the pad state has changed
  30. */
  31. public onPadStateChangedObservable = new Observable<ExtendedGamepadButton>();
  32. /**
  33. * Fired when controllers stick values have changed
  34. */
  35. public onPadValuesChangedObservable = new Observable<StickValues>();
  36. /**
  37. * Array of button availible on the controller
  38. */
  39. protected _buttons: Array<MutableGamepadButton>;
  40. private _onButtonStateChange: (controlledIndex: number, buttonIndex: number, state: ExtendedGamepadButton) => void;
  41. /**
  42. * Fired when a controller button's state has changed
  43. * @param callback the callback containing the button that was modified
  44. */
  45. public onButtonStateChange(callback: (controlledIndex: number, buttonIndex: number, state: ExtendedGamepadButton) => void) {
  46. this._onButtonStateChange = callback;
  47. }
  48. /**
  49. * X and Y axis corrisponding to the controllers joystick
  50. */
  51. public pad: StickValues = { x: 0, y: 0 };
  52. /**
  53. * 'left' or 'right', see https://w3c.github.io/gamepad/extensions.html#gamepadhand-enum
  54. */
  55. public hand: string;
  56. /**
  57. * The default controller model for the controller
  58. */
  59. public get defaultModel(): AbstractMesh {
  60. return this._defaultModel;
  61. }
  62. /**
  63. * Creates a new WebVRController from a gamepad
  64. * @param vrGamepad the gamepad that the WebVRController should be created from
  65. */
  66. constructor(vrGamepad: any) {
  67. super(vrGamepad);
  68. this._buttons = new Array<ExtendedGamepadButton>(vrGamepad.buttons.length);
  69. this.hand = vrGamepad.hand;
  70. }
  71. /**
  72. * Updates the state of the controller and mesh based on the current position and rotation of the controller
  73. */
  74. public update() {
  75. super.update();
  76. for (var index = 0; index < this._buttons.length; index++) {
  77. this._setButtonValue(this.browserGamepad.buttons[index], this._buttons[index], index);
  78. }
  79. if (this.leftStick.x !== this.pad.x || this.leftStick.y !== this.pad.y) {
  80. this.pad.x = this.leftStick.x;
  81. this.pad.y = this.leftStick.y;
  82. this.onPadValuesChangedObservable.notifyObservers(this.pad);
  83. }
  84. }
  85. /**
  86. * Function to be called when a button is modified
  87. */
  88. protected abstract _handleButtonChange(buttonIdx: number, value: ExtendedGamepadButton, changes: GamepadButtonChanges): void;
  89. /**
  90. * Loads a mesh and attaches it to the controller
  91. * @param scene the scene the mesh should be added to
  92. * @param meshLoaded callback for when the mesh has been loaded
  93. */
  94. public abstract initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void): void;
  95. private _setButtonValue(newState: ExtendedGamepadButton, currentState: ExtendedGamepadButton, buttonIndex: number) {
  96. if (!newState) {
  97. newState = {
  98. pressed: false,
  99. touched: false,
  100. value: 0
  101. };
  102. }
  103. if (!currentState) {
  104. this._buttons[buttonIndex] = {
  105. pressed: newState.pressed,
  106. touched: newState.touched,
  107. value: newState.value
  108. };
  109. return;
  110. }
  111. this._checkChanges(newState, currentState);
  112. if (this._changes.changed) {
  113. this._onButtonStateChange && this._onButtonStateChange(this.index, buttonIndex, newState);
  114. this._handleButtonChange(buttonIndex, newState, this._changes);
  115. }
  116. this._buttons[buttonIndex].pressed = newState.pressed;
  117. this._buttons[buttonIndex].touched = newState.touched;
  118. // oculus triggers are never 0, thou not touched.
  119. this._buttons[buttonIndex].value = newState.value < 0.00000001 ? 0 : newState.value;
  120. }
  121. // avoid GC, store state in a tmp object
  122. private _changes: GamepadButtonChanges = {
  123. pressChanged: false,
  124. touchChanged: false,
  125. valueChanged: false,
  126. changed: false
  127. };
  128. private _checkChanges(newState: ExtendedGamepadButton, currentState: ExtendedGamepadButton) {
  129. this._changes.pressChanged = newState.pressed !== currentState.pressed;
  130. this._changes.touchChanged = newState.touched !== currentState.touched;
  131. this._changes.valueChanged = newState.value !== currentState.value;
  132. this._changes.changed = this._changes.pressChanged || this._changes.touchChanged || this._changes.valueChanged;
  133. return this._changes;
  134. }
  135. /**
  136. * Disposes of th webVRCOntroller
  137. */
  138. public dispose(): void {
  139. super.dispose();
  140. this.onTriggerStateChangedObservable.clear();
  141. this.onMainButtonStateChangedObservable.clear();
  142. this.onSecondaryButtonStateChangedObservable.clear();
  143. this.onPadStateChangedObservable.clear();
  144. this.onPadValuesChangedObservable.clear();
  145. }
  146. }