babylon.arcRotateCamera.js 18 KB

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