GLSL Learning

GLSL shader example

Rotating Tiles

Rotate coordinates with a 2D rotation matrix, then repeat a square across the screen with fract().

Live example

Edit the code and the preview updates as you type.

How it works

rotate2d(angle) builds a 2x2 matrix from cos and sin. Multiplying a point by it turns the point around the origin by that angle. Instead of rotating a shape, the shader rotates the coordinates, and every shape drawn afterwards appears rotated.

fract(p * 3.0) - 0.5 is the repeat trick. fract keeps only the decimal part, so the coordinates count from 0 to 1, then start over, three times per unit. Subtracting 0.5 puts the origin in the middle of each cell, so whatever you draw around (0, 0) shows up in every cell.

The square itself is max(abs(x), abs(y)): the distance to the nearest edge of a square, the same way length() is the distance for a circle. Rotating local a second time spins each tile on its own center.

Try changing it

ChangeResult
p * 3.0 to p * 6.0Twice as many, smaller tiles
rotate2d(-u_time) to rotate2d(0.785)Tiles fixed at 45 degrees: a diamond pattern
max(abs(local.x), abs(local.y)) to length(local)Circles instead of squares
Delete the first rotation lineThe grid stops turning, only the tiles spin

Built-in functions used

Learn it step by step