grid2 = glsl`
/*
Here is a GLSL function that calculates MOD accurately with (float) parameters that should be integers:
*/
// https://stackoverflow.com/questions/33908644/get-accurate-integer-modulo-in-webgl-shader
float modI(float a,float b) {
float m=a-floor((a+0.5)/b)*b;
return floor(m+0.5);
}
float reposition(float ix, float i, float base, float flip_this, inout float flip_other) {
// FML floating point numbers... is why I need 0.5 here. Sometimes it won't be high enough.
float rank_in_layer = floor((ix + .5) / (pow(base, i)));
if (rank_in_layer == 0.) {return 0.;}
float position_on_axis = floor(i / 2.);
float position = modI(rank_in_layer, base);
float width_of_each = pow(base + u_separation, position_on_axis);
if (flip_this == -1.) {
position = base - position - 1.;
}
if (u_boust == 1. && modI(position, 2.) == 1.) {
flip_other *= -1.;
}
return position * width_of_each;
}
vec2 grid2(float ix, float max_ix) {
float x = 0.;
float y = 0.;
float flip_y = 1.;
float flip_x = 1.;
for (float i = 17.0; i >= 0.; i -= 1.) {
if (modI(i, 2.) == 1.) {
x += reposition(ix, i, u_base, flip_y, flip_x);
} else {
y += reposition(ix, i, u_base, flip_x, flip_y);
}
}
vec2 xy = vec2(x, y) / sqrt(max_ix) / 3.4;
return positioner(xy) * vec2(1., -1.);
}
`