HorizontalTiltShiftShader.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ( function () {
  2. /**
  3. * Simple fake tilt-shift effect, modulating two pass Gaussian blur (see above) by vertical position
  4. *
  5. * - 9 samples per pass
  6. * - standard deviation 2.7
  7. * - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  8. * - "r" parameter control where "focused" horizontal line lies
  9. */
  10. var HorizontalTiltShiftShader = {
  11. uniforms: {
  12. 'tDiffuse': {
  13. value: null
  14. },
  15. 'h': {
  16. value: 1.0 / 512.0
  17. },
  18. 'r': {
  19. value: 0.35
  20. }
  21. },
  22. vertexShader:
  23. /* glsl */
  24. `
  25. varying vec2 vUv;
  26. void main() {
  27. vUv = uv;
  28. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  29. }`,
  30. fragmentShader:
  31. /* glsl */
  32. `
  33. uniform sampler2D tDiffuse;
  34. uniform float h;
  35. uniform float r;
  36. varying vec2 vUv;
  37. void main() {
  38. vec4 sum = vec4( 0.0 );
  39. float hh = h * abs( r - vUv.y );
  40. sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * hh, vUv.y ) ) * 0.051;
  41. sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * hh, vUv.y ) ) * 0.0918;
  42. sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * hh, vUv.y ) ) * 0.12245;
  43. sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * hh, vUv.y ) ) * 0.1531;
  44. sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;
  45. sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * hh, vUv.y ) ) * 0.1531;
  46. sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * hh, vUv.y ) ) * 0.12245;
  47. sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * hh, vUv.y ) ) * 0.0918;
  48. sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * hh, vUv.y ) ) * 0.051;
  49. gl_FragColor = sum;
  50. }`
  51. };
  52. THREE.HorizontalTiltShiftShader = HorizontalTiltShiftShader;
  53. } )();