deviceOrientationCamera.js 4.7 KB

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