DOFMipMapShader.js 950 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. var DOFMipMapShader = {
  7. uniforms: {
  8. 'tColor': { value: null },
  9. 'tDepth': { value: null },
  10. 'focus': { value: 1.0 },
  11. 'maxblur': { value: 1.0 }
  12. },
  13. vertexShader: [
  14. 'varying vec2 vUv;',
  15. 'void main() {',
  16. ' vUv = uv;',
  17. ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
  18. '}'
  19. ].join( '\n' ),
  20. fragmentShader: [
  21. 'uniform float focus;',
  22. 'uniform float maxblur;',
  23. 'uniform sampler2D tColor;',
  24. 'uniform sampler2D tDepth;',
  25. 'varying vec2 vUv;',
  26. 'void main() {',
  27. ' vec4 depth = texture2D( tDepth, vUv );',
  28. ' float factor = depth.x - focus;',
  29. ' vec4 col = texture2D( tColor, vUv, 2.0 * maxblur * abs( focus - depth.x ) );',
  30. ' gl_FragColor = col;',
  31. ' gl_FragColor.a = 1.0;',
  32. '}'
  33. ].join( '\n' )
  34. };
  35. export { DOFMipMapShader };