DotScreenShader.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {
  2. Vector2
  3. } from 'three';
  4. /** @module DotScreenShader */
  5. /**
  6. * Dot screen shader based on [glfx.js sepia shader]{@link https://github.com/evanw/glfx.js}.
  7. *
  8. * @constant
  9. * @type {Object}
  10. */
  11. const DotScreenShader = {
  12. name: 'DotScreenShader',
  13. uniforms: {
  14. 'tDiffuse': { value: null },
  15. 'tSize': { value: new Vector2( 256, 256 ) },
  16. 'center': { value: new Vector2( 0.5, 0.5 ) },
  17. 'angle': { value: 1.57 },
  18. 'scale': { value: 1.0 }
  19. },
  20. vertexShader: /* glsl */`
  21. varying vec2 vUv;
  22. void main() {
  23. vUv = uv;
  24. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  25. }`,
  26. fragmentShader: /* glsl */`
  27. uniform vec2 center;
  28. uniform float angle;
  29. uniform float scale;
  30. uniform vec2 tSize;
  31. uniform sampler2D tDiffuse;
  32. varying vec2 vUv;
  33. float pattern() {
  34. float s = sin( angle ), c = cos( angle );
  35. vec2 tex = vUv * tSize - center;
  36. vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;
  37. return ( sin( point.x ) * sin( point.y ) ) * 4.0;
  38. }
  39. void main() {
  40. vec4 color = texture2D( tDiffuse, vUv );
  41. float average = ( color.r + color.g + color.b ) / 3.0;
  42. gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );
  43. }`
  44. };
  45. export { DotScreenShader };
粤ICP备19079148号