babylon.camera.js 30 KB

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