DOFMipMapShader.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @module DOFMipMapShader
  3. * @three_import import { DOFMipMapShader } from 'three/addons/shaders/DOFMipMapShader.js';
  4. */
  5. /**
  6. * Depth-of-field shader using mipmaps from Matt Handley @applmak.
  7. *
  8. * Requires power-of-2 sized render target with enabled mipmaps.
  9. *
  10. * @constant
  11. * @type {ShaderMaterial~Shader}
  12. */
  13. const DOFMipMapShader = {
  14. name: 'DOFMipMapShader',
  15. uniforms: {
  16. 'tColor': { value: null },
  17. 'tDepth': { value: null },
  18. 'focus': { value: 1.0 },
  19. 'maxblur': { value: 1.0 }
  20. },
  21. vertexShader: /* glsl */`
  22. varying vec2 vUv;
  23. void main() {
  24. vUv = uv;
  25. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  26. }`,
  27. fragmentShader: /* glsl */`
  28. uniform float focus;
  29. uniform float maxblur;
  30. uniform sampler2D tColor;
  31. uniform sampler2D tDepth;
  32. varying vec2 vUv;
  33. void main() {
  34. vec4 depth = texture2D( tDepth, vUv );
  35. float factor = depth.x - focus;
  36. vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );
  37. gl_FragColor = col;
  38. gl_FragColor.a = 1.0;
  39. }`
  40. };
  41. export { DOFMipMapShader };