KaleidoShader.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @module KaleidoShader
  3. * @three_import import { KaleidoShader } from 'three/addons/shaders/KaleidoShader.js';
  4. */
  5. /**
  6. * Kaleidoscope Shader.
  7. * Radial reflection around center point
  8. * Ported from: {@link http://pixelshaders.com/editor/}
  9. * by [Toby Schachman]{@link http://tobyschachman.com/}
  10. *
  11. * sides: number of reflections
  12. * angle: initial angle in radians
  13. *
  14. * @constant
  15. * @type {ShaderMaterial~Shader}
  16. */
  17. const KaleidoShader = {
  18. name: 'KaleidoShader',
  19. uniforms: {
  20. 'tDiffuse': { value: null },
  21. 'sides': { value: 6.0 },
  22. 'angle': { value: 0.0 }
  23. },
  24. vertexShader: /* glsl */`
  25. varying vec2 vUv;
  26. void main() {
  27. vUv = uv;
  28. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  29. }`,
  30. fragmentShader: /* glsl */`
  31. uniform sampler2D tDiffuse;
  32. uniform float sides;
  33. uniform float angle;
  34. varying vec2 vUv;
  35. void main() {
  36. vec2 p = vUv - 0.5;
  37. float r = length(p);
  38. float a = atan(p.y, p.x) + angle;
  39. float tau = 2. * 3.1416 ;
  40. a = mod(a, tau/sides);
  41. a = abs(a - tau/sides/2.) ;
  42. p = r * vec2(cos(a), sin(a));
  43. vec4 color = texture2D(tDiffuse, p + 0.5);
  44. gl_FragColor = color;
  45. }`
  46. };
  47. export { KaleidoShader };