DeviceOrientationCameraController.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import defined from "../Core/defined.js";
  2. import destroyObject from "../Core/destroyObject.js";
  3. import DeveloperError from "../Core/DeveloperError.js";
  4. import CesiumMath from "../Core/Math.js";
  5. import Matrix3 from "../Core/Matrix3.js";
  6. import Quaternion from "../Core/Quaternion.js";
  7. /**
  8. * @private
  9. */
  10. function DeviceOrientationCameraController(scene) {
  11. //>>includeStart('debug', pragmas.debug);
  12. if (!defined(scene)) {
  13. throw new DeveloperError("scene is required.");
  14. }
  15. //>>includeEnd('debug');
  16. this._scene = scene;
  17. this._lastAlpha = undefined;
  18. this._lastBeta = undefined;
  19. this._lastGamma = undefined;
  20. this._alpha = undefined;
  21. this._beta = undefined;
  22. this._gamma = undefined;
  23. const that = this;
  24. function callback(e) {
  25. const alpha = e.alpha;
  26. if (!defined(alpha)) {
  27. that._alpha = undefined;
  28. that._beta = undefined;
  29. that._gamma = undefined;
  30. return;
  31. }
  32. that._alpha = CesiumMath.toRadians(alpha);
  33. that._beta = CesiumMath.toRadians(e.beta);
  34. that._gamma = CesiumMath.toRadians(e.gamma);
  35. }
  36. window.addEventListener("deviceorientation", callback, false);
  37. this._removeListener = function () {
  38. window.removeEventListener("deviceorientation", callback, false);
  39. };
  40. }
  41. const scratchQuaternion1 = new Quaternion();
  42. const scratchQuaternion2 = new Quaternion();
  43. const scratchMatrix3 = new Matrix3();
  44. function rotate(camera, alpha, beta, gamma) {
  45. const direction = camera.direction;
  46. const right = camera.right;
  47. const up = camera.up;
  48. const bQuat = Quaternion.fromAxisAngle(direction, beta, scratchQuaternion2);
  49. const gQuat = Quaternion.fromAxisAngle(right, gamma, scratchQuaternion1);
  50. const rotQuat = Quaternion.multiply(gQuat, bQuat, gQuat);
  51. const aQuat = Quaternion.fromAxisAngle(up, alpha, scratchQuaternion2);
  52. Quaternion.multiply(aQuat, rotQuat, rotQuat);
  53. const matrix = Matrix3.fromQuaternion(rotQuat, scratchMatrix3);
  54. Matrix3.multiplyByVector(matrix, right, right);
  55. Matrix3.multiplyByVector(matrix, up, up);
  56. Matrix3.multiplyByVector(matrix, direction, direction);
  57. }
  58. DeviceOrientationCameraController.prototype.update = function () {
  59. if (!defined(this._alpha)) {
  60. return;
  61. }
  62. if (!defined(this._lastAlpha)) {
  63. this._lastAlpha = this._alpha;
  64. this._lastBeta = this._beta;
  65. this._lastGamma = this._gamma;
  66. }
  67. const a = this._lastAlpha - this._alpha;
  68. const b = this._lastBeta - this._beta;
  69. const g = this._lastGamma - this._gamma;
  70. rotate(this._scene.camera, -a, b, g);
  71. this._lastAlpha = this._alpha;
  72. this._lastBeta = this._beta;
  73. this._lastGamma = this._gamma;
  74. };
  75. /**
  76. * Returns true if this object was destroyed; otherwise, false.
  77. * <br /><br />
  78. *
  79. * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
  80. */
  81. DeviceOrientationCameraController.prototype.isDestroyed = function () {
  82. return false;
  83. };
  84. /**
  85. * Destroys the resources held by this object. Destroying an object allows for deterministic
  86. * release of resources, instead of relying on the garbage collector to destroy this object.
  87. * <br /><br />
  88. * Once an object is destroyed, it should not be used; calling any function other than
  89. * <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
  90. * assign the return value (<code>undefined</code>) to the object as done in the example.
  91. *
  92. * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
  93. */
  94. DeviceOrientationCameraController.prototype.destroy = function () {
  95. this._removeListener();
  96. return destroyObject(this);
  97. };
  98. export default DeviceOrientationCameraController;