FilmShader.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. console.warn( "THREE.FilmShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author alteredq / http://alteredqualia.com/
  4. *
  5. * Film grain & scanlines shader
  6. *
  7. * - ported from HLSL to WebGL / GLSL
  8. * http://www.truevision3d.com/forums/showcase/staticnoise_colorblackwhite_scanline_shaders-t18698.0.html
  9. *
  10. * Screen Space Static Postprocessor
  11. *
  12. * Produces an analogue noise overlay similar to a film grain / TV static
  13. *
  14. * Original implementation and noise algorithm
  15. * Pat 'Hawthorne' Shearon
  16. *
  17. * Optimized scanlines + noise version with intensity scaling
  18. * Georg 'Leviathan' Steinrohder
  19. *
  20. * This version is provided under a Creative Commons Attribution 3.0 License
  21. * http://creativecommons.org/licenses/by/3.0/
  22. */
  23. THREE.FilmShader = {
  24. uniforms: {
  25. "tDiffuse": { value: null },
  26. "time": { value: 0.0 },
  27. "nIntensity": { value: 0.5 },
  28. "sIntensity": { value: 0.05 },
  29. "sCount": { value: 4096 },
  30. "grayscale": { value: 1 }
  31. },
  32. vertexShader: [
  33. "varying vec2 vUv;",
  34. "void main() {",
  35. " vUv = uv;",
  36. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  37. "}"
  38. ].join( "\n" ),
  39. fragmentShader: [
  40. "#include <common>",
  41. // control parameter
  42. "uniform float time;",
  43. "uniform bool grayscale;",
  44. // noise effect intensity value (0 = no effect, 1 = full effect)
  45. "uniform float nIntensity;",
  46. // scanlines effect intensity value (0 = no effect, 1 = full effect)
  47. "uniform float sIntensity;",
  48. // scanlines effect count value (0 = no effect, 4096 = full effect)
  49. "uniform float sCount;",
  50. "uniform sampler2D tDiffuse;",
  51. "varying vec2 vUv;",
  52. "void main() {",
  53. // sample the source
  54. " vec4 cTextureScreen = texture2D( tDiffuse, vUv );",
  55. // make some noise
  56. " float dx = rand( vUv + time );",
  57. // add noise
  58. " vec3 cResult = cTextureScreen.rgb + cTextureScreen.rgb * clamp( 0.1 + dx, 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. };