babylon.arcRotateCamera.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. var eventPrefix = BABYLON.Tools.GetPointerPrefix();
  4. BABYLON.ArcRotateCamera = function (name, alpha, beta, radius, target, scene) {
  5. this.name = name;
  6. this.id = name;
  7. this.alpha = alpha;
  8. this.beta = beta;
  9. this.radius = radius;
  10. this.target = target;
  11. this._keys = [];
  12. this.keysUp = [38];
  13. this.keysDown = [40];
  14. this.keysLeft = [37];
  15. this.keysRight = [39];
  16. this._scene = scene;
  17. scene.cameras.push(this);
  18. if (!scene.activeCamera) {
  19. scene.activeCamera = this;
  20. }
  21. this.getViewMatrix();
  22. // Animations
  23. this.animations = [];
  24. };
  25. BABYLON.ArcRotateCamera.prototype = Object.create(BABYLON.Camera.prototype);
  26. // Members
  27. BABYLON.ArcRotateCamera.prototype.inertialAlphaOffset = 0;
  28. BABYLON.ArcRotateCamera.prototype.inertialBetaOffset = 0;
  29. // Methods
  30. BABYLON.ArcRotateCamera.prototype.attachControl = function(canvas) {
  31. var previousPosition;
  32. var that = this;
  33. var pointerId;
  34. this._onPointerDown = function (evt) {
  35. if (pointerId) {
  36. return;
  37. }
  38. pointerId = evt.pointerId;
  39. previousPosition = {
  40. x: evt.clientX,
  41. y: evt.clientY
  42. };
  43. evt.preventDefault();
  44. };
  45. this._onPointerUp = function (evt) {
  46. previousPosition = null;
  47. pointerId = null;
  48. evt.preventDefault();
  49. };
  50. this._onPointerMove = function (evt) {
  51. if (!previousPosition) {
  52. return;
  53. }
  54. if (pointerId !== evt.pointerId) {
  55. return;
  56. }
  57. var offsetX = evt.clientX - previousPosition.x;
  58. var offsetY = evt.clientY - previousPosition.y;
  59. that.inertialAlphaOffset -= offsetX / 1000;
  60. that.inertialBetaOffset -= offsetY / 1000;
  61. previousPosition = {
  62. x: evt.clientX,
  63. y: evt.clientY
  64. };
  65. evt.preventDefault();
  66. };
  67. this._wheel = function(event) {
  68. var delta = 0;
  69. if (event.wheelDelta) {
  70. delta = event.wheelDelta / 120;
  71. } else if (event.detail) {
  72. delta = -event.detail / 3;
  73. }
  74. if (delta)
  75. that.radius -= delta;
  76. if (event.preventDefault)
  77. event.preventDefault();
  78. };
  79. this._onKeyDown = function (evt) {
  80. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  81. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  82. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  83. that.keysRight.indexOf(evt.keyCode) !== -1) {
  84. var index = that._keys.indexOf(evt.keyCode);
  85. if (index === -1) {
  86. that._keys.push(evt.keyCode);
  87. }
  88. evt.preventDefault();
  89. }
  90. };
  91. this._onKeyUp = function (evt) {
  92. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  93. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  94. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  95. that.keysRight.indexOf(evt.keyCode) !== -1) {
  96. var index = that._keys.indexOf(evt.keyCode);
  97. if (index >= 0) {
  98. that._keys.splice(index, 1);
  99. }
  100. evt.preventDefault();
  101. }
  102. };
  103. this._onLostFocus = function () {
  104. that._keys = [];
  105. pointerId = null;
  106. };
  107. this._onGestureStart = function (e) {
  108. if (!that._MSGestureHandler) {
  109. that._MSGestureHandler = new MSGesture();
  110. that._MSGestureHandler.target = canvas;
  111. }
  112. that._MSGestureHandler.addPointer(e.pointerId);
  113. };
  114. this._onGesture = function(e) {
  115. that.radius *= e.scale;
  116. };
  117. canvas.addEventListener(eventPrefix + "down", this._onPointerDown);
  118. canvas.addEventListener(eventPrefix + "up", this._onPointerUp);
  119. canvas.addEventListener(eventPrefix + "out", this._onPointerUp);
  120. canvas.addEventListener(eventPrefix + "move", this._onPointerMove);
  121. canvas.addEventListener("MSPointerDown", this._onGestureStart);
  122. canvas.addEventListener("MSGestureChange", this._onGesture, true);
  123. window.addEventListener("keydown", this._onKeyDown, true);
  124. window.addEventListener("keyup", this._onKeyUp, true);
  125. window.addEventListener('mousewheel', this._wheel);
  126. window.addEventListener("blur", this._onLostFocus, true);
  127. };
  128. BABYLON.ArcRotateCamera.prototype.detachControl = function (canvas) {
  129. canvas.removeEventListener(eventPrefix + "down", this._onPointerDown);
  130. canvas.removeEventListener(eventPrefix + "up", this._onPointerUp);
  131. canvas.removeEventListener(eventPrefix + "out", this._onPointerUp);
  132. canvas.removeEventListener(eventPrefix + "move", this._onPointerMove);
  133. canvas.removeEventListener("MSPointerDown", this._onGestureStart);
  134. canvas.removeEventListener("MSGestureChange", this._onGesture);
  135. window.removeEventListener("keydown", this._onKeyDown);
  136. window.removeEventListener("keyup", this._onKeyUp);
  137. window.removeEventListener('mousewheel', this._wheel);
  138. window.removeEventListener("blur", this._onLostFocus);
  139. this._MSGestureHandler = null;
  140. };
  141. BABYLON.ArcRotateCamera.prototype._update = function () {
  142. // Keyboard
  143. for (var index = 0; index < this._keys.length; index++) {
  144. var keyCode = this._keys[index];
  145. if (this.keysLeft.indexOf(keyCode) !== -1) {
  146. this.inertialAlphaOffset -= 0.01;
  147. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  148. this.inertialBetaOffset -= 0.01;
  149. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  150. this.inertialAlphaOffset += 0.01;
  151. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  152. this.inertialBetaOffset += 0.01;
  153. }
  154. }
  155. // Inertia
  156. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0) {
  157. this.alpha += this.inertialAlphaOffset;
  158. this.beta += this.inertialBetaOffset;
  159. this.inertialAlphaOffset *= this.inertia;
  160. this.inertialBetaOffset *= this.inertia;
  161. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.epsilon)
  162. this.inertialAlphaOffset = 0;
  163. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.epsilon)
  164. this.inertialBetaOffset = 0;
  165. }
  166. };
  167. BABYLON.ArcRotateCamera.prototype.setPosition = function(position) {
  168. var radiusv3 = position.subtract(this.target.position ? this.target.position : this.target);
  169. this.radius = radiusv3.length();
  170. this.alpha = Math.atan(radiusv3.z / radiusv3.x);
  171. this.beta = Math.acos(radiusv3.y / this.radius);
  172. };
  173. BABYLON.ArcRotateCamera.prototype.getViewMatrix = function () {
  174. // Compute
  175. if (this.beta > Math.PI)
  176. this.beta = Math.PI;
  177. if (this.beta <= 0)
  178. this.beta = 0.01;
  179. var cosa = Math.cos(this.alpha);
  180. var sina = Math.sin(this.alpha);
  181. var cosb = Math.cos(this.beta);
  182. var sinb = Math.sin(this.beta);
  183. this.position = this.target.add(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb));
  184. return new BABYLON.Matrix.LookAtLH(this.position, this.target, BABYLON.Vector3.Up());
  185. };
  186. })();