camera.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  1. import { __decorate } from "../tslib.es6.js";
  2. import { serialize, serializeAsVector3 } from "../Misc/decorators.js";
  3. import { SmartArray } from "../Misc/smartArray.js";
  4. import { Tools } from "../Misc/tools.js";
  5. import { Observable } from "../Misc/observable.js";
  6. import { Matrix, Vector3, Quaternion } from "../Maths/math.vector.js";
  7. import { Node } from "../node.js";
  8. import { Logger } from "../Misc/logger.js";
  9. import { GetClass } from "../Misc/typeStore.js";
  10. import { _WarnImport } from "../Misc/devTools.js";
  11. import { Viewport } from "../Maths/math.viewport.js";
  12. import { Frustum } from "../Maths/math.frustum.js";
  13. import { SerializationHelper } from "../Misc/decorators.serialization.js";
  14. /**
  15. * This is the base class of all the camera used in the application.
  16. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras
  17. */
  18. export class Camera extends Node {
  19. /**
  20. * Define the current local position of the camera in the scene
  21. */
  22. get position() {
  23. return this._position;
  24. }
  25. set position(newPosition) {
  26. this._position = newPosition;
  27. }
  28. /**
  29. * The vector the camera should consider as up.
  30. * (default is Vector3(0, 1, 0) aka Vector3.Up())
  31. */
  32. set upVector(vec) {
  33. this._upVector = vec;
  34. }
  35. get upVector() {
  36. return this._upVector;
  37. }
  38. /**
  39. * The screen area in scene units squared
  40. */
  41. get screenArea() {
  42. let x = 0;
  43. let y = 0;
  44. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  45. if (this.fovMode === Camera.FOVMODE_VERTICAL_FIXED) {
  46. y = this.minZ * 2 * Math.tan(this.fov / 2);
  47. x = this.getEngine().getAspectRatio(this) * y;
  48. }
  49. else {
  50. x = this.minZ * 2 * Math.tan(this.fov / 2);
  51. y = x / this.getEngine().getAspectRatio(this);
  52. }
  53. }
  54. else {
  55. const halfWidth = this.getEngine().getRenderWidth() / 2.0;
  56. const halfHeight = this.getEngine().getRenderHeight() / 2.0;
  57. x = (this.orthoRight ?? halfWidth) - (this.orthoLeft ?? -halfWidth);
  58. y = (this.orthoTop ?? halfHeight) - (this.orthoBottom ?? -halfHeight);
  59. }
  60. return x * y;
  61. }
  62. /**
  63. * Define the current limit on the left side for an orthographic camera
  64. * In scene unit
  65. */
  66. set orthoLeft(value) {
  67. this._orthoLeft = value;
  68. for (const rigCamera of this._rigCameras) {
  69. rigCamera.orthoLeft = value;
  70. }
  71. }
  72. get orthoLeft() {
  73. return this._orthoLeft;
  74. }
  75. /**
  76. * Define the current limit on the right side for an orthographic camera
  77. * In scene unit
  78. */
  79. set orthoRight(value) {
  80. this._orthoRight = value;
  81. for (const rigCamera of this._rigCameras) {
  82. rigCamera.orthoRight = value;
  83. }
  84. }
  85. get orthoRight() {
  86. return this._orthoRight;
  87. }
  88. /**
  89. * Define the current limit on the bottom side for an orthographic camera
  90. * In scene unit
  91. */
  92. set orthoBottom(value) {
  93. this._orthoBottom = value;
  94. for (const rigCamera of this._rigCameras) {
  95. rigCamera.orthoBottom = value;
  96. }
  97. }
  98. get orthoBottom() {
  99. return this._orthoBottom;
  100. }
  101. /**
  102. * Define the current limit on the top side for an orthographic camera
  103. * In scene unit
  104. */
  105. set orthoTop(value) {
  106. this._orthoTop = value;
  107. for (const rigCamera of this._rigCameras) {
  108. rigCamera.orthoTop = value;
  109. }
  110. }
  111. get orthoTop() {
  112. return this._orthoTop;
  113. }
  114. /**
  115. * Define the mode of the camera (Camera.PERSPECTIVE_CAMERA or Camera.ORTHOGRAPHIC_CAMERA)
  116. */
  117. set mode(mode) {
  118. this._mode = mode;
  119. // Pass the mode down to the rig cameras
  120. for (const rigCamera of this._rigCameras) {
  121. rigCamera.mode = mode;
  122. }
  123. }
  124. get mode() {
  125. return this._mode;
  126. }
  127. /**
  128. * Gets a flag indicating that the camera has moved in some way since the last call to Camera.update()
  129. */
  130. get hasMoved() {
  131. return this._hasMoved;
  132. }
  133. /**
  134. * Instantiates a new camera object.
  135. * This should not be used directly but through the inherited cameras: ArcRotate, Free...
  136. * @see https://doc.babylonjs.com/features/featuresDeepDive/cameras
  137. * @param name Defines the name of the camera in the scene
  138. * @param position Defines the position of the camera
  139. * @param scene Defines the scene the camera belongs too
  140. * @param setActiveOnSceneIfNoneActive Defines if the camera should be set as active after creation if no other camera have been defined in the scene
  141. */
  142. constructor(name, position, scene, setActiveOnSceneIfNoneActive = true) {
  143. super(name, scene);
  144. /** @internal */
  145. this._position = Vector3.Zero();
  146. this._upVector = Vector3.Up();
  147. /**
  148. * Object containing oblique projection values (only used with ORTHOGRAPHIC_CAMERA)
  149. */
  150. this.oblique = null;
  151. this._orthoLeft = null;
  152. this._orthoRight = null;
  153. this._orthoBottom = null;
  154. this._orthoTop = null;
  155. /**
  156. * Field Of View is set in Radians. (default is 0.8)
  157. */
  158. this.fov = 0.8;
  159. /**
  160. * Projection plane tilt around the X axis (horizontal), set in Radians. (default is 0)
  161. * Can be used to make vertical lines in world space actually vertical on the screen.
  162. * See https://forum.babylonjs.com/t/add-vertical-shift-to-3ds-max-exporter-babylon-cameras/17480
  163. */
  164. this.projectionPlaneTilt = 0;
  165. /**
  166. * Define the minimum distance the camera can see from.
  167. * This is important to note that the depth buffer are not infinite and the closer it starts
  168. * the more your scene might encounter depth fighting issue.
  169. */
  170. this.minZ = 1;
  171. /**
  172. * Define the maximum distance the camera can see to.
  173. * This is important to note that the depth buffer are not infinite and the further it end
  174. * the more your scene might encounter depth fighting issue.
  175. */
  176. this.maxZ = 10000.0;
  177. /**
  178. * Define the default inertia of the camera.
  179. * This helps giving a smooth feeling to the camera movement.
  180. */
  181. this.inertia = 0.9;
  182. this._mode = Camera.PERSPECTIVE_CAMERA;
  183. /**
  184. * Define whether the camera is intermediate.
  185. * This is useful to not present the output directly to the screen in case of rig without post process for instance
  186. */
  187. this.isIntermediate = false;
  188. /**
  189. * Define the viewport of the camera.
  190. * This correspond to the portion of the screen the camera will render to in normalized 0 to 1 unit.
  191. */
  192. this.viewport = new Viewport(0, 0, 1.0, 1.0);
  193. /**
  194. * Restricts the camera to viewing objects with the same layerMask.
  195. * A camera with a layerMask of 1 will render mesh.layerMask & camera.layerMask!== 0
  196. */
  197. this.layerMask = 0x0fffffff;
  198. /**
  199. * fovMode sets the camera frustum bounds to the viewport bounds. (default is FOVMODE_VERTICAL_FIXED)
  200. */
  201. this.fovMode = Camera.FOVMODE_VERTICAL_FIXED;
  202. /**
  203. * Rig mode of the camera.
  204. * This is useful to create the camera with two "eyes" instead of one to create VR or stereoscopic scenes.
  205. * This is normally controlled byt the camera themselves as internal use.
  206. */
  207. this.cameraRigMode = Camera.RIG_MODE_NONE;
  208. /**
  209. * Defines the list of custom render target which are rendered to and then used as the input to this camera's render. Eg. display another camera view on a TV in the main scene
  210. * This is pretty helpful if you wish to make a camera render to a texture you could reuse somewhere
  211. * else in the scene. (Eg. security camera)
  212. *
  213. * To change the final output target of the camera, camera.outputRenderTarget should be used instead (eg. webXR renders to a render target corresponding to an HMD)
  214. */
  215. this.customRenderTargets = [];
  216. /**
  217. * When set, the camera will render to this render target instead of the default canvas
  218. *
  219. * If the desire is to use the output of a camera as a texture in the scene consider using camera.customRenderTargets instead
  220. */
  221. this.outputRenderTarget = null;
  222. /**
  223. * Observable triggered when the camera view matrix has changed.
  224. */
  225. this.onViewMatrixChangedObservable = new Observable();
  226. /**
  227. * Observable triggered when the camera Projection matrix has changed.
  228. */
  229. this.onProjectionMatrixChangedObservable = new Observable();
  230. /**
  231. * Observable triggered when the inputs have been processed.
  232. */
  233. this.onAfterCheckInputsObservable = new Observable();
  234. /**
  235. * Observable triggered when reset has been called and applied to the camera.
  236. */
  237. this.onRestoreStateObservable = new Observable();
  238. /**
  239. * Is this camera a part of a rig system?
  240. */
  241. this.isRigCamera = false;
  242. this._hasMoved = false;
  243. /** @internal */
  244. this._rigCameras = new Array();
  245. /** @internal */
  246. this._skipRendering = false;
  247. /** @internal */
  248. this._projectionMatrix = new Matrix();
  249. /** @internal */
  250. this._postProcesses = new Array();
  251. /** @internal */
  252. this._activeMeshes = new SmartArray(256);
  253. this._globalPosition = Vector3.Zero();
  254. /** @internal */
  255. this._computedViewMatrix = Matrix.Identity();
  256. this._doNotComputeProjectionMatrix = false;
  257. this._transformMatrix = Matrix.Zero();
  258. this._refreshFrustumPlanes = true;
  259. this._absoluteRotation = Quaternion.Identity();
  260. /** @internal */
  261. this._isCamera = true;
  262. /** @internal */
  263. this._isLeftCamera = false;
  264. /** @internal */
  265. this._isRightCamera = false;
  266. this.getScene().addCamera(this);
  267. if (setActiveOnSceneIfNoneActive && !this.getScene().activeCamera) {
  268. this.getScene().activeCamera = this;
  269. }
  270. this.position = position;
  271. this.renderPassId = this.getScene().getEngine().createRenderPassId(`Camera ${name}`);
  272. }
  273. /**
  274. * Store current camera state (fov, position, etc..)
  275. * @returns the camera
  276. */
  277. storeState() {
  278. this._stateStored = true;
  279. this._storedFov = this.fov;
  280. return this;
  281. }
  282. /**
  283. * Restores the camera state values if it has been stored. You must call storeState() first
  284. * @returns true if restored and false otherwise
  285. */
  286. _restoreStateValues() {
  287. if (!this._stateStored) {
  288. return false;
  289. }
  290. this.fov = this._storedFov;
  291. return true;
  292. }
  293. /**
  294. * Restored camera state. You must call storeState() first.
  295. * @returns true if restored and false otherwise
  296. */
  297. restoreState() {
  298. if (this._restoreStateValues()) {
  299. this.onRestoreStateObservable.notifyObservers(this);
  300. return true;
  301. }
  302. return false;
  303. }
  304. /**
  305. * Gets the class name of the camera.
  306. * @returns the class name
  307. */
  308. getClassName() {
  309. return "Camera";
  310. }
  311. /**
  312. * Gets a string representation of the camera useful for debug purpose.
  313. * @param fullDetails Defines that a more verbose level of logging is required
  314. * @returns the string representation
  315. */
  316. toString(fullDetails) {
  317. let ret = "Name: " + this.name;
  318. ret += ", type: " + this.getClassName();
  319. if (this.animations) {
  320. for (let i = 0; i < this.animations.length; i++) {
  321. ret += ", animation[0]: " + this.animations[i].toString(fullDetails);
  322. }
  323. }
  324. return ret;
  325. }
  326. /**
  327. * Automatically tilts the projection plane, using `projectionPlaneTilt`, to correct the perspective effect on vertical lines.
  328. */
  329. applyVerticalCorrection() {
  330. const rot = this.absoluteRotation.toEulerAngles();
  331. this.projectionPlaneTilt = this._scene.useRightHandedSystem ? -rot.x : rot.x;
  332. }
  333. /**
  334. * Gets the current world space position of the camera.
  335. */
  336. get globalPosition() {
  337. return this._globalPosition;
  338. }
  339. /**
  340. * Gets the list of active meshes this frame (meshes no culled or excluded by lod s in the frame)
  341. * @returns the active meshe list
  342. */
  343. getActiveMeshes() {
  344. return this._activeMeshes;
  345. }
  346. /**
  347. * Check whether a mesh is part of the current active mesh list of the camera
  348. * @param mesh Defines the mesh to check
  349. * @returns true if active, false otherwise
  350. */
  351. isActiveMesh(mesh) {
  352. return this._activeMeshes.indexOf(mesh) !== -1;
  353. }
  354. /**
  355. * Is this camera ready to be used/rendered
  356. * @param completeCheck defines if a complete check (including post processes) has to be done (false by default)
  357. * @returns true if the camera is ready
  358. */
  359. isReady(completeCheck = false) {
  360. if (completeCheck) {
  361. for (const pp of this._postProcesses) {
  362. if (pp && !pp.isReady()) {
  363. return false;
  364. }
  365. }
  366. }
  367. return super.isReady(completeCheck);
  368. }
  369. /** @internal */
  370. _initCache() {
  371. super._initCache();
  372. this._cache.position = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  373. this._cache.upVector = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  374. this._cache.mode = undefined;
  375. this._cache.minZ = undefined;
  376. this._cache.maxZ = undefined;
  377. this._cache.fov = undefined;
  378. this._cache.fovMode = undefined;
  379. this._cache.aspectRatio = undefined;
  380. this._cache.orthoLeft = undefined;
  381. this._cache.orthoRight = undefined;
  382. this._cache.orthoBottom = undefined;
  383. this._cache.orthoTop = undefined;
  384. this._cache.obliqueAngle = undefined;
  385. this._cache.obliqueLength = undefined;
  386. this._cache.obliqueOffset = undefined;
  387. this._cache.renderWidth = undefined;
  388. this._cache.renderHeight = undefined;
  389. }
  390. /**
  391. * @internal
  392. */
  393. _updateCache(ignoreParentClass) {
  394. if (!ignoreParentClass) {
  395. super._updateCache();
  396. }
  397. this._cache.position.copyFrom(this.position);
  398. this._cache.upVector.copyFrom(this.upVector);
  399. }
  400. /** @internal */
  401. _isSynchronized() {
  402. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  403. }
  404. /** @internal */
  405. _isSynchronizedViewMatrix() {
  406. if (!super._isSynchronized()) {
  407. return false;
  408. }
  409. return this._cache.position.equals(this.position) && this._cache.upVector.equals(this.upVector) && this.isSynchronizedWithParent();
  410. }
  411. /** @internal */
  412. _isSynchronizedProjectionMatrix() {
  413. let isSynchronized = this._cache.mode === this.mode && this._cache.minZ === this.minZ && this._cache.maxZ === this.maxZ;
  414. if (!isSynchronized) {
  415. return false;
  416. }
  417. const engine = this.getEngine();
  418. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  419. isSynchronized =
  420. this._cache.fov === this.fov &&
  421. this._cache.fovMode === this.fovMode &&
  422. this._cache.aspectRatio === engine.getAspectRatio(this) &&
  423. this._cache.projectionPlaneTilt === this.projectionPlaneTilt;
  424. }
  425. else {
  426. isSynchronized =
  427. this._cache.orthoLeft === this.orthoLeft &&
  428. this._cache.orthoRight === this.orthoRight &&
  429. this._cache.orthoBottom === this.orthoBottom &&
  430. this._cache.orthoTop === this.orthoTop &&
  431. this._cache.renderWidth === engine.getRenderWidth() &&
  432. this._cache.renderHeight === engine.getRenderHeight();
  433. if (this.oblique) {
  434. isSynchronized =
  435. isSynchronized &&
  436. this._cache.obliqueAngle === this.oblique.angle &&
  437. this._cache.obliqueLength === this.oblique.length &&
  438. this._cache.obliqueOffset === this.oblique.offset;
  439. }
  440. }
  441. return isSynchronized;
  442. }
  443. /**
  444. * Attach the input controls to a specific dom element to get the input from.
  445. * This function is here because typescript removes the typing of the last function.
  446. * @param _ignored defines an ignored parameter kept for backward compatibility.
  447. * @param _noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
  448. */
  449. attachControl(_ignored, _noPreventDefault) { }
  450. /**
  451. * Detach the current controls from the specified dom element.
  452. * This function is here because typescript removes the typing of the last function.
  453. * @param _ignored defines an ignored parameter kept for backward compatibility.
  454. */
  455. detachControl(_ignored) { }
  456. /**
  457. * Update the camera state according to the different inputs gathered during the frame.
  458. */
  459. update() {
  460. this._hasMoved = false;
  461. this._checkInputs();
  462. if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  463. this._updateRigCameras();
  464. }
  465. // Attempt to update the camera's view and projection matrices.
  466. // This call is being made because these matrices are no longer being updated
  467. // as a part of the picking ray process (in addition to scene.render).
  468. this.getViewMatrix();
  469. this.getProjectionMatrix();
  470. }
  471. /** @internal */
  472. _checkInputs() {
  473. this.onAfterCheckInputsObservable.notifyObservers(this);
  474. }
  475. /** @internal */
  476. get rigCameras() {
  477. return this._rigCameras;
  478. }
  479. /**
  480. * Gets the post process used by the rig cameras
  481. */
  482. get rigPostProcess() {
  483. return this._rigPostProcess;
  484. }
  485. /**
  486. * Internal, gets the first post process.
  487. * @returns the first post process to be run on this camera.
  488. */
  489. _getFirstPostProcess() {
  490. for (let ppIndex = 0; ppIndex < this._postProcesses.length; ppIndex++) {
  491. if (this._postProcesses[ppIndex] !== null) {
  492. return this._postProcesses[ppIndex];
  493. }
  494. }
  495. return null;
  496. }
  497. _cascadePostProcessesToRigCams() {
  498. // invalidate framebuffer
  499. const firstPostProcess = this._getFirstPostProcess();
  500. if (firstPostProcess) {
  501. firstPostProcess.markTextureDirty();
  502. }
  503. // glue the rigPostProcess to the end of the user postprocesses & assign to each sub-camera
  504. for (let i = 0, len = this._rigCameras.length; i < len; i++) {
  505. const cam = this._rigCameras[i];
  506. const rigPostProcess = cam._rigPostProcess;
  507. // for VR rig, there does not have to be a post process
  508. if (rigPostProcess) {
  509. const isPass = rigPostProcess.getEffectName() === "pass";
  510. if (isPass) {
  511. // any rig which has a PassPostProcess for rig[0], cannot be isIntermediate when there are also user postProcesses
  512. cam.isIntermediate = this._postProcesses.length === 0;
  513. }
  514. cam._postProcesses = this._postProcesses.slice(0).concat(rigPostProcess);
  515. rigPostProcess.markTextureDirty();
  516. }
  517. else {
  518. cam._postProcesses = this._postProcesses.slice(0);
  519. }
  520. }
  521. }
  522. /**
  523. * Attach a post process to the camera.
  524. * @see https://doc.babylonjs.com/features/featuresDeepDive/postProcesses/usePostProcesses#attach-postprocess
  525. * @param postProcess The post process to attach to the camera
  526. * @param insertAt The position of the post process in case several of them are in use in the scene
  527. * @returns the position the post process has been inserted at
  528. */
  529. attachPostProcess(postProcess, insertAt = null) {
  530. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  531. Logger.Error("You're trying to reuse a post process not defined as reusable.");
  532. return 0;
  533. }
  534. if (insertAt == null || insertAt < 0) {
  535. this._postProcesses.push(postProcess);
  536. }
  537. else if (this._postProcesses[insertAt] === null) {
  538. this._postProcesses[insertAt] = postProcess;
  539. }
  540. else {
  541. this._postProcesses.splice(insertAt, 0, postProcess);
  542. }
  543. this._cascadePostProcessesToRigCams(); // also ensures framebuffer invalidated
  544. // Update prePass
  545. if (this._scene.prePassRenderer) {
  546. this._scene.prePassRenderer.markAsDirty();
  547. }
  548. return this._postProcesses.indexOf(postProcess);
  549. }
  550. /**
  551. * Detach a post process to the camera.
  552. * @see https://doc.babylonjs.com/features/featuresDeepDive/postProcesses/usePostProcesses#attach-postprocess
  553. * @param postProcess The post process to detach from the camera
  554. */
  555. detachPostProcess(postProcess) {
  556. const idx = this._postProcesses.indexOf(postProcess);
  557. if (idx !== -1) {
  558. this._postProcesses[idx] = null;
  559. }
  560. // Update prePass
  561. if (this._scene.prePassRenderer) {
  562. this._scene.prePassRenderer.markAsDirty();
  563. }
  564. this._cascadePostProcessesToRigCams(); // also ensures framebuffer invalidated
  565. }
  566. /**
  567. * Gets the current world matrix of the camera
  568. * @returns the world matrix
  569. */
  570. getWorldMatrix() {
  571. if (this._isSynchronizedViewMatrix()) {
  572. return this._worldMatrix;
  573. }
  574. // Getting the view matrix will also compute the world matrix.
  575. this.getViewMatrix();
  576. return this._worldMatrix;
  577. }
  578. /** @internal */
  579. _getViewMatrix() {
  580. return Matrix.Identity();
  581. }
  582. /**
  583. * Gets the current view matrix of the camera.
  584. * @param force forces the camera to recompute the matrix without looking at the cached state
  585. * @returns the view matrix
  586. */
  587. getViewMatrix(force) {
  588. if (!force && this._isSynchronizedViewMatrix()) {
  589. return this._computedViewMatrix;
  590. }
  591. this._hasMoved = true;
  592. this.updateCache();
  593. this._computedViewMatrix = this._getViewMatrix();
  594. this._currentRenderId = this.getScene().getRenderId();
  595. this._childUpdateId++;
  596. this._refreshFrustumPlanes = true;
  597. if (this._cameraRigParams && this._cameraRigParams.vrPreViewMatrix) {
  598. this._computedViewMatrix.multiplyToRef(this._cameraRigParams.vrPreViewMatrix, this._computedViewMatrix);
  599. }
  600. // Notify parent camera if rig camera is changed
  601. if (this.parent && this.parent.onViewMatrixChangedObservable) {
  602. this.parent.onViewMatrixChangedObservable.notifyObservers(this.parent);
  603. }
  604. this.onViewMatrixChangedObservable.notifyObservers(this);
  605. this._computedViewMatrix.invertToRef(this._worldMatrix);
  606. return this._computedViewMatrix;
  607. }
  608. /**
  609. * Freeze the projection matrix.
  610. * It will prevent the cache check of the camera projection compute and can speed up perf
  611. * if no parameter of the camera are meant to change
  612. * @param projection Defines manually a projection if necessary
  613. */
  614. freezeProjectionMatrix(projection) {
  615. this._doNotComputeProjectionMatrix = true;
  616. if (projection !== undefined) {
  617. this._projectionMatrix = projection;
  618. }
  619. }
  620. /**
  621. * Unfreeze the projection matrix if it has previously been freezed by freezeProjectionMatrix.
  622. */
  623. unfreezeProjectionMatrix() {
  624. this._doNotComputeProjectionMatrix = false;
  625. }
  626. /**
  627. * Gets the current projection matrix of the camera.
  628. * @param force forces the camera to recompute the matrix without looking at the cached state
  629. * @returns the projection matrix
  630. */
  631. getProjectionMatrix(force) {
  632. if (this._doNotComputeProjectionMatrix || (!force && this._isSynchronizedProjectionMatrix())) {
  633. return this._projectionMatrix;
  634. }
  635. // Cache
  636. this._cache.mode = this.mode;
  637. this._cache.minZ = this.minZ;
  638. this._cache.maxZ = this.maxZ;
  639. // Matrix
  640. this._refreshFrustumPlanes = true;
  641. const engine = this.getEngine();
  642. const scene = this.getScene();
  643. const reverseDepth = engine.useReverseDepthBuffer;
  644. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  645. this._cache.fov = this.fov;
  646. this._cache.fovMode = this.fovMode;
  647. this._cache.aspectRatio = engine.getAspectRatio(this);
  648. this._cache.projectionPlaneTilt = this.projectionPlaneTilt;
  649. if (this.minZ <= 0) {
  650. this.minZ = 0.1;
  651. }
  652. let getProjectionMatrix;
  653. if (scene.useRightHandedSystem) {
  654. getProjectionMatrix = Matrix.PerspectiveFovRHToRef;
  655. }
  656. else {
  657. getProjectionMatrix = Matrix.PerspectiveFovLHToRef;
  658. }
  659. getProjectionMatrix(this.fov, engine.getAspectRatio(this), reverseDepth ? this.maxZ : this.minZ, reverseDepth ? this.minZ : this.maxZ, this._projectionMatrix, this.fovMode === Camera.FOVMODE_VERTICAL_FIXED, engine.isNDCHalfZRange, this.projectionPlaneTilt, reverseDepth);
  660. }
  661. else {
  662. const halfWidth = engine.getRenderWidth() / 2.0;
  663. const halfHeight = engine.getRenderHeight() / 2.0;
  664. if (scene.useRightHandedSystem) {
  665. if (this.oblique) {
  666. Matrix.ObliqueOffCenterRHToRef(this.orthoLeft ?? -halfWidth, this.orthoRight ?? halfWidth, this.orthoBottom ?? -halfHeight, this.orthoTop ?? halfHeight, reverseDepth ? this.maxZ : this.minZ, reverseDepth ? this.minZ : this.maxZ, this.oblique.length, this.oblique.angle, this._computeObliqueDistance(this.oblique.offset), this._projectionMatrix, engine.isNDCHalfZRange);
  667. }
  668. else {
  669. Matrix.OrthoOffCenterRHToRef(this.orthoLeft ?? -halfWidth, this.orthoRight ?? halfWidth, this.orthoBottom ?? -halfHeight, this.orthoTop ?? halfHeight, reverseDepth ? this.maxZ : this.minZ, reverseDepth ? this.minZ : this.maxZ, this._projectionMatrix, engine.isNDCHalfZRange);
  670. }
  671. }
  672. else {
  673. if (this.oblique) {
  674. Matrix.ObliqueOffCenterLHToRef(this.orthoLeft ?? -halfWidth, this.orthoRight ?? halfWidth, this.orthoBottom ?? -halfHeight, this.orthoTop ?? halfHeight, reverseDepth ? this.maxZ : this.minZ, reverseDepth ? this.minZ : this.maxZ, this.oblique.length, this.oblique.angle, this._computeObliqueDistance(this.oblique.offset), this._projectionMatrix, engine.isNDCHalfZRange);
  675. }
  676. else {
  677. Matrix.OrthoOffCenterLHToRef(this.orthoLeft ?? -halfWidth, this.orthoRight ?? halfWidth, this.orthoBottom ?? -halfHeight, this.orthoTop ?? halfHeight, reverseDepth ? this.maxZ : this.minZ, reverseDepth ? this.minZ : this.maxZ, this._projectionMatrix, engine.isNDCHalfZRange);
  678. }
  679. }
  680. this._cache.orthoLeft = this.orthoLeft;
  681. this._cache.orthoRight = this.orthoRight;
  682. this._cache.orthoBottom = this.orthoBottom;
  683. this._cache.orthoTop = this.orthoTop;
  684. this._cache.obliqueAngle = this.oblique?.angle;
  685. this._cache.obliqueLength = this.oblique?.length;
  686. this._cache.obliqueOffset = this.oblique?.offset;
  687. this._cache.renderWidth = engine.getRenderWidth();
  688. this._cache.renderHeight = engine.getRenderHeight();
  689. }
  690. this.onProjectionMatrixChangedObservable.notifyObservers(this);
  691. return this._projectionMatrix;
  692. }
  693. /**
  694. * Gets the transformation matrix (ie. the multiplication of view by projection matrices)
  695. * @returns a Matrix
  696. */
  697. getTransformationMatrix() {
  698. this._computedViewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  699. return this._transformMatrix;
  700. }
  701. _computeObliqueDistance(offset) {
  702. const arcRotateCamera = this;
  703. const targetCamera = this;
  704. return (arcRotateCamera.radius || (targetCamera.target ? Vector3.Distance(this.position, targetCamera.target) : this.position.length())) + offset;
  705. }
  706. _updateFrustumPlanes() {
  707. if (!this._refreshFrustumPlanes) {
  708. return;
  709. }
  710. this.getTransformationMatrix();
  711. if (!this._frustumPlanes) {
  712. this._frustumPlanes = Frustum.GetPlanes(this._transformMatrix);
  713. }
  714. else {
  715. Frustum.GetPlanesToRef(this._transformMatrix, this._frustumPlanes);
  716. }
  717. this._refreshFrustumPlanes = false;
  718. }
  719. /**
  720. * Checks if a cullable object (mesh...) is in the camera frustum
  721. * This checks the bounding box center. See isCompletelyInFrustum for a full bounding check
  722. * @param target The object to check
  723. * @param checkRigCameras If the rig cameras should be checked (eg. with VR camera both eyes should be checked) (Default: false)
  724. * @returns true if the object is in frustum otherwise false
  725. */
  726. isInFrustum(target, checkRigCameras = false) {
  727. this._updateFrustumPlanes();
  728. if (checkRigCameras && this.rigCameras.length > 0) {
  729. let result = false;
  730. this.rigCameras.forEach((cam) => {
  731. cam._updateFrustumPlanes();
  732. result = result || target.isInFrustum(cam._frustumPlanes);
  733. });
  734. return result;
  735. }
  736. else {
  737. return target.isInFrustum(this._frustumPlanes);
  738. }
  739. }
  740. /**
  741. * Checks if a cullable object (mesh...) is in the camera frustum
  742. * Unlike isInFrustum this checks the full bounding box
  743. * @param target The object to check
  744. * @returns true if the object is in frustum otherwise false
  745. */
  746. isCompletelyInFrustum(target) {
  747. this._updateFrustumPlanes();
  748. return target.isCompletelyInFrustum(this._frustumPlanes);
  749. }
  750. // eslint-disable-next-line jsdoc/require-returns-check
  751. /**
  752. * Gets a ray in the forward direction from the camera.
  753. * @param length Defines the length of the ray to create
  754. * @param transform Defines the transform to apply to the ray, by default the world matrix is used to create a workd space ray
  755. * @param origin Defines the start point of the ray which defaults to the camera position
  756. * @returns the forward ray
  757. */
  758. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  759. getForwardRay(length = 100, transform, origin) {
  760. throw _WarnImport("Ray");
  761. }
  762. // eslint-disable-next-line jsdoc/require-returns-check
  763. /**
  764. * Gets a ray in the forward direction from the camera.
  765. * @param refRay the ray to (re)use when setting the values
  766. * @param length Defines the length of the ray to create
  767. * @param transform Defines the transform to apply to the ray, by default the world matrx is used to create a workd space ray
  768. * @param origin Defines the start point of the ray which defaults to the camera position
  769. * @returns the forward ray
  770. */
  771. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  772. getForwardRayToRef(refRay, length = 100, transform, origin) {
  773. throw _WarnImport("Ray");
  774. }
  775. /**
  776. * Releases resources associated with this node.
  777. * @param doNotRecurse Set to true to not recurse into each children (recurse into each children by default)
  778. * @param disposeMaterialAndTextures Set to true to also dispose referenced materials and textures (false by default)
  779. */
  780. dispose(doNotRecurse, disposeMaterialAndTextures = false) {
  781. // Observables
  782. this.onViewMatrixChangedObservable.clear();
  783. this.onProjectionMatrixChangedObservable.clear();
  784. this.onAfterCheckInputsObservable.clear();
  785. this.onRestoreStateObservable.clear();
  786. // Inputs
  787. if (this.inputs) {
  788. this.inputs.clear();
  789. }
  790. // Animations
  791. this.getScene().stopAnimation(this);
  792. // Remove from scene
  793. this.getScene().removeCamera(this);
  794. while (this._rigCameras.length > 0) {
  795. const camera = this._rigCameras.pop();
  796. if (camera) {
  797. camera.dispose();
  798. }
  799. }
  800. if (this._parentContainer) {
  801. const index = this._parentContainer.cameras.indexOf(this);
  802. if (index > -1) {
  803. this._parentContainer.cameras.splice(index, 1);
  804. }
  805. this._parentContainer = null;
  806. }
  807. // Postprocesses
  808. if (this._rigPostProcess) {
  809. this._rigPostProcess.dispose(this);
  810. this._rigPostProcess = null;
  811. this._postProcesses.length = 0;
  812. }
  813. else if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  814. this._rigPostProcess = null;
  815. this._postProcesses.length = 0;
  816. }
  817. else {
  818. let i = this._postProcesses.length;
  819. while (--i >= 0) {
  820. const postProcess = this._postProcesses[i];
  821. if (postProcess) {
  822. postProcess.dispose(this);
  823. }
  824. }
  825. }
  826. // Render targets
  827. let i = this.customRenderTargets.length;
  828. while (--i >= 0) {
  829. this.customRenderTargets[i].dispose();
  830. }
  831. this.customRenderTargets.length = 0;
  832. // Active Meshes
  833. this._activeMeshes.dispose();
  834. this.getScene().getEngine().releaseRenderPassId(this.renderPassId);
  835. super.dispose(doNotRecurse, disposeMaterialAndTextures);
  836. }
  837. /**
  838. * Gets the left camera of a rig setup in case of Rigged Camera
  839. */
  840. get isLeftCamera() {
  841. return this._isLeftCamera;
  842. }
  843. /**
  844. * Gets the right camera of a rig setup in case of Rigged Camera
  845. */
  846. get isRightCamera() {
  847. return this._isRightCamera;
  848. }
  849. /**
  850. * Gets the left camera of a rig setup in case of Rigged Camera
  851. */
  852. get leftCamera() {
  853. if (this._rigCameras.length < 1) {
  854. return null;
  855. }
  856. return this._rigCameras[0];
  857. }
  858. /**
  859. * Gets the right camera of a rig setup in case of Rigged Camera
  860. */
  861. get rightCamera() {
  862. if (this._rigCameras.length < 2) {
  863. return null;
  864. }
  865. return this._rigCameras[1];
  866. }
  867. /**
  868. * Gets the left camera target of a rig setup in case of Rigged Camera
  869. * @returns the target position
  870. */
  871. getLeftTarget() {
  872. if (this._rigCameras.length < 1) {
  873. return null;
  874. }
  875. return this._rigCameras[0].getTarget();
  876. }
  877. /**
  878. * Gets the right camera target of a rig setup in case of Rigged Camera
  879. * @returns the target position
  880. */
  881. getRightTarget() {
  882. if (this._rigCameras.length < 2) {
  883. return null;
  884. }
  885. return this._rigCameras[1].getTarget();
  886. }
  887. /**
  888. * @internal
  889. */
  890. setCameraRigMode(mode, rigParams) {
  891. if (this.cameraRigMode === mode) {
  892. return;
  893. }
  894. while (this._rigCameras.length > 0) {
  895. const camera = this._rigCameras.pop();
  896. if (camera) {
  897. camera.dispose();
  898. }
  899. }
  900. this.cameraRigMode = mode;
  901. this._cameraRigParams = {};
  902. //we have to implement stereo camera calcultating left and right viewpoints from interaxialDistance and target,
  903. //not from a given angle as it is now, but until that complete code rewriting provisional stereoHalfAngle value is introduced
  904. this._cameraRigParams.interaxialDistance = rigParams.interaxialDistance || 0.0637;
  905. this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(this._cameraRigParams.interaxialDistance / 0.0637);
  906. // create the rig cameras, unless none
  907. if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  908. const leftCamera = this.createRigCamera(this.name + "_L", 0);
  909. if (leftCamera) {
  910. leftCamera._isLeftCamera = true;
  911. }
  912. const rightCamera = this.createRigCamera(this.name + "_R", 1);
  913. if (rightCamera) {
  914. rightCamera._isRightCamera = true;
  915. }
  916. if (leftCamera && rightCamera) {
  917. this._rigCameras.push(leftCamera);
  918. this._rigCameras.push(rightCamera);
  919. }
  920. }
  921. this._setRigMode(rigParams);
  922. this._cascadePostProcessesToRigCams();
  923. this.update();
  924. }
  925. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  926. _setRigMode(rigParams) {
  927. // no-op
  928. }
  929. /** @internal */
  930. _getVRProjectionMatrix() {
  931. Matrix.PerspectiveFovLHToRef(this._cameraRigParams.vrMetrics.aspectRatioFov, this._cameraRigParams.vrMetrics.aspectRatio, this.minZ, this.maxZ, this._cameraRigParams.vrWorkMatrix, true, this.getEngine().isNDCHalfZRange);
  932. this._cameraRigParams.vrWorkMatrix.multiplyToRef(this._cameraRigParams.vrHMatrix, this._projectionMatrix);
  933. return this._projectionMatrix;
  934. }
  935. /**
  936. * @internal
  937. */
  938. setCameraRigParameter(name, value) {
  939. if (!this._cameraRigParams) {
  940. this._cameraRigParams = {};
  941. }
  942. this._cameraRigParams[name] = value;
  943. //provisionnally:
  944. if (name === "interaxialDistance") {
  945. this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(value / 0.0637);
  946. }
  947. }
  948. /**
  949. * needs to be overridden by children so sub has required properties to be copied
  950. * @internal
  951. */
  952. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  953. createRigCamera(name, cameraIndex) {
  954. return null;
  955. }
  956. /**
  957. * May need to be overridden by children
  958. * @internal
  959. */
  960. _updateRigCameras() {
  961. for (let i = 0; i < this._rigCameras.length; i++) {
  962. this._rigCameras[i].minZ = this.minZ;
  963. this._rigCameras[i].maxZ = this.maxZ;
  964. this._rigCameras[i].fov = this.fov;
  965. this._rigCameras[i].upVector.copyFrom(this.upVector);
  966. }
  967. // only update viewport when ANAGLYPH
  968. if (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH) {
  969. this._rigCameras[0].viewport = this._rigCameras[1].viewport = this.viewport;
  970. }
  971. }
  972. /** @internal */
  973. _setupInputs() { }
  974. /**
  975. * Serialiaze the camera setup to a json representation
  976. * @returns the JSON representation
  977. */
  978. serialize() {
  979. const serializationObject = SerializationHelper.Serialize(this);
  980. serializationObject.uniqueId = this.uniqueId;
  981. // Type
  982. serializationObject.type = this.getClassName();
  983. // Parent
  984. if (this.parent) {
  985. this.parent._serializeAsParent(serializationObject);
  986. }
  987. if (this.inputs) {
  988. this.inputs.serialize(serializationObject);
  989. }
  990. // Animations
  991. SerializationHelper.AppendSerializedAnimations(this, serializationObject);
  992. serializationObject.ranges = this.serializeAnimationRanges();
  993. serializationObject.isEnabled = this.isEnabled();
  994. return serializationObject;
  995. }
  996. /**
  997. * Clones the current camera.
  998. * @param name The cloned camera name
  999. * @param newParent The cloned camera's new parent (none by default)
  1000. * @returns the cloned camera
  1001. */
  1002. clone(name, newParent = null) {
  1003. const camera = SerializationHelper.Clone(Camera.GetConstructorFromName(this.getClassName(), name, this.getScene(), this.interaxialDistance, this.isStereoscopicSideBySide), this);
  1004. camera.name = name;
  1005. camera.parent = newParent;
  1006. this.onClonedObservable.notifyObservers(camera);
  1007. return camera;
  1008. }
  1009. /**
  1010. * Gets the direction of the camera relative to a given local axis.
  1011. * @param localAxis Defines the reference axis to provide a relative direction.
  1012. * @returns the direction
  1013. */
  1014. getDirection(localAxis) {
  1015. const result = Vector3.Zero();
  1016. this.getDirectionToRef(localAxis, result);
  1017. return result;
  1018. }
  1019. /**
  1020. * Returns the current camera absolute rotation
  1021. */
  1022. get absoluteRotation() {
  1023. this.getWorldMatrix().decompose(undefined, this._absoluteRotation);
  1024. return this._absoluteRotation;
  1025. }
  1026. /**
  1027. * Gets the direction of the camera relative to a given local axis into a passed vector.
  1028. * @param localAxis Defines the reference axis to provide a relative direction.
  1029. * @param result Defines the vector to store the result in
  1030. */
  1031. getDirectionToRef(localAxis, result) {
  1032. Vector3.TransformNormalToRef(localAxis, this.getWorldMatrix(), result);
  1033. }
  1034. /**
  1035. * Gets a camera constructor for a given camera type
  1036. * @param type The type of the camera to construct (should be equal to one of the camera class name)
  1037. * @param name The name of the camera the result will be able to instantiate
  1038. * @param scene The scene the result will construct the camera in
  1039. * @param interaxial_distance In case of stereoscopic setup, the distance between both eyes
  1040. * @param isStereoscopicSideBySide In case of stereoscopic setup, should the sereo be side b side
  1041. * @returns a factory method to construct the camera
  1042. */
  1043. // eslint-disable-next-line @typescript-eslint/naming-convention
  1044. static GetConstructorFromName(type, name, scene, interaxial_distance = 0, isStereoscopicSideBySide = true) {
  1045. const constructorFunc = Node.Construct(type, name, scene, {
  1046. // eslint-disable-next-line @typescript-eslint/naming-convention
  1047. interaxial_distance: interaxial_distance,
  1048. isStereoscopicSideBySide: isStereoscopicSideBySide,
  1049. });
  1050. if (constructorFunc) {
  1051. return constructorFunc;
  1052. }
  1053. // Default to universal camera
  1054. return () => Camera._CreateDefaultParsedCamera(name, scene);
  1055. }
  1056. /**
  1057. * Compute the world matrix of the camera.
  1058. * @returns the camera world matrix
  1059. */
  1060. computeWorldMatrix() {
  1061. return this.getWorldMatrix();
  1062. }
  1063. /**
  1064. * Parse a JSON and creates the camera from the parsed information
  1065. * @param parsedCamera The JSON to parse
  1066. * @param scene The scene to instantiate the camera in
  1067. * @returns the newly constructed camera
  1068. */
  1069. static Parse(parsedCamera, scene) {
  1070. const type = parsedCamera.type;
  1071. const construct = Camera.GetConstructorFromName(type, parsedCamera.name, scene, parsedCamera.interaxial_distance, parsedCamera.isStereoscopicSideBySide);
  1072. const camera = SerializationHelper.Parse(construct, parsedCamera, scene);
  1073. // Parent
  1074. if (parsedCamera.parentId !== undefined) {
  1075. camera._waitingParentId = parsedCamera.parentId;
  1076. }
  1077. // Parent instance index
  1078. if (parsedCamera.parentInstanceIndex !== undefined) {
  1079. camera._waitingParentInstanceIndex = parsedCamera.parentInstanceIndex;
  1080. }
  1081. //If camera has an input manager, let it parse inputs settings
  1082. if (camera.inputs) {
  1083. camera.inputs.parse(parsedCamera);
  1084. camera._setupInputs();
  1085. }
  1086. if (parsedCamera.upVector) {
  1087. camera.upVector = Vector3.FromArray(parsedCamera.upVector); // need to force the upVector
  1088. }
  1089. if (camera.setPosition) {
  1090. // need to force position
  1091. camera.position.copyFromFloats(0, 0, 0);
  1092. camera.setPosition(Vector3.FromArray(parsedCamera.position));
  1093. }
  1094. // Target
  1095. if (parsedCamera.target) {
  1096. if (camera.setTarget) {
  1097. camera.setTarget(Vector3.FromArray(parsedCamera.target));
  1098. }
  1099. }
  1100. // Apply 3d rig, when found
  1101. if (parsedCamera.cameraRigMode) {
  1102. const rigParams = parsedCamera.interaxial_distance ? { interaxialDistance: parsedCamera.interaxial_distance } : {};
  1103. camera.setCameraRigMode(parsedCamera.cameraRigMode, rigParams);
  1104. }
  1105. // Animations
  1106. if (parsedCamera.animations) {
  1107. for (let animationIndex = 0; animationIndex < parsedCamera.animations.length; animationIndex++) {
  1108. const parsedAnimation = parsedCamera.animations[animationIndex];
  1109. const internalClass = GetClass("BABYLON.Animation");
  1110. if (internalClass) {
  1111. camera.animations.push(internalClass.Parse(parsedAnimation));
  1112. }
  1113. }
  1114. Node.ParseAnimationRanges(camera, parsedCamera, scene);
  1115. }
  1116. if (parsedCamera.autoAnimate) {
  1117. scene.beginAnimation(camera, parsedCamera.autoAnimateFrom, parsedCamera.autoAnimateTo, parsedCamera.autoAnimateLoop, parsedCamera.autoAnimateSpeed || 1.0);
  1118. }
  1119. // Check if isEnabled is defined to be back compatible with prior serialized versions.
  1120. if (parsedCamera.isEnabled !== undefined) {
  1121. camera.setEnabled(parsedCamera.isEnabled);
  1122. }
  1123. return camera;
  1124. }
  1125. /** @internal */
  1126. _calculateHandednessMultiplier() {
  1127. let handednessMultiplier = this.getScene().useRightHandedSystem ? -1 : 1;
  1128. if (this.parent && this.parent._getWorldMatrixDeterminant() < 0) {
  1129. handednessMultiplier *= -1;
  1130. }
  1131. return handednessMultiplier;
  1132. }
  1133. }
  1134. /**
  1135. * @internal
  1136. */
  1137. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  1138. Camera._CreateDefaultParsedCamera = (name, scene) => {
  1139. throw _WarnImport("UniversalCamera");
  1140. };
  1141. /**
  1142. * This is the default projection mode used by the cameras.
  1143. * It helps recreating a feeling of perspective and better appreciate depth.
  1144. * This is the best way to simulate real life cameras.
  1145. */
  1146. Camera.PERSPECTIVE_CAMERA = 0;
  1147. /**
  1148. * This helps creating camera with an orthographic mode.
  1149. * Orthographic is commonly used in engineering as a means to produce object specifications that communicate dimensions unambiguously, each line of 1 unit length (cm, meter..whatever) will appear to have the same length everywhere on the drawing. This allows the drafter to dimension only a subset of lines and let the reader know that other lines of that length on the drawing are also that length in reality. Every parallel line in the drawing is also parallel in the object.
  1150. */
  1151. Camera.ORTHOGRAPHIC_CAMERA = 1;
  1152. /**
  1153. * This is the default FOV mode for perspective cameras.
  1154. * This setting aligns the upper and lower bounds of the viewport to the upper and lower bounds of the camera frustum.
  1155. */
  1156. Camera.FOVMODE_VERTICAL_FIXED = 0;
  1157. /**
  1158. * This setting aligns the left and right bounds of the viewport to the left and right bounds of the camera frustum.
  1159. */
  1160. Camera.FOVMODE_HORIZONTAL_FIXED = 1;
  1161. /**
  1162. * This specifies there is no need for a camera rig.
  1163. * Basically only one eye is rendered corresponding to the camera.
  1164. */
  1165. Camera.RIG_MODE_NONE = 0;
  1166. /**
  1167. * Simulates a camera Rig with one blue eye and one red eye.
  1168. * This can be use with 3d blue and red glasses.
  1169. */
  1170. Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH = 10;
  1171. /**
  1172. * Defines that both eyes of the camera will be rendered side by side with a parallel target.
  1173. */
  1174. Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL = 11;
  1175. /**
  1176. * Defines that both eyes of the camera will be rendered side by side with a none parallel target.
  1177. */
  1178. Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED = 12;
  1179. /**
  1180. * Defines that both eyes of the camera will be rendered over under each other.
  1181. */
  1182. Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER = 13;
  1183. /**
  1184. * Defines that both eyes of the camera will be rendered on successive lines interlaced for passive 3d monitors.
  1185. */
  1186. Camera.RIG_MODE_STEREOSCOPIC_INTERLACED = 14;
  1187. /**
  1188. * Defines that both eyes of the camera should be renderered in a VR mode (carbox).
  1189. */
  1190. Camera.RIG_MODE_VR = 20;
  1191. /**
  1192. * Custom rig mode allowing rig cameras to be populated manually with any number of cameras
  1193. */
  1194. Camera.RIG_MODE_CUSTOM = 22;
  1195. /**
  1196. * Defines if by default attaching controls should prevent the default javascript event to continue.
  1197. */
  1198. Camera.ForceAttachControlToAlwaysPreventDefault = false;
  1199. __decorate([
  1200. serializeAsVector3("position")
  1201. ], Camera.prototype, "_position", void 0);
  1202. __decorate([
  1203. serializeAsVector3("upVector")
  1204. ], Camera.prototype, "_upVector", void 0);
  1205. __decorate([
  1206. serialize()
  1207. ], Camera.prototype, "orthoLeft", null);
  1208. __decorate([
  1209. serialize()
  1210. ], Camera.prototype, "orthoRight", null);
  1211. __decorate([
  1212. serialize()
  1213. ], Camera.prototype, "orthoBottom", null);
  1214. __decorate([
  1215. serialize()
  1216. ], Camera.prototype, "orthoTop", null);
  1217. __decorate([
  1218. serialize()
  1219. ], Camera.prototype, "fov", void 0);
  1220. __decorate([
  1221. serialize()
  1222. ], Camera.prototype, "projectionPlaneTilt", void 0);
  1223. __decorate([
  1224. serialize()
  1225. ], Camera.prototype, "minZ", void 0);
  1226. __decorate([
  1227. serialize()
  1228. ], Camera.prototype, "maxZ", void 0);
  1229. __decorate([
  1230. serialize()
  1231. ], Camera.prototype, "inertia", void 0);
  1232. __decorate([
  1233. serialize()
  1234. ], Camera.prototype, "mode", null);
  1235. __decorate([
  1236. serialize()
  1237. ], Camera.prototype, "layerMask", void 0);
  1238. __decorate([
  1239. serialize()
  1240. ], Camera.prototype, "fovMode", void 0);
  1241. __decorate([
  1242. serialize()
  1243. ], Camera.prototype, "cameraRigMode", void 0);
  1244. __decorate([
  1245. serialize()
  1246. ], Camera.prototype, "interaxialDistance", void 0);
  1247. __decorate([
  1248. serialize()
  1249. ], Camera.prototype, "isStereoscopicSideBySide", void 0);
  1250. //# sourceMappingURL=camera.js.map