Last active
September 27, 2017 23:47
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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