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>html,body{margin:0;overflow:hidden;background:#000;height:100%;cursor:none}canvas{display:block;width:100%;height:100%}</style></head><body><canvas aria-label="Audio-reactive sculptural prism ring"></canvas><script>
'use strict';
const canvas=document.querySelector('canvas');const 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 scene=`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
vec3 palette(float a){return .5+.5*cos(TAU*(a+vec3(.04,.34,.62)));}
vec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}
float wave(float a){return texture2D(history,vec2(fract(a/TAU+.5),.5)).r;}
vec3 filament(vec2 p,float lane){
float a=atan(p.y,p.x),r=length(p);float t=travel*.4;
float envelope=wave(a);
float twist=sin(a*3.+t)*.008+cos(a*5.-t*.7)*.004;
float shape=sin(a*7.+t*1.3)*sin(a*3.-t*.8);
float radius=.285+energy*.043+beat*.012;
radius+=twist+energy*shape*.019;
radius+=lane*(.0065+energy*.0015)*sin(a*2.+t*.4+lane*.17);
radius+=envelope*.012*sin(a*13.-time*.8);
float shift=.0015+peak*.0024;
vec3 d=vec3(length(p+vec2(shift,0.)),r,length(p-vec2(shift,0.)))-radius;
d-=vec3(1.,0.,-1.)*(.0009+peak*.0012)*sin(a+t);
float width=.0007+.0009*energy+.0004*cos(a*4.+lane);
vec3 core=vec3(width*width)/(d*d+width*width);
vec3 mist=exp(-abs(d)*180.)*.06;
return core+mist;
}
void main(){
vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);
// Slow precession of the glass surface, independent of its audio response.
p=rotate(p,.1*sin(time*.08));p.x*=1.+.045*sin(time*.11);p.y*=1.-.035*sin(time*.11);
float a=atan(p.y,p.x),r=length(p);
vec2 oldUV=(uv-.5)*1.0005+.5;
vec3 memory=texture2D(previous,oldUV).rgb*exp(-dt*7.);
if(r<.18||r>.43){gl_FragColor=vec4(memory,1.);return;}
vec3 color=vec3(0.);
vec2 q=rotate(p,.0872665);
for(int i=0;i<8;i++){
float lane=(float(i)-3.5)*1.35;
float depth=.5+.5*sin(a*2.+travel*.16+lane*.23);
vec3 tint=mix(vec3(.26,.55,.95),vec3(.95,.77,.43),.5+.5*sin(a+lane*.13+travel*.13));
tint=mix(tint,palette(a*.12+lane*.055+travel*.02),.36);
// Three genuinely displaced field samples create refractive channel fringes.
vec3 lines=filament(q,lane);
float caustic=pow(.5+.5*cos(a*3.-lane*.3-travel*.4),8.);
color+=lines*tint*(.045+energy*.30+peak*.13)*(.35+depth*.8+caustic*.8);
}
// A dark lens membrane ties the filaments together into one ring.
float center=.285+energy*.043+beat*.012+sin(a*3.+travel*.4)*.008+cos(a*5.-travel*.28)*.004;
float width=.028+energy*.01;
float annulus=smoothstep(width+.003,width,abs(r-center));
float radial=(r-center)/width;
float curved=sqrt(max(0.,1.-radial*radial));
float reflection=pow(max(0.,sin(a*2.+curved*4.+travel*.15)),14.);
color+=mix(vec3(.08,.24,.48),vec3(.7,.42,.2),.5+.5*sin(a+time*.04))*annulus*(.012+energy*.07)*curved;
color+=vec3(.6,.8,1.)*reflection*annulus*(.025+peak*.28);
// The bottom crescent is a true level witness: entirely off without live audio.
float bottom=pow(max(0.,-sin(a)),24.);
float witness=exp(-abs(r-center-width-.012)*750.)*bottom;
color+=vec3(.30,.85,1.)*witness*energy*.75;
// A transient releases an expanding, quickly dissipating wavefront.
float pulseRadius=center+.037+(1.-beat)*.055;
float pulse=exp(-abs(r-pulseRadius)*400.)*beat;
color+=palette(a*.09+.58)*pulse*.15;
// Motion memory: decay is frame-rate independent and cannot accumulate brightness.
color=max(color,memory);
gl_FragColor=vec4(color,1.);
}`;
const post=`precision highp float;varying vec2 uv;uniform sampler2D image;uniform vec2 resolution;
vec3 sampleAt(vec2 d){return texture2D(image,uv+d/resolution).rgb;}
void main(){vec3 c=sampleAt(vec2(0));vec3 glow=vec3(0.);
glow+=sampleAt(vec2(4,0))+sampleAt(vec2(-4,0))+sampleAt(vec2(0,4))+sampleAt(vec2(0,-4));
glow+=sampleAt(vec2(10,10))+sampleAt(vec2(-10,10))+sampleAt(vec2(10,-10))+sampleAt(vec2(-10,-10));
glow+=sampleAt(vec2(23,0))+sampleAt(vec2(-23,0))+sampleAt(vec2(0,23))+sampleAt(vec2(0,-23));
c+=glow*.045;c=1.-exp(-c*1.5);c=pow(c,vec3(.93));gl_FragColor=vec4(c,1.);}`;
function makeProgram(src){function compile(type,code){const s=gl.createShader(type);gl.shaderSource(s,code);gl.compileShader(s);if(!gl.getShaderParameter(s,gl.COMPILE_STATUS))throw Error(gl.getShaderInfoLog(s));return s}const p=gl.createProgram();gl.attachShader(p,compile(gl.VERTEX_SHADER,vertex));gl.attachShader(p,compile(gl.FRAGMENT_SHADER,src));gl.linkProgram(p);if(!gl.getProgramParameter(p,gl.LINK_STATUS))throw Error(gl.getProgramInfoLog(p));return p}
const sceneProgram=makeProgram(scene),postProgram=makeProgram(post),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);
function use(p){gl.useProgram(p);const a=gl.getAttribLocation(p,'p');gl.enableVertexAttribArray(a);gl.vertexAttribPointer(a,2,gl.FLOAT,false,0,0)}
function texture(){const t=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,t);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);return t}
const historyTexture=texture(),historyPixels=new Uint8Array(256);gl.texImage2D(gl.TEXTURE_2D,0,gl.LUMINANCE,256,1,0,gl.LUMINANCE,gl.UNSIGNED_BYTE,historyPixels);let targets=[],width=0,height=0;
function resize(){const scale=Math.min(devicePixelRatio||1,1280/innerWidth,720/innerHeight,1.5);width=canvas.width=Math.round(innerWidth*scale);height=canvas.height=Math.round(innerHeight*scale);for(const t of targets){gl.deleteTexture(t.texture);gl.deleteFramebuffer(t.framebuffer)}targets=[];for(let i=0;i<2;i++){const t=texture();gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,width,height,0,gl.RGBA,gl.UNSIGNED_BYTE,null);const f=gl.createFramebuffer();gl.bindFramebuffer(gl.FRAMEBUFFER,f);gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,t,0);if(gl.checkFramebufferStatus(gl.FRAMEBUFFER)!==gl.FRAMEBUFFER_COMPLETE)throw Error('Framebuffer unavailable');gl.clearColor(0,0,0,1);gl.clear(gl.COLOR_BUFFER_BIT);targets.push({texture:t,framebuffer:f})}gl.bindFramebuffer(gl.FRAMEBUFFER,null)}addEventListener('resize',resize);resize();
const u={};for(const n of ['resolution','previous','history','time','energy','peak','beat','travel','dt'])u[n]=gl.getUniformLocation(sceneProgram,n);const postU={image:gl.getUniformLocation(postProgram,'image'),resolution:gl.getUniformLocation(postProgram,'resolution')};
let target=0,peakTarget=0,energy=0,peak=0,beat=0,travel=0,lastSample=-Infinity,lastRms=0,samples=0,source=null,status='waiting';
function ingest(v){if(v.status!=='audio')return;const rms=Number(v.rms),pk=Number(v.peak);if(!Number.isFinite(rms)||!Number.isFinite(pk))return;lastSample=performance.now();samples++;source=v.source;status='audio';lastRms=Math.max(0,rms);
// Fixed dB mapping, not auto-gain: silence stays silent and dynamics stay legible.
const db=20*Math.log10(Math.max(1e-7,rms));target=Math.pow(Math.max(0,Math.min(1,(db+58)/48)),1.35);peakTarget=Math.max(0,Math.min(1,(20*Math.log10(Math.max(pk,1e-7))+55)/50));}
const feed=new EventSource('/audio');feed.onmessage=e=>{try{const v=JSON.parse(e.data);if(v.status!=='audio')status=v.status||'error';ingest(v)}catch{}};feed.onerror=()=>status='disconnected';
let previous=performance.now(),read=0,historyTick=0,frames=0,totalDt=0;function frame(now){requestAnimationFrame(frame);const dt=Math.min(.075,(now-previous)/1000);previous=now;frames++;totalDt+=dt;if(now-lastSample>700){target=0;peakTarget=0}const old=energy;energy+=(target-energy)*(1-Math.exp(-dt*(target>energy?24:4)));peak+=(peakTarget-peak)*(1-Math.exp(-dt*(peakTarget>peak?30:6)));beat=Math.max(beat*Math.exp(-dt*5),Math.min(1,Math.max(0,energy-old)*12));travel+=dt*(.16+energy*1.6);
historyTick+=dt;if(historyTick>=1/30){historyTick%=1/30;historyPixels.copyWithin(1,0,255);historyPixels[0]=Math.round(energy*255)}
use(sceneProgram);gl.viewport(0,0,width,height);gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,targets[read].texture);gl.uniform1i(u.previous,0);gl.activeTexture(gl.TEXTURE1);gl.bindTexture(gl.TEXTURE_2D,historyTexture);gl.texSubImage2D(gl.TEXTURE_2D,0,0,0,256,1,gl.LUMINANCE,gl.UNSIGNED_BYTE,historyPixels);gl.uniform1i(u.history,1);gl.uniform2f(u.resolution,width,height);for(const[n,v]of Object.entries({time:now/1000,energy,peak,beat,travel,dt}))gl.uniform1f(u[n],v);const write=1-read;gl.bindFramebuffer(gl.FRAMEBUFFER,targets[write].framebuffer);gl.drawArrays(gl.TRIANGLES,0,6);
use(postProgram);gl.bindFramebuffer(gl.FRAMEBUFFER,null);gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,targets[write].texture);gl.uniform1i(postU.image,0);gl.uniform2f(postU.resolution,width,height);gl.drawArrays(gl.TRIANGLES,0,6);read=write;
}requestAnimationFrame(frame);
addEventListener('dblclick',()=>{if(!document.fullscreenElement)document.documentElement.requestFullscreen?.()});addEventListener('keydown',e=>{if(e.key.toLowerCase()==='f'){if(document.fullscreenElement)document.exitFullscreen?.();else document.documentElement.requestFullscreen?.()}});
window.prismHealth=()=>({ready:true,width,height,source,status,samples,lastRms,energy,peak,beat,ageMs:Math.round(performance.now()-lastSample),averageFps:Math.round(frames/totalDt)});
</script></body></html>