babylon.freeCameraDeviceOrientationInput.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. module BABYLON {
  2. export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera> {
  3. private _camera: FreeCamera;
  4. private _screenOrientationAngle: number = 0;
  5. private _constantTranform: Quaternion;
  6. private _screenQuaternion: Quaternion = new Quaternion();
  7. private _alpha: number = 0;
  8. private _beta: number = 0;
  9. private _gamma: number = 0;
  10. constructor() {
  11. this._constantTranform = new Quaternion(- Math.sqrt(0.5), 0, 0, Math.sqrt(0.5));
  12. this._orientationChanged();
  13. }
  14. public get camera(): FreeCamera {
  15. return this._camera;
  16. }
  17. public set camera(camera: FreeCamera) {
  18. this._camera = camera;
  19. if (this._camera != null && !this._camera.rotationQuaternion) {
  20. this._camera.rotationQuaternion = new Quaternion();
  21. }
  22. }
  23. attachControl(element: HTMLElement, noPreventDefault?: boolean) {
  24. window.addEventListener("orientationchange", this._orientationChanged);
  25. window.addEventListener("deviceorientation", this._deviceOrientation);
  26. //In certain cases, the attach control is called AFTER orientation was changed,
  27. //So this is needed.
  28. this._orientationChanged();
  29. }
  30. private _orientationChanged = () => {
  31. this._screenOrientationAngle = (window.orientation !== undefined ? +window.orientation : (window.screen.orientation && (<any>window.screen.orientation)['angle'] ? (<any>window.screen.orientation).angle : 0));
  32. this._screenOrientationAngle = -Tools.ToRadians(this._screenOrientationAngle / 2);
  33. this._screenQuaternion.copyFromFloats(0, Math.sin(this._screenOrientationAngle), 0, Math.cos(this._screenOrientationAngle));
  34. }
  35. private _deviceOrientation = (evt: DeviceOrientationEvent) => {
  36. this._alpha = evt.alpha !== null ? evt.alpha : 0;
  37. this._beta = evt.beta !== null ? evt.beta : 0;
  38. this._gamma = evt.gamma !== null ? evt.gamma : 0;
  39. }
  40. detachControl(element: Nullable<HTMLElement>) {
  41. window.removeEventListener("orientationchange", this._orientationChanged);
  42. window.removeEventListener("deviceorientation", this._deviceOrientation);
  43. }
  44. public checkInputs() {
  45. //if no device orientation provided, don't update the rotation.
  46. //Only testing against alpha under the assumption thatnorientation will never be so exact when set.
  47. if (!this._alpha) return;
  48. Quaternion.RotationYawPitchRollToRef(Tools.ToRadians(this._alpha), Tools.ToRadians(this._beta), -Tools.ToRadians(this._gamma), this.camera.rotationQuaternion)
  49. this._camera.rotationQuaternion.multiplyInPlace(this._screenQuaternion);
  50. this._camera.rotationQuaternion.multiplyInPlace(this._constantTranform);
  51. //Mirror on XY Plane
  52. this._camera.rotationQuaternion.z *= -1;
  53. this._camera.rotationQuaternion.w *= -1;
  54. }
  55. getClassName(): string {
  56. return "FreeCameraDeviceOrientationInput";
  57. }
  58. getSimpleName() {
  59. return "deviceOrientation";
  60. }
  61. }
  62. (<any>CameraInputTypes)["FreeCameraDeviceOrientationInput"] = FreeCameraDeviceOrientationInput;
  63. }