DotScreenShader.js 1.1 KB

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