babylon.arcRotateCamera.js 12 KB

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