I'm trying to achieve this pattern in GLSL:
While getting that effect on the X axis is straightforward by scaling, using floor()
and mix()
:
void main() {
vec2 st = vTexCoord;
float maxColumns = 100.0;
float columns = floor(mouse.x * maxColumns);
// scale the coordinate system to the columns
st = st * (columns + 1.0);
float x = floor(st.x) / columns;
vec3 color = mix(colorA, colorB, min(1.0, x));
gl_FragColor = vec4(color,1.0);
}
However, achieving rows is not so simple. I tried using the following approach with arrays of colors and dynamic indices:
void main() {
vec2 st = vTexCoord;
float maxColumns = 100.0;
float columns = floor(mouse.x * maxColumns);
st = st * (columns + 1.0);
float x = floor(st.x) / columns;
float maxRows = 2.0;
float row = mouse.y * maxRows;
int y = int(floor(row));
vec3 color = mix(colorsA[y], colorsB[y], min(1.0, x));
gl_FragColor = vec4(color,1.0);
}
After seeing this error, it looks like I'm taking the wrong approach:
Darn! An error occurred compiling the fragment shader:ERROR: 0:46: '[]' : Index expression must be constant
ERROR: 0:46: '[]' : Index expression must be constant
What would be a correct approach to a 2D color grid in a GLSL fragment shader?