babylon.depthOfFieldEffect.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module BABYLON {
  2. /**
  3. * The depth of field effect applies a blur to objects that are closer or further from where the camera is focusing.
  4. */
  5. export class DepthOfFieldEffect extends PostProcessRenderEffect{
  6. private _depthOfFieldPass: PassPostProcess;
  7. private _circleOfConfusion: CircleOfConfusionPostProcess;
  8. private _depthOfFieldBlurX: DepthOfFieldBlurPostProcess;
  9. private _depthOfFieldBlurY: DepthOfFieldBlurPostProcess;
  10. private _depthOfFieldMerge: DepthOfFieldMergePostProcess;
  11. /**
  12. * The size of the kernel to be used for the blur
  13. */
  14. public set kernelSize(value: number){
  15. this._depthOfFieldBlurX.kernel = value;
  16. this._depthOfFieldBlurY.kernel = value;
  17. }
  18. public get kernelSize(){
  19. return this._depthOfFieldBlurX.kernel;
  20. }
  21. /**
  22. * The focal the length of the camera used in the effect
  23. */
  24. public set focalLength(value: number){
  25. this._circleOfConfusion.focalLength = value;
  26. }
  27. public get focalLength(){
  28. return this._circleOfConfusion.focalLength;
  29. }
  30. /**
  31. * F-Stop of the effect's camera. The diamater of the resulting aperture can be computed by lensSize/fStop. (default: 1.4)
  32. */
  33. public set fStop(value: number){
  34. this._circleOfConfusion.fStop = value;
  35. }
  36. public get fStop(){
  37. return this._circleOfConfusion.fStop;
  38. }
  39. /**
  40. * Distance away from the camera to focus on in scene units/1000 (eg. millimeter). (default: 2000)
  41. */
  42. public set focusDistance(value: number){
  43. this._circleOfConfusion.focusDistance = value;
  44. }
  45. public get focusDistance(){
  46. return this._circleOfConfusion.focusDistance;
  47. }
  48. /**
  49. * 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.
  50. */
  51. public set lensSize(value: number){
  52. this._circleOfConfusion.lensSize = value;
  53. }
  54. public get lensSize(){
  55. return this._circleOfConfusion.lensSize;
  56. }
  57. /**
  58. * Creates a new instance of @see DepthOfFieldEffect
  59. * @param scene The scene the effect belongs to.
  60. * @param pipelineTextureType The type of texture to be used when performing the post processing.
  61. */
  62. constructor(scene: Scene, pipelineTextureType = 0) {
  63. super(scene.getEngine(), "depth of field", ()=>{return [this._circleOfConfusion, this._depthOfFieldPass, this._depthOfFieldBlurY, this._depthOfFieldBlurX, this._depthOfFieldMerge]}, true);
  64. // Enable and get current depth map
  65. var depthMap = scene.enableDepthRenderer().getDepthMap();
  66. // Circle of confusion value for each pixel is used to determine how much to blur that pixel
  67. this._circleOfConfusion = new BABYLON.CircleOfConfusionPostProcess("circleOfConfusion", scene, depthMap, 1, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType);
  68. // Capture circle of confusion texture
  69. this._depthOfFieldPass = new PassPostProcess("depthOfFieldPass", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType);
  70. // Blur the image but do not blur on sharp far to near distance changes to avoid bleeding artifacts
  71. // See section 2.6.2 http://fileadmin.cs.lth.se/cs/education/edan35/lectures/12dof.pdf
  72. this._depthOfFieldBlurY = new DepthOfFieldBlurPostProcess("verticle blur", scene, new Vector2(0, 1.0), 15, 1.0, null, depthMap, this._circleOfConfusion, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType);
  73. this._depthOfFieldBlurX = new DepthOfFieldBlurPostProcess("horizontal blur", scene, new Vector2(1.0, 0), 15, 1.0, null, depthMap, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType);
  74. // Merge blurred images with original image based on circleOfConfusion
  75. this._depthOfFieldMerge = new DepthOfFieldMergePostProcess("depthOfFieldMerge", this._circleOfConfusion, this._depthOfFieldPass, 1, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType);
  76. }
  77. /**
  78. * Disposes each of the internal effects for a given camera.
  79. * @param camera The camera to dispose the effect on.
  80. */
  81. public disposeEffects(camera:Camera){
  82. this._depthOfFieldPass.dispose(camera);
  83. this._circleOfConfusion.dispose(camera);
  84. this._depthOfFieldBlurX.dispose(camera);
  85. this._depthOfFieldBlurY.dispose(camera);
  86. this._depthOfFieldMerge.dispose(camera);
  87. }
  88. }
  89. }