This doesn't work as expected (WebGL 1.0):
precision highp float;
...
uniform float u_time;
attribute vec2 a_texCoord;
attribute vec2 a_animation;
varying vec2 v_texCoord;
void main() {
v_texCoord = a_texCoord;
if (a_animation.y == 13.0) {
v_texCoord.x += mod(u_time, a_animation.y) * 0.0625;
}
...
This works (replacing the line inside the if):
v_texCoord.x += mod(u_time, 13.0) * 0.0625;
I have an animation with 13 frames. It should run through the frames 0 to 12, but in 1st version it seems to go from 1 to 13 (out of bounds).
The 2nd version works exactly as expected, from 0 to 12.
How is this difference even possible? What am I missing?
Edit: I fixed it!
I replaced mod with the following implementation (found here Get accurate integer modulo in WebGL shader):
/**
* Returns accurate MOD when arguments are approximate integers.
*/
float modI(float a,float b) {
float m=a-floor((a+0.5)/b)*b;
return floor(m+0.5);
}
If anybody wants to explain what was the problem, I will accept their answer as the correct one.
Edit: I'd like to add, the built-in mod did work for other frame counts, such as 2, 11, 12. But for 13 it'd have the off-by-1 problem)