depthOfFieldMergePostProcess.ts 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Nullable } from "types";
  2. import { Camera } from "Cameras";
  3. import { Effect } from "Materials";
  4. import { PostProcess, PostProcessOptions } from "PostProcess";
  5. import { Engine } from "Engine";
  6. /**
  7. * Options to be set when merging outputs from the default pipeline.
  8. */
  9. export class DepthOfFieldMergePostProcessOptions {
  10. /**
  11. * The original image to merge on top of
  12. */
  13. public originalFromInput: PostProcess;
  14. /**
  15. * Parameters to perform the merge of the depth of field effect
  16. */
  17. public depthOfField?: {
  18. circleOfConfusion: PostProcess;
  19. blurSteps: Array<PostProcess>;
  20. };
  21. /**
  22. * Parameters to perform the merge of bloom effect
  23. */
  24. public bloom?: {
  25. blurred: PostProcess;
  26. weight: number;
  27. };
  28. }
  29. /**
  30. * The DepthOfFieldMergePostProcess merges blurred images with the original based on the values of the circle of confusion.
  31. */
  32. export class DepthOfFieldMergePostProcess extends PostProcess {
  33. /**
  34. * Creates a new instance of DepthOfFieldMergePostProcess
  35. * @param name The name of the effect.
  36. * @param originalFromInput Post process which's input will be used for the merge.
  37. * @param circleOfConfusion Circle of confusion post process which's output will be used to blur each pixel.
  38. * @param blurSteps Blur post processes from low to high which will be mixed with the original image.
  39. * @param options The required width/height ratio to downsize to before computing the render pass.
  40. * @param camera The camera to apply the render pass to.
  41. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  42. * @param engine The engine which the post process will be applied. (default: current engine)
  43. * @param reusable If the post process can be reused on the same frame. (default: false)
  44. * @param textureType Type of textures used when performing the post process. (default: 0)
  45. * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
  46. */
  47. constructor(name: string, originalFromInput: PostProcess, circleOfConfusion: PostProcess, private blurSteps: Array<PostProcess>, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
  48. super(name, "depthOfFieldMerge", [], ["circleOfConfusionSampler", "blurStep0", "blurStep1", "blurStep2"], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, true);
  49. this.onApplyObservable.add((effect: Effect) => {
  50. effect.setTextureFromPostProcess("textureSampler", originalFromInput);
  51. effect.setTextureFromPostProcessOutput("circleOfConfusionSampler", circleOfConfusion);
  52. blurSteps.forEach((step, index) => {
  53. effect.setTextureFromPostProcessOutput("blurStep" + (blurSteps.length - index - 1), step);
  54. });
  55. });
  56. if (!blockCompilation) {
  57. this.updateEffect();
  58. }
  59. }
  60. /**
  61. * Updates the effect with the current post process compile time values and recompiles the shader.
  62. * @param defines Define statements that should be added at the beginning of the shader. (default: null)
  63. * @param uniforms Set of uniform variables that will be passed to the shader. (default: null)
  64. * @param samplers Set of Texture2D variables that will be passed to the shader. (default: null)
  65. * @param indexParameters The index parameters to be used for babylons include syntax "#include<kernelBlurVaryingDeclaration>[0..varyingCount]". (default: undefined) See usage in babylon.blurPostProcess.ts and kernelBlur.vertex.fx
  66. * @param onCompiled Called when the shader has been compiled.
  67. * @param onError Called if there is an error when compiling a shader.
  68. */
  69. public updateEffect(defines: Nullable<string> = null, uniforms: Nullable<string[]> = null, samplers: Nullable<string[]> = null, indexParameters?: any,
  70. onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void) {
  71. if (!defines) {
  72. defines = "";
  73. defines += "#define BLUR_LEVEL " + (this.blurSteps.length - 1) + "\n";
  74. }
  75. super.updateEffect(defines, uniforms, samplers, indexParameters, onCompiled, onError);
  76. }
  77. }