Search code examples
glslwebgl

Restrict GLSL u_time from falling below 0.5 when using it with Sin


I am in the process of learning glsl and I am having trouble restricting the u_time from falling below 0.5. I'd like the range of it to be from 0.5-1.0 with sin. Here is my code below where I have a line moving by sin. The restriction of where I want the u_time is labeled. It is for a glow effect to the line.

void main() {
    vec2 uv = gl_FragCoord.xy / u_resolution.xy * 10.0;
    
    vec3 color = vec3(0.0);
    
    float wave_width = sin(u_time); //Here is where I want the u_time restricted
    uv.y -= 4.0;
    
    uv.y += (0.2 * sin(uv.x + u_time));
    wave_width *= abs(1.0 / (10.0 * uv.y));
    color += vec3(wave_width, 0.0 , 0.0);
    
    
    
    gl_FragColor = vec4(color, 1.0);
}

Solution

  • The function sin returns a number in the range [-1, 1]. Scaling this number by 0.25 gives a number in the range [-0.25, 0.25]. If you then add 0.75, you get a number in the range [0.5, 1.0]:

    float wave_width = sin(u_time) * 0.25 + 0.75;