babylon.arcRotateCameraVRDeviceOrientationInput.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. module BABYLON {
  2. /**
  3. * Manage the device orientation inputs (gyroscope) to control an arc rotate camera.
  4. * @see http://doc.babylonjs.com/how_to/customizing_camera_inputs
  5. */
  6. export class ArcRotateCameraVRDeviceOrientationInput implements ICameraInput<ArcRotateCamera> {
  7. /**
  8. * Defines the camera the input is attached to.
  9. */
  10. public camera: ArcRotateCamera;
  11. /**
  12. * Defines a correction factor applied on the alpha value retrieved from the orientation events.
  13. */
  14. public alphaCorrection = 1;
  15. /**
  16. * Defines a correction factor applied on the beta value retrieved from the orientation events.
  17. */
  18. public betaCorrection = 1;
  19. /**
  20. * Defines a correction factor applied on the gamma value retrieved from the orientation events.
  21. */
  22. public gammaCorrection = 1;
  23. private _alpha = 0;
  24. private _gamma = 0;
  25. private _dirty = false;
  26. private _deviceOrientationHandler: () => void;
  27. /**
  28. * Instantiate a new ArcRotateCameraVRDeviceOrientationInput.
  29. */
  30. constructor() {
  31. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  32. }
  33. /**
  34. * Attach the input controls to a specific dom element to get the input from.
  35. * @param element Defines the element the controls should be listened from
  36. * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  37. */
  38. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  39. this.camera.attachControl(element, noPreventDefault);
  40. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  41. }
  42. /** @hidden */
  43. public _onOrientationEvent(evt: DeviceOrientationEvent): void {
  44. if (evt.alpha !== null) {
  45. this._alpha = +evt.alpha | 0;
  46. }
  47. if (evt.gamma !== null) {
  48. this._gamma = +evt.gamma | 0;
  49. }
  50. this._dirty = true;
  51. }
  52. /**
  53. * Update the current camera state depending on the inputs that have been used this frame.
  54. * This is a dynamically created lambda to avoid the performance penalty of looping for inputs in the render loop.
  55. */
  56. public checkInputs(): void {
  57. if (this._dirty) {
  58. this._dirty = false;
  59. if (this._gamma < 0) {
  60. this._gamma = 180 + this._gamma;
  61. }
  62. this.camera.alpha = (-this._alpha / 180.0 * Math.PI) % Math.PI * 2;
  63. this.camera.beta = (this._gamma / 180.0 * Math.PI);
  64. }
  65. }
  66. /**
  67. * Detach the current controls from the specified dom element.
  68. * @param element Defines the element to stop listening the inputs from
  69. */
  70. public detachControl(element: Nullable<HTMLElement>): void {
  71. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  72. }
  73. /**
  74. * Gets the class name of the current intput.
  75. * @returns the class name
  76. */
  77. public getClassName(): string {
  78. return "ArcRotateCameraVRDeviceOrientationInput";
  79. }
  80. /**
  81. * Get the friendly name associated with the input class.
  82. * @returns the input friendly name
  83. */
  84. public getSimpleName(): string {
  85. return "VRDeviceOrientation";
  86. }
  87. }
  88. (<any>CameraInputTypes)["ArcRotateCameraVRDeviceOrientationInput"] = ArcRotateCameraVRDeviceOrientationInput;
  89. }