DOFMipMapShader.js 891 B

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