babylon.arcRotateCamera.js 15 KB

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