Skip to content

Instantly share code, notes, and snippets.

@TaylorMutch
Last active September 27, 2017 23:47
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 TaylorMutch/b72da3c45ee082327f604ae847003512 to your computer and use it in GitHub Desktop.
Save TaylorMutch/b72da3c45ee082327f604ae847003512 to your computer and use it in GitHub Desktop.
WebGL Conditionals optimized for performance. Use these instead of canonical 'or' and 'and' statements in GLSL code.
float when_eq(float x, float y) {
return 1.0 - abs(sign(x - y));
}
float when_neq(float x, float y) {
return abs(sign(x - y));
}
float when_gt(float x, float y) {
return max(sign(x - y), 0.0);
}
float when_lt(float x, float y) {
return max(sign(y - x), 0.0);
}
float when_ge(float x, float y) {
return 1.0 - when_lt(x, y);
}
float when_le(float x, float y) {
return 1.0 - when_gt(x, y);
}
// and/or/xor
vec4 and(vec4 a, vec4 b) {
return a * b;
}
vec4 or(vec4 a, vec4 b) {
return min(a + b, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment