babylon.circleOfConfusionPostProcess.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module BABYLON {
  2. /**
  3. * The CircleOfConfusionPostProcess computes the circle of confusion value for each pixel given required lens parameters. See https://en.wikipedia.org/wiki/Circle_of_confusion
  4. */
  5. export class CircleOfConfusionPostProcess extends PostProcess {
  6. /**
  7. * Max lens size in scene units/1000 (eg. millimeter). Standard cameras are 50mm. (default: 50) The diamater of the resulting aperture can be computed by lensSize/fStop.
  8. */
  9. lensSize = 50
  10. /**
  11. * F-Stop of the effect's camera. The diamater of the resulting aperture can be computed by lensSize/fStop. (default: 1.4)
  12. */
  13. fStop = 1.4;
  14. /**
  15. * Distance away from the camera to focus on in scene units/1000 (eg. millimeter). (default: 2000)
  16. */
  17. focusDistance = 2000;
  18. /**
  19. * Focal length of the effect's camera in scene units/1000 (eg. millimeter). (default: 50)
  20. */
  21. focalLength = 50;
  22. /**
  23. * Creates a new instance of @see CircleOfConfusionPostProcess
  24. * @param name The name of the effect.
  25. * @param depthTexture The depth texture of the scene to compute the circle of confusion.
  26. * @param options The required width/height ratio to downsize to before computing the render pass.
  27. * @param camera The camera to apply the render pass to.
  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. */
  33. constructor(name: string, depthTexture: RenderTargetTexture, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT) {
  34. super(name, "circleOfConfusion", ["cameraMinMaxZ", "focusDistance", "cocPrecalculation"], ["depthSampler"], options, camera, samplingMode, engine, reusable, null, textureType);
  35. this.onApplyObservable.add((effect: Effect) => {
  36. effect.setTexture("depthSampler", depthTexture);
  37. // Circle of confusion calculation, See https://developer.nvidia.com/gpugems/GPUGems/gpugems_ch23.html
  38. var aperture = this.lensSize/this.fStop;
  39. var cocPrecalculation = ((aperture * this.focalLength)/((this.focusDistance - this.focalLength)));// * ((this.focusDistance - pixelDistance)/pixelDistance) [This part is done in shader]
  40. effect.setFloat('focusDistance', this.focusDistance);
  41. effect.setFloat('cocPrecalculation', cocPrecalculation);
  42. effect.setFloat2('cameraMinMaxZ', depthTexture.activeCamera!.minZ, depthTexture.activeCamera!.maxZ);
  43. })
  44. }
  45. }
  46. }