How to fix flickering in the glsl?

48 Views Asked by At

I forked a ShaderToy Pen,I trid to make it simple for learn,but I got some problem.

I guess the problem may because of the vec3 blanket(vec3 d, vec3 p, vec3 rd, vec3 col) and vec3 map(vec3 p) functions.

vec3 blanket(vec3 d, vec3 p, vec3 rd, vec3 col) {
    // float g0 = getRug(vec2(p.x - d.z, p.z), d.y);
    //     col = light(p, rd, color(d, g0));
    // vec3 v = vec3(g0);
    // col = v;
    // vec2 p = uv;
    // p.x -= d.z;
    p.z /= 4.;
    p.z = p.z + .5;
    p.z = 1. - p.z;
    p.x -= d.z;
    p.x /= 4.;
    p.x += .5;
    float t = mod(floor(iTime / 1.), 4.);
    if (p.y > 0. && p.y < 1.0) {
        if (t == 0.) {
            col = RED;
        } else if (t == 1.) {
            col =BLUE ;
        } else if (t == 2.) {
            col = GREEN;
        } else if (t == 3.) {
            col =YELLOW ;
        }
    }
    if (p.y > -1. && p.y < 0.0) {
        if (t == 0.) {
            col = BLUE;
        } else if (t == 1.) {
            col = GREEN;
        } else if (t == 2.) {
            col = YELLOW;
        } else if (t == 3.) {
            col = RED;
        }
    }
    if (p.y > -2. && p.y < -1.0) {
        if (t == 0.) {
            col = GREEN;
        } else if (t == 1.) {
            col = YELLOW;
        } else if (t == 2.) {
            col = RED;
        } else if (t == 3.) {
            col = BLUE;
        }
    }
    if (p.y > -3. && p.y < -2.) {
        if (t == 0.) {
            col = YELLOW;
        } else if (t == 1.) {
            col = RED;
        } else if (t == 2.) {
            col = BLUE;
        } else if (t == 3.) {
            col = GREEN;
        }
    }
    if (p.y > -4. && p.y < -3.) {
        /*if (t == 0.) {
                    col = texture(iChannel0, p.xz).xyz;
                } else if (t == 1.) {
                    col = texture(iChannel1, p.xz).xyz;
                } else if (t == 2.) {
                    col = texture(iChannel2, p.xz).xyz;
                } else if (t == 3.) {
                    col = texture(iChannel3, p.xz).xyz;
                }*/
        if (t == 0.) {
            col = RED;
        } else if (t == 1.) {
            col = BLUE;
        } else if (t == 2.) {
            col = GREEN;
        } else if (t == 3.) {
            col = YELLOW;
        }
        /*else if (t == 2.) {
                    col = vec3(0.0, 0.0, 1.0);
                } else if (t == 3.) {
                    col = vec3(1.0, 1.0, 0.0);
                }*/
    }
  
    return col;
}
vec3 map(vec3 p) {
    vec3 a = vec3(0);
    float t = iTime;
    // scroll the y axis up all the time
    p.y -= t - .5;
    // id of y axis domain repition
    float id = floor(p.y / 1.0) + .5;
    // x value to offset carpets so they fly left and right
    float xoff = max(0.0, (id + t) * 2.0);
    xoff = xoff * xoff * sign(sin(id * pi));
    ;
    // domain repition in the y axis
    p.y = pmod(p.y, 1.0);
    // Add some low poly waves to the carpets
    // float sb = .7;
    // float wscl = 0.06;
    // p.y += psin(p.x * 4.0 + id, sb) * wscl;
    // p.y -= psin(p.z * 4.0 + id, sb) * wscl;
    // calculate box sdf
    a.x = box(p - vec3(xoff, 0, 0), vec3(2, 0.025, 2));
    // pass some info to coloring code
    a.y = id;
    a.z = xoff;
    return a;
}

Can you help me to figure out how to fix the colored plan flickering?

0

There are 0 best solutions below