cameron.stream/prism
Audio-reactive WebGL visualizer for OBS streams
0
0
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Prism</title><style>*{box-sizing:border-box}html,body{margin:0;overflow:hidden;background:#000;width:100%;height:100%;cursor:none}canvas{display:block;width:100%;height:100%}</style></head><body><canvas id="scene" aria-label="Experimental system-audio prism visualization"></canvas><script>
'use strict';
const canvas=document.querySelector('canvas'),gl=canvas.getContext('webgl',{alpha:false,antialias:false,powerPreference:'high-performance'});
if(!gl)throw Error('WebGL unavailable');
const vertex=`attribute vec2 p; varying vec2 uv; void main(){uv=p*.5+.5;gl_Position=vec4(p,0.,1.);}`;
const fragment=`precision highp float;
varying vec2 uv;
uniform vec2 resolution;
uniform float time,energy,bass,treble,mode,travel;
uniform sampler2D spectrum;
#define PI 3.14159265359
float hash(vec2 p){return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453123);}
float noise(vec2 p){vec2 i=floor(p),f=fract(p);f=f*f*(3.-2.*f);return mix(mix(hash(i),hash(i+vec2(1,0)),f.x),mix(hash(i+vec2(0,1)),hash(i+vec2(1,1)),f.x),f.y);}
float fbm(vec2 p){float f=0.;f+=.55*noise(p);p=mat2(.8,.6,-.6,.8)*p*2.1;f+=.28*noise(p);p=p*2.03;f+=.14*noise(p);return f;}
float band(float x){return texture2D(spectrum,vec2(clamp(x,.01,.99),.5)).r;}
vec3 palette(float t){return .52+.48*cos(6.28318*(vec3(1.,1.,1.)*t+vec3(.0,.32,.63)));}
// The annulus inverse-samples this generated light field, just as the reference
// samples a photograph. Rotation and separated R/B lookups give actual spatial dispersion.
vec3 lightField(vec2 p){
float t=time*.13+travel*.12;
float a=atan(p.y,p.x),r=length(p);
float freq=band(.5+.46*sin(a*.5+t*.11));
float n=fbm(p*6.+vec2(t*.3,-t*.21));
float swirl=a+1.7*n+.26*sin(r*19.-t)+freq*.55;
float flow=sin(swirl*8.+r*24.-t*2.+n*9.);
float veins=pow(.5+.5*sin(flow*3.+n*15.+r*44.+t),8.);
float folds=pow(.5+.5*sin(swirl*17.-r*38.+n*8.),16.);
vec3 col=palette(n*.6+swirl*.12+t*.035+freq*.12);
if(mode<.5){
// Molten diffraction: broad liquid color with hairline internal caustics.
col*=.23+veins*.85+folds*.7+freq*.5;
col+=vec3(.7,.85,1.)*pow(folds,3.)*.65;
}else if(mode<1.5){
// Interference: folded wavefronts and cool spectral combs.
float w=sin(p.x*45.+sin(p.y*19.+t)*3.+t*2.);
float v=sin(p.y*47.+sin(p.x*22.-t)*3.-t*1.4);
float interference=pow(max(0.,1.-abs(w+v)*.65),15.);
col=palette(a*.17+n*.3+t*.03+.17)*(.12+interference*1.8+folds*.3);
}else{
// Optical memory: fine metallic ribbons around a dark glass core.
float ribbons=pow(.5+.5*cos(r*190.+sin(a*7.+t)*7.+n*17.+freq*10.),22.);
col=mix(vec3(.12,.3,.45),palette(n*.4+a*.1+t*.02),.65)*(.12+ribbons*2.2+veins*.4);
}
return col;
}
vec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}
void main(){
vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);
float a=atan(p.y,p.x),r=length(p);
float angular=band(.5+.49*sin(a*.5+time*.014));
float radius=.305+energy*.018+.003*sin(time*.23);
float width=.047+bass*.021+angular*.012;
float ripple=energy*.005*sin(a*9.+travel*.9)+treble*.002*sin(a*31.-time);
float delta=r-radius-ripple;
float edge=width-abs(delta);
float mask=smoothstep(0.,.002,edge);
float rotation=.087266+energy*.09;
float shift=.004+treble*.012+energy*.004;
vec2 rp=rotate(p+vec2(shift,0),rotation);
vec2 gp=rotate(p,rotation);
vec2 bp=rotate(p-vec2(shift,0),rotation);
vec3 refracted=vec3(lightField(rp).r,lightField(gp).g,lightField(bp).b)*1.18;
float lens=sqrt(max(0.,1.-pow(clamp(delta/width,-1.,1.),2.)));
refracted*=.55+lens*.7;
vec3 color=refracted*mask;
// Subtle diffraction halos live near the ring only; center and exterior are black.
float outer=abs(delta)-width;
float halo=exp(-max(0.,outer)*100.)*(1.-mask);
vec3 rimColor=palette(a*.13+time*.006+.2);
color+=rimColor*halo*(.035+energy*.08);
float rim=exp(-abs(abs(delta)-width)*900.);
color+=mix(rimColor,vec3(.8,.9,1.),.25)*rim*.25;
float innerCaustic=exp(-abs(delta+width*.66)*420.);
color+=palette(a*.2-time*.012)*innerCaustic*mask*(.12+angular*.4);
color=1.-exp(-color*1.22);
color=pow(max(color,0.),vec3(.92));
gl_FragColor=vec4(color,1.);
}`;
function shader(type,src){const s=gl.createShader(type);gl.shaderSource(s,src);gl.compileShader(s);if(!gl.getShaderParameter(s,gl.COMPILE_STATUS))throw Error(gl.getShaderInfoLog(s));return s}
const program=gl.createProgram();gl.attachShader(program,shader(gl.VERTEX_SHADER,vertex));gl.attachShader(program,shader(gl.FRAGMENT_SHADER,fragment));gl.linkProgram(program);if(!gl.getProgramParameter(program,gl.LINK_STATUS))throw Error(gl.getProgramInfoLog(program));gl.useProgram(program);
const buffer=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,buffer);gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]),gl.STATIC_DRAW);const position=gl.getAttribLocation(program,'p');gl.enableVertexAttribArray(position);gl.vertexAttribPointer(position,2,gl.FLOAT,false,0,0);
const u={};for(const n of ['resolution','time','energy','bass','treble','mode','travel'])u[n]=gl.getUniformLocation(program,n);
const texture=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,texture);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.LINEAR);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.LINEAR);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);
const pixels=new Uint8Array(64);gl.texImage2D(gl.TEXTURE_2D,0,gl.LUMINANCE,64,1,0,gl.LUMINANCE,gl.UNSIGNED_BYTE,pixels);
function resize(){const dpr=Math.min(devicePixelRatio||1,1.25);canvas.width=Math.round(innerWidth*dpr);canvas.height=Math.round(innerHeight*dpr);gl.viewport(0,0,canvas.width,canvas.height);gl.uniform2f(u.resolution,canvas.width,canvas.height)}addEventListener('resize',resize);resize();
let bands=Array(40).fill(0),smooth=Array(40).fill(0),target=0,energy=0,travel=0,mode=0,lastAudio=0,connected=false,audioError=null;
const feed=new EventSource('/audio');feed.onmessage=e=>{const v=JSON.parse(e.data);if(v.error){connected=false;audioError=v.error;console.warn('System audio:',v.error);return}if(v.status==='connected'){connected=true;audioError=null}if(v.bands){connected=true;audioError=null;lastAudio=performance.now();bands=v.bands;target=Math.min(1,v.level*2.4)}};feed.onerror=()=>{connected=false};
let previous=performance.now();function frame(now){requestAnimationFrame(frame);const dt=Math.min(.06,(now-previous)/1000);previous=now;const live=now-lastAudio<1000;if(!live)target=0;energy+=(target-energy)*(1-Math.exp(-dt*(target>energy?12:3)));for(let i=0;i<40;i++){const v=live?bands[i]:0;smooth[i]+=(v-smooth[i])*(1-Math.exp(-dt*(v>smooth[i]?14:4)))}for(let i=0;i<64;i++){const x=i/63*39,j=Math.floor(x),f=x-j;pixels[i]=Math.round(255*(smooth[j]*(1-f)+(smooth[Math.min(39,j+1)]||0)*f))}gl.texSubImage2D(gl.TEXTURE_2D,0,0,0,64,1,gl.LUMINANCE,gl.UNSIGNED_BYTE,pixels);const bass=smooth.slice(0,10).reduce((a,b)=>a+b,0)/10,treble=smooth.slice(25).reduce((a,b)=>a+b,0)/15;travel+=dt*(.2+energy*2.5+bass*.6);for(const [n,v]of Object.entries({time:now/1000,energy,bass,treble,mode,travel}))gl.uniform1f(u[n],v);gl.drawArrays(gl.TRIANGLES,0,6)}requestAnimationFrame(frame);
addEventListener('keydown',e=>{if(e.metaKey||e.ctrlKey||e.altKey)return;const key=e.key.toLowerCase();if(key==='f'){if(!document.fullscreenElement)document.documentElement.requestFullscreen?.();else document.exitFullscreen?.()}if(key==='m'||key===' '){mode=(mode+1)%3;e.preventDefault()}if(['1','2','3'].includes(key))mode=Number(key)-1;if(key==='r')fetch('/retry',{method:'POST'})});
addEventListener('dblclick',()=>{if(!document.fullscreenElement)document.documentElement.requestFullscreen?.()});
window.prismHealth=()=>({ready:true,webgl:!!gl,width:canvas.width,height:canvas.height,audioConnected:connected,audioError,mode});
// Keep the same OBS/browser URL while picking up edits in place.
let sourceSnapshot=null;setInterval(async()=>{try{const r=await fetch('/',{cache:'no-store'});if(!r.ok)return;const html=await r.text();if(sourceSnapshot!==null&&html!==sourceSnapshot)location.reload();sourceSnapshot=html}catch{}},2500);
</script></body></html>