~ knotless Search/
↑↓ move↵ open⇧↵ all results

cameron.stream/prism

Audio-reactive WebGL visualizer for OBS streams

0
0
main
74 lines · 4.4 KB Raw History
'use strict';
// Four self-contained looks. Each reads the measured spectrum (see SPECTRAL in
// build-presets.cjs): band(x) is the current level at log-frequency x (0 = 30 Hz,
// 1 = 16 kHz), bandAt(x,age) the level `age` (0–1 ≈ 0–2 s) ago, ringBand(a) the
// spectrum wrapped round a ring with bass at the bottom and treble at the top.
const header=`precision highp float;
varying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;
#define TAU 6.28318530718
float line(float d,float w){return w*w/(d*d+w*w);}
float hist(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}
vec3 palette(float x){return .5+.5*cos(TAU*(vec3(.02,.35,.62)+x));}
void main(){vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);float r=length(p),a=atan(p.y,p.x);vec3 c=vec3(0.);float power=energy*.85+peak*.15;
`;
const footer=`
vec3 memory=texture2D(previous,(uv-.5)*(1.+dt*.025)+.5).rgb*exp(-dt*7.);
c=min(c,vec3(1.6));gl_FragColor=vec4(max(c,memory),1.);}`;
const modes=[
 {id:'oscilloscope-tunnel',name:'Oscilloscope Tunnel',body:`
// Nested rounded-square traces fly toward the viewer. Each trace is a moment in the
// past: its perimeter is lit by the spectrum measured at that age (a spectrogram
// folded into a tunnel), its wobble follows the same bands, its weight the bass.
float square=pow(pow(abs(p.x),4.)+pow(abs(p.y),4.),.25);
float around=fract(a/TAU+.5);float mirrored=1.-abs(around*2.-1.);
for(int i=0;i<12;i++){float f=float(i);float phase=fract(f/12.+travel*.075);float radius=.055+phase*phase*.85;
float level=bandAt(mirrored,phase);
float wobble=sin(a*6.+travel+f*.7)*level*.03*phase+bassAt(phase)*.02*phase;
float trace=line(square-radius-wobble,.0006+phase*.0013+bassAt(phase)*.0012);
float gate=.15+.85*pow(level,1.4);
vec3 tone=palette(mirrored*.55+f*.03+travel*.012);
// Present loudness gates the echo, so silence still goes dark at once.
c+=tone*trace*gate*power*smoothstep(0.,.12,phase)*(1.-phase)*1.4;}
`},
 {id:'orbital-dust',name:'Orbital Dust',body:`
// Luminous bodies on tilted elliptical tracks. Each body owns one frequency band:
// its size and brightness are that band's level, and bass swells the whole system.
// Treble bodies sit on the outer orbits and travel faster.
for(int i=0;i<32;i++){float f=float(i),x=f/31.;float level=band(x);
float phase=f*2.399963+travel*(.10+mod(f,5.)*.016+x*.06);
float radius=.12+x*.30;
vec2 q=vec2(cos(phase)*radius,sin(phase)*radius*.52);float tilt=f*.37;q=mat2(cos(tilt),-sin(tilt),sin(tilt),cos(tilt))*q;
q*=1.+bass*.24+beat*.05;float d=length(p-q);float width=.0016+level*.006;
c+=palette(x*.75+travel*.012)*line(d,width)*(.2*power+level*2.4);
float orbit=length(vec2(p.x,p.y*1.15));c+=palette(x*.75)*line(orbit-radius*(1.+bass*.24),.0005)*level*.03;}
`},
 {id:'liquid-lattice',name:'Liquid Lattice',body:`
// Two warped line families interfere like a flexible wire mesh. Bass bends the mesh,
// mids bend it the other way, treble sharpens the wires, and the spectrum spreads
// outward from the centre (low at the middle, high at the rim).
vec2 q=p; q.x+=sin(p.y*9.+travel*.45)*(.02+bass*.09);q.y+=sin(p.x*8.-travel*.32)*(.02+mid*.07);
float wx=sin(q.x*45.+travel*.25),wy=sin(q.y*45.-travel*.2);
float thin=.03-high*.018;
float gx=line(wx,thin),gy=line(wy,thin);
float mask=exp(-r*r*5.)*(1.-smoothstep(.48,.72,r));
float radial=clamp(r*1.7,0.,1.),level=band(radial);
float ripple=.5+.5*sin(r*28.-travel*1.8+level*3.);
c=(palette(q.x*.5+radial*.4+travel*.015)*gx+palette(q.y*.5+radial*.4+.35)*gy)*mask*(.3*power+level*1.6)*(.3+ripple*.5);
c+=vec3(.5,.8,.9)*gx*gy*mask*(beat*.35+high*.35);
`},
 {id:'eclipse-corona',name:'Eclipse Corona',body:`
// Dark disk, amber limb and shearing coronal arcs. The limb breathes with the bass;
// the corona's reach around the disk is the spectrum (bass below, treble above);
// each of the seven arc layers is driven by its own frequency range.
float radius=.21+bass*.035+beat*.006;
float edge=line(r-radius,.0011+bass*.0015);
c+=vec3(1.,.45,.08)*edge*(.2*power+bass*.9);
float reach=.015+energy*.02+ringBand(a)*.085;
for(int i=0;i<7;i++){float f=float(i);float rays=pow(.5+.5*sin(a*(13.+f*4.)+sin(a*3.+travel*.2)*2.-travel*(.3+f*.07)),10.);
float d=r-radius-f*.003;
float halo=exp(-max(0.,d)/(reach+.001))*smoothstep(-.001,.006,d);
c+=mix(vec3(1.,.22,.025),vec3(.4,.15,.65),f/7.)*rays*halo*(.05*power+band(.05+f/6.*.9)*.45);}
c*=smoothstep(radius-.004,radius,r);
`}
];
module.exports=modes.map(({body,...m})=>({...m,source:header+body+footer}));