babylon.arcRotateCamera.js 16 KB

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