babylon.arcRotateCamera.js 11 KB

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