deviceOrientationCamera.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { FreeCamera } from "./freeCamera";
  2. import { Scene } from "../scene";
  3. import { Quaternion, Vector3 } from "../Maths/math.vector";
  4. import { Node } from "../node";
  5. import "./Inputs/freeCameraDeviceOrientationInput";
  6. import { Axis } from '../Maths/math.axis';
  7. Node.AddNodeConstructor("DeviceOrientationCamera", (name, scene) => {
  8. return () => new DeviceOrientationCamera(name, Vector3.Zero(), scene);
  9. });
  10. // We're mainly based on the logic defined into the FreeCamera code
  11. /**
  12. * This is a camera specifically designed to react to device orientation events such as a modern mobile device
  13. * being tilted forward or back and left or right.
  14. */
  15. export class DeviceOrientationCamera extends FreeCamera {
  16. private _initialQuaternion: Quaternion;
  17. private _quaternionCache: Quaternion;
  18. private _tmpDragQuaternion = new Quaternion();
  19. private _disablePointerInputWhenUsingDeviceOrientation = true;
  20. /**
  21. * Creates a new device orientation camera
  22. * @param name The name of the camera
  23. * @param position The start position camera
  24. * @param scene The scene the camera belongs to
  25. */
  26. constructor(name: string, position: Vector3, scene: Scene) {
  27. super(name, position, scene);
  28. this._quaternionCache = new Quaternion();
  29. this.inputs.addDeviceOrientation();
  30. // When the orientation sensor fires it's first event, disable mouse input
  31. if (this.inputs._deviceOrientationInput) {
  32. this.inputs._deviceOrientationInput._onDeviceOrientationChangedObservable.addOnce(() => {
  33. if (this._disablePointerInputWhenUsingDeviceOrientation) {
  34. if (this.inputs._mouseInput) {
  35. this.inputs._mouseInput._allowCameraRotation = false;
  36. this.inputs._mouseInput.onPointerMovedObservable.add((e) => {
  37. if (this._dragFactor != 0) {
  38. if (!this._initialQuaternion) {
  39. this._initialQuaternion = new Quaternion();
  40. }
  41. // Rotate the initial space around the y axis to allow users to "turn around" via touch/mouse
  42. Quaternion.FromEulerAnglesToRef(0, e.offsetX * this._dragFactor, 0, this._tmpDragQuaternion);
  43. this._initialQuaternion.multiplyToRef(this._tmpDragQuaternion, this._initialQuaternion);
  44. }
  45. });
  46. }
  47. }
  48. });
  49. }
  50. }
  51. /**
  52. * Gets or sets a boolean indicating that pointer input must be disabled on first orientation sensor update (Default: true)
  53. */
  54. public get disablePointerInputWhenUsingDeviceOrientation() {
  55. return this._disablePointerInputWhenUsingDeviceOrientation;
  56. }
  57. public set disablePointerInputWhenUsingDeviceOrientation(value: boolean) {
  58. this._disablePointerInputWhenUsingDeviceOrientation = value;
  59. }
  60. private _dragFactor = 0;
  61. /**
  62. * Enabled turning on the y axis when the orientation sensor is active
  63. * @param dragFactor the factor that controls the turn speed (default: 1/300)
  64. */
  65. public enableHorizontalDragging(dragFactor = 1 / 300) {
  66. this._dragFactor = dragFactor;
  67. }
  68. /**
  69. * Gets the current instance class name ("DeviceOrientationCamera").
  70. * This helps avoiding instanceof at run time.
  71. * @returns the class name
  72. */
  73. public getClassName(): string {
  74. return "DeviceOrientationCamera";
  75. }
  76. /**
  77. * @hidden
  78. * Checks and applies the current values of the inputs to the camera. (Internal use only)
  79. */
  80. public _checkInputs(): void {
  81. super._checkInputs();
  82. this._quaternionCache.copyFrom(this.rotationQuaternion);
  83. if (this._initialQuaternion) {
  84. this._initialQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  85. }
  86. }
  87. /**
  88. * Reset the camera to its default orientation on the specified axis only.
  89. * @param axis The axis to reset
  90. */
  91. public resetToCurrentRotation(axis: Axis = Axis.Y): void {
  92. //can only work if this camera has a rotation quaternion already.
  93. if (!this.rotationQuaternion) { return; }
  94. if (!this._initialQuaternion) {
  95. this._initialQuaternion = new Quaternion();
  96. }
  97. this._initialQuaternion.copyFrom(this._quaternionCache || this.rotationQuaternion);
  98. ['x', 'y', 'z'].forEach((axisName) => {
  99. if (!(<any>axis)[axisName]) {
  100. (<any>this._initialQuaternion)[axisName] = 0;
  101. } else {
  102. (<any>this._initialQuaternion)[axisName] *= -1;
  103. }
  104. });
  105. this._initialQuaternion.normalize();
  106. //force rotation update
  107. this._initialQuaternion.multiplyToRef(this.rotationQuaternion, this.rotationQuaternion);
  108. }
  109. }