babylon.arcRotateCameraVRDeviceOrientationInput.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 _deviceOrientationHandler: () => void;
  12. constructor() {
  13. this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
  14. }
  15. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  16. this.camera.attachControl(element, noPreventDefault);
  17. window.addEventListener("deviceorientation", this._deviceOrientationHandler);
  18. }
  19. public _onOrientationEvent(evt: DeviceOrientationEvent): void {
  20. if (evt.alpha !== null) {
  21. this._alpha = +evt.alpha | 0;
  22. }
  23. if (evt.beta !== null) {
  24. this._beta = +evt.beta | 0;
  25. }
  26. if (evt.gamma !== null) {
  27. this._gamma = +evt.gamma | 0;
  28. }
  29. this._dirty = true;
  30. }
  31. public checkInputs() {
  32. if (this._dirty) {
  33. this._dirty = false;
  34. if (this._gamma < 0) {
  35. this._gamma = 180 + this._gamma;
  36. }
  37. this.camera.alpha = (-this._alpha / 180.0 * Math.PI) % Math.PI * 2;
  38. this.camera.beta = (this._gamma / 180.0 * Math.PI);
  39. }
  40. }
  41. detachControl(element: Nullable<HTMLElement>) {
  42. window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
  43. }
  44. getClassName(): string {
  45. return "ArcRotateCameraVRDeviceOrientationInput";
  46. }
  47. getSimpleName() {
  48. return "VRDeviceOrientation";
  49. }
  50. }
  51. (<any>CameraInputTypes)["ArcRotateCameraVRDeviceOrientationInput"] = ArcRotateCameraVRDeviceOrientationInput;
  52. }