FocusShader.js 2.3 KB

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