FilmShader.js 2.3 KB

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