babylon.arcRotateCamera.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. module BABYLON {
  2. var eventPrefix = Tools.GetPointerPrefix();
  3. export class ArcRotateCamera extends Camera {
  4. public inertialAlphaOffset = 0;
  5. public inertialBetaOffset = 0;
  6. public inertialRadiusOffset = 0;
  7. public lowerAlphaLimit = null;
  8. public upperAlphaLimit = null;
  9. public lowerBetaLimit = 0.01;
  10. public upperBetaLimit = Math.PI;
  11. public lowerRadiusLimit = null;
  12. public upperRadiusLimit = null;
  13. public angularSensibility = 1000.0;
  14. public wheelPrecision = 3.0;
  15. public keysUp = [38];
  16. public keysDown = [40];
  17. public keysLeft = [37];
  18. public keysRight = [39];
  19. public zoomOnFactor = 1;
  20. private _keys = [];
  21. private _viewMatrix = new BABYLON.Matrix();
  22. private _attachedElement: HTMLElement;
  23. private _onPointerDown: (e: PointerEvent) => void;
  24. private _onPointerUp: (e: PointerEvent) => void;
  25. private _onPointerMove: (e: PointerEvent) => void;
  26. private _wheel: (e: MouseWheelEvent) => void;
  27. private _onMouseMove: (e: MouseEvent) => any;
  28. private _onKeyDown: (e: KeyboardEvent) => any;
  29. private _onKeyUp: (e: KeyboardEvent) => any;
  30. private _onLostFocus: (e: FocusEvent) => any;
  31. private _reset: () => void;
  32. private _onGestureStart: (e: PointerEvent) => void;
  33. private _onGesture: (e: MSGestureEvent) => void;
  34. private _MSGestureHandler: MSGesture;
  35. constructor(name: string, public alpha: number, public beta: number, public radius: number, public target: any, scene: Scene) {
  36. super(name, BABYLON.Vector3.Zero(), scene);
  37. this.getViewMatrix();
  38. }
  39. public _getTargetPosition(): Vector3 {
  40. return this.target.position || this.target;
  41. }
  42. // Cache
  43. public _initCache(): void {
  44. super._initCache();
  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. public _updateCache(ignoreParentClass?: boolean): void {
  51. if (!ignoreParentClass) {
  52. super._updateCache();
  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. public _isSynchronizedViewMatrix(): boolean {
  61. if (!super._isSynchronizedViewMatrix())
  62. return false;
  63. return this._cache.target.equals(this._getTargetPosition())
  64. && this._cache.alpha === this.alpha
  65. && this._cache.beta === this.beta
  66. && this._cache.radius === this.radius;
  67. }
  68. // Methods
  69. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  70. var previousPosition;
  71. var pointerId;
  72. if (this._attachedElement) {
  73. return;
  74. }
  75. this._attachedElement = element;
  76. var engine = this.getEngine();
  77. if (this._onPointerDown === undefined) {
  78. this._onPointerDown = evt => {
  79. if (pointerId) {
  80. return;
  81. }
  82. pointerId = evt.pointerId;
  83. previousPosition = {
  84. x: evt.clientX,
  85. y: evt.clientY
  86. };
  87. if (!noPreventDefault) {
  88. evt.preventDefault();
  89. }
  90. };
  91. this._onPointerUp = evt => {
  92. previousPosition = null;
  93. pointerId = null;
  94. if (!noPreventDefault) {
  95. evt.preventDefault();
  96. }
  97. };
  98. this._onPointerMove = evt => {
  99. if (!previousPosition) {
  100. return;
  101. }
  102. if (pointerId !== evt.pointerId) {
  103. return;
  104. }
  105. var offsetX = evt.clientX - previousPosition.x;
  106. var offsetY = evt.clientY - previousPosition.y;
  107. this.inertialAlphaOffset -= offsetX / this.angularSensibility;
  108. this.inertialBetaOffset -= offsetY / this.angularSensibility;
  109. previousPosition = {
  110. x: evt.clientX,
  111. y: evt.clientY
  112. };
  113. if (!noPreventDefault) {
  114. evt.preventDefault();
  115. }
  116. };
  117. this._onMouseMove = evt => {
  118. if (!engine.isPointerLock) {
  119. return;
  120. }
  121. var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
  122. var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
  123. this.inertialAlphaOffset -= offsetX / this.angularSensibility;
  124. this.inertialBetaOffset -= offsetY / this.angularSensibility;
  125. if (!noPreventDefault) {
  126. evt.preventDefault();
  127. }
  128. };
  129. this._wheel = event => {
  130. var delta = 0;
  131. if (event.wheelDelta) {
  132. delta = event.wheelDelta / (this.wheelPrecision * 40);
  133. } else if (event.detail) {
  134. delta = -event.detail / this.wheelPrecision;
  135. }
  136. if (delta)
  137. this.inertialRadiusOffset += delta;
  138. if (event.preventDefault) {
  139. if (!noPreventDefault) {
  140. event.preventDefault();
  141. }
  142. }
  143. };
  144. this._onKeyDown = evt => {
  145. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  146. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  147. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  148. 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 = evt => {
  161. if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
  162. this.keysDown.indexOf(evt.keyCode) !== -1 ||
  163. this.keysLeft.indexOf(evt.keyCode) !== -1 ||
  164. this.keysRight.indexOf(evt.keyCode) !== -1) {
  165. var index = this._keys.indexOf(evt.keyCode);
  166. if (index >= 0) {
  167. this._keys.splice(index, 1);
  168. }
  169. if (evt.preventDefault) {
  170. if (!noPreventDefault) {
  171. evt.preventDefault();
  172. }
  173. }
  174. }
  175. };
  176. this._onLostFocus = () => {
  177. this._keys = [];
  178. pointerId = null;
  179. };
  180. this._onGestureStart = e => {
  181. if (window.MSGesture === undefined) {
  182. return;
  183. }
  184. if (!this._MSGestureHandler) {
  185. this._MSGestureHandler = new MSGesture();
  186. this._MSGestureHandler.target = element;
  187. }
  188. this._MSGestureHandler.addPointer(e.pointerId);
  189. };
  190. this._onGesture = e => {
  191. this.radius *= e.scale;
  192. if (e.preventDefault) {
  193. if (!noPreventDefault) {
  194. e.stopPropagation();
  195. e.preventDefault();
  196. }
  197. }
  198. };
  199. this._reset = () => {
  200. this._keys = [];
  201. this.inertialAlphaOffset = 0;
  202. this.inertialBetaOffset = 0;
  203. previousPosition = null;
  204. pointerId = null;
  205. };
  206. }
  207. element.addEventListener(eventPrefix + "down", this._onPointerDown, false);
  208. element.addEventListener(eventPrefix + "up", this._onPointerUp, false);
  209. element.addEventListener(eventPrefix + "out", this._onPointerUp, false);
  210. element.addEventListener(eventPrefix + "move", this._onPointerMove, false);
  211. element.addEventListener("mousemove", this._onMouseMove, false);
  212. element.addEventListener("MSPointerDown", this._onGestureStart, false);
  213. element.addEventListener("MSGestureChange", this._onGesture, false);
  214. window.addEventListener("keydown", this._onKeyDown, false);
  215. window.addEventListener("keyup", this._onKeyUp, false);
  216. window.addEventListener('mousewheel', this._wheel, false);
  217. window.addEventListener('DOMMouseScroll', this._wheel, false);
  218. window.addEventListener("blur", this._onLostFocus, false);
  219. }
  220. public detachControl(element: HTMLElement): void {
  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. window.removeEventListener("keydown", this._onKeyDown);
  232. window.removeEventListener("keyup", this._onKeyUp);
  233. window.removeEventListener('mousewheel', this._wheel);
  234. window.removeEventListener('DOMMouseScroll', this._wheel);
  235. window.removeEventListener("blur", this._onLostFocus);
  236. this._MSGestureHandler = null;
  237. this._attachedElement = null;
  238. if (this._reset) {
  239. this._reset();
  240. }
  241. }
  242. public _update(): void {
  243. // Keyboard
  244. for (var index = 0; index < this._keys.length; index++) {
  245. var keyCode = this._keys[index];
  246. if (this.keysLeft.indexOf(keyCode) !== -1) {
  247. this.inertialAlphaOffset -= 0.01;
  248. } else if (this.keysUp.indexOf(keyCode) !== -1) {
  249. this.inertialBetaOffset -= 0.01;
  250. } else if (this.keysRight.indexOf(keyCode) !== -1) {
  251. this.inertialAlphaOffset += 0.01;
  252. } else if (this.keysDown.indexOf(keyCode) !== -1) {
  253. this.inertialBetaOffset += 0.01;
  254. }
  255. }
  256. // Inertia
  257. if (this.inertialAlphaOffset != 0 || this.inertialBetaOffset != 0 || this.inertialRadiusOffset != 0) {
  258. this.alpha += this.inertialAlphaOffset;
  259. this.beta += this.inertialBetaOffset;
  260. this.radius -= this.inertialRadiusOffset;
  261. this.inertialAlphaOffset *= this.inertia;
  262. this.inertialBetaOffset *= this.inertia;
  263. this.inertialRadiusOffset *= this.inertia;
  264. if (Math.abs(this.inertialAlphaOffset) < BABYLON.Engine.Epsilon)
  265. this.inertialAlphaOffset = 0;
  266. if (Math.abs(this.inertialBetaOffset) < BABYLON.Engine.Epsilon)
  267. this.inertialBetaOffset = 0;
  268. if (Math.abs(this.inertialRadiusOffset) < BABYLON.Engine.Epsilon)
  269. this.inertialRadiusOffset = 0;
  270. }
  271. // Limits
  272. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  273. this.alpha = this.lowerAlphaLimit;
  274. }
  275. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  276. this.alpha = this.upperAlphaLimit;
  277. }
  278. if (this.lowerBetaLimit && this.beta < this.lowerBetaLimit) {
  279. this.beta = this.lowerBetaLimit;
  280. }
  281. if (this.upperBetaLimit && this.beta > this.upperBetaLimit) {
  282. this.beta = this.upperBetaLimit;
  283. }
  284. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  285. this.radius = this.lowerRadiusLimit;
  286. }
  287. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  288. this.radius = this.upperRadiusLimit;
  289. }
  290. }
  291. public setPosition(position: Vector3): void {
  292. var radiusv3 = position.subtract(this._getTargetPosition());
  293. this.radius = radiusv3.length();
  294. this.alpha = Math.atan(radiusv3.z / radiusv3.x);
  295. this.beta = Math.acos(radiusv3.y / this.radius);
  296. }
  297. public _getViewMatrix(): Matrix {
  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. Matrix.LookAtLHToRef(this.position, target, this.upVector, this._viewMatrix);
  306. return this._viewMatrix;
  307. }
  308. public zoomOn(meshes?: Mesh[]): void {
  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. public focusOn(meshesOrMinMaxVectorAndDistance): void {
  316. var meshesOrMinMaxVector;
  317. var distance;
  318. if (meshesOrMinMaxVectorAndDistance.min === undefined) { // meshes
  319. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  320. meshesOrMinMaxVector = BABYLON.Mesh.MinMax(meshesOrMinMaxVector);
  321. distance = BABYLON.Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  322. }
  323. else { //minMaxVector and distance
  324. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  325. distance = meshesOrMinMaxVectorAndDistance.distance;
  326. }
  327. this.target = Mesh.Center(meshesOrMinMaxVector);
  328. this.maxZ = distance * 2;
  329. }
  330. }
  331. }