gearVRController.ts 3.9 KB

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