babylon.standardRenderingPipeline.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. module BABYLON {
  2. export class StandardRenderingPipeline extends PostProcessRenderPipeline implements IDisposable, IAnimatable {
  3. /**
  4. * Public members
  5. */
  6. // Post-processes
  7. public originalPostProcess: PostProcess;
  8. public downSampleX4PostProcess: PostProcess = null;
  9. public brightPassPostProcess: PostProcess = null;
  10. public blurHPostProcesses: PostProcess[] = [];
  11. public blurVPostProcesses: PostProcess[] = [];
  12. public textureAdderPostProcess: PostProcess = null;
  13. public volumetricLightPostProcess: PostProcess = null;
  14. public volumetricLightSmoothXPostProcess: BlurPostProcess = null;
  15. public volumetricLightSmoothYPostProcess: BlurPostProcess = null;
  16. public volumetricLightMergePostProces: PostProcess = null;
  17. public volumetricLightFinalPostProcess: PostProcess = null;
  18. public luminancePostProcess: PostProcess = null;
  19. public luminanceDownSamplePostProcesses: PostProcess[] = [];
  20. public hdrPostProcess: PostProcess = null;
  21. public textureAdderFinalPostProcess: PostProcess = null;
  22. public lensFlareFinalPostProcess: PostProcess = null;
  23. public hdrFinalPostProcess: PostProcess = null;
  24. public lensFlarePostProcess: PostProcess = null;
  25. public lensFlareComposePostProcess: PostProcess = null;
  26. public motionBlurPostProcess: PostProcess = null;
  27. public depthOfFieldPostProcess: PostProcess = null;
  28. // Values
  29. @serialize()
  30. public brightThreshold: number = 1.0;
  31. @serialize()
  32. public blurWidth: number = 512.0;
  33. @serialize()
  34. public horizontalBlur: boolean = false;
  35. @serialize()
  36. public exposure: number = 1.0;
  37. @serializeAsTexture("lensTexture")
  38. public lensTexture: Texture = null;
  39. @serialize()
  40. public volumetricLightCoefficient: number = 0.2;
  41. @serialize()
  42. public volumetricLightPower: number = 4.0;
  43. @serialize()
  44. public volumetricLightBlurScale: number = 64.0;
  45. public sourceLight: SpotLight | DirectionalLight = null;
  46. @serialize()
  47. public hdrMinimumLuminance: number = 1.0;
  48. @serialize()
  49. public hdrDecreaseRate: number = 0.5;
  50. @serialize()
  51. public hdrIncreaseRate: number = 0.5;
  52. @serializeAsTexture("lensColorTexture")
  53. public lensColorTexture: Texture = null;
  54. @serialize()
  55. public lensFlareStrength: number = 20.0;
  56. @serialize()
  57. public lensFlareGhostDispersal: number = 1.4;
  58. @serialize()
  59. public lensFlareHaloWidth: number = 0.7;
  60. @serialize()
  61. public lensFlareDistortionStrength: number = 16.0;
  62. @serializeAsTexture("lensStarTexture")
  63. public lensStarTexture: Texture = null;
  64. @serializeAsTexture("lensFlareDirtTexture")
  65. public lensFlareDirtTexture: Texture = null;
  66. @serialize()
  67. public depthOfFieldDistance: number = 10.0;
  68. @serialize()
  69. public depthOfFieldBlurWidth: number = 64.0;
  70. @serialize()
  71. public motionStrength: number = 1.0;
  72. // IAnimatable
  73. public animations: Animation[] = [];
  74. /**
  75. * Private members
  76. */
  77. private _scene: Scene;
  78. private _currentDepthOfFieldSource: PostProcess = null;
  79. private _basePostProcess: PostProcess;
  80. private _hdrCurrentLuminance: number = 1.0;
  81. private _floatTextureType: number;
  82. private _ratio: number;
  83. // Getters and setters
  84. private _bloomEnabled: boolean = true;
  85. private _depthOfFieldEnabled: boolean = false;
  86. private _vlsEnabled: boolean = false;
  87. private _lensFlareEnabled: boolean = false;
  88. private _hdrEnabled: boolean = false;
  89. private _motionBlurEnabled: boolean = false;
  90. private _motionBlurSamples: number = 64.0;
  91. private _volumetricLightStepsCount: number = 50.0;
  92. @serialize()
  93. public get BloomEnabled(): boolean {
  94. return this._bloomEnabled;
  95. }
  96. public set BloomEnabled(enabled: boolean) {
  97. if (this._bloomEnabled === enabled) {
  98. return;
  99. }
  100. this._bloomEnabled = enabled;
  101. this._buildPipeline();
  102. }
  103. @serialize()
  104. public get DepthOfFieldEnabled(): boolean {
  105. return this._depthOfFieldEnabled;
  106. }
  107. public set DepthOfFieldEnabled(enabled: boolean) {
  108. if (this._depthOfFieldEnabled === enabled) {
  109. return;
  110. }
  111. this._depthOfFieldEnabled = enabled;
  112. this._buildPipeline();
  113. }
  114. @serialize()
  115. public get LensFlareEnabled(): boolean {
  116. return this._lensFlareEnabled;
  117. }
  118. public set LensFlareEnabled(enabled: boolean) {
  119. if (this._lensFlareEnabled === enabled) {
  120. return;
  121. }
  122. this._lensFlareEnabled = enabled;
  123. this._buildPipeline();
  124. }
  125. @serialize()
  126. public get HDREnabled(): boolean {
  127. return this._hdrEnabled;
  128. }
  129. public set HDREnabled(enabled: boolean) {
  130. if (this._hdrEnabled === enabled) {
  131. return;
  132. }
  133. this._hdrEnabled = enabled;
  134. this._buildPipeline();
  135. }
  136. @serialize()
  137. public get VLSEnabled(): boolean {
  138. return this._vlsEnabled;
  139. }
  140. public set VLSEnabled(enabled) {
  141. if (this._vlsEnabled === enabled) {
  142. return;
  143. }
  144. if (enabled) {
  145. var geometry = this._scene.enableGeometryBufferRenderer();
  146. if (!geometry) {
  147. Tools.Warn("Geometry renderer is not supported, cannot create volumetric lights in Standard Rendering Pipeline");
  148. return;
  149. }
  150. }
  151. this._vlsEnabled = enabled;
  152. this._buildPipeline();
  153. }
  154. @serialize()
  155. public get MotionBlurEnabled(): boolean {
  156. return this._motionBlurEnabled;
  157. }
  158. public set MotionBlurEnabled(enabled: boolean) {
  159. if (this._motionBlurEnabled === enabled) {
  160. return;
  161. }
  162. this._motionBlurEnabled = enabled;
  163. this._buildPipeline();
  164. }
  165. @serialize()
  166. public get volumetricLightStepsCount(): number {
  167. return this._volumetricLightStepsCount;
  168. }
  169. public set volumetricLightStepsCount(count: number) {
  170. if (this.volumetricLightPostProcess) {
  171. this.volumetricLightPostProcess.updateEffect("#define VLS\n#define NB_STEPS " + count.toFixed(1));
  172. }
  173. this._volumetricLightStepsCount = count;
  174. }
  175. @serialize()
  176. public get motionBlurSamples(): number {
  177. return this._motionBlurSamples;
  178. }
  179. public set motionBlurSamples(samples: number) {
  180. if (this.motionBlurPostProcess) {
  181. this.motionBlurPostProcess.updateEffect("#define MOTION_BLUR\n#define MAX_MOTION_SAMPLES " + samples.toFixed(1));
  182. }
  183. this._motionBlurSamples = samples;
  184. }
  185. /**
  186. * @constructor
  187. * @param {string} name - The rendering pipeline name
  188. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  189. * @param {any} ratio - The size of the postprocesses (0.5 means that your postprocess will have a width = canvas.width 0.5 and a height = canvas.height 0.5)
  190. * @param {BABYLON.PostProcess} originalPostProcess - the custom original color post-process. Must be "reusable". Can be null.
  191. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  192. */
  193. constructor(name: string, scene: Scene, ratio: number, originalPostProcess: PostProcess = null, cameras?: Camera[]) {
  194. super(scene.getEngine(), name);
  195. this._cameras = cameras || [];
  196. // Initialize
  197. this._scene = scene;
  198. this._basePostProcess = originalPostProcess;
  199. this._ratio = ratio;
  200. // Misc
  201. this._floatTextureType = scene.getEngine().getCaps().textureFloatRender ? Engine.TEXTURETYPE_FLOAT : Engine.TEXTURETYPE_HALF_FLOAT;
  202. // Finish
  203. scene.postProcessRenderPipelineManager.addPipeline(this);
  204. this._buildPipeline();
  205. }
  206. private _buildPipeline(): void {
  207. var ratio = this._ratio;
  208. var scene = this._scene;
  209. this._disposePostProcesses();
  210. this._reset();
  211. // Create pass post-process
  212. if (!this._basePostProcess) {
  213. this.originalPostProcess = new PostProcess("HDRPass", "standard", [], [], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define PASS_POST_PROCESS", this._floatTextureType);
  214. this.originalPostProcess.onApply = (effect: Effect) => {
  215. this._currentDepthOfFieldSource = this.originalPostProcess;
  216. };
  217. }
  218. else {
  219. this.originalPostProcess = this._basePostProcess;
  220. }
  221. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRPassPostProcess", () => { return this.originalPostProcess; }, true));
  222. this._currentDepthOfFieldSource = this.originalPostProcess;
  223. if (this._vlsEnabled) {
  224. // Create volumetric light
  225. this._createVolumetricLightPostProcess(scene, ratio);
  226. // Create volumetric light final post-process
  227. this.volumetricLightFinalPostProcess = new PostProcess("HDRVLSFinal", "standard", [], [], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define PASS_POST_PROCESS", Engine.TEXTURETYPE_UNSIGNED_INT);
  228. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRVLSFinal", () => { return this.volumetricLightFinalPostProcess; }, true));
  229. }
  230. if (this._bloomEnabled) {
  231. // Create down sample X4 post-process
  232. this._createDownSampleX4PostProcess(scene, ratio / 2);
  233. // Create bright pass post-process
  234. this._createBrightPassPostProcess(scene, ratio / 2);
  235. // Create gaussian blur post-processes (down sampling blurs)
  236. this._createBlurPostProcesses(scene, ratio / 4, 1);
  237. // Create texture adder post-process
  238. this._createTextureAdderPostProcess(scene, ratio);
  239. // Create depth-of-field source post-process
  240. this.textureAdderFinalPostProcess = new PostProcess("HDRDepthOfFieldSource", "standard", [], [], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define PASS_POST_PROCESS", Engine.TEXTURETYPE_UNSIGNED_INT);
  241. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRBaseDepthOfFieldSource", () => { return this.textureAdderFinalPostProcess; }, true));
  242. }
  243. if (this._lensFlareEnabled) {
  244. // Create lens flare post-process
  245. this._createLensFlarePostProcess(scene, ratio);
  246. // Create depth-of-field source post-process post lens-flare and disable it now
  247. this.lensFlareFinalPostProcess = new PostProcess("HDRPostLensFlareDepthOfFieldSource", "standard", [], [], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define PASS_POST_PROCESS", Engine.TEXTURETYPE_UNSIGNED_INT);
  248. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRPostLensFlareDepthOfFieldSource", () => { return this.lensFlareFinalPostProcess; }, true));
  249. }
  250. if (this._hdrEnabled) {
  251. // Create luminance
  252. this._createLuminancePostProcesses(scene, this._floatTextureType);
  253. // Create HDR
  254. this._createHdrPostProcess(scene, ratio);
  255. // Create depth-of-field source post-process post hdr and disable it now
  256. this.hdrFinalPostProcess = new PostProcess("HDRPostHDReDepthOfFieldSource", "standard", [], [], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define PASS_POST_PROCESS", Engine.TEXTURETYPE_UNSIGNED_INT);
  257. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRPostHDReDepthOfFieldSource", () => { return this.hdrFinalPostProcess; }, true));
  258. }
  259. if (this._depthOfFieldEnabled) {
  260. // Create gaussian blur used by depth-of-field
  261. this._createBlurPostProcesses(scene, ratio / 2, 3, "depthOfFieldBlurWidth");
  262. // Create depth-of-field post-process
  263. this._createDepthOfFieldPostProcess(scene, ratio);
  264. }
  265. if (this._motionBlurEnabled) {
  266. // Create motion blur post-process
  267. this._createMotionBlurPostProcess(scene, ratio);
  268. }
  269. if (this._cameras !== null) {
  270. this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
  271. }
  272. }
  273. // Down Sample X4 Post-Processs
  274. private _createDownSampleX4PostProcess(scene: Scene, ratio: number): void {
  275. var downSampleX4Offsets = new Array<number>(32);
  276. this.downSampleX4PostProcess = new PostProcess("HDRDownSampleX4", "standard", ["dsOffsets"], [], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define DOWN_SAMPLE_X4", Engine.TEXTURETYPE_UNSIGNED_INT);
  277. this.downSampleX4PostProcess.onApply = (effect: Effect) => {
  278. var id = 0;
  279. for (var i = -2; i < 2; i++) {
  280. for (var j = -2; j < 2; j++) {
  281. downSampleX4Offsets[id] = (i + 0.5) * (1.0 / this.downSampleX4PostProcess.width);
  282. downSampleX4Offsets[id + 1] = (j + 0.5) * (1.0 / this.downSampleX4PostProcess.height);
  283. id += 2;
  284. }
  285. }
  286. effect.setArray2("dsOffsets", downSampleX4Offsets);
  287. };
  288. // Add to pipeline
  289. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRDownSampleX4", () => { return this.downSampleX4PostProcess; }, true));
  290. }
  291. // Brightpass Post-Process
  292. private _createBrightPassPostProcess(scene: Scene, ratio: number): void {
  293. var brightOffsets = new Array<number>(8);
  294. this.brightPassPostProcess = new PostProcess("HDRBrightPass", "standard", ["dsOffsets", "brightThreshold"], [], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define BRIGHT_PASS", Engine.TEXTURETYPE_UNSIGNED_INT);
  295. this.brightPassPostProcess.onApply = (effect: Effect) => {
  296. var sU = (1.0 / this.brightPassPostProcess.width);
  297. var sV = (1.0 / this.brightPassPostProcess.height);
  298. brightOffsets[0] = -0.5 * sU;
  299. brightOffsets[1] = 0.5 * sV;
  300. brightOffsets[2] = 0.5 * sU;
  301. brightOffsets[3] = 0.5 * sV;
  302. brightOffsets[4] = -0.5 * sU;
  303. brightOffsets[5] = -0.5 * sV;
  304. brightOffsets[6] = 0.5 * sU;
  305. brightOffsets[7] = -0.5 * sV;
  306. effect.setArray2("dsOffsets", brightOffsets);
  307. effect.setFloat("brightThreshold", this.brightThreshold);
  308. }
  309. // Add to pipeline
  310. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRBrightPass", () => { return this.brightPassPostProcess; }, true));
  311. }
  312. // Create blur H&V post-processes
  313. private _createBlurPostProcesses(scene: Scene, ratio: number, indice: number, blurWidthKey: string = "blurWidth"): void {
  314. var engine = scene.getEngine();
  315. var blurX = new BlurPostProcess("HDRBlurH" + "_" + indice, new Vector2(1, 0), this[blurWidthKey], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, Engine.TEXTURETYPE_UNSIGNED_INT);
  316. var blurY = new BlurPostProcess("HDRBlurV" + "_" + indice, new Vector2(0, 1), this[blurWidthKey], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, Engine.TEXTURETYPE_UNSIGNED_INT);
  317. blurX.onActivateObservable.add(() => {
  318. let dw = blurX.width / engine.getRenderingCanvas().width;
  319. blurX.kernel = this[blurWidthKey] * dw;
  320. });
  321. blurY.onActivateObservable.add(() => {
  322. let dw = blurY.height / engine.getRenderingCanvas().height;
  323. blurY.kernel = this.horizontalBlur ? 64 * dw : this[blurWidthKey] * dw;
  324. });
  325. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRBlurH" + indice, () => { return blurX; }, true));
  326. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRBlurV" + indice, () => { return blurY; }, true));
  327. this.blurHPostProcesses.push(blurX);
  328. this.blurVPostProcesses.push(blurY);
  329. }
  330. // Create texture adder post-process
  331. private _createTextureAdderPostProcess(scene: Scene, ratio: number): void {
  332. this.textureAdderPostProcess = new PostProcess("HDRTextureAdder", "standard", ["exposure"], ["otherSampler", "lensSampler"], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define TEXTURE_ADDER", Engine.TEXTURETYPE_UNSIGNED_INT);
  333. this.textureAdderPostProcess.onApply = (effect: Effect) => {
  334. effect.setTextureFromPostProcess("otherSampler", this._vlsEnabled ? this._currentDepthOfFieldSource : this.originalPostProcess);
  335. effect.setTexture("lensSampler", this.lensTexture);
  336. effect.setFloat("exposure", this.exposure);
  337. this._currentDepthOfFieldSource = this.textureAdderFinalPostProcess;
  338. };
  339. // Add to pipeline
  340. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRTextureAdder", () => { return this.textureAdderPostProcess; }, true));
  341. }
  342. private _createVolumetricLightPostProcess(scene: Scene, ratio: number): void {
  343. var geometryRenderer = scene.enableGeometryBufferRenderer();
  344. geometryRenderer.enablePosition = true;
  345. var geometry = geometryRenderer.getGBuffer();
  346. // Base post-process
  347. this.volumetricLightPostProcess = new PostProcess("HDRVLS", "standard",
  348. ["shadowViewProjection", "cameraPosition", "sunDirection", "sunColor", "scatteringCoefficient", "scatteringPower", "depthValues"],
  349. ["shadowMapSampler", "positionSampler" ],
  350. ratio / 8,
  351. null,
  352. Texture.BILINEAR_SAMPLINGMODE,
  353. scene.getEngine(),
  354. false, "#define VLS\n#define NB_STEPS " + this._volumetricLightStepsCount.toFixed(1));
  355. var depthValues = Vector2.Zero();
  356. this.volumetricLightPostProcess.onApply = (effect: Effect) => {
  357. if (this.sourceLight && this.sourceLight.getShadowGenerator()) {
  358. var generator = <ShadowGenerator>this.sourceLight.getShadowGenerator();
  359. effect.setTexture("shadowMapSampler", generator.getShadowMap());
  360. effect.setTexture("positionSampler", geometry.textures[2]);
  361. effect.setColor3("sunColor", this.sourceLight.diffuse);
  362. effect.setVector3("sunDirection", this.sourceLight.getShadowDirection());
  363. effect.setVector3("cameraPosition", scene.activeCamera.globalPosition);
  364. effect.setMatrix("shadowViewProjection", generator.getTransformMatrix());
  365. effect.setFloat("scatteringCoefficient", this.volumetricLightCoefficient);
  366. effect.setFloat("scatteringPower", this.volumetricLightPower);
  367. depthValues.x = generator.getLight().getDepthMinZ(this._scene.activeCamera);
  368. depthValues.y = generator.getLight().getDepthMaxZ(this._scene.activeCamera);
  369. effect.setVector2("depthValues", depthValues);
  370. }
  371. };
  372. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRVLS", () => { return this.volumetricLightPostProcess; }, true));
  373. // Smooth
  374. this._createBlurPostProcesses(scene, ratio / 4, 0, "volumetricLightBlurScale");
  375. // Merge
  376. this.volumetricLightMergePostProces = new PostProcess("HDRVLSMerge", "standard", [], ["originalSampler"], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define VLSMERGE");
  377. this.volumetricLightMergePostProces.onApply = (effect: Effect) => {
  378. effect.setTextureFromPostProcess("originalSampler", this.originalPostProcess);
  379. this._currentDepthOfFieldSource = this.volumetricLightFinalPostProcess;
  380. };
  381. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRVLSMerge", () => { return this.volumetricLightMergePostProces; }, true));
  382. }
  383. // Create luminance
  384. private _createLuminancePostProcesses(scene: Scene, textureType: number): void {
  385. // Create luminance
  386. var size = Math.pow(3, StandardRenderingPipeline.LuminanceSteps);
  387. this.luminancePostProcess = new PostProcess("HDRLuminance", "standard", ["lumOffsets"], [], { width: size, height: size }, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define LUMINANCE", textureType);
  388. var offsets: number[] = [];
  389. this.luminancePostProcess.onApply = (effect: Effect) => {
  390. var sU = (1.0 / this.luminancePostProcess.width);
  391. var sV = (1.0 / this.luminancePostProcess.height);
  392. offsets[0] = -0.5 * sU;
  393. offsets[1] = 0.5 * sV;
  394. offsets[2] = 0.5 * sU;
  395. offsets[3] = 0.5 * sV;
  396. offsets[4] = -0.5 * sU;
  397. offsets[5] = -0.5 * sV;
  398. offsets[6] = 0.5 * sU;
  399. offsets[7] = -0.5 * sV;
  400. effect.setArray2("lumOffsets", offsets);
  401. };
  402. // Add to pipeline
  403. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRLuminance", () => { return this.luminancePostProcess; }, true));
  404. // Create down sample luminance
  405. for (var i = StandardRenderingPipeline.LuminanceSteps - 1; i >= 0; i--) {
  406. var size = Math.pow(3, i);
  407. var defines = "#define LUMINANCE_DOWN_SAMPLE\n";
  408. if (i === 0) {
  409. defines += "#define FINAL_DOWN_SAMPLER";
  410. }
  411. var postProcess = new PostProcess("HDRLuminanceDownSample" + i, "standard", ["dsOffsets", "halfDestPixelSize"], [], { width: size, height: size }, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, defines, textureType);
  412. this.luminanceDownSamplePostProcesses.push(postProcess);
  413. }
  414. // Create callbacks and add effects
  415. var lastLuminance = this.luminancePostProcess;
  416. this.luminanceDownSamplePostProcesses.forEach((pp, index) => {
  417. var downSampleOffsets = new Array<number>(18);
  418. pp.onApply = (effect: Effect) => {
  419. var id = 0;
  420. for (var x = -1; x < 2; x++) {
  421. for (var y = -1; y < 2; y++) {
  422. downSampleOffsets[id] = x / lastLuminance.width;
  423. downSampleOffsets[id + 1] = y / lastLuminance.height;
  424. id += 2;
  425. }
  426. }
  427. effect.setArray2("dsOffsets", downSampleOffsets);
  428. effect.setFloat("halfDestPixelSize", 0.5 / lastLuminance.width);
  429. if (index === this.luminanceDownSamplePostProcesses.length - 1) {
  430. lastLuminance = this.luminancePostProcess;
  431. } else {
  432. lastLuminance = pp;
  433. }
  434. };
  435. if (index === this.luminanceDownSamplePostProcesses.length - 1) {
  436. pp.onAfterRender = (effect: Effect) => {
  437. var pixel = scene.getEngine().readPixels(0, 0, 1, 1);
  438. var bit_shift = new Vector4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
  439. this._hdrCurrentLuminance = (pixel[0] * bit_shift.x + pixel[1] * bit_shift.y + pixel[2] * bit_shift.z + pixel[3] * bit_shift.w) / 100.0;
  440. };
  441. }
  442. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRLuminanceDownSample" + index, () => { return pp; }, true));
  443. });
  444. }
  445. // Create HDR post-process
  446. private _createHdrPostProcess(scene: Scene, ratio: number): void {
  447. this.hdrPostProcess = new PostProcess("HDR", "standard", ["averageLuminance"], ["textureAdderSampler"], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define HDR", Engine.TEXTURETYPE_UNSIGNED_INT);
  448. var outputLiminance = 1;
  449. var time = 0;
  450. var lastTime = 0;
  451. this.hdrPostProcess.onApply = (effect: Effect) => {
  452. effect.setTextureFromPostProcess("textureAdderSampler", this._currentDepthOfFieldSource);
  453. time += scene.getEngine().getDeltaTime();
  454. if (outputLiminance < 0) {
  455. outputLiminance = this._hdrCurrentLuminance;
  456. } else {
  457. var dt = (lastTime - time) / 1000.0;
  458. if (this._hdrCurrentLuminance < outputLiminance + this.hdrDecreaseRate * dt) {
  459. outputLiminance += this.hdrDecreaseRate * dt;
  460. }
  461. else if (this._hdrCurrentLuminance > outputLiminance - this.hdrIncreaseRate * dt) {
  462. outputLiminance -= this.hdrIncreaseRate * dt;
  463. }
  464. else {
  465. outputLiminance = this._hdrCurrentLuminance;
  466. }
  467. }
  468. outputLiminance = MathTools.Clamp(outputLiminance, this.hdrMinimumLuminance, 1e20);
  469. effect.setFloat("averageLuminance", outputLiminance);
  470. lastTime = time;
  471. this._currentDepthOfFieldSource = this.hdrFinalPostProcess;
  472. };
  473. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDR", () => { return this.hdrPostProcess; }, true));
  474. }
  475. // Create lens flare post-process
  476. private _createLensFlarePostProcess(scene: Scene, ratio: number): void {
  477. this.lensFlarePostProcess = new PostProcess("HDRLensFlare", "standard", ["strength", "ghostDispersal", "haloWidth", "resolution", "distortionStrength"], ["lensColorSampler"], ratio / 2, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define LENS_FLARE", Engine.TEXTURETYPE_UNSIGNED_INT);
  478. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRLensFlare", () => { return this.lensFlarePostProcess; }, true));
  479. this._createBlurPostProcesses(scene, ratio / 4, 2);
  480. this.lensFlareComposePostProcess = new PostProcess("HDRLensFlareCompose", "standard", ["lensStarMatrix"], ["otherSampler", "lensDirtSampler", "lensStarSampler"], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define LENS_FLARE_COMPOSE", Engine.TEXTURETYPE_UNSIGNED_INT);
  481. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRLensFlareCompose", () => { return this.lensFlareComposePostProcess; }, true));
  482. var resolution = new Vector2(0, 0);
  483. // Lens flare
  484. this.lensFlarePostProcess.onApply = (effect: Effect) => {
  485. effect.setTextureFromPostProcess("textureSampler", this._bloomEnabled ? this.blurHPostProcesses[0] : this.originalPostProcess);
  486. effect.setTexture("lensColorSampler", this.lensColorTexture);
  487. effect.setFloat("strength", this.lensFlareStrength);
  488. effect.setFloat("ghostDispersal", this.lensFlareGhostDispersal);
  489. effect.setFloat("haloWidth", this.lensFlareHaloWidth);
  490. // Shift
  491. resolution.x = this.lensFlarePostProcess.width;
  492. resolution.y = this.lensFlarePostProcess.height;
  493. effect.setVector2("resolution", resolution);
  494. effect.setFloat("distortionStrength", this.lensFlareDistortionStrength);
  495. };
  496. // Compose
  497. var scaleBias1 = Matrix.FromValues(
  498. 2.0, 0.0, -1.0, 0.0,
  499. 0.0, 2.0, -1.0, 0.0,
  500. 0.0, 0.0, 1.0, 0.0,
  501. 0.0, 0.0, 0.0, 1.0
  502. );
  503. var scaleBias2 = Matrix.FromValues(
  504. 0.5, 0.0, 0.5, 0.0,
  505. 0.0, 0.5, 0.5, 0.0,
  506. 0.0, 0.0, 1.0, 0.0,
  507. 0.0, 0.0, 0.0, 1.0
  508. );
  509. this.lensFlareComposePostProcess.onApply = (effect: Effect) => {
  510. effect.setTextureFromPostProcess("otherSampler", this._currentDepthOfFieldSource);
  511. effect.setTexture("lensDirtSampler", this.lensFlareDirtTexture);
  512. effect.setTexture("lensStarSampler", this.lensStarTexture);
  513. // Lens start rotation matrix
  514. var camerax = this._scene.activeCamera.getViewMatrix().getRow(0);
  515. var cameraz = this._scene.activeCamera.getViewMatrix().getRow(2);
  516. var camRot = Vector3.Dot(camerax.toVector3(), new Vector3(1.0, 0.0, 0.0)) + Vector3.Dot(cameraz.toVector3(), new Vector3(0.0, 0.0, 1.0));
  517. camRot *= 4.0;
  518. var starRotation = Matrix.FromValues(
  519. Math.cos(camRot) * 0.5, -Math.sin(camRot), 0.0, 0.0,
  520. Math.sin(camRot), Math.cos(camRot) * 0.5, 0.0, 0.0,
  521. 0.0, 0.0, 1.0, 0.0,
  522. 0.0, 0.0, 0.0, 1.0
  523. );
  524. var lensStarMatrix = scaleBias2.multiply(starRotation).multiply(scaleBias1);
  525. effect.setMatrix("lensStarMatrix", lensStarMatrix);
  526. this._currentDepthOfFieldSource = this.lensFlareFinalPostProcess;
  527. };
  528. }
  529. // Create depth-of-field post-process
  530. private _createDepthOfFieldPostProcess(scene: Scene, ratio: number): void {
  531. this.depthOfFieldPostProcess = new PostProcess("HDRDepthOfField", "standard", ["distance"], ["otherSampler", "depthSampler"], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define DEPTH_OF_FIELD", Engine.TEXTURETYPE_UNSIGNED_INT);
  532. this.depthOfFieldPostProcess.onApply = (effect: Effect) => {
  533. effect.setTextureFromPostProcess("otherSampler", this._currentDepthOfFieldSource);
  534. effect.setTexture("depthSampler", this._getDepthTexture());
  535. effect.setFloat("distance", this.depthOfFieldDistance);
  536. };
  537. // Add to pipeline
  538. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRDepthOfField", () => { return this.depthOfFieldPostProcess; }, true));
  539. }
  540. // Create motion blur post-process
  541. private _createMotionBlurPostProcess(scene: Scene, ratio: number): void {
  542. this.motionBlurPostProcess = new PostProcess("HDRMotionBlur", "standard",
  543. ["inverseViewProjection", "prevViewProjection", "screenSize", "motionScale", "motionStrength"],
  544. ["depthSampler"],
  545. ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define MOTION_BLUR\n#define MAX_MOTION_SAMPLES " + this.motionBlurSamples.toFixed(1), Engine.TEXTURETYPE_UNSIGNED_INT);
  546. var motionScale: number = 0;
  547. var prevViewProjection = Matrix.Identity();
  548. var invViewProjection = Matrix.Identity();
  549. var viewProjection = Matrix.Identity();
  550. var screenSize = Vector2.Zero();
  551. this.motionBlurPostProcess.onApply = (effect: Effect) => {
  552. viewProjection = scene.getProjectionMatrix().multiply(scene.getViewMatrix());
  553. viewProjection.invertToRef(invViewProjection);
  554. effect.setMatrix("inverseViewProjection", invViewProjection);
  555. effect.setMatrix("prevViewProjection", prevViewProjection);
  556. prevViewProjection = viewProjection;
  557. screenSize.x = this.motionBlurPostProcess.width;
  558. screenSize.y = this.motionBlurPostProcess.height;
  559. effect.setVector2("screenSize", screenSize);
  560. motionScale = scene.getEngine().getFps() / 60.0;
  561. effect.setFloat("motionScale", motionScale);
  562. effect.setFloat("motionStrength", this.motionStrength);
  563. effect.setTexture("depthSampler", this._getDepthTexture());
  564. };
  565. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), "HDRMotionBlur", () => { return this.motionBlurPostProcess; }, true));
  566. }
  567. private _getDepthTexture(): Texture {
  568. var geometry = this._scene.enableGeometryBufferRenderer();
  569. if (geometry) {
  570. return geometry.getGBuffer().textures[0];
  571. }
  572. return this._scene.enableDepthRenderer().getDepthMap();
  573. }
  574. private _disposePostProcesses(): void {
  575. for (var i = 0; i < this._cameras.length; i++) {
  576. var camera = this._cameras[i];
  577. if (this.originalPostProcess) { this.originalPostProcess.dispose(camera); }
  578. if (this.downSampleX4PostProcess) { this.downSampleX4PostProcess.dispose(camera); }
  579. if (this.brightPassPostProcess) { this.brightPassPostProcess.dispose(camera); }
  580. if (this.textureAdderPostProcess) { this.textureAdderPostProcess.dispose(camera); }
  581. if (this.textureAdderFinalPostProcess) { this.textureAdderFinalPostProcess.dispose(camera); }
  582. if (this.volumetricLightPostProcess) { this.volumetricLightPostProcess.dispose(camera); }
  583. if (this.volumetricLightSmoothXPostProcess) { this.volumetricLightSmoothXPostProcess.dispose(camera); }
  584. if (this.volumetricLightSmoothYPostProcess) { this.volumetricLightSmoothYPostProcess.dispose(camera); }
  585. if (this.volumetricLightMergePostProces) { this.volumetricLightMergePostProces.dispose(camera); }
  586. if (this.volumetricLightFinalPostProcess) { this.volumetricLightFinalPostProcess.dispose(camera); }
  587. if (this.lensFlarePostProcess) { this.lensFlarePostProcess.dispose(camera); }
  588. if (this.lensFlareComposePostProcess) { this.lensFlareComposePostProcess.dispose(camera); }
  589. for (var j = 0; j < this.luminanceDownSamplePostProcesses.length; j++) {
  590. this.luminanceDownSamplePostProcesses[j].dispose(camera);
  591. }
  592. if (this.luminancePostProcess) { this.luminancePostProcess.dispose(camera); }
  593. if (this.hdrPostProcess) { this.hdrPostProcess.dispose(camera); }
  594. if (this.hdrFinalPostProcess) { this.hdrFinalPostProcess.dispose(camera); }
  595. if (this.depthOfFieldPostProcess) { this.depthOfFieldPostProcess.dispose(camera); }
  596. if (this.motionBlurPostProcess) { this.motionBlurPostProcess.dispose(camera); }
  597. for (var j = 0; j < this.blurHPostProcesses.length; j++) {
  598. this.blurHPostProcesses[j].dispose(camera);
  599. }
  600. for (var j = 0; j < this.blurVPostProcesses.length; j++) {
  601. this.blurVPostProcesses[j].dispose(camera);
  602. }
  603. }
  604. this.originalPostProcess = null;
  605. this.downSampleX4PostProcess = null;
  606. this.brightPassPostProcess = null;
  607. this.textureAdderPostProcess = null;
  608. this.textureAdderFinalPostProcess = null;
  609. this.volumetricLightPostProcess = null;
  610. this.volumetricLightSmoothXPostProcess = null;
  611. this.volumetricLightSmoothYPostProcess = null;
  612. this.volumetricLightMergePostProces = null;
  613. this.volumetricLightFinalPostProcess = null;
  614. this.lensFlarePostProcess = null;
  615. this.lensFlareComposePostProcess = null;
  616. this.luminancePostProcess = null;
  617. this.hdrPostProcess = null;
  618. this.hdrFinalPostProcess = null;
  619. this.depthOfFieldPostProcess = null;
  620. this.motionBlurPostProcess = null;
  621. this.luminanceDownSamplePostProcesses = [];
  622. this.blurHPostProcesses = [];
  623. this.blurVPostProcesses = [];
  624. }
  625. // Dispose
  626. public dispose(): void {
  627. this._disposePostProcesses();
  628. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._cameras);
  629. super.dispose();
  630. }
  631. // Serialize rendering pipeline
  632. public serialize(): any {
  633. var serializationObject = SerializationHelper.Serialize(this);
  634. serializationObject.customType = "StandardRenderingPipeline";
  635. return serializationObject;
  636. }
  637. /**
  638. * Static members
  639. */
  640. // Parse serialized pipeline
  641. public static Parse(source: any, scene: Scene, rootUrl: string): StandardRenderingPipeline {
  642. return SerializationHelper.Parse(() => new StandardRenderingPipeline(source._name, scene, source._ratio), source, scene, rootUrl);
  643. }
  644. // Luminance steps
  645. public static LuminanceSteps: number = 6;
  646. }
  647. }