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 presetDefinitions=[{"id":"molten-diffraction","name":"Molten Diffraction","source":"precision highp float;\nvarying vec2 uv;\nuniform vec2 resolution;\nuniform float time,energy,bodyLevel,edgeLevel,mode,travel;\nuniform sampler2D history;\n#define PI 3.14159265359\nfloat hash(vec2 p){return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453123);}\nfloat 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);}\nfloat 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;}\nfloat historyAt(float x){return texture2D(history,vec2(clamp(x,.01,.99),.5)).r;}\nvec3 palette(float t){return .52+.48*cos(6.28318*(vec3(1.,1.,1.)*t+vec3(.0,.32,.63)));}\n// The annulus inverse-samples this generated light field, just as the reference\n// samples a photograph. Rotation and separated R/B lookups give actual spatial dispersion.\nvec3 lightField(vec2 p){\n float t=time*.13+travel*.12;\n float a=atan(p.y,p.x),r=length(p);\n float envelope=historyAt(.5+.46*sin(a*.5+t*.11));\n float n=fbm(p*6.+vec2(t*.3,-t*.21));\n float swirl=a+1.7*n+.26*sin(r*19.-t)+envelope*.55;\n float flow=sin(swirl*8.+r*24.-t*2.+n*9.);\n float veins=pow(.5+.5*sin(flow*3.+n*15.+r*44.+t),8.);\n float folds=pow(.5+.5*sin(swirl*17.-r*38.+n*8.),16.);\n vec3 col=palette(n*.6+swirl*.12+t*.035+envelope*.12);\n if(mode<.5){\n // Molten diffraction: broad liquid color with hairline internal caustics.\n col*=.23+veins*.85+folds*.7+envelope*.5;\n col+=vec3(.7,.85,1.)*pow(folds,3.)*.65;\n }else if(mode<1.5){\n // Interference: folded wavefronts and cool spectral combs.\n float w=sin(p.x*45.+sin(p.y*19.+t)*3.+t*2.);\n float v=sin(p.y*47.+sin(p.x*22.-t)*3.-t*1.4);\n float interference=pow(max(0.,1.-abs(w+v)*.65),15.);\n col=palette(a*.17+n*.3+t*.03+.17)*(.12+interference*1.8+folds*.3);\n }else{\n // Optical memory: fine metallic ribbons around a dark glass core.\n float ribbons=pow(.5+.5*cos(r*190.+sin(a*7.+t)*7.+n*17.+envelope*10.),22.);\n col=mix(vec3(.12,.3,.45),palette(n*.4+a*.1+t*.02),.65)*(.12+ribbons*2.2+veins*.4);\n }\n return col;\n}\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n float a=atan(p.y,p.x),r=length(p);\n float angular=historyAt(.5+.49*sin(a*.5+time*.014));\n float radius=.305+energy*.018+.003*sin(time*.23);\n float width=.047+bodyLevel*.021+angular*.012;\n float ripple=energy*.005*sin(a*9.+travel*.9)+edgeLevel*.002*sin(a*31.-time);\n float delta=r-radius-ripple;\n float edge=width-abs(delta);\n float mask=smoothstep(0.,.002,edge);\n float rotation=.087266+energy*.09;\n float shift=.004+edgeLevel*.012+energy*.004;\n vec2 rp=rotate(p+vec2(shift,0),rotation);\n vec2 gp=rotate(p,rotation);\n vec2 bp=rotate(p-vec2(shift,0),rotation);\n vec3 refracted=vec3(lightField(rp).r,lightField(gp).g,lightField(bp).b)*1.18;\n float lens=sqrt(max(0.,1.-pow(clamp(delta/width,-1.,1.),2.)));\n refracted*=.55+lens*.7;\n vec3 color=refracted*mask;\n // Subtle diffraction halos live near the ring only; center and exterior are black.\n float outer=abs(delta)-width;\n float halo=exp(-max(0.,outer)*100.)*(1.-mask);\n vec3 rimColor=palette(a*.13+time*.006+.2);\n color+=rimColor*halo*(.035+energy*.08);\n float rim=exp(-abs(abs(delta)-width)*900.);\n color+=mix(rimColor,vec3(.8,.9,1.),.25)*rim*.25;\n float innerCaustic=exp(-abs(delta+width*.66)*420.);\n color+=palette(a*.2-time*.012)*innerCaustic*mask*(.12+angular*.4);\n color=1.-exp(-color*1.22);\n color=pow(max(color,0.),vec3(.92));\n gl_FragColor=vec4(color,1.);\n}","mode":0},{"id":"interference","name":"Interference","source":"precision highp float;\nvarying vec2 uv;\nuniform vec2 resolution;\nuniform float time,energy,bodyLevel,edgeLevel,mode,travel;\nuniform sampler2D history;\n#define PI 3.14159265359\nfloat hash(vec2 p){return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453123);}\nfloat 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);}\nfloat 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;}\nfloat historyAt(float x){return texture2D(history,vec2(clamp(x,.01,.99),.5)).r;}\nvec3 palette(float t){return .52+.48*cos(6.28318*(vec3(1.,1.,1.)*t+vec3(.0,.32,.63)));}\n// The annulus inverse-samples this generated light field, just as the reference\n// samples a photograph. Rotation and separated R/B lookups give actual spatial dispersion.\nvec3 lightField(vec2 p){\n float t=time*.13+travel*.12;\n float a=atan(p.y,p.x),r=length(p);\n float envelope=historyAt(.5+.46*sin(a*.5+t*.11));\n float n=fbm(p*6.+vec2(t*.3,-t*.21));\n float swirl=a+1.7*n+.26*sin(r*19.-t)+envelope*.55;\n float flow=sin(swirl*8.+r*24.-t*2.+n*9.);\n float veins=pow(.5+.5*sin(flow*3.+n*15.+r*44.+t),8.);\n float folds=pow(.5+.5*sin(swirl*17.-r*38.+n*8.),16.);\n vec3 col=palette(n*.6+swirl*.12+t*.035+envelope*.12);\n if(mode<.5){\n // Molten diffraction: broad liquid color with hairline internal caustics.\n col*=.23+veins*.85+folds*.7+envelope*.5;\n col+=vec3(.7,.85,1.)*pow(folds,3.)*.65;\n }else if(mode<1.5){\n // Interference: folded wavefronts and cool spectral combs.\n float w=sin(p.x*45.+sin(p.y*19.+t)*3.+t*2.);\n float v=sin(p.y*47.+sin(p.x*22.-t)*3.-t*1.4);\n float interference=pow(max(0.,1.-abs(w+v)*.65),15.);\n col=palette(a*.17+n*.3+t*.03+.17)*(.12+interference*1.8+folds*.3);\n }else{\n // Optical memory: fine metallic ribbons around a dark glass core.\n float ribbons=pow(.5+.5*cos(r*190.+sin(a*7.+t)*7.+n*17.+envelope*10.),22.);\n col=mix(vec3(.12,.3,.45),palette(n*.4+a*.1+t*.02),.65)*(.12+ribbons*2.2+veins*.4);\n }\n return col;\n}\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n float a=atan(p.y,p.x),r=length(p);\n float angular=historyAt(.5+.49*sin(a*.5+time*.014));\n float radius=.305+energy*.018+.003*sin(time*.23);\n float width=.047+bodyLevel*.021+angular*.012;\n float ripple=energy*.005*sin(a*9.+travel*.9)+edgeLevel*.002*sin(a*31.-time);\n float delta=r-radius-ripple;\n float edge=width-abs(delta);\n float mask=smoothstep(0.,.002,edge);\n float rotation=.087266+energy*.09;\n float shift=.004+edgeLevel*.012+energy*.004;\n vec2 rp=rotate(p+vec2(shift,0),rotation);\n vec2 gp=rotate(p,rotation);\n vec2 bp=rotate(p-vec2(shift,0),rotation);\n vec3 refracted=vec3(lightField(rp).r,lightField(gp).g,lightField(bp).b)*1.18;\n float lens=sqrt(max(0.,1.-pow(clamp(delta/width,-1.,1.),2.)));\n refracted*=.55+lens*.7;\n vec3 color=refracted*mask;\n // Subtle diffraction halos live near the ring only; center and exterior are black.\n float outer=abs(delta)-width;\n float halo=exp(-max(0.,outer)*100.)*(1.-mask);\n vec3 rimColor=palette(a*.13+time*.006+.2);\n color+=rimColor*halo*(.035+energy*.08);\n float rim=exp(-abs(abs(delta)-width)*900.);\n color+=mix(rimColor,vec3(.8,.9,1.),.25)*rim*.25;\n float innerCaustic=exp(-abs(delta+width*.66)*420.);\n color+=palette(a*.2-time*.012)*innerCaustic*mask*(.12+angular*.4);\n color=1.-exp(-color*1.22);\n color=pow(max(color,0.),vec3(.92));\n gl_FragColor=vec4(color,1.);\n}","mode":1},{"id":"metallic-ribbons","name":"Metallic Ribbons","source":"precision highp float;\nvarying vec2 uv;\nuniform vec2 resolution;\nuniform float time,energy,bodyLevel,edgeLevel,mode,travel;\nuniform sampler2D history;\n#define PI 3.14159265359\nfloat hash(vec2 p){return fract(sin(dot(p,vec2(127.1,311.7)))*43758.5453123);}\nfloat 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);}\nfloat 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;}\nfloat historyAt(float x){return texture2D(history,vec2(clamp(x,.01,.99),.5)).r;}\nvec3 palette(float t){return .52+.48*cos(6.28318*(vec3(1.,1.,1.)*t+vec3(.0,.32,.63)));}\n// The annulus inverse-samples this generated light field, just as the reference\n// samples a photograph. Rotation and separated R/B lookups give actual spatial dispersion.\nvec3 lightField(vec2 p){\n float t=time*.13+travel*.12;\n float a=atan(p.y,p.x),r=length(p);\n float envelope=historyAt(.5+.46*sin(a*.5+t*.11));\n float n=fbm(p*6.+vec2(t*.3,-t*.21));\n float swirl=a+1.7*n+.26*sin(r*19.-t)+envelope*.55;\n float flow=sin(swirl*8.+r*24.-t*2.+n*9.);\n float veins=pow(.5+.5*sin(flow*3.+n*15.+r*44.+t),8.);\n float folds=pow(.5+.5*sin(swirl*17.-r*38.+n*8.),16.);\n vec3 col=palette(n*.6+swirl*.12+t*.035+envelope*.12);\n if(mode<.5){\n // Molten diffraction: broad liquid color with hairline internal caustics.\n col*=.23+veins*.85+folds*.7+envelope*.5;\n col+=vec3(.7,.85,1.)*pow(folds,3.)*.65;\n }else if(mode<1.5){\n // Interference: folded wavefronts and cool spectral combs.\n float w=sin(p.x*45.+sin(p.y*19.+t)*3.+t*2.);\n float v=sin(p.y*47.+sin(p.x*22.-t)*3.-t*1.4);\n float interference=pow(max(0.,1.-abs(w+v)*.65),15.);\n col=palette(a*.17+n*.3+t*.03+.17)*(.12+interference*1.8+folds*.3);\n }else{\n // Optical memory: fine metallic ribbons around a dark glass core.\n float ribbons=pow(.5+.5*cos(r*190.+sin(a*7.+t)*7.+n*17.+envelope*10.),22.);\n col=mix(vec3(.12,.3,.45),palette(n*.4+a*.1+t*.02),.65)*(.12+ribbons*2.2+veins*.4);\n }\n return col;\n}\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n float a=atan(p.y,p.x),r=length(p);\n float angular=historyAt(.5+.49*sin(a*.5+time*.014));\n float radius=.305+energy*.018+.003*sin(time*.23);\n float width=.047+bodyLevel*.021+angular*.012;\n float ripple=energy*.005*sin(a*9.+travel*.9)+edgeLevel*.002*sin(a*31.-time);\n float delta=r-radius-ripple;\n float edge=width-abs(delta);\n float mask=smoothstep(0.,.002,edge);\n float rotation=.087266+energy*.09;\n float shift=.004+edgeLevel*.012+energy*.004;\n vec2 rp=rotate(p+vec2(shift,0),rotation);\n vec2 gp=rotate(p,rotation);\n vec2 bp=rotate(p-vec2(shift,0),rotation);\n vec3 refracted=vec3(lightField(rp).r,lightField(gp).g,lightField(bp).b)*1.18;\n float lens=sqrt(max(0.,1.-pow(clamp(delta/width,-1.,1.),2.)));\n refracted*=.55+lens*.7;\n vec3 color=refracted*mask;\n // Subtle diffraction halos live near the ring only; center and exterior are black.\n float outer=abs(delta)-width;\n float halo=exp(-max(0.,outer)*100.)*(1.-mask);\n vec3 rimColor=palette(a*.13+time*.006+.2);\n color+=rimColor*halo*(.035+energy*.08);\n float rim=exp(-abs(abs(delta)-width)*900.);\n color+=mix(rimColor,vec3(.8,.9,1.),.25)*rim*.25;\n float innerCaustic=exp(-abs(delta+width*.66)*420.);\n color+=palette(a*.2-time*.012)*innerCaustic*mask*(.12+angular*.4);\n color=1.-exp(-color*1.22);\n color=pow(max(color,0.),vec3(.92));\n gl_FragColor=vec4(color,1.);\n}","mode":2},{"id":"braided-glass","name":"Braided Glass","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\nvec3 palette(float a){return .5+.5*cos(TAU*(a+vec3(.04,.34,.62)));}\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nfloat wave(float a){return texture2D(history,vec2(fract(a/TAU+.5),.5)).r;}\nvec3 filament(vec2 p,float lane){\n float a=atan(p.y,p.x),r=length(p);float t=travel*.4;\n float envelope=wave(a);\n float twist=sin(a*3.+t)*.008+cos(a*5.-t*.7)*.004;\n float shape=sin(a*7.+t*1.3)*sin(a*3.-t*.8);\n float radius=.285+energy*.043+beat*.012;\n radius+=twist+energy*shape*.019;\n radius+=lane*(.0065+energy*.0015)*sin(a*2.+t*.4+lane*.17);\n radius+=envelope*.012*sin(a*13.-time*.8);\n float shift=.0015+peak*.0024;\n vec3 d=vec3(length(p+vec2(shift,0.)),r,length(p-vec2(shift,0.)))-radius;\n d-=vec3(1.,0.,-1.)*(.0009+peak*.0012)*sin(a+t);\n float width=.0007+.0009*energy+.0004*cos(a*4.+lane);\n vec3 core=vec3(width*width)/(d*d+width*width);\n vec3 mist=exp(-abs(d)*180.)*.06;\n return core+mist;\n}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n // Slow precession of the glass surface, independent of its audio response.\n p=rotate(p,.1*sin(time*.08));p.x*=1.+.045*sin(time*.11);p.y*=1.-.035*sin(time*.11);\n float a=atan(p.y,p.x),r=length(p);\n vec2 oldUV=(uv-.5)*1.0005+.5;\n vec3 memory=texture2D(previous,oldUV).rgb*exp(-dt*7.);\n if(r<.18||r>.43){gl_FragColor=vec4(memory,1.);return;}\n vec3 color=vec3(0.);\n vec2 q=rotate(p,.0872665);\n for(int i=0;i<8;i++){\n float lane=(float(i)-3.5)*1.35;\n float depth=.5+.5*sin(a*2.+travel*.16+lane*.23);\n vec3 tint=mix(vec3(.26,.55,.95),vec3(.95,.77,.43),.5+.5*sin(a+lane*.13+travel*.13));\n tint=mix(tint,palette(a*.12+lane*.055+travel*.02),.36);\n // Three genuinely displaced field samples create refractive channel fringes.\n vec3 lines=filament(q,lane);\n float caustic=pow(.5+.5*cos(a*3.-lane*.3-travel*.4),8.);\n color+=lines*tint*(.045+energy*.30+peak*.13)*(.35+depth*.8+caustic*.8);\n }\n // A dark lens membrane ties the filaments together into one ring.\n float center=.285+energy*.043+beat*.012+sin(a*3.+travel*.4)*.008+cos(a*5.-travel*.28)*.004;\n float width=.028+energy*.01;\n float annulus=smoothstep(width+.003,width,abs(r-center));\n float radial=(r-center)/width;\n float curved=sqrt(max(0.,1.-radial*radial));\n float reflection=pow(max(0.,sin(a*2.+curved*4.+travel*.15)),14.);\n color+=mix(vec3(.08,.24,.48),vec3(.7,.42,.2),.5+.5*sin(a+time*.04))*annulus*(.012+energy*.07)*curved;\n color+=vec3(.6,.8,1.)*reflection*annulus*(.025+peak*.28);\n // The bottom crescent is a true level witness: entirely off without live audio.\n float bottom=pow(max(0.,-sin(a)),24.);\n float witness=exp(-abs(r-center-width-.012)*750.)*bottom;\n color+=vec3(.30,.85,1.)*witness*energy*.75;\n // A transient releases an expanding, quickly dissipating wavefront.\n float pulseRadius=center+.037+(1.-beat)*.055;\n float pulse=exp(-abs(r-pulseRadius)*400.)*beat;\n color+=palette(a*.09+.58)*pulse*.15;\n // Motion memory: decay is frame-rate independent and cannot accumulate brightness.\n color=max(color,memory);\n gl_FragColor=vec4(color,1.);\n}"},{"id":"pressure-chamber","name":"Pressure Chamber","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\n#define PI 3.14159265359\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nfloat hash(float x){return fract(sin(x*127.1+41.7)*43758.5453);}\nfloat wave(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}\nvec3 splitColor(float x){return mix(vec3(.96,.025,.06),vec3(.015,.72,.95),smoothstep(-.35,.35,sin(x)));}\nfloat buckle(float a){\n float pressure=energy*.035+beat*.023;\n float fold=sin(a*5.+travel*.6)*sin(a*3.-travel*.27);\n return pressure*fold+.005*sin(a*2.+travel*.13);\n}\nvec3 filaments(vec2 p,float lane){\n float a=atan(p.y,p.x),r=length(p);\n float radius=.278+energy*.04+buckle(a);\n radius+=lane*(.0065+peak*.002)*sin(a*2.+travel*.17+lane*.2);\n radius+=wave(fract(a/TAU+.5))*.011*sin(a*17.-travel*.7);\n float dispersion=.001+peak*.005+beat*.003;\n vec3 d=vec3(length(p+vec2(dispersion,0.)),r,length(p-vec2(dispersion,0.)))-radius;\n float width=.00065+.00065*energy;\n return vec3(width*width)/(d*d+width*width)+exp(-abs(d)*230.)*.025;\n}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n vec2 raw=p;p=rotate(p,.07*sin(time*.07));p.y*=1.+.04*sin(time*.09);\n float r=length(p),a=atan(p.y,p.x);\n vec2 memoryUV=(uv-.5)*(1.+dt*(.025+energy*.035))+.5;\n vec3 memory=texture2D(previous,memoryUV).rgb*exp(-dt*9.);\n vec3 color=vec3(0.);\n // Polygonal pressure fronts: spatially sparse and low-luminance, never a full-screen flash.\n float angular=cos(mod(a+PI/8.+travel*.025,TAU/8.)-PI/8.);\n float polygon=r*angular;\n float outside=smoothstep(.30,.40,r)*(1.-smoothstep(.72,1.05,r));\n for(int i=0;i<3;i++){\n float offset=float(i)/3.;float phase=fract(travel*.105+offset);\n float front=.34+phase*.60;\n float envelope=wave(phase*.7);\n float line=exp(-abs(polygon-front)*650.);\n float broken=pow(max(0.,sin(a*7.+offset*19.+travel*.19)),4.);\n color+=splitColor(a*2.+offset*13.)*line*broken*outside*envelope*energy*(1.-phase)*.20;\n }\n // Narrow, angular splinters. Deterministic trajectories, not random frame flicker.\n float sector=floor((a+PI)/TAU*48.);\n float local=fract((a+PI)/TAU*48.)-.5;\n float seed=hash(sector);float flight=fract(travel*(.10+seed*.09)+seed*5.);\n float shardR=.35+flight*.42;\n float shardAngle=local-(flight-.5)*.16*sin(sector);\n float shard=exp(-abs(shardAngle)*140.)*exp(-abs(r-shardR)*100.);\n float shardPower=pow(peak,2.)*(.2+beat*.8)*step(.65,seed)*(1.-flight);\n color+=splitColor(sector*2.1)*shard*shardPower*.75;\n // Broad dark chamber walls, visible only where audio presses against the ring.\n float walls=pow(max(0.,cos(a*4.+.4)),16.)*exp(-abs(r-.46)*22.);\n color+=splitColor(a*2.)*walls*energy*.007;\n if(r>.18&&r<.43){\n vec2 q=rotate(p,.0872665);\n for(int i=0;i<8;i++){\n float lane=(float(i)-3.5)*1.35;\n vec3 f=filaments(q,lane);\n float spec=pow(max(0.,cos(a*3.+lane*.27+travel*.18)),12.);\n vec3 metal=mix(vec3(.27,.30,.34),vec3(.8,.86,.92),spec);\n vec3 edge=splitColor(a+lane*.38+travel*.13);\n vec3 tint=mix(metal,edge,.23+peak*.17);\n color+=f*tint*(.008+energy*.65+peak*.25)*(.4+spec*1.7);\n }\n float center=.278+energy*.04+buckle(a);\n float halfWidth=.027+peak*.008;\n float d=r-center;float mask=1.-smoothstep(halfWidth,halfWidth+.003,abs(d));\n float normal=sqrt(max(0.,1.-pow(d/halfWidth,2.)));\n float reflection=pow(max(0.,sin(normal*5.+a*3.+travel*.2)),16.);\n color+=vec3(.6,.67,.75)*mask*reflection*(.005+energy*.23);\n float rim=exp(-abs(abs(d)-halfWidth)*850.);\n color+=splitColor(a+travel*.12)*rim*(.006+energy*.21);\n // Audio-only segmented crescent stays legible beneath the more violent material.\n float witness=exp(-abs(r-center-halfWidth-.009)*800.)*pow(max(0.,-sin(a)),20.);\n color+=vec3(.02,.7,.95)*witness*energy*.6;\n }\n // Silence collapses new light to black; residual trails dissipate, never add up.\n color=min(color,vec3(2.));color=max(color,memory);\n gl_FragColor=vec4(color,1.);\n}"},{"id":"monochrome-chrome","name":"Monochrome Chrome","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\n#define PI 3.14159265359\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nfloat hash(float x){return fract(sin(x*127.1+41.7)*43758.5453);}\nfloat wave(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}\nvec3 splitColor(float x){return mix(vec3(.26,.30,.34),vec3(.85,.90,.95),smoothstep(-.35,.35,sin(x)));}\nfloat buckle(float a){\n float pressure=energy*.035+beat*.023;\n float fold=sin(a*5.+travel*.6)*sin(a*3.-travel*.27);\n return pressure*fold+.005*sin(a*2.+travel*.13);\n}\nvec3 filaments(vec2 p,float lane){\n float a=atan(p.y,p.x),r=length(p);\n float radius=.278+energy*.04+buckle(a);\n radius+=lane*(.0065+peak*.002)*sin(a*2.+travel*.17+lane*.2);\n radius+=wave(fract(a/TAU+.5))*.011*sin(a*17.-travel*.7);\n float dispersion=.001+peak*.005+beat*.003;\n vec3 d=vec3(length(p+vec2(dispersion,0.)),r,length(p-vec2(dispersion,0.)))-radius;\n float width=.00065+.00065*energy;\n return vec3(width*width)/(d*d+width*width)+exp(-abs(d)*230.)*.025;\n}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n vec2 raw=p;p=rotate(p,.07*sin(time*.07));p.y*=1.+.04*sin(time*.09);\n float r=length(p),a=atan(p.y,p.x);\n vec2 memoryUV=(uv-.5)*(1.+dt*(.025+energy*.035))+.5;\n vec3 memory=texture2D(previous,memoryUV).rgb*exp(-dt*9.);\n vec3 color=vec3(0.);\n // Polygonal pressure fronts: spatially sparse and low-luminance, never a full-screen flash.\n float angular=cos(mod(a+PI/8.+travel*.025,TAU/8.)-PI/8.);\n float polygon=r*angular;\n float outside=smoothstep(.30,.40,r)*(1.-smoothstep(.72,1.05,r));\n for(int i=0;i<3;i++){\n float offset=float(i)/3.;float phase=fract(travel*.105+offset);\n float front=.34+phase*.60;\n float envelope=wave(phase*.7);\n float line=exp(-abs(polygon-front)*650.);\n float broken=pow(max(0.,sin(a*7.+offset*19.+travel*.19)),4.);\n color+=splitColor(a*2.+offset*13.)*line*broken*outside*envelope*energy*(1.-phase)*.20;\n }\n // Narrow, angular splinters. Deterministic trajectories, not random frame flicker.\n float sector=floor((a+PI)/TAU*48.);\n float local=fract((a+PI)/TAU*48.)-.5;\n float seed=hash(sector);float flight=fract(travel*(.10+seed*.09)+seed*5.);\n float shardR=.35+flight*.42;\n float shardAngle=local-(flight-.5)*.16*sin(sector);\n float shard=exp(-abs(shardAngle)*140.)*exp(-abs(r-shardR)*100.);\n float shardPower=pow(peak,2.)*(.2+beat*.8)*step(.65,seed)*(1.-flight);\n color+=splitColor(sector*2.1)*shard*shardPower*.75;\n // Broad dark chamber walls, visible only where audio presses against the ring.\n float walls=pow(max(0.,cos(a*4.+.4)),16.)*exp(-abs(r-.46)*22.);\n color+=splitColor(a*2.)*walls*energy*.007;\n if(r>.18&&r<.43){\n vec2 q=rotate(p,.0872665);\n for(int i=0;i<8;i++){\n float lane=(float(i)-3.5)*1.35;\n vec3 f=filaments(q,lane);\n float spec=pow(max(0.,cos(a*3.+lane*.27+travel*.18)),12.);\n vec3 metal=mix(vec3(.27,.30,.34),vec3(.8,.86,.92),spec);\n vec3 edge=splitColor(a+lane*.38+travel*.13);\n vec3 tint=mix(metal,edge,.23+peak*.17);\n color+=f*tint*(.008+energy*.65+peak*.25)*(.4+spec*1.7);\n }\n float center=.278+energy*.04+buckle(a);\n float halfWidth=.027+peak*.008;\n float d=r-center;float mask=1.-smoothstep(halfWidth,halfWidth+.003,abs(d));\n float normal=sqrt(max(0.,1.-pow(d/halfWidth,2.)));\n float reflection=pow(max(0.,sin(normal*5.+a*3.+travel*.2)),16.);\n color+=vec3(.6,.67,.75)*mask*reflection*(.005+energy*.23);\n float rim=exp(-abs(abs(d)-halfWidth)*850.);\n color+=splitColor(a+travel*.12)*rim*(.006+energy*.21);\n // Audio-only segmented crescent stays legible beneath the more violent material.\n float witness=exp(-abs(r-center-halfWidth-.009)*800.)*pow(max(0.,-sin(a)),20.);\n color+=vec3(.02,.7,.95)*witness*energy*.6;\n }\n // Silence collapses new light to black; residual trails dissipate, never add up.\n color=min(color,vec3(2.));color=max(color,memory);\n gl_FragColor=vec4(color,1.);\n}"},{"id":"faultline","name":"Faultline","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\n#define PI 3.14159265359\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nfloat hash(float x){return fract(sin(x*127.1+41.7)*43758.5453);}\nfloat wave(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}\nvec3 splitColor(float x){return mix(vec3(.96,.025,.06),vec3(.015,.72,.95),smoothstep(-.35,.35,sin(x)));}\nfloat buckle(float a){\n float pressure=energy*.035+beat*.023;\n float fold=sin(a*5.+travel*.6)*sin(a*3.-travel*.27);\n return pressure*fold+.005*sin(a*2.+travel*.13);\n}\nvec3 filaments(vec2 p,float lane){\n float a=atan(p.y,p.x),r=length(p);\n float radius=.278+energy*.04+buckle(a);\n radius+=lane*(.0065+peak*.002)*sin(a*2.+travel*.17+lane*.2);\n radius+=wave(fract(a/TAU+.5))*.011*sin(a*17.-travel*.7);\n float dispersion=.001+peak*.005+beat*.003;\n vec3 d=vec3(length(p+vec2(dispersion,0.)),r,length(p-vec2(dispersion,0.)))-radius;\n float width=.00065+.00065*energy;\n return vec3(width*width)/(d*d+width*width)+exp(-abs(d)*230.)*.025;\n}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n vec2 raw=p;p=rotate(p,.07*sin(time*.07));p.y*=1.+.04*sin(time*.09);\n float r=length(p),a=atan(p.y,p.x);\n vec2 memoryUV=(uv-.5)*(1.+dt*(.025+energy*.035))+.5;\n vec3 memory=texture2D(previous,memoryUV).rgb*exp(-dt*9.);\n vec3 color=vec3(0.);\n // Polygonal pressure fronts: spatially sparse and low-luminance, never a full-screen flash.\n float angular=cos(mod(a+PI/5.+travel*.025,TAU/5.)-PI/8.);\n float polygon=r*angular;\n float outside=smoothstep(.30,.40,r)*(1.-smoothstep(.72,1.05,r));\n for(int i=0;i<5;i++){\n float offset=float(i)/5.;float phase=fract(travel*.105+offset);\n float front=.34+phase*.60;\n float envelope=wave(phase*.7);\n float line=exp(-abs(polygon-front)*650.);\n float broken=pow(max(0.,sin(a*7.+offset*19.+travel*.19)),4.);\n color+=splitColor(a*2.+offset*13.)*line*broken*outside*envelope*energy*(1.-phase)*.32;\n }\n // Narrow, angular splinters. Deterministic trajectories, not random frame flicker.\n float sector=floor((a+PI)/TAU*48.);\n float local=fract((a+PI)/TAU*48.)-.5;\n float seed=hash(sector);float flight=fract(travel*(.10+seed*.09)+seed*5.);\n float shardR=.35+flight*.42;\n float shardAngle=local-(flight-.5)*.16*sin(sector);\n float shard=exp(-abs(shardAngle)*140.)*exp(-abs(r-shardR)*100.);\n float shardPower=pow(peak,2.)*(.2+beat*.8)*step(.65,seed)*(1.-flight);\n color+=splitColor(sector*2.1)*shard*shardPower*.75;\n // Broad dark chamber walls, visible only where audio presses against the ring.\n float walls=pow(max(0.,cos(a*4.+.4)),16.)*exp(-abs(r-.46)*22.);\n color+=splitColor(a*2.)*walls*energy*.007;\n if(r>.18&&r<.43){\n vec2 q=rotate(p,.0872665);\n for(int i=0;i<8;i++){\n float lane=(float(i)-3.5)*1.35;\n vec3 f=filaments(q,lane);\n float spec=pow(max(0.,cos(a*3.+lane*.27+travel*.18)),12.);\n vec3 metal=mix(vec3(.27,.30,.34),vec3(.8,.86,.92),spec);\n vec3 edge=splitColor(a+lane*.38+travel*.13);\n vec3 tint=mix(metal,edge,.23+peak*.17);\n color+=f*tint*(.008+energy*.65+peak*.25)*(.4+spec*1.7);\n }\n float center=.278+energy*.04+buckle(a);\n float halfWidth=.027+peak*.008;\n float d=r-center;float mask=1.-smoothstep(halfWidth,halfWidth+.003,abs(d));\n float normal=sqrt(max(0.,1.-pow(d/halfWidth,2.)));\n float reflection=pow(max(0.,sin(normal*5.+a*3.+travel*.2)),16.);\n color+=vec3(.6,.67,.75)*mask*reflection*(.005+energy*.23);\n float rim=exp(-abs(abs(d)-halfWidth)*850.);\n color+=splitColor(a+travel*.12)*rim*(.006+energy*.21);\n // Audio-only segmented crescent stays legible beneath the more violent material.\n float witness=exp(-abs(r-center-halfWidth-.009)*800.)*pow(max(0.,-sin(a)),20.);\n color+=vec3(.02,.7,.95)*witness*energy*.6;\n }\n // Silence collapses new light to black; residual trails dissipate, never add up.\n color=min(color,vec3(2.));color=max(color,memory);\n gl_FragColor=vec4(color,1.);\n}"},{"id":"ion-wire","name":"Ion Wire","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\nvec3 palette(float a){return .5+.5*cos(TAU*(a+vec3(.04,.34,.62)));}\nvec2 rotate(vec2 p,float a){return mat2(cos(a),-sin(a),sin(a),cos(a))*p;}\nfloat wave(float a){return texture2D(history,vec2(fract(a/TAU+.5),.5)).r;}\nvec3 filament(vec2 p,float lane){\n float a=atan(p.y,p.x),r=length(p);float t=travel*.4;\n float envelope=wave(a);\n float twist=sin(a*3.+t)*.008+cos(a*5.-t*.7)*.004;\n float shape=sin(a*7.+t*1.3)*sin(a*3.-t*.8);\n float radius=.285+energy*.043+beat*.012;\n radius+=twist+energy*shape*.019;\n radius+=lane*(.0065+energy*.0015)*sin(a*2.+t*.4+lane*.17);\n radius+=envelope*.012*sin(a*13.-time*.8);\n float shift=.0015+peak*.0024;\n vec3 d=vec3(length(p+vec2(shift,0.)),r,length(p-vec2(shift,0.)))-radius;\n d-=vec3(1.,0.,-1.)*(.0009+peak*.0012)*sin(a+t);\n float width=.0005+.0007*energy+.0002*cos(a*4.+lane);\n vec3 core=vec3(width*width)/(d*d+width*width);\n vec3 mist=exp(-abs(d)*180.)*.06;\n return core+mist;\n}\nvoid main(){\n vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);\n // Slow precession of the glass surface, independent of its audio response.\n p=rotate(p,.1*sin(time*.08));p.x*=1.+.045*sin(time*.11);p.y*=1.-.035*sin(time*.11);\n float a=atan(p.y,p.x),r=length(p);\n vec2 oldUV=(uv-.5)*1.0005+.5;\n vec3 memory=texture2D(previous,oldUV).rgb*exp(-dt*7.);\n if(r<.18||r>.43){gl_FragColor=vec4(memory,1.);return;}\n vec3 color=vec3(0.);\n vec2 q=rotate(p,.0872665);\n for(int i=0;i<8;i++){\n float lane=(float(i)-3.5)*1.35;\n float depth=.5+.5*sin(a*2.+travel*.16+lane*.23);\n vec3 tint=mix(vec3(.01,.35,1.),vec3(.6,.06,.9),.5+.5*sin(a+lane*.13+travel*.13));\n tint*=1.15;\n // Three genuinely displaced field samples create refractive channel fringes.\n vec3 lines=filament(q,lane);\n float caustic=pow(.5+.5*cos(a*3.-lane*.3-travel*.4),8.);\n color+=lines*tint*(.045+energy*.30+peak*.13)*(.35+depth*.8+caustic*.8);\n }\n // A dark lens membrane ties the filaments together into one ring.\n float center=.285+energy*.043+beat*.012+sin(a*3.+travel*.4)*.008+cos(a*5.-travel*.28)*.004;\n float width=.028+energy*.01;\n float annulus=smoothstep(width+.003,width,abs(r-center));\n float radial=(r-center)/width;\n float curved=sqrt(max(0.,1.-radial*radial));\n float reflection=pow(max(0.,sin(a*2.+curved*4.+travel*.15)),14.);\n color+=mix(vec3(.08,.24,.48),vec3(.7,.42,.2),.5+.5*sin(a+time*.04))*annulus*(.012+energy*.07)*curved;\n color+=vec3(.6,.8,1.)*reflection*annulus*(.025+peak*.28);\n // The bottom crescent is a true level witness: entirely off without live audio.\n float bottom=pow(max(0.,-sin(a)),24.);\n float witness=exp(-abs(r-center-width-.012)*750.)*bottom;\n color+=vec3(.30,.85,1.)*witness*energy*.75;\n // A transient releases an expanding, quickly dissipating wavefront.\n float pulseRadius=center+.037+(1.-beat)*.055;\n float pulse=exp(-abs(r-pulseRadius)*400.)*beat;\n color+=palette(a*.09+.58)*pulse*.15;\n // Motion memory: decay is frame-rate independent and cannot accumulate brightness.\n color=max(color,memory);\n gl_FragColor=vec4(color,1.);\n}"},{"id":"oscilloscope-tunnel","name":"Oscilloscope Tunnel","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\nfloat line(float d,float w){return w*w/(d*d+w*w);}\nfloat hist(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}\nvec3 palette(float x){return .5+.5*cos(TAU*(vec3(.02,.35,.62)+x));}\nvoid 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;\n\n// Nested rounded-square traces fly toward the viewer, driven by recorded envelopes.\nfloat square=pow(pow(abs(p.x),4.)+pow(abs(p.y),4.),.25);\nfor(int i=0;i<12;i++){float f=float(i);float phase=fract(f/12.+travel*.075);float radius=.055+phase*phase*.85;\nfloat wobble=sin(a*6.+travel+f*.7)*hist(fract(a/TAU+.5))*.014*phase;\nfloat trace=line(square-radius-wobble,.0007+phase*.0015);\nfloat gate=.3+.7*pow(.5+.5*sin(a*4.-travel*.7+f),3.);\nc+=palette(f*.055+travel*.018)*trace*gate*power*smoothstep(0.,.12,phase)*(1.-phase)*.9;}\n\nvec3 memory=texture2D(previous,(uv-.5)*(1.+dt*.025)+.5).rgb*exp(-dt*7.);\nc=min(c,vec3(1.6));gl_FragColor=vec4(max(c,memory),1.);}"},{"id":"orbital-dust","name":"Orbital Dust","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\nfloat line(float d,float w){return w*w/(d*d+w*w);}\nfloat hist(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}\nvec3 palette(float x){return .5+.5*cos(TAU*(vec3(.02,.35,.62)+x));}\nvoid 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;\n\n// Small luminous bodies on tilted elliptical tracks: no full-frame flashes.\nfor(int i=0;i<32;i++){float f=float(i),phase=f*2.399963+travel*(.12+mod(f,5.)*.018);float radius=.12+mod(f,8.)*.043;\nvec2 q=vec2(cos(phase)*radius,sin(phase)*radius*.52);float tilt=f*.37; q=mat2(cos(tilt),-sin(tilt),sin(tilt),cos(tilt))*q;\nq*=1.+energy*.22;float d=length(p-q);float width=.0015+peak*.002;\nc+=palette(f*.073+travel*.015)*line(d,width)*power*1.1;\nfloat orbit=length(vec2(p.x,p.y*1.15));c+=palette(f*.073)*line(orbit-radius,.0004)*power*.006;}\n\nvec3 memory=texture2D(previous,(uv-.5)*(1.+dt*.025)+.5).rgb*exp(-dt*7.);\nc=min(c,vec3(1.6));gl_FragColor=vec4(max(c,memory),1.);}"},{"id":"liquid-lattice","name":"Liquid Lattice","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\nfloat line(float d,float w){return w*w/(d*d+w*w);}\nfloat hist(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}\nvec3 palette(float x){return .5+.5*cos(TAU*(vec3(.02,.35,.62)+x));}\nvoid 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;\n\n// Two warped line families interfere like a flexible wire mesh.\nvec2 q=p; q.x+=sin(p.y*9.+travel*.45)*(.025+energy*.07);q.y+=sin(p.x*8.-travel*.32)*(.025+energy*.06);\nfloat wx=sin(q.x*45.+travel*.25),wy=sin(q.y*45.-travel*.2);\nfloat gx=line(wx,.025),gy=line(wy,.025);\nfloat mask=exp(-r*r*5.)*(1.-smoothstep(.48,.72,r));\nfloat ripple=.5+.5*sin(r*28.-travel*1.8+hist(clamp(r,0.,1.))*2.);\nc=(palette(q.x*.5+travel*.015)*gx+palette(q.y*.5+.35)*gy)*mask*power*(.12+ripple*.28);\nc+=vec3(.5,.8,.9)*gx*gy*mask*beat*.45;\n\nvec3 memory=texture2D(previous,(uv-.5)*(1.+dt*.025)+.5).rgb*exp(-dt*7.);\nc=min(c,vec3(1.6));gl_FragColor=vec4(max(c,memory),1.);}"},{"id":"eclipse-corona","name":"Eclipse Corona","source":"precision highp float;\nvarying vec2 uv;uniform vec2 resolution;uniform sampler2D previous,history;uniform float time,energy,peak,beat,travel,dt;\n#define TAU 6.28318530718\nfloat line(float d,float w){return w*w/(d*d+w*w);}\nfloat hist(float x){return texture2D(history,vec2(clamp(x,.002,.998),.5)).r;}\nvec3 palette(float x){return .5+.5*cos(TAU*(vec3(.02,.35,.62)+x));}\nvoid 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;\n\n// Dark central disk, an amber limb and slowly shearing fine coronal arcs.\nfloat radius=.215+energy*.025;\nfloat edge=line(r-radius,.0012+peak*.001);\nc+=vec3(1.,.45,.08)*edge*power*.85;\nfor(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.);\nfloat reach=.02+energy*.06+hist(fract(a/TAU+.5))*.035;\nfloat d=r-radius-f*.003;\nfloat halo=exp(-max(0.,d)/(reach+.001))*smoothstep(-.001,.006,d);\nc+=mix(vec3(1.,.22,.025),vec3(.4,.15,.65),f/7.)*rays*halo*power*.13;}\nc*=smoothstep(radius-.004,radius,r);\n\nvec3 memory=texture2D(previous,(uv-.5)*(1.+dt*.025)+.5).rgb*exp(-dt*7.);\nc=min(c,vec3(1.6));gl_FragColor=vec4(max(c,memory),1.);}"}];
let settings={index:4,seconds:60,playing:true,shuffle:false};
try{const saved=JSON.parse(localStorage.getItem('prism-presets')||'null');if(saved&&typeof saved==='object'){if(Number.isInteger(saved.index)&&saved.index>=0&&saved.index<presetDefinitions.length)settings.index=saved.index;if(Number.isFinite(saved.seconds))settings.seconds=Math.max(10,Math.min(600,saved.seconds));if(typeof saved.playing==='boolean')settings.playing=saved.playing;if(typeof saved.shuffle==='boolean')settings.shuffle=saved.shuffle}}catch{}
let activeIndex=settings.index,nextDue=performance.now()+settings.seconds*1000,transitionStart=-Infinity;
function persist(){try{localStorage.setItem('prism-presets',JSON.stringify({...settings,index:activeIndex}))}catch{}}
const post=`precision highp float;varying vec2 uv;uniform sampler2D image,transitionImage;uniform float transition,energy,beat,intensity,travel;uniform vec2 resolution;
vec3 sampleAt(vec2 d){float zoom=.76+energy*.12+beat*.08;vec2 drift=vec2(0.);float angle=sin(travel*.18)*intensity*.16;vec2 q=uv-.5-drift;q.x*=resolution.x/resolution.y;q=mat2(cos(angle),-sin(angle),sin(angle),cos(angle))*q;q.x/=resolution.x/resolution.y;return texture2D(image,q/zoom+.5+d/resolution).rgb;}
void main(){vec3 c=sampleAt(vec2(0));vec3 glow=vec3(0.);
vec2 p=(uv*resolution-.5*resolution)/min(resolution.x,resolution.y);float r=length(p),a=atan(p.y,p.x);
float drive=smoothstep(.20,.85,intensity);float angular=pow(.5+.5*sin(a*9.+sin(r*8.-travel)*2.+travel*.35),8.);
float clouds=pow(.5+.5*sin(p.x*6.+sin(p.y*8.+travel*.4)*1.6+travel*.6),3.);
vec3 tint=mix(vec3(.025,.22,.55),vec3(.5,.045,.12),.5+.5*sin(travel*.22+a));
// Sustained energy fills the surroundings; onsets animate sparse fronts, not screen flashes.
c+=tint*drive*drive*(angular*.11+clouds*.055)*(1.-exp(-r*5.));
float fronts=pow(.5+.5*cos(r*38.-travel*4.),24.);c+=tint*fronts*(.03*drive+.20*beat)*smoothstep(.22,.4,r)*exp(-r*1.8);
c*=1.+beat*.45;
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*.028;c=mix(texture2D(transitionImage,uv).rgb,c,smoothstep(0.,1.,transition));c=1.-exp(-c*1.65);c=pow(c,vec3(1.04));vec2 edge=min(uv,1.-uv);float safeEdge=smoothstep(0.,.075,min(edge.x,edge.y));gl_FragColor=vec4(c*safeEdge,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 programCache=new Map();const programs=presetDefinitions.map(d=>{if(!programCache.has(d.source))programCache.set(d.source,makeProgram(d.source));return programCache.get(d.source)});let sceneProgram=programs[activeIndex];const 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;const transitionTexture=texture();
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.bindTexture(gl.TEXTURE_2D,transitionTexture);gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA,width,height,0,gl.RGBA,gl.UNSIGNED_BYTE,null);gl.bindFramebuffer(gl.FRAMEBUFFER,null);transitionStart=-Infinity}addEventListener('resize',resize);resize();
const uniformSets=programs.map(p=>{const out={};for(const n of ['resolution','previous','history','time','energy','peak','beat','travel','dt','mode','bodyLevel','edgeLevel'])out[n]=gl.getUniformLocation(p,n);return out});let u=uniformSets[activeIndex];const postU={};for(const n of ['image','resolution','transitionImage','transition','energy','beat','intensity','travel'])postU[n]=gl.getUniformLocation(postProgram,n);
function selectPreset(index){index=((Math.trunc(index)%programs.length)+programs.length)%programs.length;if(index===activeIndex)return;gl.activeTexture(gl.TEXTURE2);gl.bindTexture(gl.TEXTURE_2D,transitionTexture);gl.bindFramebuffer(gl.FRAMEBUFFER,targets[read].framebuffer);gl.copyTexSubImage2D(gl.TEXTURE_2D,0,0,0,0,0,width,height);for(const t of targets){gl.bindFramebuffer(gl.FRAMEBUFFER,t.framebuffer);gl.clearColor(0,0,0,1);gl.clear(gl.COLOR_BUFFER_BIT)}gl.bindFramebuffer(gl.FRAMEBUFFER,null);activeIndex=index;sceneProgram=programs[index];u=uniformSets[index];transitionStart=performance.now();nextDue=transitionStart+settings.seconds*1000;persist()}
function nextPreset(direction=1){let index=activeIndex+direction;if(settings.shuffle&&direction>0)index=(activeIndex+1+Math.floor(Math.random()*(programs.length-1)))%programs.length;selectPreset(index)}
function pauseRotation(){settings.playing=!settings.playing;nextDue=performance.now()+settings.seconds*1000;persist();return settings.playing}
function setIntervalSeconds(seconds){if(!Number.isFinite(seconds))return;settings.seconds=Math.max(10,Math.min(600,seconds));nextDue=performance.now()+settings.seconds*1000;persist()}
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 fastEnvelope=0,slowEnvelope=0,intensity=0;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;if(settings.playing&&now>=nextDue)nextPreset();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)));fastEnvelope+=(target-fastEnvelope)*(1-Math.exp(-dt*45));slowEnvelope+=(target-slowEnvelope)*(1-Math.exp(-dt*1.8));const onset=Math.max(0,fastEnvelope-slowEnvelope-.012);beat=Math.max(beat*Math.exp(-dt*7.5),Math.min(1,onset*8.));intensity+=(energy-intensity)*(1-Math.exp(-dt*1.5));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,mode:presetDefinitions[activeIndex].mode||0,bodyLevel:energy,edgeLevel:peak}))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.activeTexture(gl.TEXTURE2);gl.bindTexture(gl.TEXTURE_2D,transitionTexture);gl.uniform1i(postU.transitionImage,2);gl.uniform1f(postU.transition,Math.min(1,(now-transitionStart)/1800));for(const[n,v]of Object.entries({energy,beat,intensity,travel}))gl.uniform1f(postU[n],v);gl.drawArrays(gl.TRIANGLES,0,6);read=write;
}requestAnimationFrame(frame);
addEventListener('dblclick',()=>{if(!document.fullscreenElement)document.documentElement.requestFullscreen?.()});
addEventListener('keydown',e=>{if(e.metaKey||e.ctrlKey||e.altKey)return;const k=e.key.toLowerCase();if(k==='f'){if(document.fullscreenElement)document.exitFullscreen?.();else document.documentElement.requestFullscreen?.()}else if(k==='arrowright'||k==='m')nextPreset();else if(k==='arrowleft')nextPreset(-1);else if(k===' '||k==='p')pauseRotation();else if(k==='[')setIntervalSeconds(settings.seconds-15);else if(k===']')setIntervalSeconds(settings.seconds+15);else if(k==='s'){settings.shuffle=!settings.shuffle;persist()}else if('1234567890-='.includes(k)&&k.length===1)selectPreset('1234567890-='.indexOf(k));else return;e.preventDefault()});
window.prismPresets={list:()=>presetDefinitions.map(({id,name},index)=>({id,name,index})),select:selectPreset,next:nextPreset,pause:pauseRotation,setInterval:setIntervalSeconds,shuffle:()=>{settings.shuffle=!settings.shuffle;persist()},settings:()=>({...settings,index:activeIndex,remainingSeconds:Math.max(0,(nextDue-performance.now())/1000)})};
window.prismHealth=()=>({ready:true,width,height,source,status,samples,lastRms,energy,peak,beat,intensity,fastEnvelope,slowEnvelope,ageMs:Math.round(performance.now()-lastSample),averageFps:Math.round(frames/totalDt),preset:presetDefinitions[activeIndex].id,presetCount:programs.length,rotation:settings.playing,intervalSeconds:settings.seconds});
</script></body></html>