FocusShader.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Focus shader
  3. * based on PaintEffect postprocess from ro.me
  4. * http://code.google.com/p/3-dreams-of-black/source/browse/deploy/js/effects/PaintEffect.js
  5. */
  6. const FocusShader = {
  7. uniforms: {
  8. 'tDiffuse': { value: null },
  9. 'screenWidth': { value: 1024 },
  10. 'screenHeight': { value: 1024 },
  11. 'sampleDistance': { value: 0.94 },
  12. 'waveFactor': { value: 0.00125 }
  13. },
  14. vertexShader: /* glsl */`
  15. varying vec2 vUv;
  16. void main() {
  17. vUv = uv;
  18. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  19. }`,
  20. fragmentShader: /* glsl */`
  21. uniform float screenWidth;
  22. uniform float screenHeight;
  23. uniform float sampleDistance;
  24. uniform float waveFactor;
  25. uniform sampler2D tDiffuse;
  26. varying vec2 vUv;
  27. void main() {
  28. vec4 color, org, tmp, add;
  29. float sample_dist, f;
  30. vec2 vin;
  31. vec2 uv = vUv;
  32. add = color = org = texture2D( tDiffuse, uv );
  33. vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );
  34. sample_dist = dot( vin, vin ) * 2.0;
  35. f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;
  36. vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2( f );
  37. add += tmp = texture2D( tDiffuse, uv + vec2( 0.111964, 0.993712 ) * sampleSize );
  38. if( tmp.b < color.b ) color = tmp;
  39. add += tmp = texture2D( tDiffuse, uv + vec2( 0.846724, 0.532032 ) * sampleSize );
  40. if( tmp.b < color.b ) color = tmp;
  41. add += tmp = texture2D( tDiffuse, uv + vec2( 0.943883, -0.330279 ) * sampleSize );
  42. if( tmp.b < color.b ) color = tmp;
  43. add += tmp = texture2D( tDiffuse, uv + vec2( 0.330279, -0.943883 ) * sampleSize );
  44. if( tmp.b < color.b ) color = tmp;
  45. add += tmp = texture2D( tDiffuse, uv + vec2( -0.532032, -0.846724 ) * sampleSize );
  46. if( tmp.b < color.b ) color = tmp;
  47. add += tmp = texture2D( tDiffuse, uv + vec2( -0.993712, -0.111964 ) * sampleSize );
  48. if( tmp.b < color.b ) color = tmp;
  49. add += tmp = texture2D( tDiffuse, uv + vec2( -0.707107, 0.707107 ) * sampleSize );
  50. if( tmp.b < color.b ) color = tmp;
  51. color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );
  52. color = color + ( add / vec4( 8.0 ) - color ) * ( vec4( 1.0 ) - vec4( sample_dist * 0.5 ) );
  53. gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );
  54. }`
  55. };
  56. export { FocusShader };