FilmShader.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. name: 'FilmShader',
  22. uniforms: {
  23. 'tDiffuse': { value: null },
  24. 'time': { value: 0.0 },
  25. 'nIntensity': { value: 0.5 },
  26. 'sIntensity': { value: 0.05 },
  27. 'sCount': { value: 4096 },
  28. 'grayscale': { value: 1 }
  29. },
  30. vertexShader: /* glsl */`
  31. varying vec2 vUv;
  32. void main() {
  33. vUv = uv;
  34. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  35. }`,
  36. fragmentShader: /* glsl */`
  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. };
  69. export { FilmShader };