babylon.arcRotateCamera.js 20 KB

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