Shaders are basically mathematical operations, and everyone knows that conditional statements are not shader friendly, and yeah, we can overcome then by basically turning then into math!


See this use case:

if (x == 10) {

 value += 1;


instead of this condition when can do:

value += 1 * when_eq(x,10);


In the above example the when_eq will return only 0 or 1, 1 only when x is 10 that means that value will increased by 0 when x not equal 10. Quite simple dont you think?


Shader code
// Those snippets are in vec4 type but you can change to another types like:
// vec3, vec2, float, int ..........

vec4 when_eq(vec4 x, vec4 y) {
  return 1.0 - abs(sign(x - y));

vec4 when_neq(vec4 x, vec4 y) {
  return abs(sign(x - y));

vec4 when_gt(vec4 x, vec4 y) {
  return max(sign(x - y), 0.0);

vec4 when_lt(vec4 x, vec4 y) {
  return max(sign(y - x), 0.0);

vec4 when_ge(vec4 x, vec4 y) {
  return 1.0 - when_lt(x, y);

vec4 when_le(vec4 x, vec4 y) {
  return 1.0 - when_gt(x, y);

//Here are some logical operators to use with the results from above:

vec4 and(vec4 a, vec4 b) {
  return a * b;

vec4 or(vec4 a, vec4 b) {
  return min(a + b, 1.0);

vec4 xor(vec4 a, vec4 b) {
  return (a + b) % 2.0;

vec4 not(vec4 a) {
  return 1.0 - a;
canvas, condition, conditionals, logical, math, operator, optimize, particles, shader, Spatial
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from nonunknown

Color Range Swap [UPDATED]

Related shaders

more shaders converted (enviroment)

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
1 month ago

Thanks for sharing, if you have any further tips and knowledge would you consider creating a cheat sheet git page?
-Nevermind I found the link 🙂

Last edited 1 month ago by dreamcaster