babylon.deviceOrientationCamera.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module BABYLON {
  2. // We're mainly based on the logic defined into the FreeCamera code
  3. export class DeviceOrientationCamera extends FreeCamera {
  4. private _initialQuaternion: Quaternion;
  5. private _quaternionCache: Quaternion;
  6. constructor(name: string, position: Vector3, scene: Scene) {
  7. super(name, position, scene);
  8. this._quaternionCache = new Quaternion();
  9. this.inputs.addDeviceOrientation();
  10. }
  11. public getClassName(): string {
  12. return "DeviceOrientationCamera";
  13. }
  14. public _checkInputs(): void {
  15. super._checkInputs();
  16. this._quaternionCache.copyFrom(this.rotationQuaternion);
  17. if (this._initialQuaternion) {
  18. this._initialQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  19. }
  20. }
  21. public resetToCurrentRotation(axis: Axis = Axis.Y) {
  22. //can only work if this camera has a rotation quaternion already.
  23. if (!this.rotationQuaternion) return;
  24. if (!this._initialQuaternion) {
  25. this._initialQuaternion = new Quaternion();
  26. }
  27. this._initialQuaternion.copyFrom(this._quaternionCache || this.rotationQuaternion);
  28. ['x', 'y', 'z'].forEach((axisName) => {
  29. if (!(<any>axis)[axisName]) {
  30. (<any>this._initialQuaternion)[axisName] = 0;
  31. } else {
  32. (<any>this._initialQuaternion)[axisName] *= -1;
  33. }
  34. });
  35. this._initialQuaternion.normalize();
  36. //force rotation update
  37. this._initialQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  38. }
  39. }
  40. }