babylon.lensRenderingPipeline.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /// <reference path="babylon.d.ts" />
  2. module BABYLON {
  3. export class LensRenderingPipeline extends PostProcessRenderPipeline {
  4. // Lens effects can be of the following:
  5. // - chromatic aberration (slight shift of RGB colors)
  6. // - blur on the edge of the lens
  7. // - lens distortion
  8. // - depth-of-field 'bokeh' effect (shapes appearing in blured areas, stronger highlights)
  9. // - grain/dust-on-lens effect
  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 depth-of-field PostProcess id in the pipeline
  20. * @type {string}
  21. */
  22. public LensDepthOfFieldEffect: string = "LensDepthOfFieldEffect";
  23. private _scene: Scene;
  24. private _depthTexture: RenderTargetTexture;
  25. private _grainTexture: Texture;
  26. private _chromaticAberrationPostProcess: PostProcess;
  27. private _depthOfFieldPostProcess: PostProcess;
  28. private _edgeBlur: number;
  29. private _grainAmount: number;
  30. private _chromaticAberration: number;
  31. private _distortion: number;
  32. private _highlightsGain: number;
  33. private _highlightsThreshold: number;
  34. private _dofDepth: number;
  35. private _dofAperture: number;
  36. private _dofPentagon: boolean;
  37. private _blurNoise: boolean;
  38. /**
  39. * @constructor
  40. * @param {string} name - The rendering pipeline name
  41. * @param {object} parameters - An object containing all parameters (see below)
  42. * @param {BABYLON.Scene} scene - The scene linked to this pipeline
  43. * @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)
  44. * @param {BABYLON.Camera[]} cameras - The array of cameras that the rendering pipeline will be attached to
  45. Effect parameters are as follow:
  46. {
  47. chromatic_aberration: number; // from 0 to x (1 for realism)
  48. edge_blur: number; // from 0 to x (1 for realism)
  49. distortion: number; // from 0 to x (1 for realism)
  50. grain_amount: number; // from 0 to 1
  51. grain_texture: BABYLON.Texture; // texture to use for grain effect; if unset, use random B&W noise
  52. dof_focus_depth: number; // depth-of-field: focus depth; unset to disable
  53. dof_aperture: number; // depth-of-field: focus blur bias (default: 1)
  54. dof_pentagon: boolean; // depth-of-field: makes a pentagon-like "bokeh" effect
  55. dof_gain: boolean; // depth-of-field: depthOfField gain (default: 1)
  56. dof_threshold: boolean; // depth-of-field: depthOfField threshold (default: 1)
  57. blur_noise: boolean; // add a little bit of noise to the blur (default: true)
  58. }
  59. Note: if an effect parameter is unset, effect is disabled
  60. */
  61. constructor(name: string, parameters: any, scene: Scene, ratio: number = 1.0, cameras?: Camera[]) {
  62. super(scene.getEngine(), name);
  63. this._scene = scene;
  64. // Fetch texture samplers
  65. this._depthTexture = scene.enableDepthRenderer().getDepthMap(); // Force depth renderer "on"
  66. if(parameters.grain_texture) { this._grainTexture = parameters.grain_texture; }
  67. else { this._createGrainTexture(); }
  68. // save parameters
  69. this._edgeBlur = parameters.edge_blur ? parameters.edge_blur : 0;
  70. this._grainAmount = parameters.grain_amount ? parameters.grain_amount : 0;
  71. this._chromaticAberration = parameters.chromatic_aberration ? parameters.chromatic_aberration : 0;
  72. this._distortion = parameters.distortion ? parameters.distortion : 0;
  73. this._highlightsGain = parameters.dof_gain ? parameters.dof_gain : 1;
  74. this._highlightsThreshold = parameters.dof_threshold ? parameters.dof_threshold : 1;
  75. this._dofDepth = parameters.dof_focus_depth !== undefined ? parameters.dof_focus_depth : -1;
  76. this._dofAperture = parameters.dof_aperture ? parameters.dof_aperture : 1;
  77. this._dofPentagon = parameters.dof_pentagon !== undefined ? parameters.dof_pentagon : true;
  78. this._blurNoise = parameters.blur_noise !== undefined ? parameters.blur_noise : true;
  79. // Create effects
  80. this._createChromaticAberrationPostProcess(ratio);
  81. this._createDepthOfFieldPostProcess(ratio);
  82. // Set up pipeline
  83. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.LensChromaticAberrationEffect, () => { return this._chromaticAberrationPostProcess; }, true));
  84. this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.LensDepthOfFieldEffect, () => { return this._depthOfFieldPostProcess; }, true));
  85. // Finish
  86. scene.postProcessRenderPipelineManager.addPipeline(this);
  87. if(cameras) {
  88. scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(name, cameras);
  89. }
  90. }
  91. // public methods
  92. public setEdgeBlur(amount: number) { this._edgeBlur = amount; }
  93. public disableEdgeBlur() { this._edgeBlur = 0; }
  94. public setGrainAmount(amount: number) { this._grainAmount = amount; }
  95. public disableGrain() { this._grainAmount = 0; }
  96. public setChromaticAberration(amount: number) { this._chromaticAberration = amount; }
  97. public disableChromaticAberration() { this._chromaticAberration = 0; }
  98. public setEdgeDistortion(amount: number) { this._distortion = amount; }
  99. public disableEdgeDistortion() { this._distortion = 0; }
  100. public setHighlightsGain(amount: number) { this._highlightsGain = amount; }
  101. public setHighlightsThreshold(amount: number) { this._highlightsThreshold = amount; }
  102. public setFocusDepth(amount: number) { this._dofDepth = amount; }
  103. public disableDepthOfField() { this._dofDepth = -1; }
  104. public setAperture(amount: number) { this._dofAperture = amount; }
  105. public enablePentagonBokeh() { this._dofPentagon = true; }
  106. public disablePentagonBokeh() { this._dofPentagon = false; }
  107. public enableNoiseBlur() { this._blurNoise = true; }
  108. public disableNoiseBlur() { this._blurNoise = false; }
  109. /**
  110. * Removes the internal pipeline assets and detaches the pipeline from the scene cameras
  111. */
  112. public dispose(disableDepthRender: boolean = false): void {
  113. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  114. // this._originalColorPostProcess = undefined;
  115. this._chromaticAberrationPostProcess = undefined
  116. this._depthOfFieldPostProcess = undefined;
  117. this._grainTexture.dispose();
  118. if (disableDepthRender)
  119. this._scene.disableDepthRenderer();
  120. }
  121. // colors shifting and distortion
  122. private _createChromaticAberrationPostProcess(ratio: number): void {
  123. this._chromaticAberrationPostProcess = new PostProcess("LensChromaticAberration", "./chromaticAberration",
  124. ["chromatic_aberration", "screen_width", "screen_height"], // uniforms
  125. [], // samplers
  126. ratio, null, Texture.TRILINEAR_SAMPLINGMODE,
  127. this._scene.getEngine(), false);
  128. this._chromaticAberrationPostProcess.onApply = (effect: Effect) => {
  129. effect.setFloat('chromatic_aberration', this._chromaticAberration);
  130. effect.setFloat('screen_width', this._scene.getEngine().getRenderWidth());
  131. effect.setFloat('screen_height', this._scene.getEngine().getRenderHeight());
  132. };
  133. }
  134. // colors shifting and distortion
  135. private _createDepthOfFieldPostProcess(ratio: number): void {
  136. this._depthOfFieldPostProcess = new PostProcess("LensDepthOfField", "./depthOfField",
  137. [
  138. "gain", "threshold", "focus_depth", "aperture", "pentagon", "maxZ", "edge_blur",
  139. "chromatic_aberration", "distortion", "blur_noise", "grain_amount", "screen_width", "screen_height"
  140. ],
  141. ["depthSampler", "grainSampler"],
  142. ratio, null, Texture.TRILINEAR_SAMPLINGMODE,
  143. this._scene.getEngine(), false);
  144. this._depthOfFieldPostProcess.onApply = (effect: Effect) => {
  145. effect.setBool('pentagon', this._dofPentagon);
  146. effect.setBool('blur_noise', this._blurNoise);
  147. effect.setFloat('maxZ', this._scene.activeCamera.maxZ);
  148. effect.setFloat('grain_amount', this._grainAmount);
  149. effect.setTexture("depthSampler", this._depthTexture);
  150. effect.setTexture("grainSampler", this._grainTexture);
  151. effect.setFloat('screen_width', this._scene.getEngine().getRenderWidth());
  152. effect.setFloat('screen_height', this._scene.getEngine().getRenderHeight());
  153. effect.setFloat('distortion', this._distortion);
  154. effect.setFloat('focus_depth', this._dofDepth);
  155. effect.setFloat('aperture', this._dofAperture);
  156. effect.setFloat('gain', this._highlightsGain);
  157. effect.setFloat('threshold', this._highlightsThreshold);
  158. effect.setFloat('edge_blur', this._edgeBlur);
  159. };
  160. }
  161. // creates a black and white random noise texture, 512x512
  162. private _createGrainTexture(): void {
  163. var size = 512;
  164. this._grainTexture = new DynamicTexture("LensNoiseTexture", size, this._scene, false, Texture.BILINEAR_SAMPLINGMODE);
  165. this._grainTexture.wrapU = Texture.WRAP_ADDRESSMODE;
  166. this._grainTexture.wrapV = Texture.WRAP_ADDRESSMODE;
  167. var context = (<BABYLON.DynamicTexture>this._grainTexture).getContext();
  168. var rand = (min, max) => {
  169. return Math.random() * (max - min) + min;
  170. }
  171. var value;
  172. for (var x = 0; x < size; x++) {
  173. for (var y = 0; y < size; y++) {
  174. value = Math.floor(rand(0.42,0.58)*255);
  175. context.fillStyle = 'rgb(' + value + ', ' + value + ', ' + value + ')';
  176. context.fillRect(x, y, 1, 1);
  177. }
  178. }
  179. (<BABYLON.DynamicTexture>this._grainTexture).update(false);
  180. }
  181. }
  182. }