please please please, make it smaller!

please please please, make it smaller!
http://www.iquilezles.org/blog/?p=2828

I think size matters. However, unlike in real life, when programming the smaller the better. Generally. Also, the less branches the better, at least when programming for parallel systems. And also, the more compact and regular, the prettier (but this is my personal opinion only).
Related to this, in the last 6 months I have pointed out / proposed this same optimization to at least five different people. Basically, it seems most people make this same “mistake” over and over again, which is to write this horrifying thing
vec3 color = vec3(0.0);
if (theta < 1.0) {
color.r = 1.0;
color.g = theta;
}
else if (theta < 2.0) {
color.r = 2.0 - theta;
color.g = 1.0;
}
else if (theta < 3.0) {
color.g = 1.0;
color.b = theta - 2.0;
}
else if (theta < 4.0) {
color.g = 4.0 - theta;
color.b = 1.0;
}
else if (theta < 5.0) {
color.r = theta - 4.0;
color.b = 1.0;
}
else {
color.r = 1.0;
color.b = 6.0 - theta;
}
return color;
instead of this equivalent line:
vec3 color = clamp( abs(mod(theta+vec3(0.,4.,2.),6.)-3.)-1., 0., 1. );

你可能感兴趣的:(please please please, make it smaller!)