FilmShader.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. * Film grain & scanlines shader
  5. *
  6. * - ported from HLSL to WebGL / GLSL
  7. * http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  8. *
  9. * Screen Space Static Postprocessor
  10. *
  11. * Produces an analogue noise overlay similar to a film grain / TV static
  12. *
  13. * Original implementation and noise algorithm
  14. * Pat 'Hawthorne' Shearon
  15. *
  16. * Optimized scanlines + noise version with intensity scaling
  17. * Georg 'Leviathan' Steinrohder
  18. *
  19. * This version is provided under a Creative Commons Attribution 3.0 License
  20. * http://creativecommons.org/licenses/by/3.0/
  21. */
  22. THREE.FilmShader = {
  23. uniforms: {
  24. "tDiffuse": { type: "t", value: null },
  25. "time": { type: "f", value: 0.0 },
  26. "nIntensity": { type: "f", value: 0.5 },
  27. "sIntensity": { type: "f", value: 0.05 },
  28. "sCount": { type: "f", value: 4096 },
  29. "grayscale": { type: "i", value: 1 }
  30. },
  31. vertexShader: [
  32. "varying vec2 vUv;",
  33. "void main() {",
  34. "vUv = uv;",
  35. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  36. "}"
  37. ].join("\n"),
  38. fragmentShader: [
  39. // control parameter
  40. "uniform float time;",
  41. "uniform bool grayscale;",
  42. // noise effect intensity value (0 = no effect, 1 = full effect)
  43. "uniform float nIntensity;",
  44. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  45. "uniform float sIntensity;",
  46. // scanlines effect count value (0 = no effect, 4096 = full effect)
  47. "uniform float sCount;",
  48. "uniform sampler2D tDiffuse;",
  49. "varying vec2 vUv;",
  50. "void main() {",
  51. // sample the source
  52. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  53. // make some noise
  54. "float x = vUv.x * vUv.y * time * 1000.0;",
  55. "x = mod( x, 13.0 ) * mod( x, 123.0 );",
  56. "float dx = mod( x, 0.01 );",
  57. // add noise
  58. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx * 100.0, 0.0, 1.0 );",
  59. // get us a sine and cosine
  60. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  61. // add scanlines
  62. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  63. // interpolate between source and result by intensity
  64. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  65. // convert to grayscale if desired
  66. "if( grayscale ) {",
  67. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  68. "}",
  69. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  70. "}"
  71. ].join("\n")
  72. };