babylon.camera.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 _SUB_CAMERA_MODE_NONE = 0;
  62. private static _SUB_CAMERA_MODE_ANAGLYPH = 1;
  63. private static _SUB_CAMERA_MODE_HORIZONTAL_STEREOGRAM = 2;
  64. private static _SUB_CAMERA_MODE_VERTICAL_STEREOGRAM = 3;
  65. private static _SUB_CAMERA_MODE_VR = 4;
  66. private static _SUB_CAMERAID_A = 0;
  67. private static _SUB_CAMERAID_B = 1;
  68. public static get PERSPECTIVE_CAMERA(): number {
  69. return Camera._PERSPECTIVE_CAMERA;
  70. }
  71. public static get ORTHOGRAPHIC_CAMERA(): number {
  72. return Camera._ORTHOGRAPHIC_CAMERA;
  73. }
  74. public static get FOVMODE_VERTICAL_FIXED(): number {
  75. return Camera._FOVMODE_VERTICAL_FIXED;
  76. }
  77. public static get FOVMODE_HORIZONTAL_FIXED(): number {
  78. return Camera._FOVMODE_HORIZONTAL_FIXED;
  79. }
  80. public static get SUB_CAMERA_MODE_NONE(): number {
  81. return Camera._SUB_CAMERA_MODE_NONE;
  82. }
  83. public static get SUB_CAMERA_MODE_ANAGLYPH(): number {
  84. return Camera._SUB_CAMERA_MODE_ANAGLYPH;
  85. }
  86. public static get SUB_CAMERA_MODE_HORIZONTAL_STEREOGRAM(): number {
  87. return Camera._SUB_CAMERA_MODE_HORIZONTAL_STEREOGRAM;
  88. }
  89. public static get SUB_CAMERA_MODE_VERTICAL_STEREOGRAM(): number {
  90. return Camera._SUB_CAMERA_MODE_VERTICAL_STEREOGRAM;
  91. }
  92. public static get SUB_CAMERA_MODE_VR(): number {
  93. return Camera._SUB_CAMERA_MODE_VR;
  94. }
  95. public static get SUB_CAMERAID_A(): number {
  96. return Camera._SUB_CAMERAID_A;
  97. }
  98. public static get SUB_CAMERAID_B(): number {
  99. return Camera._SUB_CAMERAID_B;
  100. }
  101. // Members
  102. public upVector = Vector3.Up();
  103. public orthoLeft = null;
  104. public orthoRight = null;
  105. public orthoBottom = null;
  106. public orthoTop = null;
  107. public fov = 0.8;
  108. public minZ = 1.0;
  109. public maxZ = 10000.0;
  110. public inertia = 0.9;
  111. public mode = Camera.PERSPECTIVE_CAMERA;
  112. public isIntermediate = false;
  113. public viewport = new Viewport(0, 0, 1.0, 1.0);
  114. public layerMask: number = 0x0FFFFFFF;
  115. public fovMode: number = Camera.FOVMODE_VERTICAL_FIXED;
  116. // Subcamera members
  117. public subCameras = new Array<Camera>();
  118. public _subCameraMode = Camera.SUB_CAMERA_MODE_NONE;
  119. public _subCamHalfSpace: number;
  120. // VR related
  121. private _vrMetrics: VRCameraMetrics;
  122. private _vrHMatrix: Matrix;
  123. public _vrPreViewMatrix: Matrix;
  124. public _vrWorkMatrix: Matrix;
  125. public _vrActualUp: Vector3;
  126. // Cache
  127. private _computedViewMatrix = Matrix.Identity();
  128. public _projectionMatrix = new Matrix();
  129. private _worldMatrix: Matrix;
  130. public _postProcesses = new Array<PostProcess>();
  131. public _postProcessesTakenIndices = [];
  132. public _activeMeshes = new SmartArray<Mesh>(256);
  133. private _globalPosition = Vector3.Zero();
  134. constructor(name: string, public position: Vector3, scene: Scene) {
  135. super(name, scene);
  136. scene.addCamera(this);
  137. if (!scene.activeCamera) {
  138. scene.activeCamera = this;
  139. }
  140. }
  141. public get globalPosition(): Vector3 {
  142. return this._globalPosition;
  143. }
  144. public getActiveMeshes(): SmartArray<Mesh> {
  145. return this._activeMeshes;
  146. }
  147. public isActiveMesh(mesh: Mesh): boolean {
  148. return (this._activeMeshes.indexOf(mesh) !== -1);
  149. }
  150. //Cache
  151. public _initCache() {
  152. super._initCache();
  153. this._cache.position = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  154. this._cache.upVector = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  155. this._cache.mode = undefined;
  156. this._cache.minZ = undefined;
  157. this._cache.maxZ = undefined;
  158. this._cache.fov = undefined;
  159. this._cache.aspectRatio = undefined;
  160. this._cache.orthoLeft = undefined;
  161. this._cache.orthoRight = undefined;
  162. this._cache.orthoBottom = undefined;
  163. this._cache.orthoTop = undefined;
  164. this._cache.renderWidth = undefined;
  165. this._cache.renderHeight = undefined;
  166. }
  167. public _updateCache(ignoreParentClass?: boolean): void {
  168. if (!ignoreParentClass) {
  169. super._updateCache();
  170. }
  171. var engine = this.getEngine();
  172. this._cache.position.copyFrom(this.position);
  173. this._cache.upVector.copyFrom(this.upVector);
  174. this._cache.mode = this.mode;
  175. this._cache.minZ = this.minZ;
  176. this._cache.maxZ = this.maxZ;
  177. this._cache.fov = this.fov;
  178. this._cache.aspectRatio = engine.getAspectRatio(this);
  179. this._cache.orthoLeft = this.orthoLeft;
  180. this._cache.orthoRight = this.orthoRight;
  181. this._cache.orthoBottom = this.orthoBottom;
  182. this._cache.orthoTop = this.orthoTop;
  183. this._cache.renderWidth = engine.getRenderWidth();
  184. this._cache.renderHeight = engine.getRenderHeight();
  185. }
  186. public _updateFromScene(): void {
  187. this.updateCache();
  188. this._update();
  189. }
  190. // Synchronized
  191. public _isSynchronized(): boolean {
  192. return this._isSynchronizedViewMatrix() && this._isSynchronizedProjectionMatrix();
  193. }
  194. public _isSynchronizedViewMatrix(): boolean {
  195. if (!super._isSynchronized())
  196. return false;
  197. return this._cache.position.equals(this.position)
  198. && this._cache.upVector.equals(this.upVector)
  199. && this.isSynchronizedWithParent();
  200. }
  201. public _isSynchronizedProjectionMatrix(): boolean {
  202. var check = this._cache.mode === this.mode
  203. && this._cache.minZ === this.minZ
  204. && this._cache.maxZ === this.maxZ;
  205. if (!check) {
  206. return false;
  207. }
  208. var engine = this.getEngine();
  209. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  210. check = this._cache.fov === this.fov
  211. && this._cache.aspectRatio === engine.getAspectRatio(this);
  212. }
  213. else {
  214. check = this._cache.orthoLeft === this.orthoLeft
  215. && this._cache.orthoRight === this.orthoRight
  216. && this._cache.orthoBottom === this.orthoBottom
  217. && this._cache.orthoTop === this.orthoTop
  218. && this._cache.renderWidth === engine.getRenderWidth()
  219. && this._cache.renderHeight === engine.getRenderHeight();
  220. }
  221. return check;
  222. }
  223. // Controls
  224. public attachControl(element: HTMLElement): void {
  225. }
  226. public detachControl(element: HTMLElement): void {
  227. }
  228. public _update(): void {
  229. this._checkInputs();
  230. if (this._subCameraMode !== Camera.SUB_CAMERA_MODE_NONE) {
  231. this._updateSubCameras();
  232. }
  233. }
  234. public _checkInputs(): void {
  235. }
  236. public attachPostProcess(postProcess: PostProcess, insertAt: number = null): number {
  237. if (!postProcess.isReusable() && this._postProcesses.indexOf(postProcess) > -1) {
  238. Tools.Error("You're trying to reuse a post process not defined as reusable.");
  239. return 0;
  240. }
  241. if (insertAt == null || insertAt < 0) {
  242. this._postProcesses.push(postProcess);
  243. this._postProcessesTakenIndices.push(this._postProcesses.length - 1);
  244. return this._postProcesses.length - 1;
  245. }
  246. var add = 0;
  247. if (this._postProcesses[insertAt]) {
  248. var start = this._postProcesses.length - 1;
  249. for (var i = start; i >= insertAt + 1; --i) {
  250. this._postProcesses[i + 1] = this._postProcesses[i];
  251. }
  252. add = 1;
  253. }
  254. for (i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  255. if (this._postProcessesTakenIndices[i] < insertAt) {
  256. continue;
  257. }
  258. start = this._postProcessesTakenIndices.length - 1;
  259. for (var j = start; j >= i; --j) {
  260. this._postProcessesTakenIndices[j + 1] = this._postProcessesTakenIndices[j] + add;
  261. }
  262. this._postProcessesTakenIndices[i] = insertAt;
  263. break;
  264. }
  265. if (!add && this._postProcessesTakenIndices.indexOf(insertAt) == -1) {
  266. this._postProcessesTakenIndices.push(insertAt);
  267. }
  268. var result = insertAt + add;
  269. this._postProcesses[result] = postProcess;
  270. return result;
  271. }
  272. public detachPostProcess(postProcess: PostProcess, atIndices: any = null): number[] {
  273. var result = [];
  274. if (!atIndices) {
  275. var length = this._postProcesses.length;
  276. for (var i = 0; i < length; i++) {
  277. if (this._postProcesses[i] !== postProcess) {
  278. continue;
  279. }
  280. delete this._postProcesses[i];
  281. var index = this._postProcessesTakenIndices.indexOf(i);
  282. this._postProcessesTakenIndices.splice(index, 1);
  283. }
  284. }
  285. else {
  286. atIndices = (atIndices instanceof Array) ? atIndices : [atIndices];
  287. for (i = 0; i < atIndices.length; i++) {
  288. var foundPostProcess = this._postProcesses[atIndices[i]];
  289. if (foundPostProcess !== postProcess) {
  290. result.push(i);
  291. continue;
  292. }
  293. delete this._postProcesses[atIndices[i]];
  294. index = this._postProcessesTakenIndices.indexOf(atIndices[i]);
  295. this._postProcessesTakenIndices.splice(index, 1);
  296. }
  297. }
  298. return result;
  299. }
  300. public getWorldMatrix(): Matrix {
  301. if (!this._worldMatrix) {
  302. this._worldMatrix = Matrix.Identity();
  303. }
  304. var viewMatrix = this.getViewMatrix();
  305. viewMatrix.invertToRef(this._worldMatrix);
  306. return this._worldMatrix;
  307. }
  308. public _getViewMatrix(): Matrix {
  309. return Matrix.Identity();
  310. }
  311. public getViewMatrix(force?: boolean): Matrix {
  312. this._computedViewMatrix = this._computeViewMatrix(force);
  313. if (!force && this._isSynchronizedViewMatrix()) {
  314. return this._computedViewMatrix;
  315. }
  316. if (!this.parent || !this.parent.getWorldMatrix) {
  317. this._globalPosition.copyFrom(this.position);
  318. } else {
  319. if (!this._worldMatrix) {
  320. this._worldMatrix = Matrix.Identity();
  321. }
  322. this._computedViewMatrix.invertToRef(this._worldMatrix);
  323. this._worldMatrix.multiplyToRef(this.parent.getWorldMatrix(), this._computedViewMatrix);
  324. this._globalPosition.copyFromFloats(this._computedViewMatrix.m[12], this._computedViewMatrix.m[13], this._computedViewMatrix.m[14]);
  325. this._computedViewMatrix.invert();
  326. this._markSyncedWithParent();
  327. }
  328. this._currentRenderId = this.getScene().getRenderId();
  329. return this._computedViewMatrix;
  330. }
  331. public _computeViewMatrix(force?: boolean): Matrix {
  332. if (!force && this._isSynchronizedViewMatrix()) {
  333. return this._computedViewMatrix;
  334. }
  335. this._computedViewMatrix = this._getViewMatrix();
  336. this._currentRenderId = this.getScene().getRenderId();
  337. return this._computedViewMatrix;
  338. }
  339. public getProjectionMatrix(force?: boolean): Matrix {
  340. if (!force && this._isSynchronizedProjectionMatrix()) {
  341. return this._projectionMatrix;
  342. }
  343. var engine = this.getEngine();
  344. if (this.mode === Camera.PERSPECTIVE_CAMERA) {
  345. if (this.minZ <= 0) {
  346. this.minZ = 0.1;
  347. }
  348. Matrix.PerspectiveFovLHToRef(this.fov, engine.getAspectRatio(this), this.minZ, this.maxZ, this._projectionMatrix, this.fovMode);
  349. return this._projectionMatrix;
  350. }
  351. var halfWidth = engine.getRenderWidth() / 2.0;
  352. var halfHeight = engine.getRenderHeight() / 2.0;
  353. 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. public dispose(): void {
  357. // Remove from scene
  358. this.getScene().removeCamera(this);
  359. while (this.subCameras.length > 0) {
  360. this.subCameras.pop().dispose();
  361. }
  362. // Postprocesses
  363. for (var i = 0; i < this._postProcessesTakenIndices.length; ++i) {
  364. this._postProcesses[this._postProcessesTakenIndices[i]].dispose(this);
  365. }
  366. }
  367. // ---- 3D cameras section ----
  368. public setSubCameraMode(mode: number, halfSpace = 0, metrics?: VRCameraMetrics): void {
  369. while (this.subCameras.length > 0) {
  370. this.subCameras.pop().dispose();
  371. }
  372. this._subCameraMode = mode;
  373. this._subCamHalfSpace = Tools.ToRadians(halfSpace);
  374. var camA = this.getSubCamera(this.name + "_A", true);
  375. var camB = this.getSubCamera(this.name + "_B", false);
  376. var postProcessA: PostProcess;
  377. var postProcessB: PostProcess;
  378. switch (this._subCameraMode) {
  379. case Camera.SUB_CAMERA_MODE_ANAGLYPH:
  380. postProcessA = new PassPostProcess(this.name + "_leftTexture", 1.0, camA);
  381. camA.isIntermediate = true;
  382. postProcessB = new AnaglyphPostProcess(this.name + "_anaglyph", 1.0, camB);
  383. postProcessB.onApply = effect => {
  384. effect.setTextureFromPostProcess("leftSampler", postProcessA);
  385. };
  386. break;
  387. case Camera.SUB_CAMERA_MODE_HORIZONTAL_STEREOGRAM:
  388. case Camera.SUB_CAMERA_MODE_VERTICAL_STEREOGRAM:
  389. var isStereogramHoriz = this._subCameraMode === Camera.SUB_CAMERA_MODE_HORIZONTAL_STEREOGRAM;
  390. postProcessA = new PassPostProcess("passthru", 1.0, camA);
  391. camA.isIntermediate = true;
  392. postProcessB = new StereogramInterlacePostProcess("st_interlace", camB, postProcessA, isStereogramHoriz);
  393. break;
  394. case Camera.SUB_CAMERA_MODE_VR:
  395. metrics = metrics || VRCameraMetrics.GetDefault();;
  396. camA._vrMetrics = metrics;
  397. camA.viewport = new Viewport(0, 0, 0.5, 1.0);
  398. camA._vrWorkMatrix = new Matrix();
  399. camA._vrHMatrix = metrics.leftHMatrix;
  400. camA._vrPreViewMatrix = metrics.leftPreViewMatrix;
  401. camA.getProjectionMatrix = camA._getVRProjectionMatrix;
  402. if (metrics.compensateDistorsion) {
  403. postProcessA = new VRDistortionCorrectionPostProcess("Distortion Compensation Left", camA, false, metrics);
  404. }
  405. camB._vrMetrics = camA._vrMetrics;
  406. camB.viewport = new Viewport(0.5, 0, 0.5, 1.0);
  407. camB._vrWorkMatrix = new Matrix();
  408. camB._vrHMatrix = metrics.rightHMatrix;
  409. camB._vrPreViewMatrix = metrics.rightPreViewMatrix;
  410. camB.getProjectionMatrix = camB._getVRProjectionMatrix;
  411. if (metrics.compensateDistorsion) {
  412. postProcessB = new VRDistortionCorrectionPostProcess("Distortion Compensation Right", camB, true, metrics);
  413. }
  414. }
  415. if (this._subCameraMode !== Camera.SUB_CAMERA_MODE_NONE) {
  416. this.subCameras.push(camA);
  417. this.subCameras.push(camB);
  418. }
  419. this._update();
  420. }
  421. private _getVRProjectionMatrix(): Matrix {
  422. Matrix.PerspectiveFovLHToRef(this._vrMetrics.aspectRatioFov, this._vrMetrics.aspectRatio, this.minZ, this.maxZ, this._vrWorkMatrix);
  423. this._vrWorkMatrix.multiplyToRef(this._vrHMatrix, this._projectionMatrix);
  424. return this._projectionMatrix;
  425. }
  426. public setSubCamHalfSpace(halfSpace: number) {
  427. this._subCamHalfSpace = Tools.ToRadians(halfSpace);
  428. }
  429. /**
  430. * May needs to be overridden by children so sub has required properties to be copied
  431. */
  432. public getSubCamera(name: string, isA: boolean): Camera {
  433. return null;
  434. }
  435. /**
  436. * May needs to be overridden by children
  437. */
  438. public _updateSubCameras() {
  439. var camA = this.subCameras[Camera.SUB_CAMERAID_A];
  440. var camB = this.subCameras[Camera.SUB_CAMERAID_B];
  441. camA.minZ = camB.minZ = this.minZ;
  442. camA.maxZ = camB.maxZ = this.maxZ;
  443. camA.fov = camB.fov = this.fov;
  444. // only update viewport, when ANAGLYPH
  445. if (this._subCameraMode === Camera.SUB_CAMERA_MODE_ANAGLYPH) {
  446. camA.viewport = camB.viewport = this.viewport;
  447. }
  448. }
  449. }
  450. }