babylon.lensRenderingPipeline.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var __extends = 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 LensRenderingPipeline = (function (_super) {
  10. __extends(LensRenderingPipeline, _super);
  11. /**
  12. * @constructor
  13. *
  14. * Effect parameters are as follow:
  15. * {
  16. * chromatic_aberration: number; // from 0 to x (1 for realism)
  17. * edge_blur: number; // from 0 to x (1 for realism)
  18. * distortion: number; // from 0 to x (1 for realism)
  19. * grain_amount: number; // from 0 to 1
  20. * grain_texture: BABYLON.Texture; // texture to use for grain effect; if unset, use random B&W noise
  21. * dof_focus_depth: number; // depth-of-field: focus depth; unset to disable (disabled by default)
  22. * dof_aperture: number; // depth-of-field: focus blur bias (default: 1)
  23. * dof_pentagon: boolean; // depth-of-field: makes a pentagon-like "bokeh" effect
  24. * dof_gain: number; // depth-of-field: depthOfField gain; unset to disable (disabled by default)
  25. * dof_threshold: number; // depth-of-field: depthOfField threshold (default: 1)
  26. * blur_noise: boolean; // add a little bit of noise to the blur (default: true)
  27. * }
  28. * Note: if an effect parameter is unset, effect is disabled
  29. *
  30. * @param {string} name - The rendering pipeline name
  31. * @param {object} parameters - An object containing all parameters (see above)
  32. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  33. * @param {number} 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)
  34. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  35. */
  36. function LensRenderingPipeline(name, parameters, scene, ratio, cameras) {
  37. var _this = this;
  38. if (ratio === void 0) { ratio = 1.0; }
  39. _super.call(this, scene.getEngine(), name);
  40. // Lens effects can be of the following:
  41. // - chromatic aberration (slight shift of RGB colors)
  42. // - blur on the edge of the lens
  43. // - lens distortion
  44. // - depth-of-field blur & highlights enhancing
  45. // - depth-of-field 'bokeh' effect (shapes appearing in blurred areas)
  46. // - grain effect (noise or custom texture)
  47. // Two additional texture samplers are needed:
  48. // - depth map (for depth-of-field)
  49. // - grain texture
  50. /**
  51. * The chromatic aberration PostProcess id in the pipeline
  52. * @type {string}
  53. */
  54. this.LensChromaticAberrationEffect = "LensChromaticAberrationEffect";
  55. /**
  56. * The highlights enhancing PostProcess id in the pipeline
  57. * @type {string}
  58. */
  59. this.HighlightsEnhancingEffect = "HighlightsEnhancingEffect";
  60. /**
  61. * The depth-of-field PostProcess id in the pipeline
  62. * @type {string}
  63. */
  64. this.LensDepthOfFieldEffect = "LensDepthOfFieldEffect";
  65. this._scene = scene;
  66. // Fetch texture samplers
  67. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  68. if (parameters.grain_texture) {
  69. this._grainTexture = parameters.grain_texture;
  70. }
  71. else {
  72. this._createGrainTexture();
  73. }
  74. // save parameters
  75. this._edgeBlur = parameters.edge_blur ? parameters.edge_blur : 0;
  76. this._grainAmount = parameters.grain_amount ? parameters.grain_amount : 0;
  77. this._chromaticAberration = parameters.chromatic_aberration ? parameters.chromatic_aberration : 0;
  78. this._distortion = parameters.distortion ? parameters.distortion : 0;
  79. this._highlightsGain = parameters.dof_gain !== undefined ? parameters.dof_gain : -1;
  80. this._highlightsThreshold = parameters.dof_threshold ? parameters.dof_threshold : 1;
  81. this._dofDepth = parameters.dof_focus_depth !== undefined ? parameters.dof_focus_depth : -1;
  82. this._dofAperture = parameters.dof_aperture ? parameters.dof_aperture : 1;
  83. this._dofPentagon = parameters.dof_pentagon !== undefined ? parameters.dof_pentagon : true;
  84. this._blurNoise = parameters.blur_noise !== undefined ? parameters.blur_noise : true;
  85. // Create effects
  86. this._createChromaticAberrationPostProcess(ratio);
  87. this._createHighlightsPostProcess(ratio);
  88. this._createDepthOfFieldPostProcess(ratio);
  89. // Set up pipeline
  90. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.LensChromaticAberrationEffect, function () {
  91. return _this._chromaticAberrationPostProcess;
  92. }, true));
  93. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.HighlightsEnhancingEffect, function () {
  94. return _this._highlightsPostProcess;
  95. }, true));
  96. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.LensDepthOfFieldEffect, function () {
  97. return _this._depthOfFieldPostProcess;
  98. }, true));
  99. if (this._highlightsGain == -1) {
  100. this._disableEffect(this.HighlightsEnhancingEffect, null);
  101. }
  102. // Finish
  103. scene.postProcessRenderPipelineManager.addPipeline(this);
  104. if (cameras) {
  105. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  106. }
  107. }
  108. // public methods (self explanatory)
  109. LensRenderingPipeline.prototype.setEdgeBlur = function (amount) {
  110. this._edgeBlur = amount;
  111. };
  112. LensRenderingPipeline.prototype.disableEdgeBlur = function () {
  113. this._edgeBlur = 0;
  114. };
  115. LensRenderingPipeline.prototype.setGrainAmount = function (amount) {
  116. this._grainAmount = amount;
  117. };
  118. LensRenderingPipeline.prototype.disableGrain = function () {
  119. this._grainAmount = 0;
  120. };
  121. LensRenderingPipeline.prototype.setChromaticAberration = function (amount) {
  122. this._chromaticAberration = amount;
  123. };
  124. LensRenderingPipeline.prototype.disableChromaticAberration = function () {
  125. this._chromaticAberration = 0;
  126. };
  127. LensRenderingPipeline.prototype.setEdgeDistortion = function (amount) {
  128. this._distortion = amount;
  129. };
  130. LensRenderingPipeline.prototype.disableEdgeDistortion = function () {
  131. this._distortion = 0;
  132. };
  133. LensRenderingPipeline.prototype.setFocusDepth = function (amount) {
  134. this._dofDepth = amount;
  135. };
  136. LensRenderingPipeline.prototype.disableDepthOfField = function () {
  137. this._dofDepth = -1;
  138. };
  139. LensRenderingPipeline.prototype.setAperture = function (amount) {
  140. this._dofAperture = amount;
  141. };
  142. LensRenderingPipeline.prototype.enablePentagonBokeh = function () {
  143. this._dofPentagon = true;
  144. };
  145. LensRenderingPipeline.prototype.disablePentagonBokeh = function () {
  146. this._dofPentagon = false;
  147. };
  148. LensRenderingPipeline.prototype.enableNoiseBlur = function () {
  149. this._blurNoise = true;
  150. };
  151. LensRenderingPipeline.prototype.disableNoiseBlur = function () {
  152. this._blurNoise = false;
  153. };
  154. LensRenderingPipeline.prototype.setHighlightsGain = function (amount) {
  155. this._highlightsGain = amount;
  156. };
  157. LensRenderingPipeline.prototype.setHighlightsThreshold = function (amount) {
  158. if (this._highlightsGain == -1) {
  159. this._highlightsGain = 1.0;
  160. }
  161. this._highlightsThreshold = amount;
  162. };
  163. LensRenderingPipeline.prototype.disableHighlights = function () {
  164. this._highlightsGain = -1;
  165. };
  166. /**
  167. * Removes the internal pipeline assets and detaches the pipeline from the scene cameras
  168. */
  169. LensRenderingPipeline.prototype.dispose = function (disableDepthRender) {
  170. if (disableDepthRender === void 0) { disableDepthRender = false; }
  171. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  172. this._chromaticAberrationPostProcess = undefined;
  173. this._highlightsPostProcess = undefined;
  174. this._depthOfFieldPostProcess = undefined;
  175. this._grainTexture.dispose();
  176. if (disableDepthRender)
  177. this._scene.disableDepthRenderer();
  178. };
  179. // colors shifting and distortion
  180. LensRenderingPipeline.prototype._createChromaticAberrationPostProcess = function (ratio) {
  181. var _this = this;
  182. this._chromaticAberrationPostProcess = new BABYLON.PostProcess("LensChromaticAberration", "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height"], [], ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  183. this._chromaticAberrationPostProcess.onApply = function (effect) {
  184. effect.setFloat('chromatic_aberration', _this._chromaticAberration);
  185. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  186. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  187. };
  188. };
  189. // highlights enhancing
  190. LensRenderingPipeline.prototype._createHighlightsPostProcess = function (ratio) {
  191. var _this = this;
  192. this._highlightsPostProcess = new BABYLON.PostProcess("LensHighlights", "lensHighlights", ["pentagon", "gain", "threshold", "screen_width", "screen_height"], [], ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  193. this._highlightsPostProcess.onApply = function (effect) {
  194. effect.setFloat('gain', _this._highlightsGain);
  195. effect.setFloat('threshold', _this._highlightsThreshold);
  196. effect.setBool('pentagon', _this._dofPentagon);
  197. effect.setTextureFromPostProcess("textureSampler", _this._chromaticAberrationPostProcess);
  198. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  199. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  200. };
  201. };
  202. // colors shifting and distortion
  203. LensRenderingPipeline.prototype._createDepthOfFieldPostProcess = function (ratio) {
  204. var _this = this;
  205. this._depthOfFieldPostProcess = new BABYLON.PostProcess("LensDepthOfField", "depthOfField", [
  206. "focus_depth",
  207. "aperture",
  208. "pentagon",
  209. "maxZ",
  210. "edge_blur",
  211. "chromatic_aberration",
  212. "distortion",
  213. "blur_noise",
  214. "grain_amount",
  215. "screen_width",
  216. "screen_height",
  217. "highlights"
  218. ], ["depthSampler", "grainSampler", "highlightsSampler"], ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  219. this._depthOfFieldPostProcess.onApply = function (effect) {
  220. effect.setBool('blur_noise', _this._blurNoise);
  221. effect.setFloat('maxZ', _this._scene.activeCamera.maxZ);
  222. effect.setFloat('grain_amount', _this._grainAmount);
  223. effect.setTexture("depthSampler", _this._depthTexture);
  224. effect.setTexture("grainSampler", _this._grainTexture);
  225. effect.setTextureFromPostProcess("textureSampler", _this._highlightsPostProcess);
  226. effect.setTextureFromPostProcess("highlightsSampler", _this._depthOfFieldPostProcess);
  227. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  228. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  229. effect.setFloat('distortion', _this._distortion);
  230. effect.setFloat('focus_depth', _this._dofDepth);
  231. effect.setFloat('aperture', _this._dofAperture);
  232. effect.setFloat('edge_blur', _this._edgeBlur);
  233. effect.setBool('highlights', (_this._highlightsGain != -1));
  234. };
  235. };
  236. // creates a black and white random noise texture, 512x512
  237. LensRenderingPipeline.prototype._createGrainTexture = function () {
  238. var size = 512;
  239. this._grainTexture = new BABYLON.DynamicTexture("LensNoiseTexture", size, this._scene, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  240. this._grainTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  241. this._grainTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  242. var context = this._grainTexture.getContext();
  243. var rand = function (min, max) {
  244. return Math.random() * (max - min) + min;
  245. };
  246. var value;
  247. for (var x = 0; x < size; x++) {
  248. for (var y = 0; y < size; y++) {
  249. value = Math.floor(rand(0.42, 0.58) * 255);
  250. context.fillStyle = 'rgb(' + value + ', ' + value + ', ' + value + ')';
  251. context.fillRect(x, y, 1, 1);
  252. }
  253. }
  254. this._grainTexture.update(false);
  255. };
  256. return LensRenderingPipeline;
  257. })(BABYLON.PostProcessRenderPipeline);
  258. BABYLON.LensRenderingPipeline = LensRenderingPipeline;
  259. })(BABYLON || (BABYLON = {}));
  260. //# sourceMappingURL=babylon.lensRenderingPipeline.js.map