Reputation: 1527
I know that a computationally efficient way to do if-else statements is the mix
and step
combo, like the following for example:
procTexel = mix(tA, tB, step(1.250, time))
How do I do it with rectangles, as in, how can I check if a texel is within my desired rectangle area within the texture coordinate (let's say, vec4(0.5, 0.5, 0.55, 0.55)
)? Is it a simple if-elseif-else check, or is there an even better way?
EDIT: the vec4
I referred to as the 'rectangle area' follows vec4(xmin, ymin, xmax, ymax)
.
Upvotes: 2
Views: 101
Reputation: 211248
The GLSL function step(edge, x)
returns 0.0 if x < edge
, and 1.0 else.
This means, that the result of
step(edge, x)
is equal to
x >= edge ? 1.0 : 0.0
The following line of code sets the variable float in_range
to 1.0, if x >= a
and x <= b
, and sets the variable to 0.0 else:
float in_range = step(a, x) * step(x, b);
Note, this only works if a <= b
.
If you want to test, if the vec2 v
is in the rectangle defined by the 2 vectors vec2 v_min
and vec2 v_max
, the it can be done like this:
vec2 range_test = step(v_min, v) * step(v, v_max);
float in_rect = range_test.x * range_test.y;
Of course, this only works if v_min.x <= v_max.x
and v_min.y <= v_max.y
.
This means, you can write your code somehow like this:
vec2 tex_coord = ....;
vec4 rect = vec4(xmin, ymin, xmax, ymax);
vec2 in_rect = step(rect.xy, tex_coord) * step(tex_coord, rect.zw);
procTexel = mix(tA, tB, in_rect.x * in_rect.y);
Upvotes: 4