gearVRController.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Scene } from "scene";
  2. import { Vector3 } from "Math";
  3. import {WebVRController, PoseEnabledControllerType, ExtendedGamepadButton, GamepadButtonChanges} from "Gamepad"
  4. import { AbstractMesh } from "Mesh";
  5. import { _TimeToken } from "Instrumentation";
  6. import { SceneLoader } from "Loading";
  7. import { _DepthCullingState, _StencilState, _AlphaState } from "States";
  8. /**
  9. * Gear VR Controller
  10. */
  11. export class GearVRController extends WebVRController {
  12. /**
  13. * Base Url for the controller model.
  14. */
  15. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/generic/';
  16. /**
  17. * File name for the controller model.
  18. */
  19. public static MODEL_FILENAME: string = 'generic.babylon';
  20. /**
  21. * Gamepad Id prefix used to identify this controller.
  22. */
  23. public static readonly GAMEPAD_ID_PREFIX: string = 'Gear VR'; // id is 'Gear VR Controller'
  24. private readonly _buttonIndexToObservableNameMap = [
  25. 'onTrackpadChangedObservable', // Trackpad
  26. 'onTriggerStateChangedObservable' // Trigger
  27. ];
  28. /**
  29. * Creates a new GearVRController from a gamepad
  30. * @param vrGamepad the gamepad that the controller should be created from
  31. */
  32. constructor(vrGamepad: any) {
  33. super(vrGamepad);
  34. this.controllerType = PoseEnabledControllerType.GEAR_VR;
  35. // Initial starting position defaults to where hand would be (incase of only 3dof controller)
  36. this._calculatedPosition = new Vector3(this.hand == "left" ? -0.15 : 0.15, -0.5, 0.25);
  37. this._disableTrackPosition(this._calculatedPosition);
  38. }
  39. /**
  40. * Implements abstract method on WebVRController class, loading controller meshes and calling this.attachToMesh if successful.
  41. * @param scene scene in which to add meshes
  42. * @param meshLoaded optional callback function that will be called if the mesh loads successfully.
  43. */
  44. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  45. SceneLoader.ImportMesh("", GearVRController.MODEL_BASE_URL, GearVRController.MODEL_FILENAME, scene, (newMeshes) => {
  46. // Offset the controller so it will rotate around the users wrist
  47. var mesh = new BABYLON.Mesh("", scene);
  48. newMeshes[1].parent = mesh;
  49. newMeshes[1].position.z = -0.15;
  50. this._defaultModel = mesh;
  51. this.attachToMesh(this._defaultModel);
  52. if (meshLoaded) {
  53. meshLoaded(this._defaultModel);
  54. }
  55. });
  56. }
  57. /**
  58. * Called once for each button that changed state since the last frame
  59. * @param buttonIdx Which button index changed
  60. * @param state New state of the button
  61. * @param changes Which properties on the state changed since last frame
  62. */
  63. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  64. if (buttonIdx < this._buttonIndexToObservableNameMap.length) {
  65. const observableName : string = this._buttonIndexToObservableNameMap[buttonIdx];
  66. // Only emit events for buttons that we know how to map from index to observable
  67. let observable = (<any>this)[observableName];
  68. if (observable) {
  69. observable.notifyObservers(state);
  70. }
  71. }
  72. }
  73. }