Is there any way to do thing like this: (v < vec3(1.0)) ? vec3(10.1) : vec3(20.2)

is there any way to do thing like this: (v < vec3(1.0)) ? vec3(10.1) : vec3(20.2)

vec3 v;
(v.x < 1.0) ? 10.1 : 20.2;
(v.y < 1.0) ? 10.1 : 20.2;
(v.z < 1.0) ? 10.1 : 20.2;

is there any way to do things like the following? or better and faster way to things above.

vec3 v;
(v < vec3(1.0)) ? vec3(10.1) : vec3(20.2);
vec3 v;
(v < 1.0) ? 10.1 : 20.2;

Thanks!

mix(vec3(20.2),vec3(10.1),lessThan(v,vec3(1.0)))

But there’s no guarantee that this is actually faster.

You can’t use relational operators on vectors. But there are equivalent functions (e.g. lessThan);
see §8.7 “Vector Relational Functions” in the GLSL 4.6 specification.

The mix function can be used as a vector version of the ternary conditional operator (?:); specifically, the overloads where the third argument is a bool or bvec*:

genType mix(genType x, genType y, genBType a)

However, note that:

  1. The test is the last argument, not the first.

  2. False values select the first argument (x), true values select the second argument (y). This is consistent with the mix overloads whose third argument is a float or floating-point vector (0.0 returns the first argument exactly, 1.0 returns the second argument exactly).

IOW, the order of arguments is exactly reversed from what you would expect a hypothetical ifThenElse function to look like.

1 Like