But if tile is always a vector with an x and y each between 0.0 and 1.0, c should never have a value higher than 0. it should always return either 0 or -1. So why does it draw white waves?
Hi @s_e_p
That is because the length function returns a float and then the step function goes to zero if below the threshold (the hard coded number there) or 1 if above. at the end c is actually an integer, because you only added or substracted 1 and 0. last, when you pass a float to vec4( ) it will assign that value to all the values of the vec4, hence you end up with all values 0 or more or equal to 1. Does it make sense?
No, I don’t get it, because every pair of values I try to work this out for returns either -1 or 0; never 1. I even tried running the equation in c++ and printing the values
int ofApp::step(float edge, float x)
{
return edge > x ? 1 : 0;
}
float ofApp::length(ofVec2f v)
{
return std::hypot(v.x, v.y);
}
void ofApp::setup()
{
for (int i = 0; i < 100; ++i)
{
tile.y = 0.0f;
for (int j = 0; j < 100; ++j)
{
float a = (step(length(tile), 0.6) -
step(length(tile), 0.4)) +
(step(length(tile - ofVec2f(1.0f)), 0.6) -
step(length(tile - ofVec2f(1.0f)), 0.4));
cout << a << ", ";
}
tile.x += 0.01f;
}
}
and it’s all -1s and 0s only. Is there something wrong with my simulation?
I often find it helpful to break shader code into smaller pieces, to see how it works. With the original code, you can see how the step() functions affect the color. It can be a good way to troubleshoot:
// run each separately in the shader to see what they do
float c = step(length(tile), 0.6);
// float c = step(length(tile - vec2(1.0)), 0.6);
// float c = step(length(tile), 0.6) - step(length(tc), 0.4);