Skip to content

Instantly share code, notes, and snippets.

@gerard-geer
Created July 24, 2015 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerard-geer/68ac4a7a83ab133cddc6 to your computer and use it in GitHub Desktop.
Save gerard-geer/68ac4a7a83ab133cddc6 to your computer and use it in GitHub Desktop.
A rudimentary approach to emulating non-uniform, type-agnostic arrays in GLSL.
// By Gerard Geer, under MIT license
// A 2,4,8 or 16 element array implemented as a binary tree, #defined for type agnosticity.
#define ARR2(x, a,b) (x<1) ? a : b
#define ARR4(x, a,b,c,d) (x<2) ? ARR2(x,a,b) : ARR2(x-2,c,d)
#define ARR8(x, a,b,c,d, e,f,g,h) (x<4) ? ARR4(x, a,b,c,d) : ARR4(x-4, e,f,g,h)
#define ARR16(x, a,b,c,d, e,f,g,h, i,j,k,l, m,n,o,p) (x<8) ? ARR8(x, a,b,c,d, e,f,g,h) : ARR8(x-8, i,j,k,l, m,n,o,p)
// Shadertoy entry point.
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Get a value from an array of integers.
int paletteValue = ARR16(8, 0,0,0,0, 1,0,0,0, 2,0,0,0, 3,0,0,0);
// Use that value to sample the array of vec4 color values.
fragColor = ARR4(paletteValue, vec4(0.0,0.0,0.0,1.0),
vec4(0.0,1.0,0.0,1.0),
vec4(0.0,1.0,1.0,1.0),
vec4(1.0,0.0,1.0,1.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment