babylon.arcRotateCamera.js 16 KB

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