A real-time kaleidoscope fragment shader never draws mirrors — it remaps where each output pixel samples from. For every pixel this simulation converts screen position to polar coordinates around the centre, r and θ, then folds θ into a single wedge of angle 2π / N using a triangle-wave reflection:
wedge = 2π / N
a = θ mod wedge
if a > wedge/2: a = wedge − a (reflect back)
That folded angle a always lands in [0, wedge/2] — mathematically the fundamental domain of the dihedral group DN, the same symmetry group a real N-mirror kaleidoscope tube produces. The pixel then samples a small procedural source pattern at (r·cos(a+φ), r·sin(a+φ)), wrapping the coordinates so the source tiles seamlessly — exactly the texture-wrap trick a GLSL shader uses to make a bounded texture look infinite. Because the whole remap runs once per pixel with no drawn geometry at all, this is the coordinate-remapping technique itself, computed here on the CPU into an ImageData buffer instead of on the GPU.
- Mirror segments (N) — how many wedges tile the full circle; higher N gives finer, lace-like symmetry.
- Rotation speed — spins the fold's zero-angle reference, sweeping the whole pattern around the centre.
- Zoom — scales r before sampling, zooming into or out of the source pattern.
- Drift speed — slowly pans the sample origin across the source texture, so the visible motif keeps changing even at a fixed zoom.
- New Source Pattern — reseeds the procedural blotches the fold samples from, without touching the fold math.