//https://www.shadertoy.com/view/Wdf3Rf
#define BPM 128.0

float distRazers(vec3 p) {
	vec3 b = vec3(1.0, 1.0, 5.0);
    p = mod(p, b) - b * 0.5;
    p.xy = abs(p.xy) - 0.001;
    return max(length(max(p.xy, 0.0)), max(0.0, abs(p.z) - 1.0));
}

vec3 raymarchRazers(vec3 ro, vec3 rd) {
    vec3 c = vec3(0.0);
   	vec3 p = ro;
    for (int i = 0; i < 32;i++) {
    	float d = distRazers(p);
        p += 0.9 * d * rd;
     	c += 0.002 / max(d, 0.0001) * vec3(0.5, 0.4, 0.2);
    }
    return c;
}

float distParticles(vec3 p) {
    p += 0.5;
	p = mod(p, 0.5) - 0.25;
    return abs(length(p) - 0.01);
}

vec3 raymarchParticles(vec3 ro, vec3 rd) {
	vec3 c = vec3(0.0);
    vec3 p = ro;
    for (int i = 0; i < 32; i++) {
    	float d = distParticles(p);
        p += 0.9 * d * rd;
        c += 0.0005 / max(d, 0.0001) * vec3(0.8, 0.9, 1.0);
    }
    return c;
}

vec2 random2(float x) {
    return fract(sin(x * vec2(12.9898, 51.431)) * vec2(43758.5453, 71932.1354));
}

void mainImage(out vec4 fragColor,in vec2 fragCoord) {
	vec2 st = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
    
   	float bar = iTime / 60.0 * (BPM * 0.25);
	
    vec2 p1 = (random2(floor(bar)) * 2.0 - 1.0) * 5.0;
    vec2 p2 = (random2(floor(bar) + 1.0) * 2.0 - 1.0 ) * 5.0;
    
    vec2 p = mix(p1, p2, smoothstep(0.8, 1.0, fract(bar)));
    
    vec3 ro = vec3(p, -5.0 + iTime);
    vec3 ta = vec3(0.0, 0.0, 0.0 + iTime);
    vec3 front = normalize(ta - ro);
    vec3 right = normalize(cross(front, vec3(0.0, 1.0, 0.0)));
    vec3 up = normalize(cross(right, front));
    vec3 rd = normalize(st.x * right + st.y * up + 1.5 * front);

    vec3 c = vec3(0.0);
    c += raymarchRazers(ro, rd);
    c += raymarchParticles(ro, rd);

    fragColor = vec4(c, 1.0);    
}