depthOfFieldBlurPostProcess.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Nullable } from "../types";
  2. import { Vector2 } from "../Maths/math.vector";
  3. import { Camera } from "../Cameras/camera";
  4. import { Effect } from "../Materials/effect";
  5. import { Texture } from "../Materials/Textures/texture";
  6. import { PostProcess, PostProcessOptions } from "./postProcess";
  7. import { BlurPostProcess } from "./blurPostProcess";
  8. import { Engine } from "../Engines/engine";
  9. import { Scene } from "../scene";
  10. import { Constants } from "../Engines/constants";
  11. /**
  12. * The DepthOfFieldBlurPostProcess applied a blur in a give direction.
  13. * This blur differs from the standard BlurPostProcess as it attempts to avoid blurring pixels
  14. * based on samples that have a large difference in distance than the center pixel.
  15. * See section 2.6.2 http://fileadmin.cs.lth.se/cs/education/edan35/lectures/12dof.pdf
  16. */
  17. export class DepthOfFieldBlurPostProcess extends BlurPostProcess {
  18. /**
  19. * Creates a new instance CircleOfConfusionPostProcess
  20. * @param name The name of the effect.
  21. * @param scene The scene the effect belongs to.
  22. * @param direction The direction the blur should be applied.
  23. * @param kernel The size of the kernel used to blur.
  24. * @param options The required width/height ratio to downsize to before computing the render pass.
  25. * @param camera The camera to apply the render pass to.
  26. * @param circleOfConfusion The circle of confusion + depth map to be used to avoid blurring accross edges
  27. * @param imageToBlur The image to apply the blur to (default: Current rendered frame)
  28. * @param samplingMode The sampling mode to be used when computing the pass. (default: 0)
  29. * @param engine The engine which the post process will be applied. (default: current engine)
  30. * @param reusable If the post process can be reused on the same frame. (default: false)
  31. * @param textureType Type of textures used when performing the post process. (default: 0)
  32. * @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)
  33. */
  34. constructor(name: string, scene: Scene, public direction: Vector2, kernel: number, options: number | PostProcessOptions, camera: Nullable<Camera>, circleOfConfusion: PostProcess, imageToBlur: Nullable<PostProcess> = null, samplingMode: number = Texture.BILINEAR_SAMPLINGMODE, engine?: Engine, reusable?: boolean, textureType: number = Constants.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
  35. super(name, direction, kernel, options, camera, samplingMode = Constants.TEXTURE_BILINEAR_SAMPLINGMODE, engine, reusable, textureType = Constants.TEXTURETYPE_UNSIGNED_INT, `#define DOF 1\r\n`, blockCompilation);
  36. this.onApplyObservable.add((effect: Effect) => {
  37. if (imageToBlur != null) {
  38. effect.setTextureFromPostProcess("textureSampler", imageToBlur);
  39. }
  40. effect.setTextureFromPostProcessOutput("circleOfConfusionSampler", circleOfConfusion);
  41. if (scene.activeCamera) {
  42. effect.setFloat2('cameraMinMaxZ', scene.activeCamera.minZ, scene.activeCamera.maxZ);
  43. }
  44. });
  45. }
  46. }