Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Last active July 30, 2022 21:21
Show Gist options
  • Save CharStiles/5a74e98dfb644786e3d0a614f09741f5 to your computer and use it in GitHub Desktop.
Save CharStiles/5a74e98dfb644786e3d0a614f09741f5 to your computer and use it in GitHub Desktop.
// i copied and pasted these functions from the sticker sheet
// 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 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;
}
void main() {
vec2 pos = uv(); // origin is in center
// who remembers SOH CAH TOA ?
// tan, given an angle will return the ratio
// so if we only have the ratio of position
// we use atan to get the angle
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);
// 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(0.5);
vec3 contrast = vec3(0.3,0.13,0.19);
// 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.),cos(time/31.));
vec3 phase = vec3(b,0.5,0.1);
vec3 color = cosPalette(g, brightness, contrast, osc, phase);
gl_FragColor = vec4(color,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