FilmShader.js 2.4 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": { value: null },
  25. "time": { value: 0.0 },
  26. "nIntensity": { value: 0.5 },
  27. "sIntensity": { value: 0.05 },
  28. "sCount": { value: 4096 },
  29. "grayscale": { 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. "#include <common>",
  40. // control parameter
  41. "uniform float time;",
  42. "uniform bool grayscale;",
  43. // noise effect intensity value (0 = no effect, 1 = full effect)
  44. "uniform float nIntensity;",
  45. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  46. "uniform float sIntensity;",
  47. // scanlines effect count value (0 = no effect, 4096 = full effect)
  48. "uniform float sCount;",
  49. "uniform sampler2D tDiffuse;",
  50. "varying vec2 vUv;",
  51. "void main() {",
  52. // sample the source
  53. "vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  54. // make some noise
  55. "float dx = rand( vUv + time );",
  56. // add noise
  57. "vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx, 0.0, 1.0 );",
  58. // get us a sine and cosine
  59. "vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );",
  60. // add scanlines
  61. "cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;",
  62. // interpolate between source and result by intensity
  63. "cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );",
  64. // convert to grayscale if desired
  65. "if( grayscale ) {",
  66. "cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );",
  67. "}",
  68. "gl_FragColor = vec4( cResult, cTextureScreen.a );",
  69. "}"
  70. ].join( "\n" )
  71. };