2
0

FilmShader.js 2.6 KB

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