Creating non-overlapping tiles without jumping through too many loops can be quite a challenge. In such cases, code examples from the Shadertoy community can be really inspiring to us Houdini artists. This original code written by a user called fizzer cleverly offsets a checkerboard and supports scaling as well as shifting the entire pattern.
A primitive wrangle creates tiles based on randomized world positions:
float s = chf('scale');
vector2 t = chu('offset');
vector2 uv = (set(v@P.x, v@P.z) + t) * s;
vector2 f = frac(uv);
vector2 c = floor(uv);
vector2 offset;
vector2 tile = c;
float r0 = rand(c);
int orient = (int(c.x) + int(c.y)) & 1;
tile[orient] += f[orient] > r0;
offset[orient] = f[orient] > r0 ? 1.0 : -1.0;
float r1 = rand(c + offset);
tile[orient ^ 1] += f[orient ^ 1] > r1;
u@tile = tile;
In uv
we offset and scale the position attribute. The result gets split up in fractional f
and integer c
. After declaring offset
and tile
we generate random values for r0
and r1
which will be compared with f
. For orient we check if the sum of the integer components of c
is even or odd using a bitwise &
operation with 1. Next, orient ^ 1
flips the value stored in orient based on whether the sum of the x and y coordinates in c was even or odd.
The primitives get split up based on the tile
attribute and a divide node removes inner edges, which leaves us with clean tiles that can be projected onto surfaces or used as a base for texturing.