babylon.lensRenderingPipeline.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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() { this._dofPentagon = true; }
  121. public disablePentagonBokeh() { this._dofPentagon = false; }
  122. public enableNoiseBlur() { this._blurNoise = true; }
  123. public disableNoiseBlur() { this._blurNoise = false; }
  124. public setHighlightsGain(amount: number) {
  125. this._highlightsGain = amount;
  126. }
  127. public setHighlightsThreshold(amount: number) {
  128. if (this._highlightsGain === -1) {
  129. this._highlightsGain = 1.0;
  130. }
  131. this._highlightsThreshold = amount;
  132. }
  133. public disableHighlights() {
  134. this._highlightsGain = -1;
  135. }
  136. /**
  137. * Removes the internal pipeline assets and detaches the pipeline from the scene cameras
  138. */
  139. public dispose(disableDepthRender: boolean = false): void {
  140. this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
  141. this._chromaticAberrationPostProcess = undefined;
  142. this._highlightsPostProcess = undefined;
  143. this._depthOfFieldPostProcess = undefined;
  144. this._grainTexture.dispose();
  145. if (disableDepthRender)
  146. this._scene.disableDepthRenderer();
  147. }
  148. // colors shifting and distortion
  149. private _createChromaticAberrationPostProcess(ratio: number): void {
  150. this._chromaticAberrationPostProcess = new PostProcess("LensChromaticAberration", "chromaticAberration",
  151. ["chromatic_aberration", "screen_width", "screen_height"], // uniforms
  152. [], // samplers
  153. ratio, null, Texture.TRILINEAR_SAMPLINGMODE,
  154. this._scene.getEngine(), false);
  155. this._chromaticAberrationPostProcess.onApply = (effect: Effect) => {
  156. effect.setFloat('chromatic_aberration', this._chromaticAberration);
  157. effect.setFloat('screen_width', this._scene.getEngine().getRenderingCanvas().width);
  158. effect.setFloat('screen_height', this._scene.getEngine().getRenderingCanvas().height);
  159. };
  160. }
  161. // highlights enhancing
  162. private _createHighlightsPostProcess(ratio: number): void {
  163. this._highlightsPostProcess = new PostProcess("LensHighlights", "lensHighlights",
  164. ["pentagon", "gain", "threshold", "screen_width", "screen_height"], // uniforms
  165. [], // samplers
  166. ratio, null, Texture.TRILINEAR_SAMPLINGMODE,
  167. this._scene.getEngine(), false);
  168. this._highlightsPostProcess.onApply = (effect: Effect) => {
  169. effect.setFloat('gain', this._highlightsGain);
  170. effect.setFloat('threshold', this._highlightsThreshold);
  171. effect.setBool('pentagon', this._dofPentagon);
  172. effect.setTextureFromPostProcess("textureSampler", this._chromaticAberrationPostProcess);
  173. effect.setFloat('screen_width', this._scene.getEngine().getRenderingCanvas().width);
  174. effect.setFloat('screen_height', this._scene.getEngine().getRenderingCanvas().height);
  175. };
  176. }
  177. // colors shifting and distortion
  178. private _createDepthOfFieldPostProcess(ratio: number): void {
  179. this._depthOfFieldPostProcess = new PostProcess("LensDepthOfField", "depthOfField",
  180. [
  181. "grain_amount", "blur_noise", "screen_width", "screen_height", "distortion", "dof_enabled",
  182. "screen_distance", "aperture", "darken", "edge_blur", "highlights", "near", "far"
  183. ],
  184. ["depthSampler", "grainSampler", "highlightsSampler"],
  185. ratio, null, Texture.TRILINEAR_SAMPLINGMODE,
  186. this._scene.getEngine(), false);
  187. this._depthOfFieldPostProcess.onApply = (effect: Effect) => {
  188. effect.setTexture("depthSampler", this._depthTexture);
  189. effect.setTexture("grainSampler", this._grainTexture);
  190. effect.setTextureFromPostProcess("textureSampler", this._highlightsPostProcess);
  191. effect.setTextureFromPostProcess("highlightsSampler", this._depthOfFieldPostProcess);
  192. effect.setFloat('grain_amount', this._grainAmount);
  193. effect.setBool('blur_noise', this._blurNoise);
  194. effect.setFloat('screen_width', this._scene.getEngine().getRenderingCanvas().width);
  195. effect.setFloat('screen_height', this._scene.getEngine().getRenderingCanvas().height);
  196. effect.setFloat('distortion', this._distortion);
  197. effect.setBool('dof_enabled', (this._dofDistance !== -1));
  198. effect.setFloat('screen_distance', 1.0 / (0.1 - 1.0 / this._dofDistance));
  199. effect.setFloat('aperture', this._dofAperture);
  200. effect.setFloat('darken', this._dofDarken);
  201. effect.setFloat('edge_blur', this._edgeBlur);
  202. effect.setBool('highlights', (this._highlightsGain !== -1));
  203. effect.setFloat('near', this._scene.activeCamera.minZ);
  204. effect.setFloat('far', this._scene.activeCamera.maxZ);
  205. };
  206. }
  207. // creates a black and white random noise texture, 512x512
  208. private _createGrainTexture(): void {
  209. var size = 512;
  210. this._grainTexture = new DynamicTexture("LensNoiseTexture", size, this._scene, false, Texture.BILINEAR_SAMPLINGMODE);
  211. this._grainTexture.wrapU = Texture.WRAP_ADDRESSMODE;
  212. this._grainTexture.wrapV = Texture.WRAP_ADDRESSMODE;
  213. var context = (<DynamicTexture>this._grainTexture).getContext();
  214. var rand = (min, max) => {
  215. return Math.random() * (max - min) + min;
  216. }
  217. var value;
  218. for (var x = 0; x < size; x++) {
  219. for (var y = 0; y < size; y++) {
  220. value = Math.floor(rand(0.42, 0.58) * 255);
  221. context.fillStyle = 'rgb(' + value + ', ' + value + ', ' + value + ')';
  222. context.fillRect(x, y, 1, 1);
  223. }
  224. }
  225. (<DynamicTexture>this._grainTexture).update(false);
  226. }
  227. }
  228. }