depthOfFieldMergePostProcess.ts 4.8 KB

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