0

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)

J Doe
  • 57
  • 1
  • 5
  • `a_animation` is not a "variable", it is an attribute (a vertex shader input). `mod(u_time, 13.0) * 0.0625` is [dynamically uniform](https://www.khronos.org/opengl/wiki/Core_Language_(GLSL)#Dynamically_uniform_expression), but `mod(u_time, a_animation.y) * 0.0625` is it not. – Rabbid76 Mar 27 '20 at 21:18
  • So what can I do to fix it? I'm pretty sure I'm feeding the attribute the value 13 (the if statement was my debugging effort to ensure that). – J Doe Mar 27 '20 at 21:28

0 Answers0