babylon.camera.js 32 KB

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