FilmShader.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Film grain & scanlines shader
  3. *
  4. * - ported from HLSL to WebGL / GLSL
  5. * https://web.archive.org/web/20210226214859/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. const 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: /* glsl */`
  30. varying vec2 vUv;
  31. void main() {
  32. vUv = uv;
  33. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  34. }`,
  35. fragmentShader: /* glsl */`
  36. #include <common>
  37. // control parameter
  38. uniform float time;
  39. uniform bool grayscale;
  40. // noise effect intensity value (0 = no effect, 1 = full effect)
  41. uniform float nIntensity;
  42. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  43. uniform float sIntensity;
  44. // scanlines effect count value (0 = no effect, 4096 = full effect)
  45. uniform float sCount;
  46. uniform sampler2D tDiffuse;
  47. varying vec2 vUv;
  48. void main() {
  49. // sample the source
  50. vec4 cTextureScreen = texture2D( tDiffuse, vUv );
  51. // make some noise
  52. float dx = rand( vUv + time );
  53. // add noise
  54. vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx, 0.0, 1.0 );
  55. // get us a sine and cosine
  56. vec2 sc = vec2( sin( vUv.y * sCount ), cos( vUv.y * sCount ) );
  57. // add scanlines
  58. cResult += cTextureScreen.rgb * vec3( sc.x, sc.y, sc.x ) * sIntensity;
  59. // interpolate between source and result by intensity
  60. cResult = cTextureScreen.rgb + clamp( nIntensity, 0.0,1.0 ) * ( cResult - cTextureScreen.rgb );
  61. // convert to grayscale if desired
  62. if( grayscale ) {
  63. cResult = vec3( cResult.r * 0.3 + cResult.g * 0.59 + cResult.b * 0.11 );
  64. }
  65. gl_FragColor = vec4( cResult, cTextureScreen.a );
  66. }`,
  67. };
  68. export { FilmShader };