I've done a vertex shader effect, but couldn't figure out how to avoid an if statement. (You can also take a look at the entire code on Github).
The method:
-I find a common center to all connected vertices with this arithmetic sequence.
(n is a value I'm looping through from 0 to 159)
originalCenter + (centerOffset * mod(n, widthSize))
vec2 calculatedCenter = vec2(uFirstCenter.x + (uCentersOffset.x * mod(float(n), uGridSize.x)), uFirstCenter.y + (uCentersOffset.y * mod(float(n), 2.0)) + (floor(float(n) / uGridSize.x) * (2.0 * uCentersOffset.y)));
-Compare the distance of vertex to calculated center and store n value if it's the minimum distance possible. (This is where I'm having trouble)
if (distance(vec2(VERTEX.x, -VERTEX.z), vec2(calculatedCenter.x, calculatedCenter.y)) < closestCenterDistance){
centerToUse = float(n);
closestCenterDistance = distance(vec2(VERTEX.x, -VERTEX.z), vec2(calculatedCenter.x, calculatedCenter.y));
}
This code works as it is, but I don't really like that if
statement for possible performance issues. And it also needs GLSL 3.0 which renders my HTML version invisible for some reason.
What I have tried:
I use the next formula to convert the condition into a 0 or 1, so then I can add that to centerToUse. At the end of the for loop it should give me the nearest n center.
centerToUse += (sign(closestCenterDistance - distance(vec2(VERTEX.x, -VERTEX.z), calculatedCenter)) + 1.0) * 0.5;
closestCenterDistance = min(closestCenterDistance, distance(vec2(VERTEX.x, -VERTEX.z), calculatedCenter));
What I really need
I know my explanation skills are lacking so don't worry if this seems super complicated.
What I really need is to know what vertices are connected so they all behave the same way, depending on the distance to the player.
Of course, if you need more info to help me, I'll be happy to provide it.