babylon.camera.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. module BABYLON {
  2. export class VRCameraMetrics {
  3. public hResolution: number;
  4. public vResolution: number;
  5. public hScreenSize: number;
  6. public vScreenSize: number;
  7. public vScreenCenter: number;
  8. public eyeToScreenDistance: number;
  9. public lensSeparationDistance: number;
  10. public interpupillaryDistance: number;
  11. public distortionK: number[];
  12. public chromaAbCorrection: number[];
  13. public postProcessScaleFactor: number;
  14. public lensCenterOffset: number;
  15. public compensateDistorsion = true;
  16. public get aspectRatio(): number {
  17. return this.hResolution / (2 * this.vResolution);
  18. }
  19. public get aspectRatioFov(): number {
  20. return (2 * Math.atan((this.postProcessScaleFactor * this.vScreenSize) / (2 * this.eyeToScreenDistance)));
  21. }
  22. public get leftHMatrix(): Matrix {
  23. var meters = (this.hScreenSize / 4) - (this.lensSeparationDistance / 2);
  24. var h = (4 * meters) / this.hScreenSize;
  25. return Matrix.Translation(h, 0, 0);
  26. }
  27. public get rightHMatrix(): Matrix {
  28. var meters = (this.hScreenSize / 4) - (this.lensSeparationDistance / 2);
  29. var h = (4 * meters) / this.hScreenSize;
  30. return Matrix.Translation(-h, 0, 0);
  31. }
  32. public get leftPreViewMatrix(): Matrix {
  33. return Matrix.Translation(0.5 * this.interpupillaryDistance, 0, 0);
  34. }
  35. public get rightPreViewMatrix(): Matrix {
  36. return Matrix.Translation(-0.5 * this.interpupillaryDistance, 0, 0);
  37. }
  38. public static GetDefault(): VRCameraMetrics {
  39. var result = new VRCameraMetrics();
  40. result.hResolution = 1280;
  41. result.vResolution = 800;
  42. result.hScreenSize = 0.149759993;
  43. result.vScreenSize = 0.0935999975;
  44. result.vScreenCenter = 0.0467999987,
  45. result.eyeToScreenDistance = 0.0410000011;
  46. result.lensSeparationDistance = 0.0635000020;
  47. result.interpupillaryDistance = 0.0640000030;
  48. result.distortionK = [1.0, 0.219999999, 0.239999995, 0.0];
  49. result.chromaAbCorrection = [0.995999992, -0.00400000019, 1.01400006, 0.0];
  50. result.postProcessScaleFactor = 1.714605507808412;
  51. result.lensCenterOffset = 0.151976421;
  52. return result;
  53. }
  54. }
  55. export class Camera extends Node {
  56. // Statics
  57. private static _PERSPECTIVE_CAMERA = 0;
  58. private static _ORTHOGRAPHIC_CAMERA = 1;
  59. private static _FOVMODE_VERTICAL_FIXED = 0;
  60. private static _FOVMODE_HORIZONTAL_FIXED = 1;
  61. private static _RIG_MODE_NONE = 0;
  62. private static _RIG_MODE_STEREOSCOPIC_ANAGLYPH = 10;
  63. private static _RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL = 11;
  64. private static _RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED = 12;
  65. private static _RIG_MODE_STEREOSCOPIC_OVERUNDER = 13;
  66. private static _RIG_MODE_VR = 20;
  67. public static get PERSPECTIVE_CAMERA(): number {
  68. return Camera._PERSPECTIVE_CAMERA;
  69. }
  70. public static get ORTHOGRAPHIC_CAMERA(): number {
  71. return Camera._ORTHOGRAPHIC_CAMERA;
  72. }
  73. public static get FOVMODE_VERTICAL_FIXED(): number {
  74. return Camera._FOVMODE_VERTICAL_FIXED;
  75. }
  76. public static get FOVMODE_HORIZONTAL_FIXED(): number {
  77. return Camera._FOVMODE_HORIZONTAL_FIXED;
  78. }
  79. public static get RIG_MODE_NONE(): number {
  80. return Camera._RIG_MODE_NONE;
  81. }
  82. public static get RIG_MODE_STEREOSCOPIC_ANAGLYPH(): number {
  83. return Camera._RIG_MODE_STEREOSCOPIC_ANAGLYPH;
  84. }
  85. public static get RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL(): number {
  86. return Camera._RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL;
  87. }
  88. public static get RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED(): number {
  89. return Camera._RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED;
  90. }
  91. public static get RIG_MODE_STEREOSCOPIC_OVERUNDER(): number {
  92. return Camera._RIG_MODE_STEREOSCOPIC_OVERUNDER;
  93. }
  94. public static get RIG_MODE_VR(): number {
  95. return Camera._RIG_MODE_VR;
  96. }
  97. // Members
  98. public upVector = Vector3.Up();
  99. public orthoLeft = null;
  100. public orthoRight = null;
  101. public orthoBottom = null;
  102. public orthoTop = null;
  103. public fov = 0.8;
  104. public minZ = 1.0;
  105. public maxZ = 10000.0;
  106. public inertia = 0.9;
  107. public mode = Camera.PERSPECTIVE_CAMERA;
  108. public isIntermediate = false;
  109. public viewport = new Viewport(0, 0, 1.0, 1.0);
  110. public layerMask: number = 0x0FFFFFFF;
  111. public fovMode: number = Camera.FOVMODE_VERTICAL_FIXED;
  112. // Camera rig members
  113. public cameraRigMode = Camera.RIG_MODE_NONE;
  114. public _cameraRigParams: any;
  115. public _rigCameras = new Array<Camera>();
  116. // Cache
  117. private _computedViewMatrix = Matrix.Identity();
  118. public _projectionMatrix = new Matrix();
  119. private _worldMatrix: Matrix;
  120. public _postProcesses = new Array<PostProcess>();
  121. public _postProcessesTakenIndices = [];
  122. public _activeMeshes = new SmartArray<Mesh>(256);
  123. private _globalPosition = Vector3.Zero();
  124. constructor(name: string, public position: Vector3, scene: Scene) {
  125. super(name, scene);
  126. scene.addCamera(this);
  127. if (!scene.activeCamera) {
  128. scene.activeCamera = this;
  129. }
  130. }
  131. public get globalPosition(): Vector3 {
  132. return this._globalPosition;
  133. }
  134. public getActiveMeshes(): SmartArray<Mesh> {
  135. return this._activeMeshes;
  136. }
  137. public isActiveMesh(mesh: Mesh): boolean {
  138. return (this._activeMeshes.indexOf(mesh) !== -1);
  139. }
  140. //Cache
  141. public _initCache() {
  142. super._initCache();
  143. this._cache.position = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  144. this._cache.upVector = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  145. this._cache.mode = undefined;
  146. this._cache.minZ = undefined;
  147. this._cache.maxZ = undefined;
  148. this._cache.fov = undefined;
  149. this._cache.aspectRatio = undefined;
  150. this._cache.orthoLeft = undefined;
  151. this._cache.orthoRight = undefined;
  152. this._cache.orthoBottom = undefined;
  153. this._cache.orthoTop = undefined;
  154. this._cache.renderWidth = undefined;
  155. this._cache.renderHeight = undefined;
  156. }
  157. public _updateCache(ignoreParentClass?: boolean): void {
  158. if (!ignoreParentClass) {
  159. super._updateCache();
  160. }
  161. var engine = this.getEngine();
  162. this._cache.position.copyFrom(this.position);
  163. this._cache.upVector.copyFrom(this.upVector);
  164. this._cache.mode = this.mode;
  165. this._cache.minZ = this.minZ;
  166. this._cache.maxZ = this.maxZ;
  167. this._cache.fov = this.fov;
  168. this._cache.aspectRatio = engine.getAspectRatio(this);
  169. this._cache.orthoLeft = this.orthoLeft;
  170. this._cache.orthoRight = this.orthoRight;
  171. this._cache.orthoBottom = this.orthoBottom;
  172. this._cache.orthoTop = this.orthoTop;
  173. this._cache.renderWidth = engine.getRenderWidth();
  174. this._cache.renderHeight = engine.getRenderHeight();
  175. }
  176. public _updateFromScene(): void {
  177. this.updateCache();
  178. this._update();
  179. }
  180. // Synchronized
  181. public _isSynchronized(): boolean {
  182. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  183. }
  184. public _isSynchronizedViewMatrix(): boolean {
  185. if (!super._isSynchronized())
  186. return false;
  187. return this._cache.position.equals(this.position)
  188. && this._cache.upVector.equals(this.upVector)
  189. && this.isSynchronizedWithParent();
  190. }
  191. public _isSynchronizedProjectionMatrix(): boolean {
  192. var check = this._cache.mode === this.mode
  193. && this._cache.minZ === this.minZ
  194. && this._cache.maxZ === this.maxZ;
  195. if (!check) {
  196. return false;
  197. }
  198. var engine = this.getEngine();
  199. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  200. check = this._cache.fov === this.fov
  201. && this._cache.aspectRatio === engine.getAspectRatio(this);
  202. }
  203. else {
  204. check = this._cache.orthoLeft === this.orthoLeft
  205. && this._cache.orthoRight === this.orthoRight
  206. && this._cache.orthoBottom === this.orthoBottom
  207. && this._cache.orthoTop === this.orthoTop
  208. && this._cache.renderWidth === engine.getRenderWidth()
  209. && this._cache.renderHeight === engine.getRenderHeight();
  210. }
  211. return check;
  212. }
  213. // Controls
  214. public attachControl(element: HTMLElement): void {
  215. }
  216. public detachControl(element: HTMLElement): void {
  217. }
  218. public _update(): void {
  219. this._checkInputs();
  220. if (this.cameraRigMode !== Camera.RIG_MODE_NONE) {
  221. this._updateRigCameras();
  222. }
  223. }
  224. public _checkInputs(): void {
  225. }
  226. public attachPostProcess(postProcess: PostProcess, insertAt: number = null): number {
  227. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  228. Tools.Error("You're trying to reuse a post process not defined as reusable.");
  229. return 0;
  230. }
  231. if (insertAt == null || insertAt < 0) {
  232. this._postProcesses.push(postProcess);
  233. this._postProcessesTakenIndices.push(this._postProcesses.length - 1);
  234. return this._postProcesses.length - 1;
  235. }
  236. var add = 0;
  237. if (this._postProcesses[insertAt]) {
  238. var start = this._postProcesses.length - 1;
  239. for (var i = start; i >= insertAt + 1; --i) {
  240. this._postProcesses[i + 1] = this._postProcesses[i];
  241. }
  242. add = 1;
  243. }
  244. for (i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  245. if (this._postProcessesTakenIndices[i] < insertAt) {
  246. continue;
  247. }
  248. start = this._postProcessesTakenIndices.length - 1;
  249. for (var j = start; j >= i; --j) {
  250. this._postProcessesTakenIndices[j + 1] = this._postProcessesTakenIndices[j] + add;
  251. }
  252. this._postProcessesTakenIndices[i] = insertAt;
  253. break;
  254. }
  255. if (!add && this._postProcessesTakenIndices.indexOf(insertAt) == -1) {
  256. this._postProcessesTakenIndices.push(insertAt);
  257. }
  258. var result = insertAt + add;
  259. this._postProcesses[result] = postProcess;
  260. return result;
  261. }
  262. public detachPostProcess(postProcess: PostProcess, atIndices: any = null): number[] {
  263. var result = [];
  264. if (!atIndices) {
  265. var length = this._postProcesses.length;
  266. for (var i = 0; i < length; i++) {
  267. if (this._postProcesses[i] !== postProcess) {
  268. continue;
  269. }
  270. delete this._postProcesses[i];
  271. var index = this._postProcessesTakenIndices.indexOf(i);
  272. this._postProcessesTakenIndices.splice(index, 1);
  273. }
  274. }
  275. else {
  276. atIndices = (atIndices instanceof Array) ? atIndices : [atIndices];
  277. for (i = 0; i < atIndices.length; i++) {
  278. var foundPostProcess = this._postProcesses[atIndices[i]];
  279. if (foundPostProcess !== postProcess) {
  280. result.push(i);
  281. continue;
  282. }
  283. delete this._postProcesses[atIndices[i]];
  284. index = this._postProcessesTakenIndices.indexOf(atIndices[i]);
  285. this._postProcessesTakenIndices.splice(index, 1);
  286. }
  287. }
  288. return result;
  289. }
  290. public getWorldMatrix(): Matrix {
  291. if (!this._worldMatrix) {
  292. this._worldMatrix = Matrix.Identity();
  293. }
  294. var viewMatrix = this.getViewMatrix();
  295. viewMatrix.invertToRef(this._worldMatrix);
  296. return this._worldMatrix;
  297. }
  298. public _getViewMatrix(): Matrix {
  299. return Matrix.Identity();
  300. }
  301. public getViewMatrix(force?: boolean): Matrix {
  302. this._computedViewMatrix = this._computeViewMatrix(force);
  303. if (!force && this._isSynchronizedViewMatrix()) {
  304. return this._computedViewMatrix;
  305. }
  306. if (!this.parent || !this.parent.getWorldMatrix) {
  307. this._globalPosition.copyFrom(this.position);
  308. } else {
  309. if (!this._worldMatrix) {
  310. this._worldMatrix = Matrix.Identity();
  311. }
  312. this._computedViewMatrix.invertToRef(this._worldMatrix);
  313. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  314. this._globalPosition.copyFromFloats(this._computedViewMatrix.m[12], this._computedViewMatrix.m[13], this._computedViewMatrix.m[14]);
  315. this._computedViewMatrix.invert();
  316. this._markSyncedWithParent();
  317. }
  318. this._currentRenderId = this.getScene().getRenderId();
  319. return this._computedViewMatrix;
  320. }
  321. public _computeViewMatrix(force?: boolean): Matrix {
  322. if (!force && this._isSynchronizedViewMatrix()) {
  323. return this._computedViewMatrix;
  324. }
  325. this._computedViewMatrix = this._getViewMatrix();
  326. this._currentRenderId = this.getScene().getRenderId();
  327. return this._computedViewMatrix;
  328. }
  329. public getProjectionMatrix(force?: boolean): Matrix {
  330. if (!force && this._isSynchronizedProjectionMatrix()) {
  331. return this._projectionMatrix;
  332. }
  333. var engine = this.getEngine();
  334. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  335. if (this.minZ <= 0) {
  336. this.minZ = 0.1;
  337. }
  338. Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(this), this.minZ, this.maxZ, this._projectionMatrix, this.fovMode);
  339. return this._projectionMatrix;
  340. }
  341. var halfWidth = engine.getRenderWidth() / 2.0;
  342. var halfHeight = engine.getRenderHeight() / 2.0;
  343. Matrix.OrthoOffCenterLHToRef(this.orthoLeft || -halfWidth, this.orthoRight || halfWidth, this.orthoBottom || -halfHeight, this.orthoTop || halfHeight, this.minZ, this.maxZ, this._projectionMatrix);
  344. return this._projectionMatrix;
  345. }
  346. public dispose(): void {
  347. // Remove from scene
  348. this.getScene().removeCamera(this);
  349. while (this._rigCameras.length > 0) {
  350. this._rigCameras.pop().dispose();
  351. }
  352. // Postprocesses
  353. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  354. this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
  355. }
  356. }
  357. // ---- Camera rigs section ----
  358. public setCameraRigMode(mode: number, rigParams: any): void {
  359. while (this._rigCameras.length > 0) {
  360. this._rigCameras.pop().dispose();
  361. }
  362. this.cameraRigMode = mode;
  363. this._cameraRigParams = {};
  364. switch (this.cameraRigMode) {
  365. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  366. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  367. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  368. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  369. this._cameraRigParams.interaxialDistance = rigParams.interaxialDistance || 0.0637;
  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.stereoHalfAngle = Tools.ToRadians(this._cameraRigParams.interaxialDistance / 0.0637);
  373. this._rigCameras.push(this.createRigCamera(this.name + "_L", 0));
  374. this._rigCameras.push(this.createRigCamera(this.name + "_R", 1));
  375. break;
  376. }
  377. var postProcesses = new Array<PostProcess>();
  378. switch (this.cameraRigMode) {
  379. case Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH:
  380. postProcesses.push(new PassPostProcess(this.name + "_passthru", 1.0, this._rigCameras[0]));
  381. this._rigCameras[0].isIntermediate = true;
  382. postProcesses.push(new AnaglyphPostProcess(this.name + "_anaglyph", 1.0, this._rigCameras[1]));
  383. postProcesses[1].onApply = effect => {
  384. effect.setTextureFromPostProcess("leftSampler", postProcesses[0]);
  385. };
  386. break;
  387. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL:
  388. case Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED:
  389. case Camera.RIG_MODE_STEREOSCOPIC_OVERUNDER:
  390. var isStereoscopicHoriz = (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_PARALLEL || this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED);
  391. var firstCamIndex = (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_SIDEBYSIDE_CROSSEYED)? 1 : 0;
  392. var secondCamIndex = 1 - firstCamIndex;
  393. postProcesses.push(new PassPostProcess(this.name + "_passthru", 1.0, this._rigCameras[firstCamIndex]));
  394. this._rigCameras[firstCamIndex].isIntermediate = true;
  395. postProcesses.push(new StereoscopicInterlacePostProcess(this.name + "_stereoInterlace", this._rigCameras[secondCamIndex], postProcesses[0], isStereoscopicHoriz));
  396. break;
  397. case Camera.RIG_MODE_VR:
  398. this._rigCameras.push(this.createRigCamera(this.name + "_L", 0));
  399. this._rigCameras.push(this.createRigCamera(this.name + "_R", 1));
  400. var metrics = rigParams.vrCameraMetrics || VRCameraMetrics.GetDefault();
  401. this._rigCameras[0]._cameraRigParams.vrMetrics = metrics;
  402. this._rigCameras[0].viewport = new Viewport(0, 0, 0.5, 1.0);
  403. this._rigCameras[0]._cameraRigParams.vrWorkMatrix = new Matrix();
  404. this._rigCameras[0]._cameraRigParams.vrHMatrix = metrics.leftHMatrix;
  405. this._rigCameras[0]._cameraRigParams.vrPreViewMatrix = metrics.leftPreViewMatrix;
  406. this._rigCameras[0].getProjectionMatrix = this._rigCameras[0]._getVRProjectionMatrix;
  407. if (metrics.compensateDistorsion) {
  408. postProcesses.push(new VRDistortionCorrectionPostProcess("VR_Distort_Compensation_Left", this._rigCameras[0], false, metrics));
  409. }
  410. this._rigCameras[1]._cameraRigParams.vrMetrics = this._rigCameras[0]._cameraRigParams.vrMetrics;
  411. this._rigCameras[1].viewport = new Viewport(0.5, 0, 0.5, 1.0);
  412. this._rigCameras[1]._cameraRigParams.vrWorkMatrix = new Matrix();
  413. this._rigCameras[1]._cameraRigParams.vrHMatrix = metrics.rightHMatrix;
  414. this._rigCameras[1]._cameraRigParams.vrPreViewMatrix = metrics.rightPreViewMatrix;
  415. this._rigCameras[1].getProjectionMatrix = this._rigCameras[1]._getVRProjectionMatrix;
  416. if (metrics.compensateDistorsion) {
  417. postProcesses.push(new VRDistortionCorrectionPostProcess("VR_Distort_Compensation_Right", this._rigCameras[1], true, metrics));
  418. }
  419. break;
  420. }
  421. this._update();
  422. }
  423. private _getVRProjectionMatrix(): Matrix {
  424. Matrix.PerspectiveFovLHToRef(this._cameraRigParams.vrMetrics.aspectRatioFov, this._cameraRigParams.vrMetrics.aspectRatio, this.minZ, this.maxZ, this._cameraRigParams.vrWorkMatrix);
  425. this._cameraRigParams.vrWorkMatrix.multiplyToRef(this._cameraRigParams.vrHMatrix, this._projectionMatrix);
  426. return this._projectionMatrix;
  427. }
  428. public setCameraRigParameter(name: string, value: any) {
  429. this._cameraRigParams[name] = value;
  430. //provisionnally:
  431. if (name === "interaxialDistance") { this._cameraRigParams.stereoHalfAngle = Tools.ToRadians(value); }
  432. }
  433. /**
  434. * May needs to be overridden by children so sub has required properties to be copied
  435. */
  436. public createRigCamera(name: string, cameraIndex: number): Camera {
  437. return null;
  438. }
  439. /**
  440. * May needs to be overridden by children
  441. */
  442. public _updateRigCameras() {
  443. for (var i=0 ; i<this._rigCameras.length ; i++) {
  444. this._rigCameras[i].minZ = this.minZ;
  445. this._rigCameras[i].maxZ = this.maxZ;
  446. this._rigCameras[i].fov = this.fov;
  447. }
  448. // only update viewport when ANAGLYPH
  449. if (this.cameraRigMode === Camera.RIG_MODE_STEREOSCOPIC_ANAGLYPH) {
  450. this._rigCameras[0].viewport = this._rigCameras[1].viewport = this.viewport;
  451. }
  452. }
  453. }
  454. }