daydreamController.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Logger } from "Misc/logger";
  2. import { Scene } from "scene";
  3. import { AbstractMesh } from "Meshes/abstractMesh";
  4. import { _TimeToken } from "Instrumentation/timeToken";
  5. import { SceneLoader } from "Loading/sceneLoader";
  6. import { _DepthCullingState, _StencilState, _AlphaState } from "States";
  7. import { GamepadButtonChanges } from "Gamepads/gamepad";
  8. import { WebVRController } from "./webVRController";
  9. import { PoseEnabledControllerType, ExtendedGamepadButton, PoseEnabledControllerHelper } from "./poseEnabledController";
  10. /**
  11. * Google Daydream controller
  12. */
  13. export class DaydreamController extends WebVRController {
  14. /**
  15. * Base Url for the controller model.
  16. */
  17. public static MODEL_BASE_URL: string = 'https://controllers.babylonjs.com/generic/';
  18. /**
  19. * File name for the controller model.
  20. */
  21. public static MODEL_FILENAME: string = 'generic.babylon';
  22. /**
  23. * Gamepad Id prefix used to identify Daydream Controller.
  24. */
  25. public static readonly GAMEPAD_ID_PREFIX: string = 'Daydream'; // id is 'Daydream Controller'
  26. /**
  27. * Creates a new DaydreamController from a gamepad
  28. * @param vrGamepad the gamepad that the controller should be created from
  29. */
  30. constructor(vrGamepad: any) {
  31. super(vrGamepad);
  32. this.controllerType = PoseEnabledControllerType.DAYDREAM;
  33. }
  34. /**
  35. * Implements abstract method on WebVRController class, loading controller meshes and calling this.attachToMesh if successful.
  36. * @param scene scene in which to add meshes
  37. * @param meshLoaded optional callback function that will be called if the mesh loads successfully.
  38. */
  39. public initControllerMesh(scene: Scene, meshLoaded?: (mesh: AbstractMesh) => void) {
  40. SceneLoader.ImportMesh("", DaydreamController.MODEL_BASE_URL, DaydreamController.MODEL_FILENAME, scene, (newMeshes) => {
  41. this._defaultModel = newMeshes[1];
  42. this.attachToMesh(this._defaultModel);
  43. if (meshLoaded) {
  44. meshLoaded(this._defaultModel);
  45. }
  46. });
  47. }
  48. /**
  49. * Called once for each button that changed state since the last frame
  50. * @param buttonIdx Which button index changed
  51. * @param state New state of the button
  52. * @param changes Which properties on the state changed since last frame
  53. */
  54. protected _handleButtonChange(buttonIdx: number, state: ExtendedGamepadButton, changes: GamepadButtonChanges) {
  55. // Daydream controller only has 1 GamepadButton (on the trackpad).
  56. if (buttonIdx === 0) {
  57. let observable = this.onTriggerStateChangedObservable;
  58. if (observable) {
  59. observable.notifyObservers(state);
  60. }
  61. } else {
  62. // If the app or home buttons are ever made available
  63. Logger.Warn(`Unrecognized Daydream button index: ${buttonIdx}`);
  64. }
  65. }
  66. }
  67. PoseEnabledControllerHelper._ControllerFactories.push({
  68. canCreate: (gamepadInfo) => {
  69. return gamepadInfo.id.indexOf(DaydreamController.GAMEPAD_ID_PREFIX) === 0;
  70. },
  71. create: (gamepadInfo) => {
  72. return new DaydreamController(gamepadInfo);
  73. }
  74. });