babylon.arcRotateCamera.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. _this.inertialRadiusOffset = 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. element.addEventListener('mousewheel', this._wheel, false);
  207. element.addEventListener('DOMMouseScroll', this._wheel, false);
  208. BABYLON.Tools.RegisterTopRootEvents([
  209. { name: "keydown", handler: this._onKeyDown },
  210. { name: "keyup", handler: this._onKeyUp },
  211. { name: "blur", handler: this._onLostFocus }
  212. ]);
  213. };
  214. ArcRotateCamera.prototype.detachControl = function (element) {
  215. if (this._attachedElement != element) {
  216. return;
  217. }
  218. element.removeEventListener(eventPrefix + "down", this._onPointerDown);
  219. element.removeEventListener(eventPrefix + "up", this._onPointerUp);
  220. element.removeEventListener(eventPrefix + "out", this._onPointerUp);
  221. element.removeEventListener(eventPrefix + "move", this._onPointerMove);
  222. element.removeEventListener("mousemove", this._onMouseMove);
  223. element.removeEventListener("MSPointerDown", this._onGestureStart);
  224. element.removeEventListener("MSGestureChange", this._onGesture);
  225. element.removeEventListener('mousewheel', this._wheel);
  226. element.removeEventListener('DOMMouseScroll', this._wheel);
  227. BABYLON.Tools.UnregisterTopRootEvents([
  228. { name: "keydown", handler: this._onKeyDown },
  229. { name: "keyup", handler: this._onKeyUp },
  230. { name: "blur", handler: this._onLostFocus }
  231. ]);
  232. this._MSGestureHandler = null;
  233. this._attachedElement = null;
  234. if (this._reset) {
  235. this._reset();
  236. }
  237. };
  238. ArcRotateCamera.prototype._update = function () {
  239. for (var index = 0; index < this._keys.length; index++) {
  240. var keyCode = this._keys[index];
  241. if (this.keysLeft.indexOf(keyCode) !== -1) {
  242. this.inertialAlphaOffset -= 0.01;
  243. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  244. this.inertialBetaOffset -= 0.01;
  245. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  246. this.inertialAlphaOffset += 0.01;
  247. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  248. this.inertialBetaOffset += 0.01;
  249. }
  250. }
  251. // Inertia
  252. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0 || this.inertialRadiusOffset != 0) {
  253. this.alpha += this.inertialAlphaOffset;
  254. this.beta += this.inertialBetaOffset;
  255. this.radius -= this.inertialRadiusOffset;
  256. this.inertialAlphaOffset *= this.inertia;
  257. this.inertialBetaOffset *= this.inertia;
  258. this.inertialRadiusOffset *= this.inertia;
  259. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.Epsilon)
  260. this.inertialAlphaOffset = 0;
  261. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.Epsilon)
  262. this.inertialBetaOffset = 0;
  263. if (Math.abs(this.inertialRadiusOffset) < BABYLON.Engine.Epsilon)
  264. this.inertialRadiusOffset = 0;
  265. }
  266. // Limits
  267. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  268. this.alpha = this.lowerAlphaLimit;
  269. }
  270. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  271. this.alpha = this.upperAlphaLimit;
  272. }
  273. if (this.lowerBetaLimit && this.beta < this.lowerBetaLimit) {
  274. this.beta = this.lowerBetaLimit;
  275. }
  276. if (this.upperBetaLimit && this.beta > this.upperBetaLimit) {
  277. this.beta = this.upperBetaLimit;
  278. }
  279. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  280. this.radius = this.lowerRadiusLimit;
  281. }
  282. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  283. this.radius = this.upperRadiusLimit;
  284. }
  285. };
  286. ArcRotateCamera.prototype.setPosition = function (position) {
  287. var radiusv3 = position.subtract(this._getTargetPosition());
  288. this.radius = radiusv3.length();
  289. // Alpha
  290. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  291. if (radiusv3.z < 0) {
  292. this.alpha = 2 * Math.PI - this.alpha;
  293. }
  294. // Beta
  295. this.beta = Math.acos(radiusv3.y / this.radius);
  296. };
  297. ArcRotateCamera.prototype._getViewMatrix = function () {
  298. // Compute
  299. var cosa = Math.cos(this.alpha);
  300. var sina = Math.sin(this.alpha);
  301. var cosb = Math.cos(this.beta);
  302. var sinb = Math.sin(this.beta);
  303. var target = this._getTargetPosition();
  304. target.addToRef(new BABYLON.Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this.position);
  305. BABYLON.Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
  306. return this._viewMatrix;
  307. };
  308. ArcRotateCamera.prototype.zoomOn = function (meshes) {
  309. meshes = meshes || this.getScene().meshes;
  310. var minMaxVector = BABYLON.Mesh.MinMax(meshes);
  311. var distance = BABYLON.Vector3.Distance(minMaxVector.min, minMaxVector.max);
  312. this.radius = distance * this.zoomOnFactor;
  313. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance });
  314. };
  315. ArcRotateCamera.prototype.focusOn = function (meshesOrMinMaxVectorAndDistance) {
  316. var meshesOrMinMaxVector;
  317. var distance;
  318. if (meshesOrMinMaxVectorAndDistance.min === undefined) {
  319. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  320. meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  321. distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  322. } else {
  323. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  324. distance = meshesOrMinMaxVectorAndDistance.distance;
  325. }
  326. this.target = BABYLON.Mesh.Center(meshesOrMinMaxVector);
  327. this.maxZ = distance * 2;
  328. };
  329. return ArcRotateCamera;
  330. })(BABYLON.Camera);
  331. BABYLON.ArcRotateCamera = ArcRotateCamera;
  332. })(BABYLON || (BABYLON = {}));
  333. //# sourceMappingURL=babylon.arcRotateCamera.js.map