babylon.lensRenderingPipeline.ts 14 KB

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