DotScreenShader.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. name: 'DotScreenShader',
  11. uniforms: {
  12. 'tDiffuse': { value: null },
  13. 'tSize': { value: new Vector2( 256, 256 ) },
  14. 'center': { value: new Vector2( 0.5, 0.5 ) },
  15. 'angle': { value: 1.57 },
  16. 'scale': { value: 1.0 }
  17. },
  18. vertexShader: /* glsl */`
  19. varying vec2 vUv;
  20. void main() {
  21. vUv = uv;
  22. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  23. }`,
  24. fragmentShader: /* glsl */`
  25. uniform vec2 center;
  26. uniform float angle;
  27. uniform float scale;
  28. uniform vec2 tSize;
  29. uniform sampler2D tDiffuse;
  30. varying vec2 vUv;
  31. float pattern() {
  32. float s = sin( angle ), c = cos( angle );
  33. vec2 tex = vUv * tSize - center;
  34. vec2 point = vec2( c * tex.x - s * tex.y, s * tex.x + c * tex.y ) * scale;
  35. return ( sin( point.x ) * sin( point.y ) ) * 4.0;
  36. }
  37. void main() {
  38. vec4 color = texture2D( tDiffuse, vUv );
  39. float average = ( color.r + color.g + color.b ) / 3.0;
  40. gl_FragColor = vec4( vec3( average * 10.0 - 5.0 + pattern() ), color.a );
  41. }`
  42. };
  43. export { DotScreenShader };