Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Last active February 16, 2021 00:01
Show Gist options
  • Save CharStiles/87827a34b2e2edca42a43609365b8f8a to your computer and use it in GitHub Desktop.
Save CharStiles/87827a34b2e2edca42a43609365b8f8a to your computer and use it in GitHub Desktop.
// As t runs from 0 to 1 (our normalized palette index or domain),
//the cosine oscilates c times with a phase of d.
//The result is scaled and biased by a and b to meet the desired constrast and brightness.
// http://www.iquilezles.org/www/articles/palettes/palettes.htm
// to see this function graphed out go to: https://www.desmos.com/calculator/rz7abjujdj
vec3 cosPalette( float t , vec3 brightness, vec3 contrast, vec3 osc, vec3 phase)
{
return brightness + contrast*cos( 6.28318*(osc*t+phase) );
}
float sgn(float x) {
return (x<0.)?-1.:1.;
}
vec2 sgn(vec2 v) {
return vec2((v.x<0.)?-1.:1., (v.y<0.)?-1.:1.);
}
// https://math.stackexchange.com/questions/2491494/does-there-exist-a-smooth-approximation-of-x-bmod-y
// found this equation and converted it to GLSL, usually e is supposed to be squared but in this case I like the way it looks as 0 //idk
float smoothMod(float x, float y, float e){
float top = cos(PI * (x/y)) * sin(PI * (x/y));
float bot = pow(sin(PI * (x/y)),2.)+ pow(e, sin(time));
float at = atan(top/bot);
return y * (1./2.) - (1./PI) * at ;
}
// Repeat around the origin by a fixed angle.
// For easier use, num of repetitions is use to specify the angle.
vec2 modPolar(vec2 p, float repetitions) {
float angle = 2.*PI/repetitions;
float a = atan(p.y, p.x) + angle/2.;
float r = length(p);
//float c = floor(a/angle);
a = smoothMod(a,angle,0.1) - angle/2.;
//a = mix(a,)
vec2 p2 = vec2(cos(a), sin(a))*r;
// p = mix(p,p2, pow(angle - abs(angle-(angle/2.) ) /angle , 2.) );
return p2;
}
float getBPMVis(float bpm){
// this function can be found graphed out here :https://www.desmos.com/calculator/rx86e6ymw7
float bps = 60./bpm; // beats per second
float bpmVis = tan((time*PI)/bps);
// multiply it by PI so that tan has a regular spike every 1 instead of PI
// divide by the beat per second so there are that many spikes per second
bpmVis = clamp(bpmVis,0.,10.);
// tan goes to infinity so lets clamp it at 10
bpmVis = abs(bpmVis)/20.;
// tan goes up and down but we only want it to go up
// (so it looks like a spike) so we take the absolute value
// dividing by 20 makes the tan function more spiking than smoothly going
// up and down, check out the desmos link to see what i mean
bpmVis = 1.+(bpmVis*0.1);
// we want to multiply by this number, but its too big
// by itself (it would be too stroby) so we want the number to multiply
// by to be between 1.0 and 1.05 so its a subtle effect
return bpmVis;
}
// Mirror at an axis-aligned plane which is at a specified distance <dist> from the origin.
float pMirror (inout float p, float dist) {
float s = sgn(p);
p = abs(p)-dist;
return s;
}
// Mirror in both dimensions and at the diagonal, yielding one eighth of the space.
// translate by dist before mirroring.
vec2 pMirrorOctant (inout vec2 p, vec2 dist) {
vec2 s = sgn(p);
pMirror(p.x, dist.x);
pMirror(p.y, dist.y);
if (p.y > p.x)
p.xy = p.yx;
return s;
}
void pR(inout vec2 p, float a) {
p = cos(a)*p + sin(a)*vec2(p.y, -p.x);
}
void main() {
vec2 pos = uv(); // origin is in center
float swirl =length( mix((pos*.5)* snoise(vec2(cos(time/24.)*(sin(time/20.)*.2),snoise(vec2(sin(time/40.), cos(time/77.)*0.1)*1.)))*13., pos+ snoise(vec2(noise(time/31.)*0.12,snoise(vec2(time*.1, time*.093248)))), sin(time/40.) ));
swirl = mix(swirl,0., noise(pos+snoise(vec2(time/20.))*0.1));
pR(pos,time/20. + swirl);
vec2 pos2 = pos;
vec2 s = pMirrorOctant(pos2,vec2(sin(time/21.), cos(time/17.)));
pos*=1.0;
pos = modPolar(pos2,sin(time) + 1.);
pos = mix(pos,pos2,(sin(time/2.)+1.)/2.0);
pos -= vec2(0.5 + (sin(time) + 1.0) * 0.2,0);
// pos = pos2;
// pos = mix(pos2,pos,sin(time));
float angle = atan(pos.y/abs(pos.x+0.01));
// we take the absolute value of x and add a small number to avoid
// dividing by zero which is undefined behavior
float r = sin(angle + time);
// sin returns a number from -1 to 1, and colors are from 0 to 1, so thats
// why you only see red on the screen half the time. the angle goes around
// the screen, adding time moves it clockwise
float bpmVis = getBPMVis(128.);
float ringFrequency = 5.; // making this number bigger will increase frequency
float g = cos(length(pos*ringFrequency *bpmVis) - time + (time+fbm((angle*10. + time) +time/20., 1)+sin(time)));
// the distance (aka length) from the center put in a cos, time moves the
// circles in.
float b = cos(angle+ cos(length(pos*15.)) + bands.y );
// this combines what we learned in the red and green channels
// angle is going through a cos and so is the length, so we see the
// blue channel oscillating in both dimensions the polar coordinates give us
// here the music effects the place where the phase of the cosine starts
// please play around with these numbers to get a better palette
vec3 brightness = vec3(pow(voronoi(vec3(pos*5.,time/2.)).x,4. + (sin(time+(pos.y*20.))*01.94)))+0.325;
vec3 contrast = vec3(0.2,0.09,0.1);
// the numbers that divide time are pretty arbitrary, as long as they are not the same and are somewhere between 10-100 id say it gives the desired effect
vec3 osc = vec3(r,cos(time/20.)+1.,cos(time/31.)+1.0);
vec3 phase = vec3(r* 1.-length(uv()),r* 1.-length(uv()),r* 1.-length(uv()));
contrast = mix(osc,phase*b*b*r*r*g*g,sin(time+pos.x))*(0.1* abs(sin(time/10.+pos.x*2.)));
vec3 color = cosPalette((g/90.)+(swirl/10.), brightness, contrast, osc, phase);
// color.r = pow(color.r,.2);
// color.g = pow(color.g,2.);
// color.b = pow(color.b,2.);
vec4 p = texture2D(backbuffer, uvN())*01.004;
vec4 c = mix(1.-vec4(color,1.), vec4(color,1.),color.x);
gl_FragColor =mix(c,p, clamp((swirl*0.21 + pos.x + pos.y)*0.3 + 0.4,0.,1. ));
// now play it to a bpm bop like I feel love by Donna Summer
//https://www.youtube.com/watch?v=Nm-ISatLDG0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment