FocusShader.js 2.4 KB

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