KaleidoShader.js 1.0 KB

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