babylon.lensRenderingPipeline.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  5. };
  6. var BABYLON;
  7. (function (BABYLON) {
  8. var LensRenderingPipeline = (function (_super) {
  9. __extends(LensRenderingPipeline, _super);
  10. /**
  11. * @constructor
  12. *
  13. * Effect parameters are as follow:
  14. * {
  15. * chromatic_aberration: number; // from 0 to x (1 for realism)
  16. * edge_blur: number; // from 0 to x (1 for realism)
  17. * distortion: number; // from 0 to x (1 for realism)
  18. * grain_amount: number; // from 0 to 1
  19. * grain_texture: BABYLON.Texture; // texture to use for grain effect; if unset, use random B&W noise
  20. * dof_focus_distance: number; // depth-of-field: focus distance; unset to disable (disabled by default)
  21. * dof_aperture: number; // depth-of-field: focus blur bias (default: 1)
  22. * dof_darken: number; // depth-of-field: darken that which is out of focus (from 0 to 1, disabled by default)
  23. * dof_pentagon: boolean; // depth-of-field: makes a pentagon-like "bokeh" effect
  24. * dof_gain: number; // depth-of-field: highlights gain; unset to disable (disabled by default)
  25. * dof_threshold: number; // depth-of-field: highlights 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._dofDistance = parameters.dof_focus_distance !== undefined ? parameters.dof_focus_distance : -1;
  82. this._dofAperture = parameters.dof_aperture ? parameters.dof_aperture : 1;
  83. this._dofDarken = parameters.dof_darken ? parameters.dof_darken : 0;
  84. this._dofPentagon = parameters.dof_pentagon !== undefined ? parameters.dof_pentagon : true;
  85. this._blurNoise = parameters.blur_noise !== undefined ? parameters.blur_noise : true;
  86. // Create effects
  87. this._createChromaticAberrationPostProcess(ratio);
  88. this._createHighlightsPostProcess(ratio);
  89. this._createDepthOfFieldPostProcess(ratio / 4);
  90. // Set up pipeline
  91. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.LensChromaticAberrationEffect, function () { return _this._chromaticAberrationPostProcess; }, true));
  92. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.HighlightsEnhancingEffect, function () { return _this._highlightsPostProcess; }, true));
  93. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.LensDepthOfFieldEffect, function () { return _this._depthOfFieldPostProcess; }, true));
  94. if (this._highlightsGain == -1) {
  95. this._disableEffect(this.HighlightsEnhancingEffect, null);
  96. }
  97. // Finish
  98. scene.postProcessRenderPipelineManager.addPipeline(this);
  99. if (cameras) {
  100. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  101. }
  102. }
  103. // public methods (self explanatory)
  104. LensRenderingPipeline.prototype.setEdgeBlur = function (amount) { this._edgeBlur = amount; };
  105. LensRenderingPipeline.prototype.disableEdgeBlur = function () { this._edgeBlur = 0; };
  106. LensRenderingPipeline.prototype.setGrainAmount = function (amount) { this._grainAmount = amount; };
  107. LensRenderingPipeline.prototype.disableGrain = function () { this._grainAmount = 0; };
  108. LensRenderingPipeline.prototype.setChromaticAberration = function (amount) { this._chromaticAberration = amount; };
  109. LensRenderingPipeline.prototype.disableChromaticAberration = function () { this._chromaticAberration = 0; };
  110. LensRenderingPipeline.prototype.setEdgeDistortion = function (amount) { this._distortion = amount; };
  111. LensRenderingPipeline.prototype.disableEdgeDistortion = function () { this._distortion = 0; };
  112. LensRenderingPipeline.prototype.setFocusDistance = function (amount) { this._dofDistance = amount; };
  113. LensRenderingPipeline.prototype.disableDepthOfField = function () { this._dofDistance = -1; };
  114. LensRenderingPipeline.prototype.setAperture = function (amount) { this._dofAperture = amount; };
  115. LensRenderingPipeline.prototype.setDarkenOutOfFocus = function (amount) { this._dofDarken = amount; };
  116. LensRenderingPipeline.prototype.enablePentagonBokeh = function () { this._dofPentagon = true; };
  117. LensRenderingPipeline.prototype.disablePentagonBokeh = function () { this._dofPentagon = false; };
  118. LensRenderingPipeline.prototype.enableNoiseBlur = function () { this._blurNoise = true; };
  119. LensRenderingPipeline.prototype.disableNoiseBlur = function () { this._blurNoise = false; };
  120. LensRenderingPipeline.prototype.setHighlightsGain = function (amount) {
  121. this._highlightsGain = amount;
  122. };
  123. LensRenderingPipeline.prototype.setHighlightsThreshold = function (amount) {
  124. if (this._highlightsGain == -1) {
  125. this._highlightsGain = 1.0;
  126. }
  127. this._highlightsThreshold = amount;
  128. };
  129. LensRenderingPipeline.prototype.disableHighlights = function () {
  130. this._highlightsGain = -1;
  131. };
  132. /**
  133. * Removes the internal pipeline assets and detaches the pipeline from the scene cameras
  134. */
  135. LensRenderingPipeline.prototype.dispose = function (disableDepthRender) {
  136. if (disableDepthRender === void 0) { disableDepthRender = false; }
  137. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  138. this._chromaticAberrationPostProcess = undefined;
  139. this._highlightsPostProcess = undefined;
  140. this._depthOfFieldPostProcess = undefined;
  141. this._grainTexture.dispose();
  142. if (disableDepthRender)
  143. this._scene.disableDepthRenderer();
  144. };
  145. // colors shifting and distortion
  146. LensRenderingPipeline.prototype._createChromaticAberrationPostProcess = function (ratio) {
  147. var _this = this;
  148. this._chromaticAberrationPostProcess = new BABYLON.PostProcess("LensChromaticAberration", "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height"], // uniforms
  149. [], // samplers
  150. ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  151. this._chromaticAberrationPostProcess.onApply = function (effect) {
  152. effect.setFloat('chromatic_aberration', _this._chromaticAberration);
  153. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  154. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  155. };
  156. };
  157. // highlights enhancing
  158. LensRenderingPipeline.prototype._createHighlightsPostProcess = function (ratio) {
  159. var _this = this;
  160. this._highlightsPostProcess = new BABYLON.PostProcess("LensHighlights", "lensHighlights", ["pentagon", "gain", "threshold", "screen_width", "screen_height"], // uniforms
  161. [], // samplers
  162. ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  163. this._highlightsPostProcess.onApply = function (effect) {
  164. effect.setFloat('gain', _this._highlightsGain);
  165. effect.setFloat('threshold', _this._highlightsThreshold);
  166. effect.setBool('pentagon', _this._dofPentagon);
  167. effect.setTextureFromPostProcess("textureSampler", _this._chromaticAberrationPostProcess);
  168. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  169. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  170. };
  171. };
  172. // colors shifting and distortion
  173. LensRenderingPipeline.prototype._createDepthOfFieldPostProcess = function (ratio) {
  174. var _this = this;
  175. this._depthOfFieldPostProcess = new BABYLON.PostProcess("LensDepthOfField", "depthOfField", [
  176. "grain_amount", "blur_noise", "screen_width", "screen_height", "distortion", "dof_enabled",
  177. "screen_distance", "aperture", "darken", "edge_blur", "highlights", "near", "far"
  178. ], ["depthSampler", "grainSampler", "highlightsSampler"], ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  179. this._depthOfFieldPostProcess.onApply = function (effect) {
  180. effect.setTexture("depthSampler", _this._depthTexture);
  181. effect.setTexture("grainSampler", _this._grainTexture);
  182. effect.setTextureFromPostProcess("textureSampler", _this._highlightsPostProcess);
  183. effect.setTextureFromPostProcess("highlightsSampler", _this._depthOfFieldPostProcess);
  184. effect.setFloat('grain_amount', _this._grainAmount);
  185. effect.setBool('blur_noise', _this._blurNoise);
  186. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  187. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  188. effect.setFloat('distortion', _this._distortion);
  189. effect.setBool('dof_enabled', (_this._dofDistance != -1));
  190. effect.setFloat('screen_distance', 1.0 / (0.1 - 1.0 / _this._dofDistance));
  191. effect.setFloat('aperture', _this._dofAperture);
  192. effect.setFloat('darken', _this._dofDarken);
  193. effect.setFloat('edge_blur', _this._edgeBlur);
  194. effect.setBool('highlights', (_this._highlightsGain != -1));
  195. effect.setFloat('near', _this._scene.activeCamera.minZ);
  196. effect.setFloat('far', _this._scene.activeCamera.maxZ);
  197. };
  198. };
  199. // creates a black and white random noise texture, 512x512
  200. LensRenderingPipeline.prototype._createGrainTexture = function () {
  201. var size = 512;
  202. this._grainTexture = new BABYLON.DynamicTexture("LensNoiseTexture", size, this._scene, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  203. this._grainTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  204. this._grainTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  205. var context = this._grainTexture.getContext();
  206. var rand = function (min, max) {
  207. return Math.random() * (max - min) + min;
  208. };
  209. var value;
  210. for (var x = 0; x < size; x++) {
  211. for (var y = 0; y < size; y++) {
  212. value = Math.floor(rand(0.42, 0.58) * 255);
  213. context.fillStyle = 'rgb(' + value + ', ' + value + ', ' + value + ')';
  214. context.fillRect(x, y, 1, 1);
  215. }
  216. }
  217. this._grainTexture.update(false);
  218. };
  219. return LensRenderingPipeline;
  220. })(BABYLON.PostProcessRenderPipeline);
  221. BABYLON.LensRenderingPipeline = LensRenderingPipeline;
  222. })(BABYLON || (BABYLON = {}));
  223. //# sourceMappingURL=babylon.lensRenderingPipeline.js.map