babylon.hdrRenderingPipeline.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. __.prototype = b.prototype;
  5. d.prototype = new __();
  6. };
  7. var BABYLON;
  8. (function (BABYLON) {
  9. var HDRRenderingPipeline = (function (_super) {
  10. __extends(HDRRenderingPipeline, _super);
  11. /**
  12. * @constructor
  13. * @param {string} name - The rendering pipeline name
  14. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  15. * @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)
  16. * @param {BABYLON.PostProcess} originalPostProcess - the custom original color post-process. Must be "reusable". Can be null.
  17. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  18. */
  19. function HDRRenderingPipeline(name, scene, ratio, originalPostProcess, cameras) {
  20. var _this = this;
  21. if (originalPostProcess === void 0) { originalPostProcess = null; }
  22. _super.call(this, scene.getEngine(), name);
  23. /**
  24. * Public members
  25. */
  26. // Gaussian Blur
  27. /**
  28. * Gaussian blur coefficient
  29. * @type {number}
  30. */
  31. this.gaussCoeff = 0.3;
  32. /**
  33. * Gaussian blur mean
  34. * @type {number}
  35. */
  36. this.gaussMean = 1.0;
  37. /**
  38. * Gaussian blur standard derivation
  39. * @type {number}
  40. */
  41. this.gaussStandDev = 0.8;
  42. // HDR
  43. /**
  44. * Exposure, controls the overall intensity of the pipeline
  45. * @type {number}
  46. */
  47. this.exposure = 1.0;
  48. /**
  49. * Minimum luminance that the post-process can output. Luminance is >= 0
  50. * @type {number}
  51. */
  52. this.minimumLuminance = 1.0;
  53. /**
  54. * Maximum luminance that the post-process can output. Must be suprerior to minimumLuminance
  55. * @type {number}
  56. */
  57. this.maximumLuminance = 1e20;
  58. /**
  59. * Increase rate for luminance: eye adaptation speed to bright
  60. * @type {number}
  61. */
  62. this.luminanceIncreaserate = 0.5;
  63. /**
  64. * Decrease rate for luminance: eye adaptation speed to dark
  65. * @type {number}
  66. */
  67. this.luminanceDecreaseRate = 0.5;
  68. // Bright pass
  69. /**
  70. * Minimum luminance needed to compute HDR
  71. * @type {number}
  72. */
  73. this.brightThreshold = 0.8;
  74. // Global
  75. this._needUpdate = true;
  76. // Bright pass
  77. this._createBrightPassPostProcess(scene, ratio);
  78. // Down sample X4
  79. this._createDownSampleX4PostProcess(scene, ratio);
  80. // Create gaussian blur post-processes
  81. this._createGaussianBlurPostProcess(scene, ratio);
  82. // Texture adder
  83. this._createTextureAdderPostProcess(scene, ratio);
  84. // Luminance generator
  85. this._createLuminanceGeneratorPostProcess(scene);
  86. // HDR
  87. this._createHDRPostProcess(scene, ratio);
  88. // Pass postprocess
  89. if (originalPostProcess === null) {
  90. this._originalPostProcess = new BABYLON.PassPostProcess("hdr", ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
  91. }
  92. else {
  93. this._originalPostProcess = originalPostProcess;
  94. }
  95. // Configure pipeline
  96. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDRPassPostProcess", function () { return _this._originalPostProcess; }, true));
  97. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDRBrightPass", function () { return _this._brightPassPostProcess; }, true));
  98. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDRDownSampleX4", function () { return _this._downSampleX4PostProcess; }, true));
  99. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDRGaussianBlurH", function () { return _this._guassianBlurHPostProcess; }, true));
  100. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDRGaussianBlurV", function () { return _this._guassianBlurVPostProcess; }, true));
  101. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDRTextureAdder", function () { return _this._textureAdderPostProcess; }, true));
  102. var addDownSamplerPostProcess = function (id) {
  103. _this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDRDownSampler" + id, function () { return _this._downSamplePostProcesses[id]; }, true));
  104. };
  105. for (var i = HDRRenderingPipeline.LUM_STEPS - 1; i >= 0; i--) {
  106. addDownSamplerPostProcess(i);
  107. }
  108. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), "HDR", function () { return _this._hdrPostProcess; }, true));
  109. // Finish
  110. scene.postProcessRenderPipelineManager.addPipeline(this);
  111. this.update();
  112. }
  113. /**
  114. * Tells the pipeline to update its post-processes
  115. */
  116. HDRRenderingPipeline.prototype.update = function () {
  117. this._needUpdate = true;
  118. };
  119. /**
  120. * Returns the current calculated luminance
  121. */
  122. HDRRenderingPipeline.prototype.getCurrentLuminance = function () {
  123. return this._hdrCurrentLuminance;
  124. };
  125. /**
  126. * Returns the currently drawn luminance
  127. */
  128. HDRRenderingPipeline.prototype.getOutputLuminance = function () {
  129. return this._hdrOutputLuminance;
  130. };
  131. /**
  132. * Creates the HDR post-process and computes the luminance adaptation
  133. */
  134. HDRRenderingPipeline.prototype._createHDRPostProcess = function (scene, ratio) {
  135. var _this = this;
  136. var hdrLastLuminance = 0.0;
  137. this._hdrOutputLuminance = -1.0;
  138. this._hdrCurrentLuminance = 1.0;
  139. this._hdrPostProcess = new BABYLON.PostProcess("hdr", "hdr", ["exposure", "avgLuminance"], ["otherSampler"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define HDR");
  140. this._hdrPostProcess.onApply = function (effect) {
  141. if (_this._hdrOutputLuminance < 0.0) {
  142. _this._hdrOutputLuminance = _this._hdrCurrentLuminance;
  143. }
  144. else {
  145. var dt = (hdrLastLuminance - (hdrLastLuminance + scene.getEngine().getDeltaTime())) / 1000.0;
  146. if (_this._hdrCurrentLuminance < _this._hdrOutputLuminance + _this.luminanceDecreaseRate * dt) {
  147. _this._hdrOutputLuminance += _this.luminanceDecreaseRate * dt;
  148. }
  149. else if (_this._hdrCurrentLuminance > _this._hdrOutputLuminance - _this.luminanceIncreaserate * dt) {
  150. _this._hdrOutputLuminance -= _this.luminanceIncreaserate * dt;
  151. }
  152. else {
  153. _this._hdrOutputLuminance = _this._hdrCurrentLuminance;
  154. }
  155. }
  156. _this._hdrOutputLuminance = BABYLON.Tools.Clamp(_this._hdrOutputLuminance, _this.minimumLuminance, _this.maximumLuminance);
  157. hdrLastLuminance += scene.getEngine().getDeltaTime();
  158. effect.setTextureFromPostProcess("textureSampler", _this._originalPostProcess);
  159. effect.setTextureFromPostProcess("otherSampler", _this._textureAdderPostProcess);
  160. effect.setFloat("exposure", _this.exposure);
  161. effect.setFloat("avgLuminance", _this._hdrOutputLuminance);
  162. _this._needUpdate = false;
  163. };
  164. };
  165. /**
  166. * Texture Adder post-process
  167. */
  168. HDRRenderingPipeline.prototype._createTextureAdderPostProcess = function (scene, ratio) {
  169. var _this = this;
  170. this._textureAdderPostProcess = new BABYLON.PostProcess("hdr", "hdr", [], ["otherSampler"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define TEXTURE_ADDER");
  171. this._textureAdderPostProcess.onApply = function (effect) {
  172. effect.setTextureFromPostProcess("otherSampler", _this._originalPostProcess);
  173. };
  174. };
  175. /**
  176. * Down sample X4 post-process
  177. */
  178. HDRRenderingPipeline.prototype._createDownSampleX4PostProcess = function (scene, ratio) {
  179. var _this = this;
  180. var downSampleX4Offsets = new Array(32);
  181. this._downSampleX4PostProcess = new BABYLON.PostProcess("hdr", "hdr", ["dsOffsets"], [], ratio / 4, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define DOWN_SAMPLE_X4");
  182. this._downSampleX4PostProcess.onApply = function (effect) {
  183. if (_this._needUpdate) {
  184. var id = 0;
  185. for (var i = -2; i < 2; i++) {
  186. for (var j = -2; j < 2; j++) {
  187. downSampleX4Offsets[id] = (i + 0.5) * (1.0 / _this._downSampleX4PostProcess.width);
  188. downSampleX4Offsets[id + 1] = (j + 0.5) * (1.0 / _this._downSampleX4PostProcess.height);
  189. id += 2;
  190. }
  191. }
  192. }
  193. effect.setArray2("dsOffsets", downSampleX4Offsets);
  194. };
  195. };
  196. /**
  197. * Bright pass post-process
  198. */
  199. HDRRenderingPipeline.prototype._createBrightPassPostProcess = function (scene, ratio) {
  200. var _this = this;
  201. var brightOffsets = new Array(8);
  202. var brightPassCallback = function (effect) {
  203. if (_this._needUpdate) {
  204. var sU = (1.0 / _this._brightPassPostProcess.width);
  205. var sV = (1.0 / _this._brightPassPostProcess.height);
  206. brightOffsets[0] = -0.5 * sU;
  207. brightOffsets[1] = 0.5 * sV;
  208. brightOffsets[2] = 0.5 * sU;
  209. brightOffsets[3] = 0.5 * sV;
  210. brightOffsets[4] = -0.5 * sU;
  211. brightOffsets[5] = -0.5 * sV;
  212. brightOffsets[6] = 0.5 * sU;
  213. brightOffsets[7] = -0.5 * sV;
  214. }
  215. effect.setArray2("dsOffsets", brightOffsets);
  216. effect.setFloat("brightThreshold", _this.brightThreshold);
  217. };
  218. this._brightPassPostProcess = new BABYLON.PostProcess("hdr", "hdr", ["dsOffsets", "brightThreshold"], [], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define BRIGHT_PASS");
  219. this._brightPassPostProcess.onApply = brightPassCallback;
  220. };
  221. /**
  222. * Luminance generator. Creates the luminance post-process and down sample post-processes
  223. */
  224. HDRRenderingPipeline.prototype._createLuminanceGeneratorPostProcess = function (scene) {
  225. var _this = this;
  226. var lumSteps = HDRRenderingPipeline.LUM_STEPS;
  227. var luminanceOffsets = new Array(8);
  228. var downSampleOffsets = new Array(18);
  229. var halfDestPixelSize;
  230. this._downSamplePostProcesses = new Array(lumSteps);
  231. // Utils for luminance
  232. var luminanceUpdateSourceOffsets = function (width, height) {
  233. var sU = (1.0 / width);
  234. var sV = (1.0 / height);
  235. luminanceOffsets[0] = -0.5 * sU;
  236. luminanceOffsets[1] = 0.5 * sV;
  237. luminanceOffsets[2] = 0.5 * sU;
  238. luminanceOffsets[3] = 0.5 * sV;
  239. luminanceOffsets[4] = -0.5 * sU;
  240. luminanceOffsets[5] = -0.5 * sV;
  241. luminanceOffsets[6] = 0.5 * sU;
  242. luminanceOffsets[7] = -0.5 * sV;
  243. };
  244. var luminanceUpdateDestOffsets = function (width, height) {
  245. var id = 0;
  246. for (var x = -1; x < 2; x++) {
  247. for (var y = -1; y < 2; y++) {
  248. downSampleOffsets[id] = (x) / width;
  249. downSampleOffsets[id + 1] = (y) / height;
  250. id += 2;
  251. }
  252. }
  253. };
  254. // Luminance callback
  255. var luminanceCallback = function (effect) {
  256. if (_this._needUpdate) {
  257. luminanceUpdateSourceOffsets(_this._textureAdderPostProcess.width, _this._textureAdderPostProcess.height);
  258. }
  259. effect.setTextureFromPostProcess("textureSampler", _this._textureAdderPostProcess);
  260. effect.setArray2("lumOffsets", luminanceOffsets);
  261. };
  262. // Down sample callbacks
  263. var downSampleCallback = function (indice) {
  264. var i = indice;
  265. return function (effect) {
  266. luminanceUpdateSourceOffsets(_this._downSamplePostProcesses[i].width, _this._downSamplePostProcesses[i].height);
  267. luminanceUpdateDestOffsets(_this._downSamplePostProcesses[i].width, _this._downSamplePostProcesses[i].height);
  268. halfDestPixelSize = 0.5 / _this._downSamplePostProcesses[i].width;
  269. effect.setTextureFromPostProcess("textureSampler", _this._downSamplePostProcesses[i + 1]);
  270. effect.setFloat("halfDestPixelSize", halfDestPixelSize);
  271. effect.setArray2("dsOffsets", downSampleOffsets);
  272. };
  273. };
  274. var downSampleAfterRenderCallback = function (effect) {
  275. // Unpack result
  276. var pixel = scene.getEngine().readPixels(0, 0, 1, 1);
  277. var bit_shift = new BABYLON.Vector4(1.0 / (255.0 * 255.0 * 255.0), 1.0 / (255.0 * 255.0), 1.0 / 255.0, 1.0);
  278. _this._hdrCurrentLuminance = (pixel[0] * bit_shift.x + pixel[1] * bit_shift.y + pixel[2] * bit_shift.z + pixel[3] * bit_shift.w) / 100.0;
  279. };
  280. // Create luminance post-process
  281. var ratio = { width: Math.pow(3, lumSteps - 1), height: Math.pow(3, lumSteps - 1) };
  282. this._downSamplePostProcesses[lumSteps - 1] = new BABYLON.PostProcess("hdr", "hdr", ["lumOffsets"], [], ratio, null, BABYLON.Texture.NEAREST_SAMPLINGMODE, scene.getEngine(), false, "#define LUMINANCE_GENERATOR", BABYLON.Engine.TEXTURETYPE_FLOAT);
  283. this._downSamplePostProcesses[lumSteps - 1].onApply = luminanceCallback;
  284. // Create down sample post-processes
  285. for (var i = lumSteps - 2; i >= 0; i--) {
  286. var length = Math.pow(3, i);
  287. ratio = { width: length, height: length };
  288. var defines = "#define DOWN_SAMPLE\n";
  289. if (i === 0) {
  290. defines += "#define FINAL_DOWN_SAMPLE\n"; // To pack the result
  291. }
  292. this._downSamplePostProcesses[i] = new BABYLON.PostProcess("hdr", "hdr", ["dsOffsets", "halfDestPixelSize"], [], ratio, null, BABYLON.Texture.NEAREST_SAMPLINGMODE, scene.getEngine(), false, defines, BABYLON.Engine.TEXTURETYPE_FLOAT);
  293. this._downSamplePostProcesses[i].onApply = downSampleCallback(i);
  294. if (i === 0) {
  295. this._downSamplePostProcesses[i].onAfterRender = downSampleAfterRenderCallback;
  296. }
  297. }
  298. };
  299. /**
  300. * Gaussian blur post-processes. Horizontal and Vertical
  301. */
  302. HDRRenderingPipeline.prototype._createGaussianBlurPostProcess = function (scene, ratio) {
  303. var _this = this;
  304. var blurOffsetsW = new Array(9);
  305. var blurOffsetsH = new Array(9);
  306. var blurWeights = new Array(9);
  307. var uniforms = ["blurOffsets", "blurWeights"];
  308. // Utils for gaussian blur
  309. var calculateBlurOffsets = function (height) {
  310. var lastOutputDimensions = {
  311. width: scene.getEngine().getRenderWidth() * (ratio / 4),
  312. height: scene.getEngine().getRenderHeight() * (ratio / 4)
  313. };
  314. for (var i = 0; i < 9; i++) {
  315. var value = (i - 4.0) * (1.0 / (height === true ? lastOutputDimensions.height : lastOutputDimensions.width));
  316. if (height) {
  317. blurOffsetsH[i] = value;
  318. }
  319. else {
  320. blurOffsetsW[i] = value;
  321. }
  322. }
  323. };
  324. var calculateWeights = function () {
  325. var x = 0.0;
  326. for (var i = 0; i < 9; i++) {
  327. x = (i - 4.0) / 4.0;
  328. blurWeights[i] = _this.gaussCoeff * (1.0 / Math.sqrt(2.0 * Math.PI * _this.gaussStandDev * _this.gaussStandDev)) * Math.exp((-((x - _this.gaussMean) * (x - _this.gaussMean))) / (2.0 * _this.gaussStandDev * _this.gaussStandDev));
  329. }
  330. };
  331. // Callback
  332. var gaussianBlurCallback = function (height) {
  333. return function (effect) {
  334. if (_this._needUpdate) {
  335. calculateWeights();
  336. calculateBlurOffsets(height);
  337. }
  338. effect.setArray("blurOffsets", height ? blurOffsetsH : blurOffsetsW);
  339. effect.setArray("blurWeights", blurWeights);
  340. };
  341. };
  342. // Create horizontal gaussian blur post-processes
  343. this._guassianBlurHPostProcess = new BABYLON.PostProcess("hdr", "hdr", uniforms, [], ratio / 4, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define GAUSSIAN_BLUR_H");
  344. this._guassianBlurHPostProcess.onApply = gaussianBlurCallback(false);
  345. // Create vertical gaussian blur post-process
  346. this._guassianBlurVPostProcess = new BABYLON.PostProcess("hdr", "hdr", uniforms, [], ratio / 4, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define GAUSSIAN_BLUR_V");
  347. this._guassianBlurVPostProcess.onApply = gaussianBlurCallback(true);
  348. };
  349. // Luminance generator
  350. HDRRenderingPipeline.LUM_STEPS = 6;
  351. return HDRRenderingPipeline;
  352. })(BABYLON.PostProcessRenderPipeline);
  353. BABYLON.HDRRenderingPipeline = HDRRenderingPipeline;
  354. })(BABYLON || (BABYLON = {}));
  355. //# sourceMappingURL=babylon.hdrRenderingPipeline.js.map