babylon.camera.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. module BABYLON {
  2. export class Camera extends Node {
  3. public inputs: CameraInputsManager<Camera>;
  4. // Statics
  5. private static _PERSPECTIVE_CAMERA = 0;
  6. private static _ORTHOGRAPHIC_CAMERA = 1;
  7. private static _FOVMODE_VERTICAL_FIXED = 0;
  8. private static _FOVMODE_HORIZONTAL_FIXED = 1;
  9. private static _RIG_MODE_NONE = 0;
  10. private static _RIG_MODE_STEREOSCOPIC_ANAGLYPH = 10;
  11. private static _RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL = 11;
  12. private static _RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED = 12;
  13. private static _RIG_MODE_STEREOSCOPIC_OVERUNDER = 13;
  14. private static _RIG_MODE_VR = 20;
  15. public static get PERSPECTIVE_CAMERA(): number {
  16. return Camera._PERSPECTIVE_CAMERA;
  17. }
  18. public static get ORTHOGRAPHIC_CAMERA(): number {
  19. return Camera._ORTHOGRAPHIC_CAMERA;
  20. }
  21. public static get FOVMODE_VERTICAL_FIXED(): number {
  22. return Camera._FOVMODE_VERTICAL_FIXED;
  23. }
  24. public static get FOVMODE_HORIZONTAL_FIXED(): number {
  25. return Camera._FOVMODE_HORIZONTAL_FIXED;
  26. }
  27. public static get RIG_MODE_NONE(): number {
  28. return Camera._RIG_MODE_NONE;
  29. }
  30. public static get RIG_MODE_STEREOSCOPIC_ANAGLYPH(): number {
  31. return Camera._RIG_MODE_STEREOSCOPIC_ANAGLYPH;
  32. }
  33. public static get RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL(): number {
  34. return Camera._RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL;
  35. }
  36. public static get RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED(): number {
  37. return Camera._RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED;
  38. }
  39. public static get RIG_MODE_STEREOSCOPIC_OVERUNDER(): number {
  40. return Camera._RIG_MODE_STEREOSCOPIC_OVERUNDER;
  41. }
  42. public static get RIG_MODE_VR(): number {
  43. return Camera._RIG_MODE_VR;
  44. }
  45. public static ForceAttachControlToAlwaysPreventDefault = false;
  46. // Members
  47. @serializeAsVector3()
  48. public position: Vector3;
  49. @serialize()
  50. public upVector = Vector3.Up();
  51. @serialize()
  52. public orthoLeft = null;
  53. @serialize()
  54. public orthoRight = null;
  55. @serialize()
  56. public orthoBottom = null;
  57. @serialize()
  58. public orthoTop = null;
  59. @serialize()
  60. public fov = 0.8;
  61. @serialize()
  62. public minZ = 1.0;
  63. @serialize()
  64. public maxZ = 10000.0;
  65. @serialize()
  66. public inertia = 0.9;
  67. @serialize()
  68. public mode = Camera.PERSPECTIVE_CAMERA;
  69. public isIntermediate = false;
  70. public viewport = new Viewport(0, 0, 1.0, 1.0);
  71. @serialize()
  72. public layerMask: number = 0x0FFFFFFF;
  73. @serialize()
  74. public fovMode: number = Camera.FOVMODE_VERTICAL_FIXED;
  75. // Camera rig members
  76. @serialize()
  77. public cameraRigMode = Camera.RIG_MODE_NONE;
  78. @serialize()
  79. public interaxialDistance: number
  80. @serialize()
  81. public isStereoscopicSideBySide: boolean
  82. public _cameraRigParams: any;
  83. public _rigCameras = new Array<Camera>();
  84. // Cache
  85. private _computedViewMatrix = Matrix.Identity();
  86. public _projectionMatrix = new Matrix();
  87. private _worldMatrix: Matrix;
  88. public _postProcesses = new Array<PostProcess>();
  89. public _postProcessesTakenIndices = [];
  90. public _activeMeshes = new SmartArray<Mesh>(256);
  91. private _globalPosition = Vector3.Zero();
  92. constructor(name: string, position: Vector3, scene: Scene) {
  93. super(name, scene);
  94. scene.addCamera(this);
  95. if (!scene.activeCamera) {
  96. scene.activeCamera = this;
  97. }
  98. this.position = position;
  99. }
  100. /**
  101. * @param {boolean} fullDetails - support for multiple levels of logging within scene loading
  102. */
  103. public toString(fullDetails?: boolean): string {
  104. var ret = "Name: " + this.name;
  105. ret += ", type: " + this.getTypeName();
  106. if (this.animations) {
  107. for (var i = 0; i < this.animations.length; i++) {
  108. ret += ", animation[0]: " + this.animations[i].toString(fullDetails);
  109. }
  110. }
  111. if (fullDetails) {
  112. }
  113. return ret;
  114. }
  115. public get globalPosition(): Vector3 {
  116. return this._globalPosition;
  117. }
  118. public getActiveMeshes(): SmartArray<Mesh> {
  119. return this._activeMeshes;
  120. }
  121. public isActiveMesh(mesh: Mesh): boolean {
  122. return (this._activeMeshes.indexOf(mesh) !== -1);
  123. }
  124. //Cache
  125. public _initCache() {
  126. super._initCache();
  127. this._cache.position = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  128. this._cache.upVector = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  129. this._cache.mode = undefined;
  130. this._cache.minZ = undefined;
  131. this._cache.maxZ = undefined;
  132. this._cache.fov = undefined;
  133. this._cache.aspectRatio = undefined;
  134. this._cache.orthoLeft = undefined;
  135. this._cache.orthoRight = undefined;
  136. this._cache.orthoBottom = undefined;
  137. this._cache.orthoTop = undefined;
  138. this._cache.renderWidth = undefined;
  139. this._cache.renderHeight = undefined;
  140. }
  141. public _updateCache(ignoreParentClass?: boolean): void {
  142. if (!ignoreParentClass) {
  143. super._updateCache();
  144. }
  145. var engine = this.getEngine();
  146. this._cache.position.copyFrom(this.position);
  147. this._cache.upVector.copyFrom(this.upVector);
  148. this._cache.mode = this.mode;
  149. this._cache.minZ = this.minZ;
  150. this._cache.maxZ = this.maxZ;
  151. this._cache.fov = this.fov;
  152. this._cache.aspectRatio = engine.getAspectRatio(this);
  153. this._cache.orthoLeft = this.orthoLeft;
  154. this._cache.orthoRight = this.orthoRight;
  155. this._cache.orthoBottom = this.orthoBottom;
  156. this._cache.orthoTop = this.orthoTop;
  157. this._cache.renderWidth = engine.getRenderWidth();
  158. this._cache.renderHeight = engine.getRenderHeight();
  159. }
  160. public _updateFromScene(): void {
  161. this.updateCache();
  162. this._update();
  163. }
  164. // Synchronized
  165. public _isSynchronized(): boolean {
  166. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  167. }
  168. public _isSynchronizedViewMatrix(): boolean {
  169. if (!super._isSynchronized())
  170. return false;
  171. return this._cache.position.equals(this.position)
  172. && this._cache.upVector.equals(this.upVector)
  173. && this.isSynchronizedWithParent();
  174. }
  175. public _isSynchronizedProjectionMatrix(): boolean {
  176. var check = this._cache.mode === this.mode
  177. && this._cache.minZ === this.minZ
  178. && this._cache.maxZ === this.maxZ;
  179. if (!check) {
  180. return false;
  181. }
  182. var engine = this.getEngine();
  183. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  184. check = this._cache.fov === this.fov
  185. && this._cache.aspectRatio === engine.getAspectRatio(this);
  186. }
  187. else {
  188. check = this._cache.orthoLeft === this.orthoLeft
  189. && this._cache.orthoRight === this.orthoRight
  190. && this._cache.orthoBottom === this.orthoBottom
  191. && this._cache.orthoTop === this.orthoTop
  192. && this._cache.renderWidth === engine.getRenderWidth()
  193. && this._cache.renderHeight === engine.getRenderHeight();
  194. }
  195. return check;
  196. }
  197. // Controls
  198. public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
  199. }
  200. public detachControl(element: HTMLElement): void {
  201. }
  202. public _update(): void {
  203. if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  204. this._updateRigCameras();
  205. }
  206. this._checkInputs();
  207. }
  208. public _checkInputs(): void {
  209. }
  210. public attachPostProcess(postProcess: PostProcess, insertAt: number = null): number {
  211. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  212. Tools.Error("You're trying to reuse a post process not defined as reusable.");
  213. return 0;
  214. }
  215. if (insertAt == null || insertAt < 0) {
  216. this._postProcesses.push(postProcess);
  217. this._postProcessesTakenIndices.push(this._postProcesses.length - 1);
  218. return this._postProcesses.length - 1;
  219. }
  220. var add = 0;
  221. var i: number;
  222. var start: number;
  223. if (this._postProcesses[insertAt]) {
  224. start = this._postProcesses.length - 1;
  225. for (i = start; i >= insertAt + 1; --i) {
  226. this._postProcesses[i + 1] = this._postProcesses[i];
  227. }
  228. add = 1;
  229. }
  230. for (i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  231. if (this._postProcessesTakenIndices[i] < insertAt) {
  232. continue;
  233. }
  234. start = this._postProcessesTakenIndices.length - 1;
  235. for (var j = start; j >= i; --j) {
  236. this._postProcessesTakenIndices[j + 1] = this._postProcessesTakenIndices[j] + add;
  237. }
  238. this._postProcessesTakenIndices[i] = insertAt;
  239. break;
  240. }
  241. if (!add && this._postProcessesTakenIndices.indexOf(insertAt) === -1) {
  242. this._postProcessesTakenIndices.push(insertAt);
  243. }
  244. var result = insertAt + add;
  245. this._postProcesses[result] = postProcess;
  246. return result;
  247. }
  248. public detachPostProcess(postProcess: PostProcess, atIndices: any = null): number[] {
  249. var result = [];
  250. var i: number;
  251. var index: number;
  252. if (!atIndices) {
  253. var length = this._postProcesses.length;
  254. for (i = 0; i < length; i++) {
  255. if (this._postProcesses[i] !== postProcess) {
  256. continue;
  257. }
  258. delete this._postProcesses[i];
  259. index = this._postProcessesTakenIndices.indexOf(i);
  260. this._postProcessesTakenIndices.splice(index, 1);
  261. }
  262. }
  263. else {
  264. atIndices = (atIndices instanceof Array) ? atIndices : [atIndices];
  265. for (i = 0; i < atIndices.length; i++) {
  266. var foundPostProcess = this._postProcesses[atIndices[i]];
  267. if (foundPostProcess !== postProcess) {
  268. result.push(i);
  269. continue;
  270. }
  271. delete this._postProcesses[atIndices[i]];
  272. index = this._postProcessesTakenIndices.indexOf(atIndices[i]);
  273. this._postProcessesTakenIndices.splice(index, 1);
  274. }
  275. }
  276. return result;
  277. }
  278. public getWorldMatrix(): Matrix {
  279. if (!this._worldMatrix) {
  280. this._worldMatrix = Matrix.Identity();
  281. }
  282. var viewMatrix = this.getViewMatrix();
  283. viewMatrix.invertToRef(this._worldMatrix);
  284. return this._worldMatrix;
  285. }
  286. public _getViewMatrix(): Matrix {
  287. return Matrix.Identity();
  288. }
  289. public getViewMatrix(force?: boolean): Matrix {
  290. this._computedViewMatrix = this._computeViewMatrix(force);
  291. if (!force && this._isSynchronizedViewMatrix()) {
  292. return this._computedViewMatrix;
  293. }
  294. if (!this.parent || !this.parent.getWorldMatrix) {
  295. this._globalPosition.copyFrom(this.position);
  296. } else {
  297. if (!this._worldMatrix) {
  298. this._worldMatrix = Matrix.Identity();
  299. }
  300. this._computedViewMatrix.invertToRef(this._worldMatrix);
  301. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  302. this._globalPosition.copyFromFloats(this._computedViewMatrix.m[12], this._computedViewMatrix.m[13], this._computedViewMatrix.m[14]);
  303. this._computedViewMatrix.invert();
  304. this._markSyncedWithParent();
  305. }
  306. this._currentRenderId = this.getScene().getRenderId();
  307. return this._computedViewMatrix;
  308. }
  309. public _computeViewMatrix(force?: boolean): Matrix {
  310. if (!force && this._isSynchronizedViewMatrix()) {
  311. return this._computedViewMatrix;
  312. }
  313. this._computedViewMatrix = this._getViewMatrix();
  314. this._currentRenderId = this.getScene().getRenderId();
  315. return this._computedViewMatrix;
  316. }
  317. public getProjectionMatrix(force?: boolean): Matrix {
  318. if (!force && this._isSynchronizedProjectionMatrix()) {
  319. return this._projectionMatrix;
  320. }
  321. var engine = this.getEngine();
  322. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  323. if (this.minZ <= 0) {
  324. this.minZ = 0.1;
  325. }
  326. Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(this), this.minZ, this.maxZ, this._projectionMatrix, this.fovMode === Camera.FOVMODE_VERTICAL_FIXED);
  327. return this._projectionMatrix;
  328. }
  329. var halfWidth = engine.getRenderWidth() / 2.0;
  330. var halfHeight = engine.getRenderHeight() / 2.0;
  331. Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  332. return this._projectionMatrix;
  333. }
  334. public dispose(): void {
  335. // Animations
  336. this.getScene().stopAnimation(this);
  337. // Remove from scene
  338. this.getScene().removeCamera(this);
  339. while (this._rigCameras.length > 0) {
  340. this._rigCameras.pop().dispose();
  341. }
  342. // Postprocesses
  343. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  344. this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
  345. }
  346. }
  347. // ---- Camera rigs section ----
  348. public setCameraRigMode(mode: number, rigParams: any): void {
  349. while (this._rigCameras.length > 0) {
  350. this._rigCameras.pop().dispose();
  351. }
  352. this.cameraRigMode = mode;
  353. this._cameraRigParams = {};
  354. switch (this.cameraRigMode) {
  355. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  356. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  357. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  358. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  359. this._cameraRigParams.interaxialDistance = rigParams.interaxialDistance || 0.0637;
  360. //we have to implement stereo camera calcultating left and right viewpoints from interaxialDistance and target,
  361. //not from a given angle as it is now, but until that complete code rewriting provisional stereoHalfAngle value is introduced
  362. this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(this._cameraRigParams.interaxialDistance / 0.0637);
  363. this._rigCameras.push(this.createRigCamera(this.name + "_L", 0));
  364. this._rigCameras.push(this.createRigCamera(this.name + "_R", 1));
  365. break;
  366. }
  367. var postProcesses = new Array<PostProcess>();
  368. switch (this.cameraRigMode) {
  369. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  370. postProcesses.push(new PassPostProcess(this.name + "_passthru", 1.0, this._rigCameras[0]));
  371. this._rigCameras[0].isIntermediate = true;
  372. postProcesses.push(new AnaglyphPostProcess(this.name + "_anaglyph", 1.0, this._rigCameras[1]));
  373. postProcesses[1].onApply = effect => {
  374. effect.setTextureFromPostProcess("leftSampler", postProcesses[0]);
  375. };
  376. break;
  377. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  378. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  379. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  380. var isStereoscopicHoriz = (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL || this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED);
  381. var firstCamIndex = (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED) ? 1 : 0;
  382. var secondCamIndex = 1 - firstCamIndex;
  383. postProcesses.push(new PassPostProcess(this.name + "_passthru", 1.0, this._rigCameras[firstCamIndex]));
  384. this._rigCameras[firstCamIndex].isIntermediate = true;
  385. postProcesses.push(new StereoscopicInterlacePostProcess(this.name + "_stereoInterlace", this._rigCameras[secondCamIndex], postProcesses[0], isStereoscopicHoriz));
  386. break;
  387. case Camera.RIG_MODE_VR:
  388. this._rigCameras.push(this.createRigCamera(this.name + "_L", 0));
  389. this._rigCameras.push(this.createRigCamera(this.name + "_R", 1));
  390. var metrics = rigParams.vrCameraMetrics || VRCameraMetrics.GetDefault();
  391. this._rigCameras[0]._cameraRigParams.vrMetrics = metrics;
  392. this._rigCameras[0].viewport = new Viewport(0, 0, 0.5, 1.0);
  393. this._rigCameras[0]._cameraRigParams.vrWorkMatrix = new Matrix();
  394. this._rigCameras[0]._cameraRigParams.vrHMatrix = metrics.leftHMatrix;
  395. this._rigCameras[0]._cameraRigParams.vrPreViewMatrix = metrics.leftPreViewMatrix;
  396. this._rigCameras[0].getProjectionMatrix = this._rigCameras[0]._getVRProjectionMatrix;
  397. if (metrics.compensateDistortion) {
  398. postProcesses.push(new VRDistortionCorrectionPostProcess("VR_Distort_Compensation_Left", this._rigCameras[0], false, metrics));
  399. }
  400. this._rigCameras[1]._cameraRigParams.vrMetrics = this._rigCameras[0]._cameraRigParams.vrMetrics;
  401. this._rigCameras[1].viewport = new Viewport(0.5, 0, 0.5, 1.0);
  402. this._rigCameras[1]._cameraRigParams.vrWorkMatrix = new Matrix();
  403. this._rigCameras[1]._cameraRigParams.vrHMatrix = metrics.rightHMatrix;
  404. this._rigCameras[1]._cameraRigParams.vrPreViewMatrix = metrics.rightPreViewMatrix;
  405. this._rigCameras[1].getProjectionMatrix = this._rigCameras[1]._getVRProjectionMatrix;
  406. if (metrics.compensateDistortion) {
  407. postProcesses.push(new VRDistortionCorrectionPostProcess("VR_Distort_Compensation_Right", this._rigCameras[1], true, metrics));
  408. }
  409. break;
  410. }
  411. this._update();
  412. }
  413. private _getVRProjectionMatrix(): Matrix {
  414. Matrix.PerspectiveFovLHToRef(this._cameraRigParams.vrMetrics.aspectRatioFov, this._cameraRigParams.vrMetrics.aspectRatio, this.minZ, this.maxZ, this._cameraRigParams.vrWorkMatrix);
  415. this._cameraRigParams.vrWorkMatrix.multiplyToRef(this._cameraRigParams.vrHMatrix, this._projectionMatrix);
  416. return this._projectionMatrix;
  417. }
  418. public setCameraRigParameter(name: string, value: any) {
  419. this._cameraRigParams[name] = value;
  420. //provisionnally:
  421. if (name === "interaxialDistance") {
  422. this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(value / 0.0637);
  423. }
  424. }
  425. /**
  426. * May needs to be overridden by children so sub has required properties to be copied
  427. */
  428. public createRigCamera(name: string, cameraIndex: number): Camera {
  429. return null;
  430. }
  431. /**
  432. * May needs to be overridden by children
  433. */
  434. public _updateRigCameras() {
  435. for (var i = 0; i < this._rigCameras.length; i++) {
  436. this._rigCameras[i].minZ = this.minZ;
  437. this._rigCameras[i].maxZ = this.maxZ;
  438. this._rigCameras[i].fov = this.fov;
  439. }
  440. // only update viewport when ANAGLYPH
  441. if (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH) {
  442. this._rigCameras[0].viewport = this._rigCameras[1].viewport = this.viewport;
  443. }
  444. }
  445. public serialize(): any {
  446. var serializationObject = SerializationHelper.Serialize(this);
  447. // Type
  448. serializationObject.type = this.getTypeName();
  449. // Parent
  450. if (this.parent) {
  451. serializationObject.parentId = this.parent.id;
  452. }
  453. if (this.inputs) {
  454. this.inputs.serialize(serializationObject);
  455. }
  456. // Animations
  457. Animation.AppendSerializedAnimations(this, serializationObject);
  458. serializationObject.ranges = this.serializeAnimationRanges();
  459. return serializationObject;
  460. }
  461. public getTypeName(): string {
  462. return "Camera";
  463. }
  464. public clone(name: string): Camera {
  465. return SerializationHelper.Clone(Camera.GetConstructorFromName(this.getTypeName(), name, this.getScene(), this.interaxialDistance, this.isStereoscopicSideBySide), this);
  466. }
  467. static GetConstructorFromName(type: string, name: string, scene: Scene, interaxial_distance: number = 0, isStereoscopicSideBySide: boolean = true): () => Camera {
  468. switch (type) {
  469. case "ArcRotateCamera":
  470. return () => new ArcRotateCamera(name, 0, 0, 1.0, Vector3.Zero(), scene);
  471. case "DeviceOrientationCamera":
  472. return () => new DeviceOrientationCamera(name, Vector3.Zero(), scene);
  473. case "FollowCamera":
  474. return () => new FollowCamera(name, Vector3.Zero(), scene);
  475. case "ArcFollowCamera":
  476. return () => new ArcFollowCamera(name, 0, 0, 1.0, null, scene);
  477. case "GamepadCamera":
  478. return () => new GamepadCamera(name, Vector3.Zero(), scene);
  479. case "TouchCamera":
  480. return () => new TouchCamera(name, Vector3.Zero(), scene);
  481. case "VirtualJoysticksCamera":
  482. return () => new VirtualJoysticksCamera(name, Vector3.Zero(), scene);
  483. case "WebVRFreeCamera":
  484. return () => new WebVRFreeCamera(name, Vector3.Zero(), scene);
  485. case "VRDeviceOrientationFreeCamera":
  486. return () => new VRDeviceOrientationFreeCamera(name, Vector3.Zero(), scene);
  487. case "AnaglyphArcRotateCamera":
  488. return () => new AnaglyphArcRotateCamera(name, 0, 0, 1.0, Vector3.Zero(), interaxial_distance, scene);
  489. case "AnaglyphFreeCamera":
  490. return () => new AnaglyphFreeCamera(name, Vector3.Zero(), interaxial_distance, scene);
  491. case "AnaglyphGamepadCamera":
  492. return () => new AnaglyphGamepadCamera(name, Vector3.Zero(), interaxial_distance, scene);
  493. case "AnaglyphUniversalCamera":
  494. return () => new AnaglyphUniversalCamera(name, Vector3.Zero(), interaxial_distance, scene);
  495. case "StereoscopicArcRotateCamera":
  496. return () => new StereoscopicArcRotateCamera(name, 0, 0, 1.0, Vector3.Zero(), interaxial_distance, isStereoscopicSideBySide, scene);
  497. case "StereoscopicFreeCamera":
  498. return () => new StereoscopicFreeCamera(name, Vector3.Zero(), interaxial_distance, isStereoscopicSideBySide, scene);
  499. case "StereoscopicGamepadCamera":
  500. return () => new StereoscopicGamepadCamera(name, Vector3.Zero(), interaxial_distance, isStereoscopicSideBySide, scene);
  501. case "StereoscopicUniversalCamera":
  502. return () => new StereoscopicUniversalCamera(name, Vector3.Zero(), interaxial_distance, isStereoscopicSideBySide, scene);
  503. case "FreeCamera": // Forcing Universal here
  504. return () => new UniversalCamera(name, Vector3.Zero(), scene);
  505. default: // Universal Camera is the default value
  506. return () => new UniversalCamera(name, Vector3.Zero(), scene);
  507. }
  508. }
  509. public static Parse(parsedCamera: any, scene: Scene): Camera {
  510. var type = parsedCamera.type;
  511. var construct = Camera.GetConstructorFromName(type, parsedCamera.name, scene, parsedCamera.interaxial_distance, parsedCamera.isStereoscopicSideBySide);
  512. var camera = SerializationHelper.Parse(construct, parsedCamera, scene);
  513. // Parent
  514. if (parsedCamera.parentId) {
  515. camera._waitingParentId = parsedCamera.parentId;
  516. }
  517. //If camera has an input manager, let it parse inputs settings
  518. if (camera.inputs) {
  519. camera.inputs.parse(parsedCamera);
  520. }
  521. // Target
  522. if (parsedCamera.target) {
  523. if ((<any>camera).setTarget) {
  524. (<any>camera).setTarget(Vector3.FromArray(parsedCamera.target));
  525. }
  526. }
  527. // Apply 3d rig, when found
  528. if (parsedCamera.cameraRigMode) {
  529. var rigParams = (parsedCamera.interaxial_distance) ? { interaxialDistance: parsedCamera.interaxial_distance } : {};
  530. camera.setCameraRigMode(parsedCamera.cameraRigMode, rigParams);
  531. }
  532. // Animations
  533. if (parsedCamera.animations) {
  534. for (var animationIndex = 0; animationIndex < parsedCamera.animations.length; animationIndex++) {
  535. var parsedAnimation = parsedCamera.animations[animationIndex];
  536. camera.animations.push(Animation.Parse(parsedAnimation));
  537. }
  538. Node.ParseAnimationRanges(camera, parsedCamera, scene);
  539. }
  540. if (parsedCamera.autoAnimate) {
  541. scene.beginAnimation(camera, parsedCamera.autoAnimateFrom, parsedCamera.autoAnimateTo, parsedCamera.autoAnimateLoop, parsedCamera.autoAnimateSpeed || 1.0);
  542. }
  543. return camera;
  544. }
  545. }
  546. }