babylon.lensRenderingPipeline.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 () { return _this._chromaticAberrationPostProcess; }, true));
  91. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.HighlightsEnhancingEffect, function () { return _this._highlightsPostProcess; }, true));
  92. this.addEffect(new BABYLON.PostProcessRenderEffect(scene.getEngine(), this.LensDepthOfFieldEffect, function () { return _this._depthOfFieldPostProcess; }, true));
  93. if (this._highlightsGain == -1) {
  94. this._disableEffect(this.HighlightsEnhancingEffect, null);
  95. }
  96. // Finish
  97. scene.postProcessRenderPipelineManager.addPipeline(this);
  98. if (cameras) {
  99. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  100. }
  101. }
  102. // public methods (self explanatory)
  103. LensRenderingPipeline.prototype.setEdgeBlur = function (amount) { this._edgeBlur = amount; };
  104. LensRenderingPipeline.prototype.disableEdgeBlur = function () { this._edgeBlur = 0; };
  105. LensRenderingPipeline.prototype.setGrainAmount = function (amount) { this._grainAmount = amount; };
  106. LensRenderingPipeline.prototype.disableGrain = function () { this._grainAmount = 0; };
  107. LensRenderingPipeline.prototype.setChromaticAberration = function (amount) { this._chromaticAberration = amount; };
  108. LensRenderingPipeline.prototype.disableChromaticAberration = function () { this._chromaticAberration = 0; };
  109. LensRenderingPipeline.prototype.setEdgeDistortion = function (amount) { this._distortion = amount; };
  110. LensRenderingPipeline.prototype.disableEdgeDistortion = function () { this._distortion = 0; };
  111. LensRenderingPipeline.prototype.setFocusDepth = function (amount) { this._dofDepth = amount; };
  112. LensRenderingPipeline.prototype.disableDepthOfField = function () { this._dofDepth = -1; };
  113. LensRenderingPipeline.prototype.setAperture = function (amount) { this._dofAperture = amount; };
  114. LensRenderingPipeline.prototype.enablePentagonBokeh = function () { this._dofPentagon = true; };
  115. LensRenderingPipeline.prototype.disablePentagonBokeh = function () { this._dofPentagon = false; };
  116. LensRenderingPipeline.prototype.enableNoiseBlur = function () { this._blurNoise = true; };
  117. LensRenderingPipeline.prototype.disableNoiseBlur = function () { this._blurNoise = false; };
  118. LensRenderingPipeline.prototype.setHighlightsGain = function (amount) {
  119. this._highlightsGain = amount;
  120. };
  121. LensRenderingPipeline.prototype.setHighlightsThreshold = function (amount) {
  122. if (this._highlightsGain == -1) {
  123. this._highlightsGain = 1.0;
  124. }
  125. this._highlightsThreshold = amount;
  126. };
  127. LensRenderingPipeline.prototype.disableHighlights = function () {
  128. this._highlightsGain = -1;
  129. };
  130. /**
  131. * Removes the internal pipeline assets and detaches the pipeline from the scene cameras
  132. */
  133. LensRenderingPipeline.prototype.dispose = function (disableDepthRender) {
  134. if (disableDepthRender === void 0) { disableDepthRender = false; }
  135. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  136. this._chromaticAberrationPostProcess = undefined;
  137. this._highlightsPostProcess = undefined;
  138. this._depthOfFieldPostProcess = undefined;
  139. this._grainTexture.dispose();
  140. if (disableDepthRender)
  141. this._scene.disableDepthRenderer();
  142. };
  143. // colors shifting and distortion
  144. LensRenderingPipeline.prototype._createChromaticAberrationPostProcess = function (ratio) {
  145. var _this = this;
  146. this._chromaticAberrationPostProcess = new BABYLON.PostProcess("LensChromaticAberration", "chromaticAberration", ["chromatic_aberration", "screen_width", "screen_height"], [], ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  147. this._chromaticAberrationPostProcess.onApply = function (effect) {
  148. effect.setFloat('chromatic_aberration', _this._chromaticAberration);
  149. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  150. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  151. };
  152. };
  153. // highlights enhancing
  154. LensRenderingPipeline.prototype._createHighlightsPostProcess = function (ratio) {
  155. var _this = this;
  156. this._highlightsPostProcess = new BABYLON.PostProcess("LensHighlights", "lensHighlights", ["pentagon", "gain", "threshold", "screen_width", "screen_height"], [], ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  157. this._highlightsPostProcess.onApply = function (effect) {
  158. effect.setFloat('gain', _this._highlightsGain);
  159. effect.setFloat('threshold', _this._highlightsThreshold);
  160. effect.setBool('pentagon', _this._dofPentagon);
  161. effect.setTextureFromPostProcess("textureSampler", _this._chromaticAberrationPostProcess);
  162. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  163. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  164. };
  165. };
  166. // colors shifting and distortion
  167. LensRenderingPipeline.prototype._createDepthOfFieldPostProcess = function (ratio) {
  168. var _this = this;
  169. this._depthOfFieldPostProcess = new BABYLON.PostProcess("LensDepthOfField", "depthOfField", [
  170. "focus_depth", "aperture", "pentagon", "maxZ", "edge_blur", "chromatic_aberration",
  171. "distortion", "blur_noise", "grain_amount", "screen_width", "screen_height", "highlights"
  172. ], ["depthSampler", "grainSampler", "highlightsSampler"], ratio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false);
  173. this._depthOfFieldPostProcess.onApply = function (effect) {
  174. effect.setBool('blur_noise', _this._blurNoise);
  175. effect.setFloat('maxZ', _this._scene.activeCamera.maxZ);
  176. effect.setFloat('grain_amount', _this._grainAmount);
  177. effect.setTexture("depthSampler", _this._depthTexture);
  178. effect.setTexture("grainSampler", _this._grainTexture);
  179. effect.setTextureFromPostProcess("textureSampler", _this._highlightsPostProcess);
  180. effect.setTextureFromPostProcess("highlightsSampler", _this._depthOfFieldPostProcess);
  181. effect.setFloat('screen_width', _this._scene.getEngine().getRenderingCanvas().width);
  182. effect.setFloat('screen_height', _this._scene.getEngine().getRenderingCanvas().height);
  183. effect.setFloat('distortion', _this._distortion);
  184. effect.setFloat('focus_depth', _this._dofDepth);
  185. effect.setFloat('aperture', _this._dofAperture);
  186. effect.setFloat('edge_blur', _this._edgeBlur);
  187. effect.setBool('highlights', (_this._highlightsGain != -1));
  188. };
  189. };
  190. // creates a black and white random noise texture, 512x512
  191. LensRenderingPipeline.prototype._createGrainTexture = function () {
  192. var size = 512;
  193. this._grainTexture = new BABYLON.DynamicTexture("LensNoiseTexture", size, this._scene, false, BABYLON.Texture.BILINEAR_SAMPLINGMODE);
  194. this._grainTexture.wrapU = BABYLON.Texture.WRAP_ADDRESSMODE;
  195. this._grainTexture.wrapV = BABYLON.Texture.WRAP_ADDRESSMODE;
  196. var context = this._grainTexture.getContext();
  197. var rand = function (min, max) {
  198. return Math.random() * (max - min) + min;
  199. };
  200. var value;
  201. for (var x = 0; x < size; x++) {
  202. for (var y = 0; y < size; y++) {
  203. value = Math.floor(rand(0.42, 0.58) * 255);
  204. context.fillStyle = 'rgb(' + value + ', ' + value + ', ' + value + ')';
  205. context.fillRect(x, y, 1, 1);
  206. }
  207. }
  208. this._grainTexture.update(false);
  209. };
  210. return LensRenderingPipeline;
  211. })(BABYLON.PostProcessRenderPipeline);
  212. BABYLON.LensRenderingPipeline = LensRenderingPipeline;
  213. })(BABYLON || (BABYLON = {}));
  214. //# sourceMappingURL=babylon.lensRenderingPipeline.js.map