babylon.arcRotateCamera.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.ArcRotateCamera = function (name, alpha, beta, radius, target, scene) {
  4. this.name = name;
  5. this.id = name;
  6. this.alpha = alpha;
  7. this.beta = beta;
  8. this.radius = radius;
  9. this.target = target;
  10. this._scene = scene;
  11. scene.cameras.push(this);
  12. if (!scene.activeCamera) {
  13. scene.activeCamera = this;
  14. }
  15. this.getViewMatrix();
  16. // Animations
  17. this.animations = [];
  18. };
  19. BABYLON.ArcRotateCamera.prototype = Object.create(BABYLON.Camera.prototype);
  20. // Members
  21. BABYLON.ArcRotateCamera.prototype.inertialAlphaOffset = 0;
  22. BABYLON.ArcRotateCamera.prototype.inertialBetaOffset = 0;
  23. // Methods
  24. BABYLON.ArcRotateCamera.prototype.attachControl = function(canvas) {
  25. var previousPosition;
  26. var that = this;
  27. this._onPointerDown = function (evt) {
  28. previousPosition = {
  29. x: evt.clientX,
  30. y: evt.clientY
  31. };
  32. evt.preventDefault();
  33. };
  34. this._onPointerUp = function (evt) {
  35. previousPosition = null;
  36. evt.preventDefault();
  37. };
  38. this._onPointerMove = function (evt) {
  39. if (!previousPosition) {
  40. return;
  41. }
  42. var offsetX = evt.clientX - previousPosition.x;
  43. var offsetY = evt.clientY - previousPosition.y;
  44. that.inertialAlphaOffset -= offsetX / 1000;
  45. that.inertialBetaOffset -= offsetY / 1000;
  46. previousPosition = {
  47. x: evt.clientX,
  48. y: evt.clientY
  49. };
  50. evt.preventDefault();
  51. };
  52. this._wheel = function(event) {
  53. var delta = 0;
  54. if (event.wheelDelta) {
  55. delta = event.wheelDelta / 120;
  56. } else if (event.detail) {
  57. delta = -event.detail / 3;
  58. }
  59. if (delta)
  60. that.radius -= delta;
  61. if (event.preventDefault)
  62. event.preventDefault();
  63. };
  64. canvas.addEventListener("pointerdown", this._onPointerDown);
  65. canvas.addEventListener("pointerup", this._onPointerUp);
  66. canvas.addEventListener("pointerout", this._onPointerUp);
  67. canvas.addEventListener("pointermove", this._onPointerMove);
  68. window.addEventListener('mousewheel', this._wheel);
  69. };
  70. BABYLON.ArcRotateCamera.prototype.detachControl = function (canvas) {
  71. canvas.removeEventListener("pointerdown", this._onPointerDown);
  72. canvas.removeEventListener("pointerup", this._onPointerUp);
  73. canvas.removeEventListener("pointerout", this._onPointerUp);
  74. canvas.removeEventListener("pointermove", this._onPointerMove);
  75. window.removeEventListener('mousewheel', this._wheel);
  76. };
  77. BABYLON.ArcRotateCamera.prototype._update = function() {
  78. // Inertia
  79. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0) {
  80. this.alpha += this.inertialAlphaOffset;
  81. this.beta += this.inertialBetaOffset;
  82. this.inertialAlphaOffset *= this.inertia;
  83. this.inertialBetaOffset *= this.inertia;
  84. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.epsilon)
  85. this.inertialAlphaOffset = 0;
  86. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.epsilon)
  87. this.inertialBetaOffset = 0;
  88. }
  89. };
  90. BABYLON.ArcRotateCamera.prototype.setPosition = function(position) {
  91. var radiusv3 = position.subtract(this.target.position ? this.target.position : this.target);
  92. this.radius = radiusv3.length();
  93. this.alpha = Math.atan(radiusv3.z / radiusv3.x);
  94. this.beta = Math.acos(radiusv3.y / this.radius);
  95. };
  96. BABYLON.ArcRotateCamera.prototype.getViewMatrix = function () {
  97. // Compute
  98. if (this.beta > Math.PI)
  99. this.beta = Math.PI;
  100. if (this.beta <= 0)
  101. this.beta = 0.01;
  102. var cosa = Math.cos(this.alpha);
  103. var sina = Math.sin(this.alpha);
  104. var cosb = Math.cos(this.beta);
  105. var sinb = Math.sin(this.beta);
  106. this.position = this.target.add(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb));
  107. return new BABYLON.Matrix.LookAtLH(this.position, this.target, BABYLON.Vector3.Up());
  108. };
  109. })();