babylon.arcRotateCamera.js 14 KB

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