CMH Studio

Rotate Glow Square

A rotating square with a glowing effect.


#ifdef GL_ES
precision mediump float;
#define GLSLIFY 1
#endif

uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;

#define GLSLIFY 1
mat2 rotate2d(float _angle){
    return mat2(cos(_angle),-sin(_angle), sin(_angle),cos(_angle));
}

float random (vec2 st) {
    return fract(sin(dot(st.xy, vec2(12.9898,78.233)))*43758.5453123);
}

#define M_SQRT_2 1.41421356
float square(vec2 P, float size){
    return abs(max(abs(P.x), abs(P.y)) - size/(2.0*M_SQRT_2));
}

float glow(float d, float str, float thickness){
    return thickness / pow(d, str);
}

void main() {
    vec2 uv = gl_FragCoord.xy/u_resolution.xy;
    // uv.x *= u_resolution.x/u_resolution.y;
    
    float glow_draw;
    for (int index=0; index<5; index++)
    {
        //grid repetition
        float tilingSiize=pow(2.0, float(index));
        vec2 uvs=uv*vec2(tilingSiize);
        vec2 ipos = floor(uvs)/tilingSiize;  // get the integer coords
        vec2 fpos = fract(uvs);  // get the fractional coords
        uvs= fpos*2.0-1.0;
        uvs*= rotate2d(u_time*0.5);
            
        float threshold=0.48+sin(u_time*0.5)*0.2;
        if(random(ipos)>threshold) continue;
            
        //drawing distance function 
        float draw = square(uvs, 0.314);//sdFish(1.0, uv, 0.1), 
            // square(uv, 0.362), heart(uv, 0.5);
        //float glow_draw = smoothstep(0.003,0.00,draw); //1st method
        glow_draw += glow(draw, 0.412, 0.052); //2nd method
        //float glow_draw = exp(-draw*800.0); //3rd method
    }

    gl_FragColor = vec4(vec3(glow_draw),1.0);
}