babylon.arcRotateCamera.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.position = BABYLON.Vector3.Zero();
  12. this._keys = [];
  13. this.keysUp = [38];
  14. this.keysDown = [40];
  15. this.keysLeft = [37];
  16. this.keysRight = [39];
  17. this._scene = scene;
  18. scene.cameras.push(this);
  19. if (!scene.activeCamera) {
  20. scene.activeCamera = this;
  21. }
  22. this._viewMatrix = new BABYLON.Matrix();
  23. this.getViewMatrix();
  24. // Animations
  25. this.animations = [];
  26. };
  27. BABYLON.ArcRotateCamera.prototype = Object.create(BABYLON.Camera.prototype);
  28. // Members
  29. BABYLON.ArcRotateCamera.prototype.inertialAlphaOffset = 0;
  30. BABYLON.ArcRotateCamera.prototype.inertialBetaOffset = 0;
  31. BABYLON.ArcRotateCamera.prototype.lowerAlphaLimit = null;
  32. BABYLON.ArcRotateCamera.prototype.upperAlphaLimit = null;
  33. BABYLON.ArcRotateCamera.prototype.lowerBetaLimit = null;
  34. BABYLON.ArcRotateCamera.prototype.upperBetaLimit = null;
  35. BABYLON.ArcRotateCamera.prototype.lowerRadiusLimit = null;
  36. BABYLON.ArcRotateCamera.prototype.upperRadiusLimit = null;
  37. // Methods
  38. BABYLON.ArcRotateCamera.prototype.attachControl = function(canvas) {
  39. var previousPosition;
  40. var that = this;
  41. var pointerId;
  42. var engine = this._scene.getEngine();
  43. this._onPointerDown = function (evt) {
  44. if (pointerId) {
  45. return;
  46. }
  47. pointerId = evt.pointerId;
  48. previousPosition = {
  49. x: evt.clientX,
  50. y: evt.clientY
  51. };
  52. evt.preventDefault();
  53. };
  54. this._onPointerUp = function (evt) {
  55. previousPosition = null;
  56. pointerId = null;
  57. evt.preventDefault();
  58. };
  59. this._onPointerMove = function (evt) {
  60. if (!previousPosition) {
  61. return;
  62. }
  63. if (pointerId !== evt.pointerId) {
  64. return;
  65. }
  66. var offsetX = evt.clientX - previousPosition.x;
  67. var offsetY = evt.clientY - previousPosition.y;
  68. that.inertialAlphaOffset -= offsetX / 1000;
  69. that.inertialBetaOffset -= offsetY / 1000;
  70. previousPosition = {
  71. x: evt.clientX,
  72. y: evt.clientY
  73. };
  74. evt.preventDefault();
  75. };
  76. this._onMouseMove = function (evt) {
  77. if (!engine.isPointerLock) {
  78. return;
  79. }
  80. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  81. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  82. that.inertialAlphaOffset -= offsetX / 1000;
  83. that.inertialBetaOffset -= offsetY / 1000;
  84. evt.preventDefault();
  85. };
  86. this._wheel = function(event) {
  87. var delta = 0;
  88. if (event.wheelDelta) {
  89. delta = event.wheelDelta / 120;
  90. } else if (event.detail) {
  91. delta = -event.detail / 3;
  92. }
  93. if (delta)
  94. that.radius -= delta;
  95. if (event.preventDefault)
  96. event.preventDefault();
  97. };
  98. this._onKeyDown = function (evt) {
  99. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  100. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  101. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  102. that.keysRight.indexOf(evt.keyCode) !== -1) {
  103. var index = that._keys.indexOf(evt.keyCode);
  104. if (index === -1) {
  105. that._keys.push(evt.keyCode);
  106. }
  107. evt.preventDefault();
  108. }
  109. };
  110. this._onKeyUp = function (evt) {
  111. if (that.keysUp.indexOf(evt.keyCode) !== -1 ||
  112. that.keysDown.indexOf(evt.keyCode) !== -1 ||
  113. that.keysLeft.indexOf(evt.keyCode) !== -1 ||
  114. that.keysRight.indexOf(evt.keyCode) !== -1) {
  115. var index = that._keys.indexOf(evt.keyCode);
  116. if (index >= 0) {
  117. that._keys.splice(index, 1);
  118. }
  119. evt.preventDefault();
  120. }
  121. };
  122. this._onLostFocus = function () {
  123. that._keys = [];
  124. pointerId = null;
  125. };
  126. this._onGestureStart = function (e) {
  127. if (!that._MSGestureHandler) {
  128. that._MSGestureHandler = new MSGesture();
  129. that._MSGestureHandler.target = canvas;
  130. }
  131. that._MSGestureHandler.addPointer(e.pointerId);
  132. };
  133. this._onGesture = function(e) {
  134. that.radius *= e.scale;
  135. e.preventDefault();
  136. e.stopPropagation();
  137. };
  138. canvas.addEventListener(eventPrefix + "down", this._onPointerDown);
  139. canvas.addEventListener(eventPrefix + "up", this._onPointerUp);
  140. canvas.addEventListener(eventPrefix + "out", this._onPointerUp);
  141. canvas.addEventListener(eventPrefix + "move", this._onPointerMove);
  142. canvas.addEventListener("mousemove", this._onMouseMove);
  143. canvas.addEventListener("MSPointerDown", this._onGestureStart);
  144. canvas.addEventListener("MSGestureChange", this._onGesture);
  145. window.addEventListener("keydown", this._onKeyDown);
  146. window.addEventListener("keyup", this._onKeyUp);
  147. window.addEventListener('mousewheel', this._wheel);
  148. window.addEventListener("blur", this._onLostFocus);
  149. };
  150. BABYLON.ArcRotateCamera.prototype.detachControl = function (canvas) {
  151. canvas.removeEventListener(eventPrefix + "down", this._onPointerDown);
  152. canvas.removeEventListener(eventPrefix + "up", this._onPointerUp);
  153. canvas.removeEventListener(eventPrefix + "out", this._onPointerUp);
  154. canvas.removeEventListener(eventPrefix + "move", this._onPointerMove);
  155. canvas.removeEventListener("mousemove", this._onMouseMove);
  156. canvas.removeEventListener("MSPointerDown", this._onGestureStart);
  157. canvas.removeEventListener("MSGestureChange", this._onGesture);
  158. window.removeEventListener("keydown", this._onKeyDown);
  159. window.removeEventListener("keyup", this._onKeyUp);
  160. window.removeEventListener('mousewheel', this._wheel);
  161. window.removeEventListener("blur", this._onLostFocus);
  162. this._MSGestureHandler = null;
  163. };
  164. BABYLON.ArcRotateCamera.prototype._update = function () {
  165. // Keyboard
  166. for (var index = 0; index < this._keys.length; index++) {
  167. var keyCode = this._keys[index];
  168. if (this.keysLeft.indexOf(keyCode) !== -1) {
  169. this.inertialAlphaOffset -= 0.01;
  170. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  171. this.inertialBetaOffset -= 0.01;
  172. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  173. this.inertialAlphaOffset += 0.01;
  174. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  175. this.inertialBetaOffset += 0.01;
  176. }
  177. }
  178. // Inertia
  179. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0) {
  180. this.alpha += this.inertialAlphaOffset;
  181. this.beta += this.inertialBetaOffset;
  182. this.inertialAlphaOffset *= this.inertia;
  183. this.inertialBetaOffset *= this.inertia;
  184. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.epsilon)
  185. this.inertialAlphaOffset = 0;
  186. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.epsilon)
  187. this.inertialBetaOffset = 0;
  188. }
  189. // Limits
  190. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  191. this.alpha = this.lowerAlphaLimit;
  192. }
  193. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  194. this.alpha = this.upperAlphaLimit;
  195. }
  196. if (this.lowerBetaLimit && this.beta < this.lowerBetaLimit) {
  197. this.beta = this.lowerBetaLimit;
  198. }
  199. if (this.upperBetaLimit && this.beta > this.upperBetaLimit) {
  200. this.beta = this.upperBetaLimit;
  201. }
  202. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  203. this.radius = this.lowerRadiusLimit;
  204. }
  205. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  206. this.radius = this.upperRadiusLimit;
  207. }
  208. };
  209. BABYLON.ArcRotateCamera.prototype.setPosition = function(position) {
  210. var radiusv3 = position.subtract(this.target.position ? this.target.position : this.target);
  211. this.radius = radiusv3.length();
  212. this.alpha = Math.atan(radiusv3.z / radiusv3.x);
  213. this.beta = Math.acos(radiusv3.y / this.radius);
  214. };
  215. BABYLON.ArcRotateCamera.prototype.getViewMatrix = function () {
  216. // Compute
  217. if (this.beta > Math.PI)
  218. this.beta = Math.PI;
  219. if (this.beta <= 0)
  220. this.beta = 0.01;
  221. var cosa = Math.cos(this.alpha);
  222. var sina = Math.sin(this.alpha);
  223. var cosb = Math.cos(this.beta);
  224. var sinb = Math.sin(this.beta);
  225. this.target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
  226. BABYLON.Matrix.LookAtLHToRef(this.position, this.target, BABYLON.Vector3.Up(), this._viewMatrix);
  227. return this._viewMatrix;
  228. };
  229. })();