home/ atoms/ wgsl-grid-coordinate-transform

Transforming clip-space vertices into grid cells requires scaling by 1/N, translating by -1, then adding a per-instance cell offset scaled by 2/N

To place a unit quad in cell (cx, cy) of an N×N grid using only clip-space math: (1) shift vertices +1 to move from [-1,1] to [0,2]; (2) divide by grid size N to scale to one cell width; (3) subtract 1 to move the origin to clip-space corner (-1,-1); (4) add cell / grid * 2 as the per-instance cell offset — the factor of 2 accounts for the full 2-unit clip-space width. The grid size enters via a uniform vec2f and WGSL applies component-wise vector arithmetic automatically.

Examples

let cell = vec2f(i % grid.x, floor(i / grid.x));
let cellOffset = cell / grid * 2;
let gridPos = (pos + 1) / grid - 1 + cellOffset;
return vec4f(gridPos, 0, 1);

Assessment

Trace vertex (-0.8, -0.8) through this transform for a 4×4 grid with cell = (1, 1). What clip-space coordinate results? Why does the offset use * 2 rather than * 1?

“canvas coordinates go from -1 to +1, it's _actually 2 units across_. That means”
corpus · your-first-webgpu-app-google-codelab · chunk 11