babylon.arcRotateCameraVRDeviceOrientationInput.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. module BABYLON {
  2. export class ArcRotateCameraVRDeviceOrientationInput implements ICameraInput<ArcRotateCamera> {
  3. camera: ArcRotateCamera;
  4. public alphaCorrection = 1;
  5. public betaCorrection = 1;
  6. public gammaCorrection = 1;
  7. private _alpha = 0;
  8. private _beta = 0;
  9. private _gamma = 0;
  10. private _dirty = false;
  11. private _offsetOrientation: { yaw: number; pitch: number; roll: number };
  12. private _deviceOrientationHandler;
  13. constructor() {
  14. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  15. }
  16. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  17. this.camera.attachControl(element, noPreventDefault);
  18. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  19. }
  20. public _onOrientationEvent(evt: DeviceOrientationEvent): void {
  21. var camera = this.camera;
  22. this._alpha = +evt.alpha | 0;
  23. this._beta = +evt.beta | 0;
  24. this._gamma = +evt.gamma | 0;
  25. this._dirty = true;
  26. }
  27. public checkInputs() {
  28. if (this._dirty) {
  29. this._dirty = false;
  30. if (this._gamma < 0) {
  31. this._gamma = 180 + this._gamma;
  32. }
  33. this.camera.alpha = (-this._alpha / 180.0 * Math.PI) % Math.PI * 2;
  34. this.camera.beta = (this._gamma / 180.0 * Math.PI);
  35. }
  36. }
  37. detachControl(element: HTMLElement) {
  38. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  39. }
  40. getTypeName(): string {
  41. return "ArcRotateCameraVRDeviceOrientationInput";
  42. }
  43. getSimpleName() {
  44. return "VRDeviceOrientation";
  45. }
  46. }
  47. CameraInputTypes["ArcRotateCameraVRDeviceOrientationInput"] = ArcRotateCameraVRDeviceOrientationInput;
  48. }