babylon.arcRotateCamera.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /// <reference path="babylon.targetCamera.ts" />
  2. /// <reference path="..\Tools\babylon.tools.ts" />
  3. module BABYLON {
  4. export class ArcRotateCamera extends TargetCamera {
  5. @serialize()
  6. public alpha: number;
  7. @serialize()
  8. public beta: number;
  9. @serialize()
  10. public radius: number;
  11. @serializeAsVector3("target")
  12. protected _target: Vector3;
  13. protected _targetHost: AbstractMesh;
  14. public get target(): Vector3 {
  15. return this._target;
  16. }
  17. public set target(value: Vector3) {
  18. this.setTarget(value);
  19. }
  20. @serialize()
  21. public inertialAlphaOffset = 0;
  22. @serialize()
  23. public inertialBetaOffset = 0;
  24. @serialize()
  25. public inertialRadiusOffset = 0;
  26. @serialize()
  27. public lowerAlphaLimit = null;
  28. @serialize()
  29. public upperAlphaLimit = null;
  30. @serialize()
  31. public lowerBetaLimit = 0.01;
  32. @serialize()
  33. public upperBetaLimit = Math.PI;
  34. @serialize()
  35. public lowerRadiusLimit = null;
  36. @serialize()
  37. public upperRadiusLimit = null;
  38. @serialize()
  39. public inertialPanningX: number = 0;
  40. @serialize()
  41. public inertialPanningY: number = 0;
  42. @serialize()
  43. public pinchToPanMaxDistance: number = 2;
  44. @serialize()
  45. public panningDistanceLimit: number = null;
  46. @serializeAsVector3()
  47. public panningOriginTarget: Vector3 = Vector3.Zero();
  48. @serialize()
  49. public panningInertia = 0.9;
  50. //-- begin properties for backward compatibility for inputs
  51. public get angularSensibilityX() {
  52. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  53. if (pointers)
  54. return pointers.angularSensibilityX;
  55. }
  56. public set angularSensibilityX(value) {
  57. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  58. if (pointers) {
  59. pointers.angularSensibilityX = value;
  60. }
  61. }
  62. public get angularSensibilityY() {
  63. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  64. if (pointers)
  65. return pointers.angularSensibilityY;
  66. }
  67. public set angularSensibilityY(value) {
  68. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  69. if (pointers) {
  70. pointers.angularSensibilityY = value;
  71. }
  72. }
  73. public get pinchPrecision() {
  74. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  75. if (pointers)
  76. return pointers.pinchPrecision;
  77. }
  78. public set pinchPrecision(value) {
  79. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  80. if (pointers) {
  81. pointers.pinchPrecision = value;
  82. }
  83. }
  84. public get panningSensibility() {
  85. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  86. if (pointers)
  87. return pointers.panningSensibility;
  88. }
  89. public set panningSensibility(value) {
  90. var pointers = <ArcRotateCameraPointersInput>this.inputs.attached["pointers"];
  91. if (pointers) {
  92. pointers.panningSensibility = value;
  93. }
  94. }
  95. public get keysUp() {
  96. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  97. if (keyboard)
  98. return keyboard.keysUp;
  99. }
  100. public set keysUp(value) {
  101. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  102. if (keyboard)
  103. keyboard.keysUp = value;
  104. }
  105. public get keysDown() {
  106. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  107. if (keyboard)
  108. return keyboard.keysDown;
  109. }
  110. public set keysDown(value) {
  111. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  112. if (keyboard)
  113. keyboard.keysDown = value;
  114. }
  115. public get keysLeft() {
  116. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  117. if (keyboard)
  118. return keyboard.keysLeft;
  119. }
  120. public set keysLeft(value) {
  121. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  122. if (keyboard)
  123. keyboard.keysLeft = value;
  124. }
  125. public get keysRight() {
  126. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  127. if (keyboard)
  128. return keyboard.keysRight;
  129. }
  130. public set keysRight(value) {
  131. var keyboard = <ArcRotateCameraKeyboardMoveInput>this.inputs.attached["keyboard"];
  132. if (keyboard)
  133. keyboard.keysRight = value;
  134. }
  135. public get wheelPrecision() {
  136. var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
  137. if (mousewheel)
  138. return mousewheel.wheelPrecision;
  139. }
  140. public set wheelPrecision(value) {
  141. var mousewheel = <ArcRotateCameraMouseWheelInput>this.inputs.attached["mousewheel"];
  142. if (mousewheel)
  143. mousewheel.wheelPrecision = value;
  144. }
  145. //-- end properties for backward compatibility for inputs
  146. @serialize()
  147. public zoomOnFactor = 1;
  148. public targetScreenOffset = Vector2.Zero();
  149. @serialize()
  150. public allowUpsideDown = true;
  151. public _viewMatrix = new Matrix();
  152. public _useCtrlForPanning: boolean;
  153. public _panningMouseButton: number;
  154. public inputs: ArcRotateCameraInputsManager;
  155. public _reset: () => void;
  156. // Panning
  157. public panningAxis: Vector3 = new Vector3(1, 1, 0);
  158. protected _localDirection: Vector3;
  159. protected _transformedDirection: Vector3;
  160. // Behaviors
  161. private _bouncingBehavior: BouncingBehavior;
  162. public get bouncingBehavior(): BouncingBehavior {
  163. return this._bouncingBehavior;
  164. }
  165. public get useBouncingBehavior(): boolean {
  166. return this._bouncingBehavior != null;
  167. }
  168. public set useBouncingBehavior(value: boolean) {
  169. if (value === this.useBouncingBehavior) {
  170. return;
  171. }
  172. if (value) {
  173. this._bouncingBehavior = new BouncingBehavior();
  174. this.addBehavior(this._bouncingBehavior);
  175. } else {
  176. this.removeBehavior(this._bouncingBehavior);
  177. this._bouncingBehavior = null;
  178. }
  179. }
  180. private _framingBehavior: FramingBehavior;
  181. public get framingBehavior(): FramingBehavior {
  182. return this._framingBehavior;
  183. }
  184. public get useFramingBehavior(): boolean {
  185. return this._framingBehavior != null;
  186. }
  187. public set useFramingBehavior(value: boolean) {
  188. if (value === this.useFramingBehavior) {
  189. return;
  190. }
  191. if (value) {
  192. this._framingBehavior = new FramingBehavior();
  193. this.addBehavior(this._framingBehavior);
  194. } else {
  195. this.removeBehavior(this._framingBehavior);
  196. this._framingBehavior = null;
  197. }
  198. }
  199. private _autoRotationBehavior: AutoRotationBehavior;
  200. public get autoRotationBehavior(): AutoRotationBehavior {
  201. return this._autoRotationBehavior;
  202. }
  203. public get useAutoRotationBehavior(): boolean {
  204. return this._autoRotationBehavior != null;
  205. }
  206. public set useAutoRotationBehavior(value: boolean) {
  207. if (value === this.useAutoRotationBehavior) {
  208. return;
  209. }
  210. if (value) {
  211. this._autoRotationBehavior = new AutoRotationBehavior();
  212. this.addBehavior(this._autoRotationBehavior);
  213. } else {
  214. this.removeBehavior(this._autoRotationBehavior);
  215. this._autoRotationBehavior = null;
  216. }
  217. }
  218. public onMeshTargetChangedObservable = new Observable<AbstractMesh>();
  219. // Collisions
  220. public onCollide: (collidedMesh: AbstractMesh) => void;
  221. public checkCollisions = false;
  222. public collisionRadius = new Vector3(0.5, 0.5, 0.5);
  223. protected _collider: Collider;
  224. protected _previousPosition = Vector3.Zero();
  225. protected _collisionVelocity = Vector3.Zero();
  226. protected _newPosition = Vector3.Zero();
  227. protected _previousAlpha: number;
  228. protected _previousBeta: number;
  229. protected _previousRadius: number;
  230. //due to async collision inspection
  231. protected _collisionTriggered: boolean;
  232. protected _targetBoundingCenter: Vector3;
  233. constructor(name: string, alpha: number, beta: number, radius: number, target: Vector3, scene: Scene) {
  234. super(name, Vector3.Zero(), scene);
  235. this._target = Vector3.Zero();
  236. if (target) {
  237. this.setTarget(target);
  238. }
  239. this.alpha = alpha;
  240. this.beta = beta;
  241. this.radius = radius;
  242. this.getViewMatrix();
  243. this.inputs = new ArcRotateCameraInputsManager(this);
  244. this.inputs.addKeyboard().addMouseWheel().addPointers();
  245. }
  246. // Cache
  247. public _initCache(): void {
  248. super._initCache();
  249. this._cache._target = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  250. this._cache.alpha = undefined;
  251. this._cache.beta = undefined;
  252. this._cache.radius = undefined;
  253. this._cache.targetScreenOffset = Vector2.Zero();
  254. }
  255. public _updateCache(ignoreParentClass?: boolean): void {
  256. if (!ignoreParentClass) {
  257. super._updateCache();
  258. }
  259. this._cache._target.copyFrom(this._getTargetPosition());
  260. this._cache.alpha = this.alpha;
  261. this._cache.beta = this.beta;
  262. this._cache.radius = this.radius;
  263. this._cache.targetScreenOffset.copyFrom(this.targetScreenOffset);
  264. }
  265. protected _getTargetPosition(): Vector3 {
  266. if (this._targetHost && this._targetHost.getAbsolutePosition) {
  267. var pos : Vector3 = this._targetHost.getAbsolutePosition();
  268. if (this._targetBoundingCenter) {
  269. pos.addToRef(this._targetBoundingCenter, this._target);
  270. } else {
  271. this._target.copyFrom(pos);
  272. }
  273. }
  274. var lockedTargetPosition = this._getLockedTargetPosition();
  275. if (lockedTargetPosition) {
  276. return lockedTargetPosition;
  277. }
  278. return this._target;
  279. }
  280. // State
  281. /**
  282. * Store current camera state (fov, position, etc..)
  283. */
  284. private _storedAlpha: number;
  285. private _storedBeta: number;
  286. private _storedRadius: number;
  287. private _storedTarget: Vector3;
  288. public storeState(): Camera {
  289. this._storedAlpha = this.alpha;
  290. this._storedBeta = this.beta;
  291. this._storedRadius = this.radius;
  292. this._storedTarget = this._getTargetPosition().clone();
  293. return super.storeState();
  294. }
  295. /**
  296. * Restored camera state. You must call storeState() first
  297. */
  298. public _restoreStateValues(): boolean {
  299. if (!super._restoreStateValues()) {
  300. return false;
  301. }
  302. this.alpha = this._storedAlpha;
  303. this.beta = this._storedBeta;
  304. this.radius = this._storedRadius;
  305. this.setTarget(this._storedTarget);
  306. this.inertialAlphaOffset = 0;
  307. this.inertialBetaOffset = 0;
  308. this.inertialRadiusOffset = 0;
  309. this.inertialPanningX = 0;
  310. this.inertialPanningY = 0;
  311. return true;
  312. }
  313. // Synchronized
  314. public _isSynchronizedViewMatrix(): boolean {
  315. if (!super._isSynchronizedViewMatrix())
  316. return false;
  317. return this._cache._target.equals(this._getTargetPosition())
  318. && this._cache.alpha === this.alpha
  319. && this._cache.beta === this.beta
  320. && this._cache.radius === this.radius
  321. && this._cache.targetScreenOffset.equals(this.targetScreenOffset);
  322. }
  323. // Methods
  324. public attachControl(element: HTMLElement, noPreventDefault?: boolean, useCtrlForPanning: boolean = true, panningMouseButton: number = 2): void {
  325. this._useCtrlForPanning = useCtrlForPanning;
  326. this._panningMouseButton = panningMouseButton;
  327. this.inputs.attachElement(element, noPreventDefault);
  328. this._reset = () => {
  329. this.inertialAlphaOffset = 0;
  330. this.inertialBetaOffset = 0;
  331. this.inertialRadiusOffset = 0;
  332. this.inertialPanningX = 0;
  333. this.inertialPanningY = 0;
  334. };
  335. }
  336. public detachControl(element: HTMLElement): void {
  337. this.inputs.detachElement(element);
  338. if (this._reset) {
  339. this._reset();
  340. }
  341. }
  342. public _checkInputs(): void {
  343. //if (async) collision inspection was triggered, don't update the camera's position - until the collision callback was called.
  344. if (this._collisionTriggered) {
  345. return;
  346. }
  347. this.inputs.checkInputs();
  348. // Inertia
  349. if (this.inertialAlphaOffset !== 0 || this.inertialBetaOffset !== 0 || this.inertialRadiusOffset !== 0) {
  350. if (this.getScene().useRightHandedSystem) {
  351. this.alpha -= this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
  352. } else {
  353. this.alpha += this.beta <= 0 ? -this.inertialAlphaOffset : this.inertialAlphaOffset;
  354. }
  355. this.beta += this.inertialBetaOffset;
  356. this.radius -= this.inertialRadiusOffset;
  357. this.inertialAlphaOffset *= this.inertia;
  358. this.inertialBetaOffset *= this.inertia;
  359. this.inertialRadiusOffset *= this.inertia;
  360. if (Math.abs(this.inertialAlphaOffset) < Epsilon)
  361. this.inertialAlphaOffset = 0;
  362. if (Math.abs(this.inertialBetaOffset) < Epsilon)
  363. this.inertialBetaOffset = 0;
  364. if (Math.abs(this.inertialRadiusOffset) < this.speed * Epsilon)
  365. this.inertialRadiusOffset = 0;
  366. }
  367. // Panning inertia
  368. if (this.inertialPanningX !== 0 || this.inertialPanningY !== 0) {
  369. if (!this._localDirection) {
  370. this._localDirection = Vector3.Zero();
  371. this._transformedDirection = Vector3.Zero();
  372. }
  373. this._localDirection.copyFromFloats(this.inertialPanningX, this.inertialPanningY, this.inertialPanningY);
  374. this._localDirection.multiplyInPlace(this.panningAxis);
  375. this._viewMatrix.invertToRef(this._cameraTransformMatrix);
  376. Vector3.TransformNormalToRef(this._localDirection, this._cameraTransformMatrix, this._transformedDirection);
  377. //Eliminate y if map panning is enabled (panningAxis == 1,0,1)
  378. if (!this.panningAxis.y) {
  379. this._transformedDirection.y = 0;
  380. }
  381. if (!this._targetHost) {
  382. if (this.panningDistanceLimit) {
  383. this._transformedDirection.addInPlace(this._target);
  384. var distanceSquared = Vector3.DistanceSquared(this._transformedDirection, this.panningOriginTarget);
  385. if (distanceSquared <= (this.panningDistanceLimit * this.panningDistanceLimit)) {
  386. this._target.copyFrom(this._transformedDirection);
  387. }
  388. }
  389. else {
  390. this._target.addInPlace(this._transformedDirection);
  391. }
  392. }
  393. this.inertialPanningX *= this.panningInertia;
  394. this.inertialPanningY *= this.panningInertia;
  395. if (Math.abs(this.inertialPanningX) < this.speed * Epsilon)
  396. this.inertialPanningX = 0;
  397. if (Math.abs(this.inertialPanningY) < this.speed * Epsilon)
  398. this.inertialPanningY = 0;
  399. }
  400. // Limits
  401. this._checkLimits();
  402. super._checkInputs();
  403. }
  404. protected _checkLimits() {
  405. if (this.lowerBetaLimit === null || this.lowerBetaLimit === undefined) {
  406. if (this.allowUpsideDown && this.beta > Math.PI) {
  407. this.beta = this.beta - (2 * Math.PI);
  408. }
  409. } else {
  410. if (this.beta < this.lowerBetaLimit) {
  411. this.beta = this.lowerBetaLimit;
  412. }
  413. }
  414. if (this.upperBetaLimit === null || this.upperBetaLimit === undefined) {
  415. if (this.allowUpsideDown && this.beta < -Math.PI) {
  416. this.beta = this.beta + (2 * Math.PI);
  417. }
  418. } else {
  419. if (this.beta > this.upperBetaLimit) {
  420. this.beta = this.upperBetaLimit;
  421. }
  422. }
  423. if (this.lowerAlphaLimit && this.alpha < this.lowerAlphaLimit) {
  424. this.alpha = this.lowerAlphaLimit;
  425. }
  426. if (this.upperAlphaLimit && this.alpha > this.upperAlphaLimit) {
  427. this.alpha = this.upperAlphaLimit;
  428. }
  429. if (this.lowerRadiusLimit && this.radius < this.lowerRadiusLimit) {
  430. this.radius = this.lowerRadiusLimit;
  431. }
  432. if (this.upperRadiusLimit && this.radius > this.upperRadiusLimit) {
  433. this.radius = this.upperRadiusLimit;
  434. }
  435. }
  436. public rebuildAnglesAndRadius() {
  437. var radiusv3 = this.position.subtract(this._getTargetPosition());
  438. this.radius = radiusv3.length();
  439. if (this.radius === 0) {
  440. this.radius = 0.0001; // Just to avoid division by zero
  441. }
  442. // Alpha
  443. this.alpha = Math.acos(radiusv3.x / Math.sqrt(Math.pow(radiusv3.x, 2) + Math.pow(radiusv3.z, 2)));
  444. if (radiusv3.z < 0) {
  445. this.alpha = 2 * Math.PI - this.alpha;
  446. }
  447. // Beta
  448. this.beta = Math.acos(radiusv3.y / this.radius);
  449. this._checkLimits();
  450. }
  451. public setPosition(position: Vector3): void {
  452. if (this.position.equals(position)) {
  453. return;
  454. }
  455. this.position.copyFrom(position);
  456. this.rebuildAnglesAndRadius();
  457. }
  458. public setTarget(target: AbstractMesh | Vector3, toBoundingCenter = false, allowSamePosition = false): void {
  459. if ((<any>target).getBoundingInfo){
  460. if (toBoundingCenter){
  461. this._targetBoundingCenter = (<any>target).getBoundingInfo().boundingBox.centerWorld.clone();
  462. } else {
  463. this._targetBoundingCenter = null;
  464. }
  465. this._targetHost = <AbstractMesh>target;
  466. this._target = this._getTargetPosition();
  467. this.onMeshTargetChangedObservable.notifyObservers(this._targetHost);
  468. } else {
  469. var newTarget = <Vector3>target;
  470. var currentTarget = this._getTargetPosition();
  471. if (currentTarget && !allowSamePosition && currentTarget.equals(newTarget)) {
  472. return;
  473. }
  474. this._targetHost = null;
  475. this._target = newTarget;
  476. this._targetBoundingCenter = null;
  477. this.onMeshTargetChangedObservable.notifyObservers(null);
  478. }
  479. this.rebuildAnglesAndRadius();
  480. }
  481. public _getViewMatrix(): Matrix {
  482. // Compute
  483. var cosa = Math.cos(this.alpha);
  484. var sina = Math.sin(this.alpha);
  485. var cosb = Math.cos(this.beta);
  486. var sinb = Math.sin(this.beta);
  487. if (sinb === 0) {
  488. sinb = 0.0001;
  489. }
  490. var target = this._getTargetPosition();
  491. target.addToRef(new Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this._newPosition);
  492. if (this.getScene().collisionsEnabled && this.checkCollisions) {
  493. if (!this._collider) {
  494. this._collider = new Collider();
  495. }
  496. this._collider.radius = this.collisionRadius;
  497. this._newPosition.subtractToRef(this.position, this._collisionVelocity);
  498. this._collisionTriggered = true;
  499. this.getScene().collisionCoordinator.getNewPosition(this.position, this._collisionVelocity, this._collider, 3, null, this._onCollisionPositionChange, this.uniqueId);
  500. } else {
  501. this.position.copyFrom(this._newPosition);
  502. var up = this.upVector;
  503. if (this.allowUpsideDown && sinb < 0) {
  504. up = up.clone();
  505. up = up.negate();
  506. }
  507. if (this.getScene().useRightHandedSystem) {
  508. Matrix.LookAtRHToRef(this.position, target, up, this._viewMatrix);
  509. } else {
  510. Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
  511. }
  512. this._viewMatrix.m[12] += this.targetScreenOffset.x;
  513. this._viewMatrix.m[13] += this.targetScreenOffset.y;
  514. }
  515. this._currentTarget = target;
  516. return this._viewMatrix;
  517. }
  518. protected _onCollisionPositionChange = (collisionId: number, newPosition: Vector3, collidedMesh: AbstractMesh = null) => {
  519. if (this.getScene().workerCollisions && this.checkCollisions) {
  520. newPosition.multiplyInPlace(this._collider.radius);
  521. }
  522. if (!collidedMesh) {
  523. this._previousPosition.copyFrom(this.position);
  524. } else {
  525. this.setPosition(newPosition);
  526. if (this.onCollide) {
  527. this.onCollide(collidedMesh);
  528. }
  529. }
  530. // Recompute because of constraints
  531. var cosa = Math.cos(this.alpha);
  532. var sina = Math.sin(this.alpha);
  533. var cosb = Math.cos(this.beta);
  534. var sinb = Math.sin(this.beta);
  535. if (sinb === 0) {
  536. sinb = 0.0001;
  537. }
  538. var target = this._getTargetPosition();
  539. target.addToRef(new Vector3(this.radius * cosa * sinb, this.radius * cosb, this.radius * sina * sinb), this._newPosition);
  540. this.position.copyFrom(this._newPosition);
  541. var up = this.upVector;
  542. if (this.allowUpsideDown && this.beta < 0) {
  543. up = up.clone();
  544. up = up.negate();
  545. }
  546. Matrix.LookAtLHToRef(this.position, target, up, this._viewMatrix);
  547. this._viewMatrix.m[12] += this.targetScreenOffset.x;
  548. this._viewMatrix.m[13] += this.targetScreenOffset.y;
  549. this._collisionTriggered = false;
  550. }
  551. public zoomOn(meshes?: AbstractMesh[], doNotUpdateMaxZ = false): void {
  552. meshes = meshes || this.getScene().meshes;
  553. var minMaxVector = Mesh.MinMax(meshes);
  554. var distance = Vector3.Distance(minMaxVector.min, minMaxVector.max);
  555. this.radius = distance * this.zoomOnFactor;
  556. this.focusOn({ min: minMaxVector.min, max: minMaxVector.max, distance: distance }, doNotUpdateMaxZ);
  557. }
  558. public focusOn(meshesOrMinMaxVectorAndDistance, doNotUpdateMaxZ = false): void {
  559. var meshesOrMinMaxVector;
  560. var distance;
  561. if (meshesOrMinMaxVectorAndDistance.min === undefined) { // meshes
  562. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance || this.getScene().meshes;
  563. meshesOrMinMaxVector = Mesh.MinMax(meshesOrMinMaxVector);
  564. distance = Vector3.Distance(meshesOrMinMaxVector.min, meshesOrMinMaxVector.max);
  565. }
  566. else { //minMaxVector and distance
  567. meshesOrMinMaxVector = meshesOrMinMaxVectorAndDistance;
  568. distance = meshesOrMinMaxVectorAndDistance.distance;
  569. }
  570. this._target = Mesh.Center(meshesOrMinMaxVector);
  571. if (!doNotUpdateMaxZ) {
  572. this.maxZ = distance * 2;
  573. }
  574. }
  575. /**
  576. * @override
  577. * Override Camera.createRigCamera
  578. */
  579. public createRigCamera(name: string, cameraIndex: number): Camera {
  580. var alphaShift : number;
  581. switch (this.cameraRigMode) {
  582. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  583. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  584. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  585. case Camera.RIG_MODE_VR:
  586. alphaShift = this._cameraRigParams.stereoHalfAngle * (cameraIndex === 0 ? 1 : -1);
  587. break;
  588. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  589. alphaShift = this._cameraRigParams.stereoHalfAngle * (cameraIndex === 0 ? -1 : 1);
  590. break;
  591. }
  592. var rigCam = new ArcRotateCamera(name, this.alpha + alphaShift, this.beta, this.radius, this._target, this.getScene());
  593. rigCam._cameraRigParams = {};
  594. return rigCam;
  595. }
  596. /**
  597. * @override
  598. * Override Camera._updateRigCameras
  599. */
  600. public _updateRigCameras() {
  601. var camLeft = <ArcRotateCamera>this._rigCameras[0];
  602. var camRight = <ArcRotateCamera>this._rigCameras[1];
  603. camLeft.beta = camRight.beta = this.beta;
  604. camLeft.radius = camRight.radius = this.radius;
  605. switch (this.cameraRigMode) {
  606. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  607. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  608. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  609. case Camera.RIG_MODE_VR:
  610. camLeft.alpha = this.alpha - this._cameraRigParams.stereoHalfAngle;
  611. camRight.alpha = this.alpha + this._cameraRigParams.stereoHalfAngle;
  612. break;
  613. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  614. camLeft.alpha = this.alpha + this._cameraRigParams.stereoHalfAngle;
  615. camRight.alpha = this.alpha - this._cameraRigParams.stereoHalfAngle;
  616. break;
  617. }
  618. super._updateRigCameras();
  619. }
  620. public dispose(): void {
  621. this.inputs.clear();
  622. super.dispose();
  623. }
  624. public getClassName(): string {
  625. return "ArcRotateCamera";
  626. }
  627. }
  628. }