<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>FURTILE</title>
<meta name=viewport content="width=device-width,initial-scale=1">
<style>html,body{margin:0;height:100%;background:#000;overflow:hidden}canvas{display:block;position:absolute;left:0;top:50%;transform:translateY(-50%);width:100vw;height:min(100vh,56.25vw);cursor:pointer}
#h{position:fixed;top:0;right:0;box-sizing:border-box;max-width:100vw;min-width:64px;min-height:40px;padding:7px 10px;font:min(13px,3vw)/1.45 ui-monospace,Menlo,Consolas,monospace;color:#fff;text-align:right;white-space:pre-wrap;cursor:pointer;-webkit-user-select:none;user-select:none}
#t{position:fixed;left:0;right:0;top:50%;transform:translateY(-50%);text-align:center;color:#fff;font:200 min(11vw,19vh)/1 "Helvetica Neue",Helvetica,Arial,sans-serif;letter-spacing:.32em;text-indent:.32em;pointer-events:none;text-shadow:0 0 40px #000;transition:opacity .8s}
#t small{display:block;position:absolute;left:0;right:0;top:50%;margin-top:min(8.6vw,15.3vh);font:400 min(1.5vw,2.6vh)/1 ui-monospace,Menlo,Consolas,monospace;letter-spacing:.3em;opacity:.8;color:#fff;text-shadow:0 0 12px #000}</style>
</head>
<body>
<canvas id=c></canvas><div id=t>FURTILE<small>click to start · F fullscreen</small></div><div id=h></div>
<script>
/* FURTILE - hair and cloth, and a way through them.
Five scenes, each in a room of its own: a fall of hair; five braids that plait themselves and then swing into each other; a
curtain of red satin with something leaning far into it from behind; ringlets; a rope twisted and flicked until it throws a loop,
and then pulled tight.
The piece opens inside the first - a shutter over the whole picture flying open - and from then on only goes inward. Every scene
ends with a cube of glass arriving in it and getting in the way of what is there: it pushes through the hair; it ploughs along
the row of braids; it is what was leaning into the curtain; it comes up among the ringlets; it drops on the tight rope, and
is thrown up by it. The next scene is to be seen inside that cube, and the camera goes in. Inside the fifth cube is a
dark room with one more cube in it, not of glass but of tiles, which every bass drum throws about; its tiles turn out of the
way, and what was shut up in it - a solid of twenty faces, covered in fur - is thrown about with it, as a head is shaken, until
the fur lets go: tuft by tuft it bursts out through the cube and comes down on it as spiders, forty of them - tarantulas - which walk
about the cube as its tiles turn back, and rear when the claps stop them. The camera goes down into the black of one tile; and in the black a spider sits in the middle
of its web, which is spun as we watch, shaken by the bass drum, and at last left to come to rest. Then the shutter closes, and
it begins again.
Where the music has claps the picture is cut on them: to another time in the same scene's flight, and back.
The piece's name stands before it and after it, written in fur - a brown bear's.
Everything that moves is moved here, on the page (chains of points for hair, nets of points for cloth and for the web, feet and
knees for the spiders); the card only draws.
Keys: F fullscreen; M the meter; left and right, a scene back or on. */
(()=>{
'use strict';
/* ---------------- synth (runs inside an AudioWorklet) ---------------- */
function SYN(sr,b0,b1,solo){
// A hundred beats a minute, in G minor, in the manner of a club record of the early 2000s: a long low bass drum, a clap on two and
// four, tight hats that break into rolls, a sub bass that slides, short low stabs that keep hitting the same chord and now and then
// the one a semitone above it, a rim and a wood block off the beat, a crowd of one shouting "hey". Nothing sings above it.
// The bass drum, the bass, the stabs and the shout are the same all through. The rest of the kit is changed a little for every scene
// (KIT, below), and every scene is come into its own way:
// the braids (bar 16) after a full stop - the last beat is not there;
// the red cloth (28) after a bar in which the beat stops for a run on a snare drum, twice as fast and twice as fast again under a
// rising hiss, then a gap with a shout in it, and then everything at once, the bass falling into its note;
// the ringlets (44) as a tape that runs down and stops, is pushed to and fro by hand, and is wound on fast to where it should be;
// the rope (60) on one stroke of the snare said again, and again in half the time, and again;
// the last cube's room (68) out of four bars heard as if through a wall - in the first of them the rope is pulled tight, and hums;
// the spiders (76), with the first kit again, on a record scratched;
// the web (84) after half a bar played backwards, dying away.
// And the end: for two bars (88, 89) the music goes away, quieter and duller, as if that were all; then, on the one of bar 90, a blow -
// bass drum, crash, the chord, the shout - and nothing after it but its ringing, until the four claps that bring the beginning round.
// Those that are done to the sound itself (stop, tape, repeat, scratch, backwards) are done after everything else: what has been
// played is kept, a few seconds of it, and what is heard is read out of that - at another speed, or from another place, or not at
// all - while the music itself goes on in time underneath, so that it is in its place again on the bar line.
const BPM=100,NBAR=92;
// how hard the beat goes in each bar: 0 nothing, 1 a bass drum on the one, 2 the verse, 3 the hook, 4 the hook at its hardest
const lv=bar=>bar<4?1:bar<20?2:bar<36?3:bar<52?2:bar<64?3:bar<68?1:bar<76?2:bar<88?4:1,
// which scene a bar belongs to (hair, braids, red cloth, ringlets, rope, the last cube, the spiders, the web)
SCB=[16,28,44,60,68,76,84],sty=bar=>{let k=0;while(k<7&&bar>=SCB[k])k++;return k},
// Each scene's kit: what is struck on two and four (0 the clap, 1 a snare drum) - that snare's pitch, how fast it dies away, how much
// of it is its wires, whether it is brushed; the hats (0 six squares, 1 cymbals, 2 a shaker) - how long a cymbal rings, how loud;
// what is played off the beat (0 rim and block, 1 two congas, 2 the snare, quietly, 3 a cowbell, 4 rim and block and a scuttling of
// ticks, 5 rim and block with a long echo); how bright the stabs.
KIT=[[0,1,1,1.5,0, 0,.021,1, 0,1],[0,1,1,1.5,0, 2,.021,1, 1,1.15],[1,1,1,1.5,0, 1,.021,1, 2,.8],[1,1.3,1.5,1.3,0, 1,.014,.9, 3,1.3],
[1,.76,.8,1.5,0, 1,.045,.85, 2,.7],[1,.9,.6,1.2,1, 1,.018,.7, 2,.9],[0,1,1,1.5,0, 0,.021,1, 4,1.2],[0,1,1,1.5,0, 0,.021,1, 5,1]];
// What is struck on each sixteenth (the picture reads this too): 1 the bass drum, 2 the clap or the snare, 4 a hat, 8 an open hat,
// 16 whatever is played off the beat, 32 a stab, 64 a shout. In the last half of every fourth bar the beat breaks off for a run into the next one.
const hit=st=>{const s=st&15,bar=st>>4,two=bar&1,l=lv(bar),turn=(bar&3)==3&&s>=8,k=sty(bar);let h=0;
if(bar==27){if(s==0)h|=1;if(!(s&3)&&s<12||s==10||s==12||s==13)h|=2;return h}
if(l==1){const out=bar>=88&&bar<90;if(s==0&&bar!=91)h|=1;if(bar>1&&bar<4&&!(s&1)||out&&!(s&3))h|=4;if(bar>=64&&bar<68&&(s==3||s==6||s==11||s==14)||out&&(s==6||s==14))h|=16;if(turn&&s>=12)h|=2;return h}
if(turn){if(s==8)h|=1;if(s>=12||s==10)h|=2;h|=4;return h}
if(l==2){if(s==0||s==7||s==10||two&&s==3)h|=1;if(s==4||s==12)h|=2;if(!(s&1)||two&&s==15||KIT[k][5])h|=4;if(two&&s==14)h|=8;if((bar>=36||k==1)&&(s==2||s==5||s==9||s==13))h|=16;if(!two&&s==0)h|=32}
if(l>=3){if(s==0||s==3||s==10||(two?s==7||l>3&&s==14:s==6))h|=1;if(s==4||s==12)h|=2;h|=4;if(s==6&&two||s==14)h|=8;if(s==2||s==9||s==13||two&&s==5)h|=16;
if(s==0||s==3||s==6||s==10)h|=32;if(two&&s==12)h|=64}
return h};
if(!sr)return{BPM,NBAR,hit,lv,sty};
// b0, b1: play from bar b0, and at bar b1 go round to b0 again. solo is for the test: only the parts whose bits are set are heard
// (1 kick, 2 clap and snare, 4 hats, 8 whatever is off the beat, 16 the sub, 64 stabs, 128 the voice, 256 the rise and the crash; 1024 no echo and no room;
// 2048 before the master; 4096 nothing done to the sound at the ways in).
b0=b0||0;b1=b1||NBAR;
const TAU=6.283185307179586,T=1/sr,S16=sr*15/BPM,SW=.035,mf=n=>440*2**((n-69)/12),dc=s=>Math.exp(-T/s),lag=s=>1-Math.exp(-T/s),
on=b=>solo&&!(solo&b)?0:1,MK=on(1),MS=on(2),MH=on(4),MC=on(8),MB=on(16),MT=on(64),MV=on(128),MX=on(256),DRY=solo&1024,RAW=solo&2048,NOFX=solo&4096;
let rs=20051130;const rnd=()=>{rs^=rs<<13;rs^=rs>>>17;rs^=rs<<5;return(rs>>>0)/2147483648-1};
// a filter that returns its low-pass and leaves its band-pass in s[2]; fs sets where it turns and how sharply. hp: what is left above
const F=()=>new Float64Array(7),
fs=(s,fc,k)=>{const g=Math.tan(Math.PI*Math.min(fc,sr*.45)*T);s[3]=1/(1+g*(g+k));s[4]=g*s[3];s[5]=g*s[4];s[6]=k;return s},
fl=(s,x)=>{const v3=x-s[1],v1=s[3]*s[0]+s[4]*v3,v2=s[1]+s[4]*s[0]+s[5]*v3;s[0]=2*v1-s[0];s[1]=2*v2-s[1];s[2]=v1;return v2},
hp=(s,x)=>{const y=fl(s,x);return x-s[6]*s[2]-y},
saw=(p,d)=>{let v=2*p-1;if(p<d){const t=p/d;v-=t+t-t*t-1}else if(p>1-d){const t=(p-1)/d;v-=t*t+t+t+1}return v};
// The sub bass, two bars at a time: step, note, how many steps, and whether it slides up to the note from the one before. V the verse's, H the hook's.
const SV=[0,31,6,0, 7,31,3,0, 10,34,5,1, 16,31,3,0, 19,31,4,0, 23,29,3,0, 26,31,5,1],
SH=[0,31,3,0, 3,31,3,0, 6,32,4,1, 10,31,5,0, 16,31,3,0, 19,31,3,0, 22,29,3,0, 26,34,3,1, 29,36,3,1],
// the stabs of the hook, a bar at a time: step, how far above the chord (in semitones)
ST=[0,0,3,0,6,1,10,0],ST2=[0,0,3,0,6,-2,10,0],
// what the voice shouts: for each sound the three resonances of the mouth, how much voice, how much hiss and where the hiss sits
PH={EH:[530,1840,2480,1,0,0],IY:[270,2290,3010,1,0,0],AX:[620,1200,2500,1,0,0],HH:[500,1500,2500,.15,.5,1800],_:[400,1500,2500,0,0,0]},
WD=[['HH',.04,'EH',.09,'IY',.12,'_',.05],['HH',.03,'AX',.16,'_',.05]];
let pos=0,nx=0,st=b0*16-1,
kt=-1,kp=0,kv=1,kd=4,duck=0,dks=0,
ct=-1,cv=0,sne=0,sp=0,sst=0,
he=0,hd=.99,hv=0,hr=0,hrn=0,
gt=-1,gf=300,gp=0,gv=1,gpn=0,gdl=.25,gdc=38,
bf=49,bft=49,bp=0,be=0,bg=0,bv=1,bgv=0,
zt=-1,zv=0,zg=0,zp1=0,zp2=0,zr=1,zk=1,zw=1.5,zb=0,z2=0,zn=0,KK=KIT[0],
xe=0,xk=0,xv=0,xd=.99,
qt=-1,qv=0,ut=-1,uf=200,up=0,un=0,yt=-1,yp1=0,yp2=0,wt=-1,sk=0,skn=0,pt=-1,pp=0,
ste=0,stv=1,
vw=null,vi=0,vt=0,va=0,vn=0,vat=0,vnt=0,vp1=0,vp2=0,vp3=0,vf1=400,vf2=1500,vf3=2500,vg1=400,vg2=1500,vg3=2500,
rz=0,rzt=0,cr=0,
lpw=Math.log(2e4),lpt=2e4,
di=0,ea=0,eb=0,cenv=0,cg=1,og=1,ogt=1,
tm=0,tq=null,ti=0,tn=0,tl=1,tk=0,tr=1,tdr=0,tg=1,tdg=0,th=0,ta=0,tll=1;
const snb=fs(F(),1700,.7),snh=fs(F(),5200,1.1),clb=fs(F(),1150,.55),hf=fs(F(),7200,.9),
stl=fs(F(),1400,.9),v1=fs(F(),400,.11),v2=fs(F(),1500,.1),v3=fs(F(),2500,.1),vnb=fs(F(),1800,.5),rzf=fs(F(),800,.35),crf=fs(F(),6000,1),
mL=fs(F(),2e4,1.2),mR=fs(F(),2e4,1.2),sf=new Float64Array(4),sph=new Float64Array(8),hq=[205.3,304.4,369.6,522.7,540,800].map(f=>f*1.52*T),hph=new Float64Array(6),hb=fs(F(),9800,.5);
const zh=fs(F(),780,1.2),zl=fs(F(),7800,1.1),xh=fs(F(),7000,.8),xb=fs(F(),10200,.45),xq=[3111,4217,5303,6449,7723,9001].map(f=>f*T),xph=new Float64Array(6),xkd=dc(.0022),xdo=dc(.19),
qf=fs(F(),6200,.9),yf=fs(F(),2640,.6),wf=fs(F(),900,.45),wh=fs(F(),3500,.9),pf=fs(F(),520,.35);
const sdn=dc(.06),hdc=dc(.016),hdo=dc(.13),dkd=dc(.14),dka=lag(.003),bat=lag(.004),brl=dc(.07),bgl=lag(.035),
std=dc(.085),vla=lag(.008),cat=lag(.012),crl=lag(.2),cu=1-Math.exp(-32*T/.05);
// stereo echo (a dotted eighth) and a short room
const DN=Math.round(S16*3),eL=new Float32Array(DN),eR=new Float32Array(DN);
const mk=(a,o)=>a.map(n=>new Float32Array(Math.round((n+o)*sr/44100)));
const cl=[1116,1188,1277,1356,1422,1491,1557,1617],al=[556,441,341,225];
const cL=mk(cl,0),cR=mk(cl,23),aL=mk(al,0),aR=mk(al,23),ci=new Int32Array(16),ai=new Int32Array(8),cf=new Float64Array(16);
const verb=(x,C,A,o)=>{let y=0;
for(let j=0;j<8;j++){const b=C[j],q=o*8+j,i=ci[q],v=b[i];cf[q]=v*.7+cf[q]*.3;b[i]=x+cf[q]*.78;ci[q]=i+1==b.length?0:i+1;y+=v}
for(let j=0;j<4;j++){const b=A[j],q=o*4+j,i=ai[q],v=b[i];b[i]=y+v*.5;ai[q]=i+1==b.length?0:i+1;y=v-y}
return y};
// what has been played, kept (some five seconds of it), to be read out of at the ways in
const RM=(1<<18)-1,RBL=new Float32Array(RM+1),RBR=new Float32Array(RM+1);
const kick=(v,long)=>{kt=0;kp=0;kv=v;kd=long?2.6:4.2;duck=Math.max(duck,v)},
clap=v=>{ct=0;cv=v;sne=v;sp=0;sst=0},
hat=(v,open)=>{he=1;hv=v;hd=open?hdo:hdc},
// the snare (g: a quiet stroke, between the beats; K: whose kit) and the cymbals (dt: how long one rings) - each stroke a little louder or softer, longer or shorter than the last
snare=(v,g,K)=>{zt=0;zv=v*(.94+.06*rnd());zg=g;zp1=zp2=0;zr=K[1]*(1+.012*rnd());zk=K[2];zw=K[3];zb=K[4]},
hat2=(v,open,dt)=>{xe=xk=1;xv=v*(.88+.12*rnd());xd=open?xdo:dc(dt*(1+.3*rnd()));fs(xh,open?5600:7000,.8)},
shk=v=>{qt=0;qv=v*(.9+.1*rnd())},
cng=(f,pan)=>{ut=0;uf=f*(1+.01*rnd());up=0;un=pan},
// rim, block, tick: how high, how loud, where, how much of it into the echo, how fast it dies away
rim=(f,v,pan,dl,dcy)=>{gt=0;gf=f;gp=0;gv=v;gpn=pan;gdl=dl;gdc=dcy},
sub=(n,len,v,sl)=>{bft=mf(n);if(!sl||be<.01)bf=bft;bg=Math.round(len*S16);bv=v;bgv=bgl},
stab=(v,up)=>{const c=[43,46,50,55];for(let j=0;j<4;j++)sf[j]=mf(c[j]+(up||0));ste=1;stv=v},
say=k=>{vw=WD[k];vi=-2;vt=0},
// A way in that is done to the sound: from now to the bar line (tot samples) what is heard is read out of what has been played. In
// order, each for so many sixteenths (the last for whatever is left): [n,0] nothing; [n,1,a,b,g,h] read on from where the reading
// is, at a speed going from a to b (less than nothing: backwards), and a loudness going from g to h; [n,2] read fast enough to be level
// with the music at the end of it; [n,3,len,b] the sixteenth before this began, or the first len of it, round and round, its speed going from 1 to b.
go=(tot,segs)=>{if(NOFX)return;tq=segs.map(g=>[Math.round(g[0]*S16),...g.slice(1)]);let sum=0;for(let i=0;i<tq.length-1;i++)sum+=tq[i][0];tq[tq.length-1][0]=Math.max(64,Math.round(tot)-sum);
ti=-1;tn=0;tm=1;th=pos-1;ta=pos-Math.round(S16*(1+SW))};
const trig=()=>{
const s=st&15,bar=st>>4,two=bar&1,c=two*16+s,l=lv(bar),h=hit(st),turn=(bar&3)==3&&s>=8,land=!(bar&3)&&s==0&&bar>0,bld=bar==27,S=sty(bar),K=KK=KIT[bld?2:S];
// where the whole is muffled: at the very start the beat comes up out of the dark; in the break; and at the end, where it goes back into
// the dark, and quieter too (og) - until the blow, which is all there at once
lpt=bar<4?240*2**((bar+s/16)*1.6):bar>=64&&bar<68?(bar<67?700:700*2**((s/16)*4.8)):bar>=88&&bar<90?2e4*2**(-(bar-88+s/16)*3):2e4;
ogt=bar>=88&&bar<90?1-.3*(bar-88+s/16):1;if(bar==90&&s==0){og=1;lpw=Math.log(2e4);cr=1;say(0);stab(1,0)}
if(h&1)kick(land?1:s==0||s==8?.95:.82,l==1||land);
if(h&2){const v=bld?.42+.04*s:turn?.5+.035*s:1;K[0]?snare(v,0,K):clap(v)}
// (a hat on the beat, on the half beat, or between)
if(h&4){const v=s&1?0:s&3?1:2;K[5]==1?hat2([.3,.62,.85][v]*K[7],0,K[6]):K[5]==2?shk([.3,.44,.55][v]):hat([.26,.36,.48][v],0)}
if(h&8)K[5]==1?hat2(.8*K[7],1,K[6]):hat(.5,1);
// the hats' rolls: thirty-seconds through the last beat of a turn, and here and there in the hook; and the snare's, in the bar before the red cloth
hr=!bld&&(turn&&s>=12||l>=3&&two&&s==11)?1:0;z2=bld&&(s==12||s==13)?1:0;
if(h&16){const P=l>1?K[8]:0;
if(P==1)cng(s==9?196:s==5?262:330,s&2?.35:-.35);else if(P==2||P==3&&s==9)snare(.2+.05*(s&3),1,K);else if(P==3)yt=0;
else rim(s==2||s==13?620:s==9?415:830,.8,s&2?.4:-.4,K[8]==5?.9:.25,38)}
// the spiders' scuttling: ticks, two to a sixteenth, here and there, now on one side and now on the other
sk=S==6&&!turn&&(s==6||s==7||s==14||s==15)?1:0;if(sk)rim(2500+500*rnd(),.45,rnd()*.8,.1,130);
// the whip: in the rope's scene, as each flick passes the middle of the rope - a rushing that climbs through this sixteenth's first half, and the crack
if(S==4&&s==15&&bar>60&&bar<64)wt=0;
// and the rope pulled tight, once: half way through the first bar of the break
if(bar==64&&s==8){pt=0;pp=0}
if(h&32){if(l>=3){const R=(bar&3)==2?ST2:ST;for(let i=0;i<R.length;i+=2)if(R[i]==s)stab(l>3?1:.85,R[i+1])}else stab(.6,0)}
if(h&64||bld&&s==14)say(0);
if(l==1&&bar>=64&&s==8&&two)say(1);
// the sub bass
if(bld){if(s==0)sub(31,3,1,0)}
else if(l==1){if(s==0&&bar!=91)sub(31,bar<4||bar>=88?12:10,.9,0)}
else if(!turn){const R=l>=3?SH:SV;for(let i=0;i<R.length;i+=4)if(R[i]==c)sub(R[i+1],R[i+2],1,R[i+3])}
else if(s==8)sub(29,7,.9,1);
if(bar==28&&s==0){bf=bft*3;bgv=lag(.1)}
// into the turn a rise, and where the next phrase lands a crash
if(bld){rzt=s<14?(s+2)/15:0;if(s==14)rz=0}else if(turn&&s==8)rzt=1;if(land){rzt=0;rz=0;cr=l>1?1:.5}
// the ways in that are done to the sound (see the top)
if(bar==15&&s==12||bar==43&&s==8||bar==59&&s==13||bar==75&&s==12||bar==83&&s==8){let e=nx;for(let j=s+1;j<16;j++)e+=S16*(j&1?1-SW:1+SW);
go(e-pos,bar==15?[[0,0]]:bar==43?[[2,1,1,0],[2.6,0],[.3,1,2.2,2.2],[.35,1,-1.6,-1.6],[.3,1,2.6,2.6],[.35,1,-1.4,-1.4],[0,2]]:bar==59?[[1,3,1,1],[1,3,.5,1.08],[0,3,.25,1.26]]:
bar==75?[[.45,1,-2.2,-1.2],[.5,1,1.8,2.6],[.4,1,-2.6,-1.5],[.45,1,2.4,1.4],[.35,1,-3,-2],[.4,1,2.2,3],[.3,1,-2.4,-2.4],[.55,1,2,3.2],[0,0]]:[[6.6,1,-1,-1.5,1,.1],[0,0]])}
};
// the slow things, thirty-two samples at a time
const ctl=()=>{
lpw+=(Math.log(lpt)-lpw)*cu;const c=Math.exp(lpw);fs(mL,c,1.2);fs(mR,c,1.2);
fs(stl,260+3600*KK[9]*ste*ste,.75);
vf1+=(vg1-vf1)*.32;vf2+=(vg2-vf2)*.32;vf3+=(vg3-vf3)*.32;fs(v1,vf1,.11);fs(v2,vf2,.1);fs(v3,vf3,.1);
rz+=(rzt-rz)*.004;fs(rzf,300+5200*rz*rz,.3);og+=(ogt-og)*.02;
if(wt>=0)fs(wf,900*Math.pow(8,Math.min(1,wt/.072)),.45);
};
const f=(L,R)=>{
for(let i=0;i<L.length;i++,pos++){
if(pos>=nx){st++;if(st>=b1*16)st=b0*16;const d=S16*(st&1?1-SW:1+SW);nx+=d;trig();hrn=hr?pos+d*.5:0;zn=z2?pos+d*.5:0;skn=sk?pos+d*.5:0}
if(hrn&&pos>=hrn){KK[5]==1?hat2(.36*KK[7],0,KK[6]):KK[5]==2?shk(.3):hat(.3,0);hrn=0}
if(zn&&pos>=zn){snare(.46+.04*(st&15),0,KK);zn=0}
if(skn&&pos>=skn){rim(2500+500*rnd(),.4,rnd()*.8,.1,130);skn=0}
if(!(pos&31))ctl();
const n=rnd(),n2=rnd();let l=0,r=0,m=0,rv=0,dly=0;
// the bass drum: a click, and a sine falling from it to a long low note, pushed into a soft ceiling. Everything low leans back while it sounds
if(kt>=0){kp+=(47+120*Math.exp(-kt*36)+400*Math.exp(-kt*330))*T;
const y=Math.sin(TAU*kp)*Math.min(1,kt*3000)*Math.exp(-kt*kd)+n*Math.exp(-kt*700)*.25;m+=Math.tanh(y*kv*3)*.84*MK;if((kt+=T)>1.4)kt=-1}
duck*=dkd;dks+=(duck-dks)*dka;
// the clap: three slaps and a tail, with a little of a snare's skin under it
if(ct>=0){const e=(ct<.03?Math.exp(-(ct%.01)*300):Math.exp(-(ct-.03)*24))*cv;fl(clb,n2);fl(snb,n);sst+=T;sp+=(185+70*Math.exp(-sst*60))*T;sne*=sdn;
const y=(clb[2]*e*.9+Math.tanh((Math.sin(TAU*sp)*sne*.5+(snb[2]*1.2+hp(snh,n)*.4)*e)*1.4)*.55)*MS;l+=y*.9;r+=y;rv+=y*.3;if((ct+=T)>.4)ct=-1}
// the snare drum: its skin - two notes that begin high and fall at once - the wires under it, which hiss longer, and the stick; brushed, there is
// little skin and no stick, and the wires come up slowly
if(zt>=0){const pe=Math.exp(-zt*85);zp1+=186*zr*(1+.55*pe)*T;zp2+=332*zr*(1+.25*pe)*T;
const w=fl(zl,hp(zh,n2)),y=Math.tanh(((Math.sin(TAU*zp1)*Math.exp(-zt*(zg?34:19)*zk)+.45*Math.sin(TAU*zp2)*Math.exp(-zt*30*zk))*Math.min(1,zt*2500)*(zb?.3:.85)
+w*(.6*Math.exp(-zt*(zg?60:30)*zk)+.4*Math.exp(-zt*(zg?26:9.5)*zk))*zw*(zg?.6:1)*Math.min(1,zt*(zb?55:9e3))+(zb?0:n*Math.exp(-zt*900)*.5))*1.5*zv)*.62*MS;
l+=y*.92;r+=y;rv+=y*(zg?.15:.34);if((zt+=T)>.7)zt=-1}
// cymbals: three pairs of squares, each pair multiplied together, and hiss - all but the top of it taken off; a tick where the stick lands
if(xe>.0004){xe*=xd;xk*=xkd;let o=0;for(let j=0;j<6;j+=2){xph[j]+=xq[j];if(xph[j]>=1)xph[j]-=1;xph[j+1]+=xq[j+1];if(xph[j+1]>=1)xph[j+1]-=1;o+=(xph[j]<.5)==(xph[j+1]<.5)?1:-1}
const hh=hp(xh,o*.2+n*.6);fl(xb,hh);const y=(hh+xb[2]*.8)*(xe+xk*.7)*xv*.2*MH;l+=y*.78;r+=y;rv+=y*.07}
// hats: six squares that do not agree, and what is left of them above everything else
if(he>.0005){he*=hd;let o=0;for(let j=0;j<6;j++){hph[j]+=hq[j];if(hph[j]>=1)hph[j]-=1;o+=hph[j]<.5?1:-1}
fl(hb,o*.3+n*.5);const y=hp(hf,hb[2]*1.4+n*.25)*he*hv*.42*MH;l+=y*.85;r+=y}
// a shaker: hiss that comes up quickly and goes
if(qt>=0){fl(qf,n2);const y=qf[2]*Math.min(1,qt/.007)*Math.exp(-qt/.032)*qv*.55*MH;l+=y;r+=y*.8;rv+=y*.05;if((qt+=T)>.25)qt=-1}
// rim, wood block, tick: a short high knock
if(gt>=0){gp+=gf*(1+.5*Math.exp(-gt*160))*T;const y=(Math.sin(TAU*gp)+.4*Math.sin(TAU*gp*2.71))*Math.exp(-gt*gdc)*Math.min(1,gt*4000)*.2*gv*MC;l+=y*(1-gpn);r+=y*(1+gpn);rv+=y*.35;dly+=y*gdl;if((gt+=T)>.2)gt=-1}
// a conga: a skin that falls a little in pitch as it rings, and the slap of the hand
if(ut>=0){up+=uf*(1+.4*Math.exp(-ut*60))*T;const y=Math.tanh((Math.sin(TAU*up)*Math.exp(-ut*13)+n*Math.exp(-ut*600)*.35)*1.3)*Math.min(1,ut*3000)*.18*MC;l+=y*(1-un);r+=y*(1+un);rv+=y*.3;if((ut+=T)>.4)ut=-1}
// a cowbell: two squares a fifth and a bit apart, through a narrow place
if(yt>=0){yp1+=540*T;if(yp1>=1)yp1-=1;yp2+=800*T;if(yp2>=1)yp2-=1;fl(yf,(yp1<.5?1:-1)+(yp2<.5?1:-1));
const y=yf[2]*(.75*Math.exp(-yt*70)+.25*Math.exp(-yt*9))*Math.min(1,yt*4000)*.14*MC;l+=y*1.1;r+=y*.8;rv+=y*.25;dly+=y*.15;if((yt+=T)>.45)yt=-1}
// the whip: hiss through a narrow place that climbs, louder and louder, and then the crack
if(wt>=0){const u=wt/.072;fl(wf,n);const y=(u<1?wf[2]*u*u*.5:hp(wh,n2)*Math.exp(-(wt-.072)*85)+wf[2]*Math.exp(-(wt-.072)*40)*.6)*.3*MC;l+=y*.8;r+=y;rv+=y*.4;dly+=y*.3;if((wt+=T)>.35)wt=-1}
// the rope pulled tight: a low string whose note climbs an octave as it tightens, and hums on; and its fibres creaking as they take the strain
if(pt>=0){const u=Math.min(1,pt/.09),e=Math.exp(-pt*3.6)*Math.min(1,pt*300);pp+=49*(1+u*u*(3-2*u))*T;fl(pf,n2);
const y=Math.tanh((Math.sin(TAU*pp)+.45*Math.sin(TAU*pp*2)+.2*Math.sin(TAU*pp*3))*e*1.5)*.34*MC,c=pf[2]*(Math.sin(TAU*pt*(38+500*pt))>0?1:.15)*Math.exp(-pt*16)*.3*MC;
m+=y;l+=c;r+=c*.8;rv+=(y+c)*.25;if((pt+=T)>1.4)pt=-1}
// the sub: a sine that slides to its note, held, and pushed into a soft ceiling so that small speakers get its overtones
if(bg>0||be>.0005){if(bg>0){bg--;be+=(bv-be)*bat}else be*=brl;bf+=(bft-bf)*bgv;
bp+=bf*T;if(bp>=1)bp-=1;const s=Math.sin(TAU*bp);m+=Math.tanh((s+.22*Math.sin(2*TAU*bp))*be*2.3)*.46*(1-.55*dks)*MB}
// stabs: four saws struck together and choked
if(ste>.001){ste*=std;let o=0;for(let j=0;j<4;j++){sph[j]+=sf[j]*T;if(sph[j]>=1)sph[j]-=1;sph[j+4]+=sf[j]*1.006*T;if(sph[j+4]>=1)sph[j+4]-=1;o+=saw(sph[j],sf[j]*T)+saw(sph[j+4],sf[j]*T)}
const sb=Math.tanh(fl(stl,o+n*ste*1.5)*.9)*ste*stv*.5*MT;l+=sb;r+=sb*.9;rv+=sb*.45;dly+=sb*.2}
// the rise before a turn (noise through a narrowing that climbs) and the crash where it ends
if(rz>.01){fl(rzf,n);const y=rzf[2]*rz*rz*.16*MX;l+=y;r+=y*.9;rv+=y*.3}
if(cr>.001){cr*=.99993;const y=hp(crf,n2)*cr*cr*.2*MX;l+=y*.8;r+=y;rv+=y*.2}
// the voice: a flat chord of saws through three resonances that move from one shape of the mouth to the next, and breath
if(vw){if(vt<=0){vi+=2;if(vi>=vw.length){vw=null;vat=vnt=0}else{const p=PH[vw[vi]];vg1=p[0];vg2=p[1];vg3=p[2];vat=p[3];vnt=p[4];if(p[5])fs(vnb,p[5],.5);vt=vw[vi+1]}}vt-=T}
va+=(vat-va)*vla;vn+=(vnt-vn)*vla;
if(va>.001||vn>.001){vp1+=98*T;if(vp1>=1)vp1-=1;vp2+=146.83*T;if(vp2>=1)vp2-=1;vp3+=196.6*T;if(vp3>=1)vp3-=1;
const x=saw(vp1,98*T)+.55*saw(vp2,147*T)+.75*saw(vp3,197*T);fl(v1,x);fl(v2,x);fl(v3,x);fl(vnb,n);
const y=Math.tanh((v1[2]+v2[2]*.8+v3[2]*.45)*va*.55+vnb[2]*vn*.7)*.5*MV;l+=y*.95;r+=y;rv+=y*.4;dly+=y*.35}
// echo
{const a=eL[di],b=eR[di];ea+=(b-ea)*.4;eb+=(a-eb)*.4;eL[di]=dly+ea*.35;eR[di]=eb*.35;if(++di==DN)di=0;if(!DRY){l+=a*.5;r+=b*.5;rv+=(a+b)*.1}}
// the room
if(!DRY){l+=verb(rv*.03+1e-18,cL,aL,0)*1.1;r+=verb(rv*.03+1e-18,cR,aR,1)*1.1}
let oL=fl(mL,(m+l)*og),oR=fl(mR,(m+r)*og);
// the whole held together: when it is loud it is turned down a little and let up again slowly; then a soft ceiling
{const pk=Math.max(Math.abs(oL),Math.abs(oR));cenv+=(pk-cenv)*(pk>cenv?cat:crl);const t=cenv>.7?(.7+(cenv-.7)*.5)/cenv:1;cg+=(t-cg)*.002}
if(!RAW){oL=Math.tanh(oL*cg*.9)*.98;oR=Math.tanh(oR*cg*.9)*.98}
// That is what is played, and it is kept. What is heard is that - or, at a way in, something read out of what has been kept (th: where
// the reading is; every piece of it, and every time round, is brought in and let out over a moment, so that nothing clicks).
RBL[pos&RM]=oL;RBR[pos&RM]=oR;
if(tm){
if(tn<=0){if(++ti>=tq.length)tm=0;else{const g=tq[ti];tk=g[1];tn=tl=g[0];tg=1;tdg=tdr=0;
if(tk==1){tr=g[2];tdr=(g[3]-g[2])/tl;if(g[4]!=null){tg=g[4];tdg=(g[5]-g[4])/tl}}
else if(tk==2)tr=1+(pos-1-th)/tl;
else if(tk==3){tll=Math.round(g[2]*S16);th=ta;tr=1;tdr=(g[3]-1)/tl}}}
if(tm){
if(!tk){const e=Math.max(0,1-(tl-tn)/64);oL*=e;oR*=e}
else{let e=Math.min(1,(tl-tn)/64,tn/64);if(tk==3){if(th>=ta+tll)th-=tll;e=Math.min(e,(th-ta)/48,(ta+tll-th)/48)}
if(th>pos-1)th=pos-1;const i0=Math.floor(th),u=th-i0,a=i0&RM,b=i0+1&RM;e*=tg;
oL=(RBL[a]+(RBL[b]-RBL[a])*u)*e;oR=(RBR[a]+(RBR[b]-RBR[a])*u)*e;th+=tr;tr+=tdr;tg+=tdg}
tn--}}
L[i]=oL;R[i]=oR;
}
return pos};
return f;
}
/* ---------------- the solid, and the road it rolls ---------------- */
const TAU=6.283185307179586,cl=(x,a,b)=>Math.min(b,Math.max(a,x)),mix=(a,b,t)=>a+(b-a)*t,ss=(a,b,x)=>{x=cl((x-a)/(b-a),0,1);return x*x*(3-2*x)};
const add=(a,b)=>[a[0]+b[0],a[1]+b[1],a[2]+b[2]],sub=(a,b)=>[a[0]-b[0],a[1]-b[1],a[2]-b[2]],mul=(a,s)=>[a[0]*s,a[1]*s,a[2]*s],
dot=(a,b)=>a[0]*b[0]+a[1]*b[1]+a[2]*b[2],cross=(a,b)=>[a[1]*b[2]-a[2]*b[1],a[2]*b[0]-a[0]*b[2],a[0]*b[1]-a[1]*b[0]],
len=a=>Math.hypot(a[0],a[1],a[2]),nrm=a=>mul(a,1/(len(a)||1)),mix3=(a,b,t)=>[mix(a[0],b[0],t),mix(a[1],b[1],t),mix(a[2],b[2],t)];
// a turn about the unit axis k by the angle a, as nine numbers row by row; a matrix times a vector; a matrix times a matrix
const rot=(k,a)=>{const c=Math.cos(a),s=Math.sin(a),t=1-c,x=k[0],y=k[1],z=k[2];return[t*x*x+c,t*x*y-s*z,t*x*z+s*y,t*x*y+s*z,t*y*y+c,t*y*z-s*x,t*x*z-s*y,t*y*z+s*x,t*z*z+c]},
mv=(m,v)=>[m[0]*v[0]+m[1]*v[1]+m[2]*v[2],m[3]*v[0]+m[4]*v[1]+m[5]*v[2],m[6]*v[0]+m[7]*v[1]+m[8]*v[2]],
mm=(a,b)=>{const o=[];for(let r=0;r<3;r++)for(let c=0;c<3;c++)o.push(a[r*3]*b[c]+a[r*3+1]*b[3+c]+a[r*3+2]*b[6+c]);return o};
// Where a thing is: a turning r and a shift t (a point p of it stands at r p + t). pAt: where; pDir: which way; pRot: the same thing
// turned about the line through the point q along k
const pAt=(P,p)=>add(mv(P.r,p),P.t),pDir=(P,d)=>mv(P.r,d),pRot=(P,q,k,a)=>{const R=rot(k,a);return{r:mm(R,P.r),t:add(q,mv(R,sub(P.t,q)))}};
// The cube: eight corners, six faces - in the order it will show them (each: its four corners, counted anticlockwise seen from
// outside; its middle; the way it faces; two directions in its own plane), and for each face the four it shares an edge with.
const EDGE=.525,HE=EDGE/2,IV=[],IF=[];
for(let i=0;i<8;i++)IV.push([i&1?HE:-HE,i&2?HE:-HE,i&4?HE:-HE]);
for(const n of[[0,0,1],[1,0,0],[0,0,-1],[0,1,0],[-1,0,0],[0,-1,0]]){const e1=[n[1],n[2],n[0]],e2=cross(n,e1),c=mul(n,HE),v=[];
for(let i=0;i<8;i++)if(dot(IV[i],n)>0)v.push(i);
const an=i=>Math.atan2(dot(sub(IV[i],c),e2),dot(sub(IV[i],c),e1));v.sort((a,b)=>an(a)-an(b));
IF.push({v,c,n,e1,e2,nb:[]})}
for(let f=0;f<6;f++)for(let g=0;g<6;g++)if(f!=g){const e=IF[f].v.filter(v=>IF[g].v.includes(v));if(e.length==2)IF[f].nb.push({g,e})}
const NFC=6,ROAD=[0,1,2,3,4,5],TILT=Math.PI/2;
// Where the cube stands with each face of the road in front (in front: flat against the plane z = 0, facing +z, which is toward us),
// and the hinge it turns on to bring the next: an edge, and an angle. Turning over one edge after another like that it goes along a
// road on the plane, and the six places where it has stood are a net of the cube.
const TRAIL=[],HINGE=[];
{const f=IF[ROAD[0]],r=[...f.e1,...f.e2,...f.n];let P={r,t:mul(mv(r,f.c),-1)};
for(let i=0;i<NFC;i++){TRAIL.push(P);if(i==NFC-1)break;
const e=IF[ROAD[i]].nb.find(x=>x.g==ROAD[i+1]).e,q=pAt(P,IV[e[0]]),k=nrm(sub(pAt(P,IV[e[1]]),q)),n=IF[ROAD[i+1]].n;
const a=pDir(pRot(P,q,k,TILT),n)[2]>pDir(pRot(P,q,k,-TILT),n)[2]?TILT:-TILT;HINGE.push({e,a});P=pRot(P,q,k,a)}
// its first step is to the right, and the whole road is put in the middle
const C=TRAIL.map((P,i)=>pAt(P,IF[ROAD[i]].c)),d=sub(C[1],C[0]),a=-Math.atan2(d[1],d[0]);
for(let i=0;i<NFC;i++)TRAIL[i]=pRot(TRAIL[i],[0,0,0],[0,0,1],a);
const X=[],Y=[];TRAIL.forEach((P,i)=>IF[ROAD[i]].v.forEach(v=>{const p=pAt(P,IV[v]);X.push(p[0]);Y.push(p[1])}));
const o=[-(Math.min(...X)+Math.max(...X))/2,-(Math.min(...Y)+Math.max(...Y))/2,0];
for(let i=0;i<NFC;i++)TRAIL[i]={r:TRAIL[i].r,t:add(TRAIL[i].t,o)}}
const SPOT=TRAIL.map((P,i)=>pAt(P,IF[ROAD[i]].c)); // the middle of each place it stands
// Each face is a window. What is seen through one has its own middle - the middle of the face - and its own directions: right, up
// and toward us, as its face stands when it is in front. frameOf: those, for face m of the road standing as P.
// fAt: where a place of the scene is; fDir: which way one of its directions points; fIn: a place of the world, as the scene counts it
const SCN=ROAD.map((f,m)=>{const r=TRAIL[m].r;return{o:IF[f].c,x:[r[0],r[1],r[2]],y:[r[3],r[4],r[5]],z:[r[6],r[7],r[8]]}}),
frameOf=(m,P)=>{const s=SCN[m];return{o:pAt(P,s.o),x:pDir(P,s.x),y:pDir(P,s.y),z:pDir(P,s.z)}},
fAt=(F,q)=>[F.o[0]+F.x[0]*q[0]+F.y[0]*q[1]+F.z[0]*q[2],F.o[1]+F.x[1]*q[0]+F.y[1]*q[1]+F.z[1]*q[2],F.o[2]+F.x[2]*q[0]+F.y[2]*q[1]+F.z[2]*q[2]],
fDir=(F,d)=>[F.x[0]*d[0]+F.y[0]*d[1]+F.z[0]*d[2],F.x[1]*d[0]+F.y[1]*d[1]+F.z[1]*d[2],F.x[2]*d[0]+F.y[2]*d[1]+F.z[2]*d[2]],
fIn=(F,w)=>{const d=sub(w,F.o);return[dot(d,F.x),dot(d,F.y),dot(d,F.z)]},F0={o:[0,0,0],x:[1,0,0],y:[0,1,0],z:[0,0,1]};
// the camera's two matrices (sixteen numbers, column by column) and their product
const persp=(fy,as,n,f)=>{const t=1/Math.tan(fy/2);return[t/as,0,0,0,0,t,0,0,0,0,(f+n)/(n-f),-1,0,0,2*f*n/(n-f),0]},
look=(e,c,u)=>{const z=nrm(sub(e,c)),x=nrm(cross(u,z)),y=cross(z,x);return[x[0],y[0],z[0],0,x[1],y[1],z[1],0,x[2],y[2],z[2],0,-dot(x,e),-dot(y,e),-dot(z,e),1]},
m4=(a,b)=>{const o=new Float32Array(16);for(let c=0;c<4;c++)for(let r=0;r<4;r++)o[c*4+r]=a[r]*b[c*4]+a[4+r]*b[c*4+1]+a[8+r]*b[c*4+2]+a[12+r]*b[c*4+3];return o};
/* ---------------- GL setup ---------------- */
const C=document.getElementById('c'),gl=C.getContext('webgl2',{antialias:false,alpha:false,powerPreference:'high-performance'});
const XF=gl&&gl.getExtension('EXT_color_buffer_float'),XH=XF||gl&&gl.getExtension('EXT_color_buffer_half_float');
if(!XH){document.body.innerHTML='<p style="color:#fff;font:16px sans-serif;padding:2em">This demo needs WebGL2 with floating-point render targets.</p>';return}
gl.getExtension('KHR_parallel_shader_compile');
const F4=[gl.RGBA16F,gl.RGBA,gl.HALF_FLOAT],UQ=new URLSearchParams(location.search);
// What the graphics card was last told is remembered, and it is told again only what has changed
let CP=null,AT=-1,VW=-1,VH=-1;const TB=[],raw0=()=>{gl.activeTexture(gl.TEXTURE0);AT=0;TB[0]=null};
// a texture that can be drawn into (dep: with a depth buffer of its own)
const fld=(w,h,f,lin,dep)=>{const t=gl.createTexture(),fi=lin?gl.LINEAR:gl.NEAREST;raw0();gl.bindTexture(gl.TEXTURE_2D,t);
gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,fi);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,fi);
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);
gl.texImage2D(gl.TEXTURE_2D,0,f[0],w,h,0,f[1],f[2],null);
const fb=gl.createFramebuffer();gl.bindFramebuffer(gl.FRAMEBUFFER,fb);gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,t,0);let d=null;
if(dep){d=gl.createRenderbuffer();gl.bindRenderbuffer(gl.RENDERBUFFER,d);gl.renderbufferStorage(gl.RENDERBUFFER,gl.DEPTH24_STENCIL8,w,h);gl.framebufferRenderbuffer(gl.FRAMEBUFFER,gl.DEPTH_STENCIL_ATTACHMENT,gl.RENDERBUFFER,d)}
return{t,f:fb,d,w,h,fm:f}};
const free=o=>{if(o){gl.deleteTexture(o.t);gl.deleteFramebuffer(o.f);if(o.d)gl.deleteRenderbuffer(o.d)}};
// a texture of numbers worked out here (four to a texel), for a shader to look things up in; and the same, given new numbers
const data=(w,h,a)=>{const t=gl.createTexture();raw0();gl.bindTexture(gl.TEXTURE_2D,t);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA32F,w,h,0,gl.RGBA,gl.FLOAT,a);return{t,w,h}},
redata=(o,a,r0,r1)=>{raw0();gl.bindTexture(gl.TEXTURE_2D,o.t);r0=r0||0;r1=r1==null?o.h:r1;if(r1>r0)gl.texSubImage2D(gl.TEXTURE_2D,0,0,r0,o.w,r1-r0,gl.RGBA,gl.FLOAT,a,r0*o.w*4)}; // (rows r0 to r1 only)
const VS=`#version 300 es
void main(){gl_Position=vec4(vec2(gl_VertexID&1,gl_VertexID>>1)*4.-1.,0,1);}`;
const prog=(fs,vs)=>{const p=gl.createProgram(),sh=[];
for(const[ty,src]of[[gl.VERTEX_SHADER,vs||VS],[gl.FRAGMENT_SHADER,fs]]){const s=gl.createShader(ty);gl.shaderSource(s,src);gl.compileShader(s);gl.attachShader(p,s);sh.push(s)}
gl.linkProgram(p);return{p,sh,u:null,c:{}}};
const errs=[];
const use=o=>{
if(!o.u){o.u={};
if(!gl.getProgramParameter(o.p,gl.LINK_STATUS)){const e=gl.getProgramInfoLog(o.p)+'\n'+gl.getShaderInfoLog(o.sh[0])+'\n'+gl.getShaderInfoLog(o.sh[1]);errs.push(e);console.error('shader',e)}
for(let i=0,n=gl.getProgramParameter(o.p,gl.ACTIVE_UNIFORMS);i<n;i++){const a=gl.getActiveUniform(o.p,i);o.u[a.name.replace('[0]','')]=gl.getUniformLocation(o.p,a.name)}}
if(CP!=o){gl.useProgram(o.p);CP=o}return o};
// set uniforms from an object: a number is a float, {i} an int, {m} a 3x3 or 4x4 matrix, {t} a texture (given the next free unit), {v3} a list of threes, an array one vector
const same=(a,b)=>{if(!a||a.length!=b.length)return 0;for(let i=0;i<b.length;i++)if(a[i]!==b[i])return 0;return 1};
const su=(o,un,k)=>{const u=o.u,c=o.c;k=k||0;for(const n in un){const l=u[n],v=un[n];if(l==null||v==null)continue;
if(typeof v=='number'){if(c[n]!==v){gl.uniform1f(l,v);c[n]=v}}
else if(v.t){if(TB[k]!=v.t){if(AT!=k){gl.activeTexture(gl.TEXTURE0+k);AT=k}gl.bindTexture(gl.TEXTURE_2D,v.t);TB[k]=v.t}if(c[n]!==k){gl.uniform1i(l,k);c[n]=k}k++}
else if(v.i!=null){if(c[n]!==v.i){gl.uniform1i(l,v.i);c[n]=v.i}}
else{const a=v.m||v.v3||v;if(!same(c[n],a)){v.m?a.length>9?gl.uniformMatrix4fv(l,false,a):gl.uniformMatrix3fv(l,false,a):v.v3||a.length==3?gl.uniform3fv(l,a):a.length==2?gl.uniform2fv(l,a):gl.uniform4fv(l,a);c[n]=Array.from(a)}}}
return k};
let W=0,H=0;
const CA0=[gl.COLOR_ATTACHMENT0];
const vport=(w,h)=>{if(VW!=w||VH!=h){gl.viewport(0,0,w,h);VW=w;VH=h}};
// into a target (or the screen): to(dst), then draws; pass: one full-screen draw of a program
const to=(dst,keep)=>{gl.bindFramebuffer(gl.FRAMEBUFFER,dst?dst.f:null);vport(dst?dst.w:W,dst?dst.h:H);if(dst&&!keep)gl.invalidateFramebuffer(gl.FRAMEBUFFER,CA0)};
const pass=(o,dst,un,keep)=>{use(o);to(dst,keep);su(o,un);gl.bindVertexArray(null);gl.drawArrays(gl.TRIANGLES,0,3)};
// a list of corners that changes from frame to frame: so many numbers to a corner, laid out as the shader's inputs (their sizes in order)
const mesh=(lay,max)=>{const st=lay.reduce((a,b)=>a+b,0),va=gl.createVertexArray(),b=gl.createBuffer(),a=new Float32Array(max*st);
gl.bindVertexArray(va);gl.bindBuffer(gl.ARRAY_BUFFER,b);gl.bufferData(gl.ARRAY_BUFFER,a.byteLength,gl.DYNAMIC_DRAW);let o=0;
lay.forEach((n,i)=>{gl.enableVertexAttribArray(i);gl.vertexAttribPointer(i,n,gl.FLOAT,false,st*4,o*4);o+=n});gl.bindVertexArray(null);return{va,b,a,st,n:0}},
fill=m=>{gl.bindBuffer(gl.ARRAY_BUFFER,m.b);gl.bufferSubData(gl.ARRAY_BUFFER,0,m.a,0,m.n*m.st)},
drawMesh=(m,first,count)=>{gl.bindVertexArray(m.va);gl.drawArrays(gl.TRIANGLES,first||0,count==null?m.n:count)};
/* ---------------- audio ---------------- */
let ctx=null,an=null,node=null,t0=null,fb=null,mode='';
const MUTE=UQ.has('mute'); // (for measuring and for looking: no sound, and the piece runs on its own clock)
const start=async()=>{
if(MUTE)return;
if(ctx){ctx.resume();return}
ctx=new(window.AudioContext||window.webkitAudioContext)({latencyHint:'playback'});ctx.resume();
an=ctx.createAnalyser();an.fftSize=2048;an.smoothingTimeConstant=.35;an.minDecibels=-85;an.maxDecibels=-18;an.connect(ctx.destination);
fb=new Uint8Array(1024);
try{ // data: URL, because Blob URL worklets fail to load from file:// in Chrome
await ctx.audioWorklet.addModule('data:text/javascript;charset=utf-8,'+encodeURIComponent(SYN+`;registerProcessor("s",class extends AudioWorkletProcessor{constructor(){super();this.f=SYN(sampleRate,0,0);this.n=0}process(i,o){const p=this.f(o[0][0],o[0][1]);if(!(this.n++&31))this.port.postMessage([p,currentTime]);return true}})`));
node=new AudioWorkletNode(ctx,'s',{numberOfInputs:0,outputChannelCount:[2]});
node.port.onmessage=e=>{t0=e.data[1]-(e.data[0]-128)/ctx.sampleRate};mode='worklet';
}catch(e){ // no worklet: same synth on the main thread
const f=SYN(ctx.sampleRate,0,0);node=ctx.createScriptProcessor(2048,0,2);
node.onaudioprocess=e=>{const b=e.outputBuffer,p=f(b.getChannelData(0),b.getChannelData(1));t0=e.playbackTime-(p-2048)/ctx.sampleRate};mode='scriptprocessor: '+e;
}
node.connect(an);
};
addEventListener('pointerdown',start);
/* ---------------- shaders ---------------- */
const GH=`#version 300 es
precision highp float;precision highp int;
#define PI 3.14159265
float h21(vec2 p){p=fract(p*vec2(.1031,.1030));p+=dot(p,p.yx+33.33);return fract((p.x+p.y)*p.x);}
float sat(float x){return clamp(x,0.,1.);}
`;
// The wall behind the cube: dark, with a pool of light where the lamp is nearest it
const BGF=GH+`uniform vec3 uCam,uCx,uCy,uCz,uLamp,uLampC;uniform vec2 uR;uniform float uTan,uT;out vec4 O;
void main(){
vec2 q=(gl_FragCoord.xy/uR*2.-1.)*vec2(uR.x/uR.y,1.)*uTan;
vec3 d=normalize(uCx*q.x+uCy*q.y-uCz),p=uCam+d*((-2.4-uCam.z)/d.z),l=p-uLamp;float r2=dot(l,l);
float g=.75+.25*h21(floor(p.xy*140.))*h21(floor(p.yx*37.));
gl_FragDepth=.99999;O=vec4((vec3(.012,.013,.018)+uLampC*.14/(1.+r2*.9))*g,1);}`;
// What is behind everything in a scene: the room it is in - a wall some way back, a ceiling, a floor, and in some a wall at either
// side - in the scene's own directions, lit by the scene's lamp, with the lamp itself as a glow where it stands. It also says how far
// off the room is at each place, so that what goes behind a wall or up into the ceiling is hidden by it.
// uRm: how high the ceiling, how low the floor, how far off the side walls (0: none).
const SBF=GH+`uniform mat4 uVP;uniform vec3 uCam,uCx,uCy,uCz,uSO,uSX,uSY,uSZ,uLS,uLC,uWC,uRm;uniform vec2 uR;uniform float uTan,uZW,uGl;out vec4 O;
void main(){
vec2 q=(gl_FragCoord.xy/uR*2.-1.)*vec2(uR.x/uR.y,1.)*uTan;
vec3 dw=normalize(uCx*q.x+uCy*q.y-uCz),cw=uCam-uSO,c=vec3(dot(cw,uSX),dot(cw,uSY),dot(cw,uSZ)),d=vec3(dot(dw,uSX),dot(dw,uSY),dot(dw,uSZ));
float t=d.z<-1e-4?(uZW-c.z)/d.z:1e4,a=abs(d.y)>1e-4?((d.y>0.?uRm.x:uRm.y)-c.y)/d.y:1e4;vec3 n=vec3(0,0,1);
if(a<t){t=a;n=vec3(0,-sign(d.y),0);}
if(uRm.z>0.&&abs(d.x)>1e-4){a=(sign(d.x)*uRm.z-c.x)/d.x;if(a<t){t=a;n=vec3(-sign(d.x),0,0);}}
t=clamp(t,0.,36.);vec3 p=c+d*t,l=uLS-p;float r2=dot(l,l);
vec2 pc=n.z>.5?p.xy:abs(n.y)>.5?p.xz:p.zy;
float g=1.-.2*exp(-t*.35)*(1.-h21(floor(pc*150.))*h21(floor(pc.yx*41.)));
vec3 col=uWC*g*(.25+.75*mix(1.,max(dot(n,l)*inversesqrt(r2),0.),n.z>.5?0.:.55)/(1.+r2*.6));
// the lamp itself: where the line of sight passes nearest it
vec3 lc=uLS-c;float along=dot(lc,d);vec3 off=lc-d*along;float d2=dot(off,off);col+=uLC*uGl*(.02/(d2+.012)+.1/(d2*5.+1.))*step(0.,along)*step(along,t);
vec4 cp=uVP*vec4(uSO+uSX*p.x+uSY*p.y+uSZ*p.z,1);gl_FragDepth=clamp(cp.z/cp.w*.5+.5,0.,.99999);
O=vec4(col,1);}`;
// A face of the cube, or a cloth, or anything else flat. Inputs: where, which way it faces, which way its weft runs, its own flat
// coordinates, (three numbers not used), and (kind of stuff, a colour shift, a number of its own, one not used).
const SFV=`#version 300 es
layout(location=0)in vec3 aP;layout(location=1)in vec3 aN;layout(location=2)in vec3 aT;layout(location=3)in vec2 aU;layout(location=4)in vec3 aB;layout(location=5)in vec4 aF;
uniform mat4 uVP;out vec3 vP,vN,vT,vB;out vec2 vU;flat out vec4 vF;
void main(){vP=aP;vN=aN;vT=aT;vU=aU;vB=aB;vF=aF;gl_Position=uVP*vec4(aP,1);}`;
/* Satin is drawn thread by thread where a thread is wider than a pixel, and as what threads add up to where it is not.
A thread: round across (so its sides turn away and are darker, and light from behind comes through its edges), and rising and
falling along its length as it goes over and under the others, so that it catches the light where it rides up. In satin a thread
goes over four and under one: long floats that all lie the same way, and shine together.
uOp: how much of what is behind the cloth it hides (1: all).
Kinds: 2 satin, 7 what the fur grows on, 9 a face of a cube of glass, 10 the shutter over the whole picture, 11 a face of the last cube,
12 a face of one of the solids that fly through the ringlets. */
const SFF=GH+`in vec3 vP,vN,vT,vB;in vec2 vU;flat in vec4 vF;const float HE=${HE.toFixed(5)},RA=${(HE*.81).toFixed(5)};
uniform vec3 uCam,uLamp,uLampC,uKey,uKeyC,uAmb,uBxC,uBxH;uniform float uTs,uMd,uOp;uniform mat3 uBxR;uniform vec4 uTl;uniform vec3 uTd,uTc;out vec4 O;
// how much of the lamp's light reaches a place when there is a box about (uBxC its middle, uBxH its half sizes, uBxR into its own directions): none, if the box is on the way
float boxsh(vec3 p,vec3 lp){vec3 o=uBxR*(p-uBxC),d=uBxR*(lp-p),iv=1./(d+vec3(1e-6)),t0=(-uBxH-o)*iv,t1=(uBxH-o)*iv,a=min(t0,t1),b=max(t0,t1);
float tn=max(max(max(a.x,a.y),a.z),0.),tf=min(min(min(b.x,b.y),b.z),1.);return 1.-smoothstep(0.,.05,tf-tn);}
vec3 V,Ll,lc,gLit;float nlamp,gRho,gTw,dd[9];
// a blade of the shutter: black, with a soft sheen along it that moves as it turns; in the shade of the blade lying over it; and a
// fine bright line along its own edge
vec3 blade(int k,vec2 p,float aw){float a=float(k)*.69813+gTw;vec2 q=p-gRho*vec2(cos(a),sin(a));q=vec2(-q.y,q.x)/length(q);
float e=dd[k],sh=1.-.72*exp(min(dd[(k+8)%9],0.)/.02),sn=pow(.5+.5*dot(q,vec2(.5,.866)),4.);
vec3 c=(vec3(.014,.015,.018)+vec3(.075,.08,.095)*sn*(.5+.5*smoothstep(0.,.1,e)))*gLit*sh;
return c+gLit*vec3(.42,.44,.5)*(1.-smoothstep(aw*.5,aw*2.5+.0009,e))*(.35+.65*sh);}
// one thread: its colour; c across it (-1 to 1); sl its slope where it rides over; n the cloth's own normal; a along the thread, b across it
vec3 thread(vec3 col,float c,float sl,vec3 n,vec3 a,vec3 b,float sp,float sn,float tr){
vec3 nn=normalize(n+b*c*.9-a*sl),t=normalize(a+n*sl);
float d=max(dot(nn,uKey),0.),th=dot(t,normalize(uKey+V));
vec3 sc=mix(vec3(1),col*2.5,.55);
vec3 o=col*(uAmb+uKeyC*(d*.85+.08))+uKeyC*sc*sp*pow(max(1.-th*th,0.),sn)*(d*.8+.2);
o+=col*lc*max(dot(nn,Ll),0.);
float tl=dot(t,normalize(Ll+V));o+=lc*sc*sp*.5*pow(max(1.-tl*tl,0.),sn)*max(nlamp,0.);
o+=col*lc*tr*max(-nlamp,0.)*(.3+.7*c*c);
return o*(1.-.4*c*c);}
void main(){
int ty=int(vF.x+.5);
vec3 n=normalize(vN)*(gl_FrontFacing?1.:-1.),T=normalize(vT-n*dot(vT,n)),B=cross(n,T);
V=normalize(uCam-vP);Ll=uLamp-vP;float r2=dot(Ll,Ll);Ll*=inversesqrt(r2);lc=uLampC/(.25+r2);nlamp=dot(n,Ll);if(uBxH.x>0.)lc*=boxsh(vP,uLamp);
if(ty==9){
// A face of a cube that stands in a scene and has the next scene in it: glass. It is drawn twice: first (uMd 0) dark, to be
// there; then, when what is inside it has been drawn where it shows, what the glass itself adds - a bright line along its edges,
// a gleam, and a little of the room thrown back where it is seen aslant (uMd: how much of that).
float eg=HE-max(abs(vU.x),abs(vU.y)),aw=length(fwidth(vU))*.8+1e-5;
if(uMd<=0.){O=vec4(.004,.004,.005,1);return;}
float ln=1.-smoothstep(aw,aw*2.5+.0014,eg),gm=pow(max(dot(n,normalize(uKey+V)),0.),90.),fr=pow(1.-max(dot(n,V),0.),4.);
O=vec4(mix(uAmb*8.+lc*.3,uKeyC,sat(ln+gm)),sat(ln*.75+gm*.6+fr*.3)*uMd);return;}
if(ty==12){
// A solid that flies through the ringlets: glass - a dark tint, a bright line along every edge (vU.x: how far from the edge), a gleam, and more
// of it to be seen where it is seen aslant. Only the faces that are toward us are drawn.
float aw=fwidth(vU.x)*.8+1e-5,ln=1.-smoothstep(aw,aw*2.5+.0012,vU.x),gm=pow(max(dot(n,normalize(uKey+V)),0.),70.),fr=pow(1.-max(dot(n,V),0.),3.);
O=vec4(mix(vec3(.012,.013,.018)+uAmb*2.+lc*(.06+.25*fr),uKeyC*1.1,sat(ln+gm)),sat(.3+ln*.7+gm*.6+fr*.4));return;}
if(ty==7){O=vec4(vec3(.02,.016,.014)*vB*(uAmb*8.+uKeyC*max(dot(n,uKey),0.)*.5+lc*max(nlamp,0.)*.3),1);return;}
if(ty==10){
// The shutter over the whole picture, like a camera's: nine blades, each turning on a pin at the rim and each lying over the
// next, whose curved edges leave a hole between them - round when they are drawn back, closing to nothing as they swing in, and
// the whole turning as it opens. vF.w: how far open.
vec2 p=vU;float aw=length(fwidth(p))*.8+1e-5;gLit=uAmb*4.+uKeyC;
gRho=RA*(1.-vF.w);gTw=asin(min(gRho/(2.3*RA),1.))-1.5708+vF.w*1.3;
for(int k=0;k<9;k++){float a=float(k)*.69813+gTw;dd[k]=length(p-gRho*vec2(cos(a),sin(a)))-RA;}
int top=-1;for(int k=0;k<9;k++)if(dd[k]>0.&&dd[(k+8)%9]<=0.)top=k;
if(top<0){O=vec4(0);return;} // through the hole
// a blade; and at its very edge, part of what is under it - the next blade, or the hole
int un=(top+1)%9;float a=clamp(dd[top]/aw,0.,1.);vec3 col=blade(top,p,aw);
O=dd[un]>0.?vec4(mix(blade(un,p,aw),col,a),1):vec4(col,a);return;}
if(ty==11){
// A face of the last cube: sixty-four tiles, each of which can turn on its own middle line - a quarter turn, and it is edge on and
// out of the way. uTl: how many of them have turned for good (they go in no order); how far a wave of turning has got across the
// cube (uTd: which way it runs; uTc: the cube's middle); how big the cube is. A tile catches the light as it turns. The cube's
// edges stay where they are.
float eg=HE-max(abs(vU.x),abs(vU.y)),fl=gl_FrontFacing?1.:.45;vec2 g=vU/HE*4.,ce=floor(g),f=fract(g)-.5;float aw=fwidth(g.y)*.8+1e-5;
vec3 pc=vP-(T*f.x+B*f.y)*(HE*uTl.z*.25);
float hs=h21(ce+vec2(vF.z*17.3,3.1)),wq=uTl.y*2.7-.35-(dot(pc-uTc,uTd)/(HE*uTl.z*1.75)*.5+.5),
op=max(smoothstep(hs-.07,hs,uTl.x*1.14-.07),smoothstep(0.,.22,wq)*(1.-smoothstep(.5,.85,wq))),an=op*1.5708;
float a=clamp(min(.46*cos(an)-abs(f.y),.46-abs(f.x))/aw,0.,1.),ln=1.-smoothstep(aw*HE*.25,aw*HE*.625+.0016,eg);
vec3 n2=normalize(n*cos(an)+B*sin(an)*(hs>.5?1.:-1.));
vec3 col=vec3(.03,.03,.036)*(uAmb*4.+uKeyC*(.25+.75*max(dot(n2,uKey),0.))+lc*max(dot(n2,Ll),0.)*.5)
+(uKeyC*pow(max(dot(n2,normalize(uKey+V)),0.),30.)+lc*pow(max(dot(n2,normalize(Ll+V)),0.),30.))*.35*sin(an*2.+.15);
col=mix(col*fl,(uKeyC*.5+lc*.2)*fl,ln);a=max(a,ln);
if(a<.02)discard;O=vec4(col,a);return;}
// what is stretched on it: threads to the unit, how wide a thread is in its place, shine and how sharp, how much light comes through, how much a thread rises and falls, the two colours
float tc=230.*uTs,w=.97,sp=.75,sn=120.,tr=.36,kr=.08;vec3 cw=vec3(.36,.008,.04),cf=vec3(.62,.3,.04);
float hs=vF.y*6.283;cw=mix(cw,cw.gbr,.5-.5*cos(hs));cf=mix(cf,cf.brg,.5-.5*cos(hs*.7));
vec2 s=vU*tc;float px=length(fwidth(s)),lod=smoothstep(.3,.8,px),aa=min(px*.6,.25);
ivec2 ce=ivec2(floor(s));
// what the threads add up to
float sna=sn/(1.+5.*kr);
vec3 col=vec3(0),ca=mix(thread(cf,0.,0.,n,T,B,sp,sna,tr),thread(cw,0.,0.,n,B,T,sp,sna,tr),.82)*.82;float al=0.,aaL=1.-(1.-w)*(1.-w);
if(lod<1.){
vec2 f=fract(s)-.5;bool wo=((ce.x*2+ce.y)%5+5)%5!=0;
float mw=1.-smoothstep(w*.5-aa,w*.5+aa,abs(f.x)),mf=1.-smoothstep(w*.5-aa,w*.5+aa,abs(f.y));
// the warp runs along B (up the cloth), the weft along T. A thread on top rises in the middle of the crossing; the one under dips
vec3 cW=thread(cw*(.9+.2*h21(vec2(ce.x,7.))),f.x/(w*.5),(wo?1.:-1.)*kr*3.14*sin(3.14159*f.y),n,B,T,sp,sn,tr),
cF=thread(cf*(.9+.2*h21(vec2(ce.y,3.))),f.y/(w*.5),(wo?-1.:1.)*kr*3.14*sin(3.14159*f.x),n,T,B,sp,sn,tr);
vec3 top=wo?cW:cF,und=(wo?cF:cW)*.6;float mt=wo?mw:mf,mu=wo?mf:mw;
col=mix(und,top,mt);al=1.-(1.-mt)*(1.-mu);
}
O=vec4(mix(col,ca,lod),mix(al,aaL,lod)*uOp);}`;
/* Hair, and anything else that is a strand. Each is a ribbon turned to face us. Its line is read from a texture that is written
afresh every frame: the few points of a guide (a strand that is moved, or worked out), with a smooth curve put through them, so
that a strand can be drawn with more corners than its guide has points. The strands of a lock lie between two guides and a little
off the line. With uCurl a strand does not lie along its guide but winds round it (a ringlet); with uWave it wanders from side to
side of it. A strand a pixel wide or less is drawn a pixel wide and fainter;
and one that would be wider than uWX, because we are close, is drawn no wider than that - hair seen close is fine, not fat.
uCut: how much of each guide's length is drawn (a thread that is still being spun). */
const HRV=`#version 300 es
precision highp float;precision highp int;
uniform sampler2D uPts;uniform mat4 uVP;uniform vec3 uCam,uOut;
uniform float uNP,uNV,uCH,uG,uW,uWX,uPS,uSpread,uRow,uCurl,uTurns,uNbr,uWave,uWvN,uRag,uTap,uLenK,uCut;
out vec3 vP,vTn;out vec2 vUV;flat out vec4 vR;out float vA,vS;
float h1(float n){return fract(sin(n*127.1)*43758.5453);}
// a guide's line at f (counted in its points): where it is, and which way it runs
void gd(int row,float f,out vec4 p,out vec3 t){
int n=int(uNP)-1,k=min(int(f),n-1);float x=f-float(k);
vec4 a=texelFetch(uPts,ivec2(max(k-1,0),row),0),b=texelFetch(uPts,ivec2(k,row),0),c=texelFetch(uPts,ivec2(k+1,row),0),d=texelFetch(uPts,ivec2(min(k+2,n),row),0);
p=.5*(2.*b+(c-a)*x+(2.*a-5.*b+4.*c-d)*x*x+(3.*b-a-3.*c+d)*x*x*x);
t=.5*((c-a)+2.*(2.*a-5.*b+4.*c-d)*x+3.*(3.*b-a-3.*c+d)*x*x).xyz;}
void main(){
int id=gl_VertexID>>1,ch=int(uCH),g=gl_InstanceID/ch,c=gl_InstanceID-g*ch,g2=uNbr>.5?min(g+1,int(uG)-1):g,r0=int(uRow);
float r=float(g)*61.+float(c)+uRow*77.,b=h1(r+7.3),d=h1(r+13.7),e=h1(r+29.1),sd=float(gl_VertexID&1)*2.-1.,u=float(id)/(uNV-1.);
float a=c==0?0.:h1(r),lk=mix(1.,uLenK,.55+.45*h1(float(g)+.7)),f=u*lk*uCut*(uNP-1.);
vec4 p4,q4;vec3 t,t2;gd(r0+g,f,p4,t);
if(g2!=g){gd(r0+g2,f,q4,t2);p4=mix(p4,q4,a);t=mix(t,t2,a);}
vec3 p=p4.xyz,dp=t*lk*uCut*(uNP-1.),tn=normalize(t),bn=normalize(cross(tn,uOut)+vec3(1e-5,0,0)),on=cross(bn,tn);
p+=(on*(b-.25)*.7+bn*(d-.5))*uSpread*(c==0?0.:.2+.8*u);
if(uCurl>0.){float w=uTurns*6.2832,th=u*w+float(g)*2.4+h1(r+3.1)*2.,rad=uCurl*(.86+.28*d)*smoothstep(0.,.1,u),cs=cos(th),sn=sin(th);
p+=(bn*cs+on*sn)*rad;dp+=(on*cs-bn*sn)*rad*w;}
if(uWave>0.){float w=uWvN*6.2832,gg=float(g)+a,th=u*w+2.2*sin(gg*.23+1.)+1.6*sin(gg*.071)+b*.4,am=uWave*smoothstep(0.,.3,u)*(.6+.8*d);
p+=on*sin(th)*am+bn*sin(th*.7+1.3)*am*.6;dp+=(on*cos(th)+bn*cos(th*.7+1.3)*.42)*am*w;}
tn=normalize(dp);
vec3 sdv=normalize(cross(tn,uCam-p));
vec4 cp=uVP*vec4(p,1);float wpx=uW*uPS/cp.w,k=clamp(wpx,1.3,uWX)/wpx;
cp=uVP*vec4(p+sdv*sd*uW*.5*k*(1.-uTap*u*u),1);
vP=p;vTn=tn;vUV=vec2(u,sd);vR=vec4(b,e,d,(float(g)+a)/max(uG-1.,1.));vA=min(1.,wpx/1.3)*(1.-uRag*smoothstep(.7+.24*e,.76+.24*e,u));vS=p4.w;gl_Position=cp;}`;
/* A braid, made here from nothing but its middle line (one moved strand) and how many crossings have been made. Three locks, each
a bundle of fibres. Seen end on, each lock goes round a figure of eight, a third of the way behind the one before - that is all a
braid is - and whichever is passing in front is the one coming in from the side. Above the last crossing made it is plaited; below,
the three locks hang loose. When the last is made it is tied, and what is left over is a tassel. */
const BRV=`#version 300 es
precision highp float;precision highp int;
uniform sampler2D uPts;uniform mat4 uVP;uniform vec3 uCam,uOut;
uniform float uNP,uNV,uNL,uW,uWX,uPS,uRow,uNc,uNC,uT,uSz;
out vec3 vP,vTn;out vec2 vUV;flat out vec4 vR;out float vA,vS;
float h1(float n){return fract(sin(n*127.1)*43758.5453);}
void gd(int row,float f,out vec4 p,out vec3 t){
int n=int(uNP)-1,k=min(int(f),n-1);float x=f-float(k);
vec4 a=texelFetch(uPts,ivec2(max(k-1,0),row),0),b=texelFetch(uPts,ivec2(k,row),0),c=texelFetch(uPts,ivec2(k+1,row),0),d=texelFetch(uPts,ivec2(min(k+2,n),row),0);
p=.5*(2.*b+(c-a)*x+(2.*a-5.*b+4.*c-d)*x*x+(3.*b-a-3.*c+d)*x*x*x);
t=.5*((c-a)+2.*(2.*a-5.*b+4.*c-d)*x+3.*(3.*b-a-3.*c+d)*x*x).xyz;}
float sh;
// where a fibre is at s along the braid: lock k, and B its own place in the lock
vec3 at(float s,float k,vec4 B){
vec4 p;vec3 t;gd(int(uRow),s*(uNP-1.),p,t);t=normalize(t);
vec3 rt=normalize(cross(t,uOut)+vec3(1e-5,0,0)),ov=cross(rt,t);
float q=s*uNC,th=1.0472*min(q,uNc)+.5,below=max(0.,q-uNc),loose=smoothstep(0.,2.5,below),en=smoothstep(uNC-1.,uNC,uNc),
tie=max(en*exp(-pow((s-.9)/.014,2.)),exp(-pow(s/.03,2.))),tass=en*smoothstep(.9,1.,s),
A=.13*(1.-.75*tie)*(1.+.6*loose)*(1.-.3*s*(1.-loose)),Bz=.07*(1.-loose)*(1.-tie),rho=(.085+.05*loose+.09*tass)*(1.-.7*tie)*(1.-.25*s*(1.-loose)),
ph=th+2.0944*k,x=A*sin(ph),z=-Bz*sin(2.*ph),tw=s*5.+k*2.+B.z*.6*loose,cs=cos(tw),sn=sin(tw),du=B.x*cs-B.y*sn,dv=B.x*sn+B.y*cs,
sw=loose*.012*sin(uT*1.1+B.w*9.+s*6.)*min(below,4.);
// a lock that is passing behind is in the others' shade, and so is the inside of every lock
sh=(.38+.62*(.5+.5*(Bz>1e-4?z/Bz:1.)))*(.6+.4*min(length(B.xy),1.))*(1.-.5*tie);
return p.xyz+(rt*(x+rho*du+sw)+ov*(z+rho*.6*dv))*uSz;}
void main(){
int id=gl_VertexID>>1,nl=int(uNL);float u=float(id)/(uNV-1.),sd=float(gl_VertexID&1)*2.-1.,k=float(gl_InstanceID/nl),r=float(gl_InstanceID)+uRow*77.;
float e=h1(r+29.1),rr=sqrt(h1(r+1.1))*(1.+.9*step(.965,e)),ra=6.2832*h1(r+2.3);vec4 B=vec4(rr*cos(ra),rr*sin(ra),h1(r+3.7),h1(r+4.9));
float du=.5/(uNV-1.);
vec3 p=at(u,k,B);float s0=sh;vec3 tn=normalize(at(min(u+du,1.),k,B)-at(max(u-du,0.),k,B));
vec3 sdv=normalize(cross(tn,uCam-p));
vec4 cp=uVP*vec4(p,1);float wpx=uW*uPS/cp.w,kk=clamp(wpx,1.3,uWX)/wpx;
cp=uVP*vec4(p+sdv*sd*uW*.5*kk,1);
vP=p;vTn=tn;vUV=vec2(u,sd);vR=vec4(h1(r+7.3),e*.9,h1(r+13.7),.5);vA=min(1.,wpx/1.3)*(1.-smoothstep(.93+.06*e,.95+.06*e,u));vS=s0;gl_Position=cp;}`;
// Fur. Each hair knows only where on the furred solid it grows and which way that faces; it stands out from there, as long as it has
// grown, and bends - down, with its own weight, lying over the solid where the solid is in its way; a very little with the air; a
// little its own way; and as the solid's being thrown about leaves it (see below).
// And a hair may leave the solid (uMo) and be a hair of a spider instead: the hairs of each of the solid's twenty faces go to two
// spiders (NSP of them in all), every other hair to each. uSp: where every spider is - a row to each: its body, which way is up for it, which way it faces; then for each of its eight
// legs the hip, the knee and the foot (the fourth number of the second: how far that spider's hairs have got with leaving, each in
// its own time). Of every twelve hairs eight go to the legs, one each, three to its back and one to its front - half of those to the
// two feelers it carries before it. A leg is the size of a tarantula's: one hair of each is the leg itself - wide, and dark - a third
// of the rest lie along it, and the others stand off it, short. uOne: every hair to the spider of that one row.
const NSP=40,FUV=`#version 300 es
precision highp float;precision highp int;
uniform sampler2D uPts,uSp;uniform mat4 uVP;uniform mat3 uFR,uDa,uDb;uniform vec3 uCam,uFO,uGrav,uD0a,uD0b;uniform float uNP,uW,uWX,uPS,uLen,uT,uGrow,uOne,uMo,uShl;
out vec3 vP,vTn;out vec2 vUV;flat out vec4 vR;out float vA,vS;
float h1(float n){return fract(sin(n*127.1)*43758.5453);}
vec3 fr,fn,fb,sH,sK,sA,sG,sB,sU,sF,sS,sN,hd;float fL,fsh,sz,s0,ls,rr,fl,cph,sph,tau,hq,co,pl;int pt;
// as a hair of the fur (a hair bends, it does not stretch; and it does not go through what it grows on, but lies on it - fsh: how far out that is)
vec3 at(float u){vec3 p=fr+fL*u*normalize(fn+fb*(u*.5))-uFO;return uFO+p*max(1.,mix(length(fr-uFO),fsh,smoothstep(0.,.25,u))/length(p));}
// A spider's leg, s along it: from the hip up to the knee, on outward to a second joint (sA: worked out here, off the straight line
// from knee to foot, on the knee's side of it), and down to the foot. And how thick the leg is there: less and less, but for its joints.
vec3 leg(float s,out vec3 tg){if(s<.43){tg=sK-sH;return mix(sH,sK,s/.43);}if(s<.745){tg=sA-sK;return mix(sK,sA,(s-.43)/.315);}tg=sG-sA;return mix(sA,sG,(s-.745)/.255);}
float rad(float s){float a=(s-.43)/.04,b=(s-.745)/.04;return sz*(mix(.0145,.004,s)+.0026*(exp(-a*a)+exp(-b*b)));}
// as a hair of a spider: the leg itself; or lying along a leg, on its skin; or standing off it; or one of a feeler's (pl: which side's);
// or growing on its back, swept the way it has come from
vec3 sp(float u){
if(pt<8){float s=min(s0+ls*u,1.);vec3 tg,c=leg(s,tg);
return c+(sN*cph+normalize(cross(tg,sN))*sph)*(rad(s)*rr+fl*sz*u);}
if(pl!=0.){vec3 p0=sB+(sF*.036+sS*(pl*.011)-sU*.004)*sz,p1=p0+(sF*.028+sU*.006+sS*(pl*.004))*sz,p2=p1+(sF*.013-sU*.04)*sz;
return(u<.5?mix(p0,p1,u*2.):mix(p1,p2,u*2.-1.))+(sS*cph+sU*sph)*sz*(.0055*rr*(1.-.6*u)+fl*u);}
bool ab=pt<11;vec3 c=ab?sB-sF*(.056*sz)+sU*(.012*sz):sB+sF*(.016*sz),ra=ab?vec3(.035,.033,.048):vec3(.026,.016,.03);
// (its coat lies back along it, whatever its shape: nb is the way its skin faces where this hair grows)
vec3 nb=normalize(sS*(hd.x/ra.x)+sU*(hd.y/ra.y)+sF*(hd.z/ra.z)),g=normalize(-sF-nb*dot(-sF,nb)+nb*.3+sU*.05);float hl=(ab?.026:.017)*(.5+rr);
return c+((sS*hd.x*ra.x+sU*hd.y*ra.y+sF*hd.z*ra.z)+g*hl*u-nb*hl*.2*u*u)*sz;}
// where a hair is: on the solid, or on a spider, or on its way from the one to the other - which it goes root first, out and round
vec3 pos(float u){
float e=smoothstep(0.,1.,tau-.2*u);if(e<=0.)return at(u);
vec3 b=sp(u);if(e>=1.)return b;
vec3 a=at(u),p=mix(a,b,e),o=normalize(p-uFO);float w=sin(3.14159*e);
return p+o*w*(.08+.3*hq)+cross(o,hd)*w*.2*(hq-.5);}
void main(){
int id=gl_VertexID>>1;float u=float(id)/(uNP-1.),sd=float(gl_VertexID&1)*2.-1.,q=float(gl_InstanceID);
vec4 a=texelFetch(uPts,ivec2(gl_InstanceID&255,gl_InstanceID>>8),0),nn=texelFetch(uPts,ivec2(gl_InstanceID&255,(gl_InstanceID>>8)+128),0);
// (no two hairs stand quite the same way, and all of them lean a little the way they hang)
fr=uFO+uFR*a.xyz;fn=normalize(uFR*nn.xyz+(vec3(h1(q+11.),h1(q+12.),h1(q+13.))-.5)*.85+uGrav*.22);
fL=uLen*(.55+.45*a.w)*smoothstep(0.,.35,uGrow*1.35-nn.w-.1*a.w);float e=h1(q+1.7);
// What the solid's being thrown about does to a hair: its tip is left behind, swings, and comes to rest. Two such swings are worked
// out on the page - a slack one and a stiff one - each as so much for the solid as a whole and so much more according to where on
// the solid the hair's tip belongs; a long hair has more of the slack one. (And it can be left no further behind than flat.)
vec3 rt=a.xyz+nn.xyz*fL,dl=mix(uD0b+uDb*rt,uD0a+uDa*rt,.2+.8*a.w)/max(fL,1e-4);dl*=min(1.,1.7/max(length(dl),1e-4));fsh=(.16+.022*e)*uShl;
float wv=sin(dot(fr,vec3(3.1,1.7,2.3))+uT*2.2)+.6*sin(dot(fr,vec3(-1.9,3.3,1.1))+uT*1.3);
fb=uGrav*3.2+cross(fn,vec3(.3,1.,.2))*wv*.07+(vec3(h1(q+3.),h1(q+5.),h1(q+7.))-.5)*.7+dl*2.;
tau=0.;pt=0;co=0.;pl=0.;float shd=.3+.7*u,em=0.;
if(uMo>.5){
int one=int(uOne),si=one<0?gl_InstanceID%${NSP}:one,hi=one<0?gl_InstanceID/${NSP}:gl_InstanceID;pt=hi%12;
vec4 B0=texelFetch(uSp,ivec2(0,si),0),N0=texelFetch(uSp,ivec2(1,si),0);
hq=h1(q+21.);tau=N0.w*1.8-.6*h1(q+23.);
if(tau>0.){
sB=B0.xyz;sz=B0.w;sU=N0.xyz;sF=texelFetch(uSp,ivec2(2,si),0).xyz;sS=cross(sU,sF);
float ph=6.2832*h1(q+25.),z=2.*h1(q+27.)-1.,sq=sqrt(1.-z*z);cph=cos(ph);sph=sin(ph);hd=vec3(sq*cph,sq*sph,z);rr=sqrt(h1(q+29.));
em=smoothstep(0.,1.,tau-.2*u);float sh2=.36+.64*u;
if(pt<8){sH=texelFetch(uSp,ivec2(3+3*pt,si),0).xyz;sK=texelFetch(uSp,ivec2(4+3*pt,si),0).xyz;sG=texelFetch(uSp,ivec2(5+3*pt,si),0).xyz;
vec3 kg=sG-sK,tn=normalize(kg),kv=sK-.5*(sH+sG);sA=sK+kg*.56+normalize(kv-tn*dot(kv,tn)+sU*1e-4)*(.2*length(kg));
sN=normalize(cross(sG-sH,sK-sH)+cross(sG-sH,sU)*1e-3+vec3(1e-6,0,0));
if(hi<8){co=1.;s0=0.;ls=1.;rr=0.;fl=0.;}
else if(h1(q+31.)<.45){s0=.02*hq;ls=.98-s0;rr=.8+.5*rr;fl=.0015*hq;}
else{s0=.92*h1(q+33.);ls=.04+.08*hq;rr=1.;fl=(.007+.016*h1(q+35.))*(1.-.7*s0*s0);}
// (the leg itself is dark; its hair is lighter in a ring about each joint, and the hairs that stand off it toward their ends)
float s=min(s0+ls*u,1.),a=(s-.43)/.06,b=(s-.745)/.05;sh2=co>0.?.1:min(1.,(ls>.5?.36:.34+.6*u)+.45*(exp(-a*a)+exp(-b*b)));}
else if(pt==11&&h1(q+37.)<.5){pl=h1(q+39.)<.5?-1.:1.;fl=.005*hq;sh2=.34+.5*u;}
shd=mix(shd,sh2,em);}}
vec3 p=pos(u),t=normalize(pos(u+.04)-pos(max(u-.04,0.)));
vec3 sdv=normalize(cross(t,uCam-p));
vec4 cp=uVP*vec4(p,1);float wpx=uW*uPS/cp.w,k=clamp(wpx,1.3,uWX)/wpx,hw=uW*.5*k*(1.-.8*u),al=min(1.,wpx/1.3);
// (the one hair that is a leg is as wide as the leg, and all there)
if(co>0.){hw=mix(hw,max(hw,rad(u)*1.3),em);al=mix(al,1.,em);}
else if(pt>7&&pl==0.)hw*=1.+1.2*em; // (and its coat is of thicker hair than its legs')
cp=uVP*vec4(p+sdv*sd*hw,1);
vP=p;vTn=t;vUV=vec2(u,sd);vR=vec4(.4+.6*e,h1(q+9.),a.w,nn.w);vA=fL>1e-4?al:0.;vS=shd;gl_Position=cp;}`;
/* What a hair does with light (after Marschner and others, much simplified): a white highlight straight off its surface; a second,
the hair's own colour, from light that went in and came back out, a little further along the strand (the scales on a hair's
surface are tilted, which moves the two apart) and broken up; and, with the light behind it, a glow of light that has gone
straight through. uSh: how much it shines (a thread of wool hardly does); uHue: a turn of its colour, by the strand's own number.
uThn: a thread so thin that the light behind comes through all of it; uBd: drops along it - how many to a strand, how bright. */
const HRF=GH+`in vec3 vP,vTn;in vec2 vUV;flat in vec4 vR;in float vA,vS;
uniform vec3 uCam,uLamp,uLampC,uKey,uKeyC,uAmb,uHC,uBxC,uBxH;uniform mat3 uBxR;uniform float uTr,uLay,uLat,uSh,uHue,uDp,uThn,uCut;uniform vec2 uBd;out vec4 O;
float boxsh(vec3 p,vec3 lp){vec3 o=uBxR*(p-uBxC),d=uBxR*(lp-p),iv=1./(d+vec3(1e-6)),t0=(-uBxH-o)*iv,t1=(uBxH-o)*iv,a=min(t0,t1),b=max(t0,t1);
float tn=max(max(max(a.x,a.y),a.z),0.),tf=min(min(min(b.x,b.y),b.z),1.);return 1.-smoothstep(0.,.05,tf-tn);}
void main(){
float ed=1.-vUV.y*vUV.y;
// (first only to say which strand is in front: nothing is drawn, but where the strand is solid its distance is kept)
if(uDp>.5){if(ed*vA<.5)discard;O=vec4(0);return;}
vec3 T=normalize(vTn),V=normalize(uCam-vP),N=normalize(V-T*dot(V,T));
vec3 col=uHC*(.55+.9*vR.x);if(uHue>0.){float h=vR.w*6.2832*uHue;col=mix(col,col.gbr,.5-.5*cos(h));col=mix(col,col.brg,.5-.5*cos(h*.61+1.));}
vec3 H=normalize(uKey+V);float stl=sqrt(max(1.-pow(dot(T,uKey),2.),0.));
vec3 T1=normalize(T+N*.1),T2=normalize(T-N*.22);
float s1=pow(max(1.-pow(dot(T1,H),2.),0.),110.),s2=pow(max(1.-pow(dot(T2,H),2.),0.),38.)*(.3+1.4*h21(vec2(vR.y*91.,floor(vUV.x*24.))));
vec3 o=col*(uAmb+uKeyC*stl*.38)+uKeyC*uSh*(s1*.2+s2*.5*sqrt(col)*1.5);
vec3 Ll=uLamp-vP;float r2=dot(Ll,Ll);Ll*=inversesqrt(r2);vec3 lc=uLampC/(.25+r2)*uTr;if(uBxH.x>0.)lc*=boxsh(vP,uLamp);
float sll=sqrt(max(1.-pow(dot(T,Ll),2.),0.)),tt=pow(max(dot(-V,Ll),0.),3.);
// only where the lock is thin does that light reach us: at its sides, at its ends, and in the odd strand that stands off
float thin=max(max(max(uLat*(1.-smoothstep(0.,.1,min(vR.w,1.-vR.w))),smoothstep(.72,1.,vUV.x)),step(.94,vR.y)),uThn);
o+=lc*sll*(sqrt(col)*tt*2.4*mix(.04,1.,thin)+col*(.08+.5*max(dot(N,Ll),0.)*(1.-uLat)));
o*=mix(1.,mix(.28,1.,vR.x),uLay)*vS*mix(.55+.45*smoothstep(0.,.3,vUV.x),1.,uThn);
// (a thread of a web: all of it is thin; and it is hung with drops, which catch the light)
if(uBd.x>0.){float g=vUV.x*uCut*uBd.x+vR.y*7.;o+=pow(.5+.5*cos(g*6.2832),14.)*step(.3,h21(vec2(floor(g+.5),vR.y*91.)))*uBd.y*(uKeyC*stl*.3+lc*(.12+1.6*tt));}
O=vec4(o,ed*vA);}`;
// the glow: the picture made smaller and smaller, only its brightest kept, then made bigger again and added
const DNF=GH+`uniform sampler2D uC;uniform vec2 uI;uniform float uTh;out vec4 O;
void main(){vec2 v=gl_FragCoord.xy*uI*2.;vec3 s=texture(uC,v+uI*vec2(-.5,-.5)).rgb+texture(uC,v+uI*vec2(.5,-.5)).rgb+texture(uC,v+uI*vec2(-.5,.5)).rgb+texture(uC,v+uI*vec2(.5,.5)).rgb;s*=.25;
if(uTh>0.){float l=max(s.r,max(s.g,s.b));s*=smoothstep(uTh,uTh*2.5,l);}O=vec4(s,1);}`;
const UPF=GH+`uniform sampler2D uC;uniform vec2 uI;out vec4 O;
void main(){vec2 v=gl_FragCoord.xy*uI*.5;vec3 s=texture(uC,v+uI*vec2(-1,-1)).rgb+texture(uC,v+uI*vec2(1,-1)).rgb+texture(uC,v+uI*vec2(-1,1)).rgb+texture(uC,v+uI*vec2(1,1)).rgb;O=vec4(s*.25,1);}`;
// the last pass: the picture and its glow, pressed into what a screen can show, darker at the corners, with a little grain
const CMF=GH+`uniform sampler2D uC,uB;uniform vec2 uR;uniform float uT,uF,uBm;out vec4 O;
vec3 aces(vec3 x){return clamp((x*(2.51*x+.03))/(x*(2.43*x+.59)+.14),0.,1.);}
void main(){
vec2 v=gl_FragCoord.xy/uR,c=v-.5;float r2=dot(c,c);
vec3 col=texture(uC,v).rgb+texture(uB,v).rgb*uBm;
col*=1.-.9*r2;
col=pow(aces(col*1.15),vec3(.4545));
col+=(h21(gl_FragCoord.xy+fract(uT)*61.)-.5)*.022;
O=vec4(col*uF,1);}`;
const PBG=prog(BGF),PSB=prog(SBF),PSF=prog(SFF,SFV),PHR=prog(HRF,HRV),PBR=prog(HRF,BRV),PFU=prog(HRF,FUV),PDN=prog(DNF),PUP=prog(UPF),PCM=prog(CMF);
/* ---------------- things that move ---------------- */
const rnd0=s=>()=>{s=s+0x6D2B79F5|0;let t=Math.imul(s^s>>>15,1|s);t=t+Math.imul(t^t>>>7,61|t)^t;return((t^t>>>14)>>>0)/4294967296};
const G=[0,-9.8*.12,0]; // things fall slowly here: a scene is a yard or two across
/* Strands that are moved one by one (the guides): NP points each, their places kept four numbers to a point (the fourth: how much in
shade) in one sheet that the shader reads. A strand is a chain: each point falls and keeps most of the speed it had; then, a few
times over, every two neighbours are put back at their distance (each giving way equally, so that what one loses the other gains
and nothing is made out of nothing) and every two next-but-one neighbours are pushed apart toward twice that (a hair has some body:
it does not fold); and it is pushed out of whatever is in its way. The first point is held to its root, a place in its scene, and the
next few are led the way the strand leaves it. The scene moves with the solid, so every roll swings the lot. */
const NP=20,HMAX=560,HP=new Float32Array(HMAX*NP*4),HQ=new Float32Array(HMAX*NP*3),HT=data(NP,HMAX,HP);let hrow=0;
// roots: places in the scene; len: how long; o: stiff (body), damp, wind, out (the way a strand leaves its root), col (what is in its way:
// ['x',y,z,r] a rod along the scene's width, ['s',x,y,z,r] a ball, ['b',a cube standing in the scene,how far off it to stay],
// ['c',any solid with flat faces and no dents (see solid),how far off it to stay]: what is inside one is put out through its nearest face)
const strands=(roots,len,o)=>{const r=rnd0(77+hrow),S={n:roots.length,row:hrow,len,seg:len/(NP-1),root:roots,lens:roots.map(()=>.82+.18*r()),fresh:1,stiff:.25,damp:.985,wind:1,out:[0,-1,.1],col:[],...o};hrow+=S.n;return S};
// When the scene itself moves (the solid is rolling) what is in it is carried along, all but a part: enough to swing, not enough to whip.
const KEEP=.1,carry=(a,k,G0,F)=>{const O=G0.o,X=G0.x,Y=G0.y,Z=G0.z,lx=a[k]-O[0],ly=a[k+1]-O[1],lz=a[k+2]-O[2],qx=lx*X[0]+ly*X[1]+lz*X[2],qy=lx*Y[0]+ly*Y[1]+lz*Y[2],qz=lx*Z[0]+ly*Z[1]+lz*Z[2];
a[k]+=(F.o[0]+F.x[0]*qx+F.y[0]*qy+F.z[0]*qz-a[k])*(1-KEEP);a[k+1]+=(F.o[1]+F.x[1]*qx+F.y[1]*qy+F.z[1]*qz-a[k+1])*(1-KEEP);a[k+2]+=(F.o[2]+F.x[2]*qx+F.y[2]*qy+F.z[2]*qz-a[k+2])*(1-KEEP)},
moved=(A,B)=>Math.abs(A.o[0]-B.o[0])+Math.abs(A.o[1]-B.o[1])+Math.abs(A.o[2]-B.o[2])+Math.abs(A.x[0]-B.x[0])+Math.abs(A.x[1]-B.x[1])+Math.abs(A.y[2]-B.y[2])>1e-7;
// E: what the music is doing: gz a push toward us, gx(x,i) a push sideways at a place along the scene's width, ry a lift of the roots.
// A strand may be held at its far end too (S.tip: where, for each): then it hangs between the two.
const stepStrands=(S,F,dt,t,E)=>{const n=S.n,X=F.x,Y=F.y,Z=F.z,O=F.o,ow=nrm(fDir(F,S.out)),bs=S.stiff,d2=dt*dt,dm=S.damp,wind=S.wind,col=S.col,nc=col.length,gz=E&&E.gz||0,gxf=E&&E.gx,ry=E&&E.ry||0,tp=S.tip,L=NP-1;
if(!S.fresh&&S.pF&&moved(S.pF,F))for(let k=S.row*NP,e=(S.row+n)*NP;k<e;k++){carry(HP,k*4,S.pF,F);carry(HQ,k*3,S.pF,F)}S.pF=F;
for(let s=0;s<n;s++){const o=(S.row+s)*NP,o4=o*4,q=S.root[s],rw=fAt(F,ry?[q[0],q[1]+ry*(S.lift?S.lift[s]:1),q[2]]:q),sg=S.seg*S.lens[s],tw=tp?fAt(F,tp[s]):null,e4=o4+L*4,M=tw?L:NP;
if(S.fresh){
// one held at both ends starts as the curve it will hang in, near enough
if(tw){const dd=Math.hypot(tw[0]-rw[0],tw[1]-rw[1],tw[2]-rw[2]),sag=.6*Math.sqrt(Math.max(0,sg*L*sg*L-dd*dd));for(let i=0;i<NP;i++){const j=o4+i*4,k=(o+i)*3,u=i/L;HP[j]=HQ[k]=mix(rw[0],tw[0],u);HP[j+1]=HQ[k+1]=mix(rw[1],tw[1],u)-sag*4*u*(1-u);HP[j+2]=HQ[k+2]=mix(rw[2],tw[2],u);HP[j+3]=1}}
// one just come to life leaves its root its own way and turns to hang
else{let x=rw[0],y=rw[1],z=rw[2];for(let i=0;i<NP;i++){const j=o4+i*4,k=(o+i)*3;if(i){const f=ss(0,7,i),dx=ow[0]*(1-f),dy=ow[1]*(1-f)-f,dz=ow[2]*(1-f),l=sg/(Math.hypot(dx,dy,dz)||1);x+=dx*l;y+=dy*l;z+=dz*l}HP[j]=HQ[k]=x;HP[j+1]=HQ[k+1]=y;HP[j+2]=HQ[k+2]=z;HP[j+3]=1}}}
HP[o4]=rw[0];HP[o4+1]=rw[1];HP[o4+2]=rw[2];
const wx=(.5*Math.sin(t*1.1+s*.37)+.35*Math.sin(t*2.3+s*.11))*wind*.12+(gxf?gxf(q[0],s):0),wz=.4*Math.sin(t*.8+s*.23)*wind*.1+gz,
ax=(G[0]+X[0]*wx+Z[0]*wz)*d2,ay=(G[1]+X[1]*wx+Z[1]*wz)*d2,az=(G[2]+X[2]*wx+Z[2]*wz)*d2;
for(let i=1;i<M;i++){const j=o4+i*4,k=(o+i)*3,x=HP[j],y=HP[j+1],z=HP[j+2],u=tw?1:.3+i/L;
HP[j]=x+(x-HQ[k])*dm+ax*u;HP[j+1]=y+(y-HQ[k+1])*dm+ay;HP[j+2]=z+(z-HQ[k+2])*dm+az*u;HQ[k]=x;HQ[k+1]=y;HQ[k+2]=z}
for(let it=0;it<3;it++){
if(tw){HP[e4]=tw[0];HP[e4+1]=tw[1];HP[e4+2]=tw[2]}
// the way it leaves its root
else for(let i=1;i<4;i++){const j=o4+i*4,w=i==1?.6:i==2?.25:.08;HP[j]+=(rw[0]+ow[0]*sg*i-HP[j])*w;HP[j+1]+=(rw[1]+ow[1]*sg*i-HP[j+1])*w;HP[j+2]+=(rw[2]+ow[2]*sg*i-HP[j+2])*w}
for(let i=1;i<NP;i++){const j=o4+i*4,a=j-4,end=tw&&i==L;
let ex=HP[j]-HP[a],ey=HP[j+1]-HP[a+1],ez=HP[j+2]-HP[a+2],l=Math.sqrt(ex*ex+ey*ey+ez*ez)||1,c=(l-sg)/l;
if(i==1){if(!end){HP[j]-=ex*c;HP[j+1]-=ey*c;HP[j+2]-=ez*c}}else if(end){HP[a]+=ex*c;HP[a+1]+=ey*c;HP[a+2]+=ez*c}else{c*=.5;HP[a]+=ex*c;HP[a+1]+=ey*c;HP[a+2]+=ez*c;HP[j]-=ex*c;HP[j+1]-=ey*c;HP[j+2]-=ez*c}
if(i>1){const b=j-8;ex=HP[j]-HP[b];ey=HP[j+1]-HP[b+1];ez=HP[j+2]-HP[b+2];l=Math.sqrt(ex*ex+ey*ey+ez*ez)||1;c=(l-2*sg)/l*bs;
if(i==2){if(!end){HP[j]-=ex*c;HP[j+1]-=ey*c;HP[j+2]-=ez*c}}else if(end){HP[b]+=ex*c;HP[b+1]+=ey*c;HP[b+2]+=ez*c}else{c*=.5;HP[b]+=ex*c;HP[b+1]+=ey*c;HP[b+2]+=ez*c;HP[j]-=ex*c;HP[j+1]-=ey*c;HP[j+2]-=ez*c}}}}
// out of what is in its way, as the scene counts places; and what it rubs against slows it
if(nc)for(let i=1;i<M;i++){const j=o4+i*4,k=(o+i)*3,lx=HP[j]-O[0],ly=HP[j+1]-O[1],lz=HP[j+2]-O[2];let qx=lx*X[0]+ly*X[1]+lz*X[2],qy=lx*Y[0]+ly*Y[1]+lz*Y[2],qz=lx*Z[0]+ly*Z[1]+lz*Z[2],hitc=0;
for(let c=0;c<nc;c++){const C=col[c];
if(C[0]=='x'){const dy=qy-C[1],dz=qz-C[2],d=Math.hypot(dy,dz),r=C[3]+.012;if(d<r){const f=r/(d||1);qy=C[1]+dy*f;qz=C[2]+dz*f;hitc=1}}
else if(C[0]=='b'){const B=C[1],c=B.c,R=B.R,h=B.hs+C[2],sx=qx-c[0],sy=qy-c[1],sz=qz-c[2];let a=R[0]*sx+R[3]*sy+R[6]*sz,b=R[1]*sx+R[4]*sy+R[7]*sz,e=R[2]*sx+R[5]*sy+R[8]*sz;const pa=h-Math.abs(a),pb=h-Math.abs(b),pe=h-Math.abs(e);
if(pa>0&&pb>0&&pe>0){if(pa<pb&&pa<pe)a=a<0?-h:h;else if(pb<pe)b=b<0?-h:h;else e=e<0?-h:h;qx=R[0]*a+R[1]*b+R[2]*e+c[0];qy=R[3]*a+R[4]*b+R[5]*e+c[1];qz=R[6]*a+R[7]*b+R[8]*e+c[2];hitc=1}}
else if(C[0]=='c'){const B=C[1],c=B.c,sx=qx-c[0],sy=qy-c[1],sz=qz-c[2];if(sx*sx+sy*sy+sz*sz<B.r2){const R=B.R,P=B.P,a=R[0]*sx+R[3]*sy+R[6]*sz,b=R[1]*sx+R[4]*sy+R[7]*sz,e=R[2]*sx+R[5]*sy+R[8]*sz;let bi=0,be=-1e9;
for(let i=0;i<P.length;i++){const p=P[i],v=p[0]*a+p[1]*b+p[2]*e-p[3]-C[2];if(v>be){be=v;bi=i}}
if(be<0){const p=P[bi],a2=a-p[0]*be,b2=b-p[1]*be,e2=e-p[2]*be;qx=R[0]*a2+R[1]*b2+R[2]*e2+c[0];qy=R[3]*a2+R[4]*b2+R[5]*e2+c[1];qz=R[6]*a2+R[7]*b2+R[8]*e2+c[2];hitc=1}}}
else{const dx=qx-C[1],dy=qy-C[2],dz=qz-C[3],d=Math.hypot(dx,dy,dz),r=C[4]+.012;if(d<r){const f=r/(d||1);qx=C[1]+dx*f;qy=C[2]+dy*f;qz=C[3]+dz*f;hitc=1}}}
if(hitc){const x=O[0]+X[0]*qx+Y[0]*qy+Z[0]*qz,y=O[1]+X[1]*qx+Y[1]*qy+Z[1]*qz,z=O[2]+X[2]*qx+Y[2]*qy+Z[2]*qz;HP[j]=x;HP[j+1]=y;HP[j+2]=z;HQ[k]+=(x-HQ[k])*.15;HQ[k+1]+=(y-HQ[k+1])*.15;HQ[k+2]+=(z-HQ[k+2])*.15}}
// and last every point is put at exactly its distance from the one before - without that counting as speed
if(tw){const k=(o+L)*3;HP[e4]=HQ[k]=tw[0];HP[e4+1]=HQ[k+1]=tw[1];HP[e4+2]=HQ[k+2]=tw[2]}
else for(let i=1;i<NP;i++){const j=o4+i*4,a=j-4,k=(o+i)*3,ex=HP[j]-HP[a],ey=HP[j+1]-HP[a+1],ez=HP[j+2]-HP[a+2],c=sg/(Math.sqrt(ex*ex+ey*ey+ez*ez)||1)-1;
HP[j]+=ex*c;HP[j+1]+=ey*c;HP[j+2]+=ez*c;HQ[k]+=ex*c;HQ[k+1]+=ey*c;HQ[k+2]+=ez*c}}
S.fresh=0;S.dirty=1};
// Strands thick enough to be in each other's way (braids): where a point of one is nearer a point of another than their thicknesses
// allow, both give way equally - and most of that giving way is not counted as speed, so that they meet softly and lean on each other.
const collide=(S,R)=>{const n=S.n;
for(let a=0;a<n;a++)for(let b=a+1;b<n;b++){const oa=(S.row+a)*NP,ob=(S.row+b)*NP,rr=R[a]+R[b],r2=rr*rr;
for(let i=1;i<NP;i++){const j=(oa+i)*4,jq=(oa+i)*3;
for(let k=1;k<NP;k++){const m=(ob+k)*4,dx=HP[m]-HP[j],dy=HP[m+1]-HP[j+1],dz=HP[m+2]-HP[j+2],d2=dx*dx+dy*dy+dz*dz;
if(d2<r2&&d2>1e-10){const d=Math.sqrt(d2),c=(rr-d)/d*.5,cx=dx*c,cy=dy*c,cz=dz*c,mq=(ob+k)*3,f=.8;
HP[j]-=cx;HP[j+1]-=cy;HP[j+2]-=cz;HP[m]+=cx;HP[m+1]+=cy;HP[m+2]+=cz;
HQ[jq]-=cx*f;HQ[jq+1]-=cy*f;HQ[jq+2]-=cz*f;HQ[mq]+=cx*f;HQ[mq+1]+=cy*f;HQ[mq+2]+=cz*f}}}}};
/* Strands that are not moved point by point but worked out afresh each frame from a few numbers (a rope's fibres): NF points each,
in a sheet of their own. */
const NF=128,FMAX=720,FP=new Float32Array(FMAX*NF*4),FT=data(NF,FMAX,FP);let frow=0;
const rows=n=>{const r=frow;frow+=n;return r};
/* A sheet of cloth: nx by ny points, each of which falls and keeps its speed, held to its neighbours (and, more loosely, to their
neighbours, so that it does not fold like paper). It hangs by its top row from a rail narrower than itself, so it falls in folds;
or (flat) it starts lying level above the scene, held by nothing, and falls on what is under it. */
const sheet=(nx,ny,o)=>{const n=nx*ny,S={nx,ny,n,x:new Float32Array(n*3),q:new Float32Array(n*3),rest:new Float32Array(n*3),pp:new Float32Array(n*3),pin:new Uint8Array(n),free:new Uint8Array(n),fx:new Uint8Array(n*3),nr:new Float32Array(n*3),tn:new Float32Array(n*3),uv:new Float32Array(n*2),
mesh:mesh([3,3,3,2,3,4],(nx-1)*(ny-1)*6),fresh:1,gather:.8,ty:2,hue:0,wind:1,flat:0,box:null,its:6,...o},xc=(S.x0+S.x1)/2,dx=(S.x1-S.x0)/(nx-1),dy=S.h/(ny-1),ci=[],cl=[],cs=[];
for(let j=0;j<ny;j++)for(let i=0;i<nx;i++){const k=j*nx+i,p=S.flat?[S.x0+dx*i,S.y1,S.z-S.h/2+dy*j]:[S.x0+dx*i,S.y1-dy*j,S.z];S.rest.set(p,k*3);S.uv[k*2]=dx*i;S.uv[k*2+1]=-dy*j;
if(!j&&!S.flat){S.pin[k]=1;S.pp.set([xc+(p[0]-xc)*S.gather,S.y1,S.z+.05*Math.sin(i*1.25)],k*3)}
const c=(b,st,l)=>{ci.push(k*3,b*3);cl.push(l);cs.push(st)};
if(i<nx-1)c(k+1,1,dx);if(j<ny-1)c(k+nx,1,dy);if(i<nx-1&&j<ny-1)c(k+nx+1,.5,Math.hypot(dx,dy));
if(i>0&&j<ny-1)c(k+nx-1,.5,Math.hypot(dx,dy));if(i<nx-2)c(k+2,.3,2*dx);if(j<ny-2)c(k+2*nx,.3,2*dy)}
S.ci=new Int32Array(ci);S.cl=new Float32Array(cl);S.cs=new Float32Array(cs);return S};
// E: gz a push toward us (it is never the same in two places); S.rel: how much of the rail has let go, from the left.
// S.floor: a floor under it. S.box: a box in its way - c its middle and R its turning (in the scene), h its half sizes, m how far off it the cloth stays, cp and Rp
// where it was a step ago: cloth that would be inside is put out through the nearest side, and what touches the box is carried round by it.
const stepSheet=(S,F,dt,t,E)=>{const n=S.n,nx=S.nx,ny=S.ny,flo=S.floor,x=S.x,q=S.q,d2=dt*dt,Z=F.z,X=F.x,Y=F.y,O=F.o,gz=E&&E.gz||0,rel=(S.rel||0)*nx,wd=S.wind,bx=S.box,rs=S.rest,fre=S.free,fx=S.fx,pp=S.pp;
if(!S.fresh&&S.pF&&moved(S.pF,F))for(let k=0;k<n*3;k+=3){carry(x,k,S.pF,F);carry(q,k,S.pF,F)}S.pF=F;
if(S.fresh){const xc=(S.x0+S.x1)/2;for(let k=0;k<n;k++){const j=k/nx|0,i=k-j*nx,p=fAt(F,S.flat?[rs[k*3],rs[k*3+1],rs[k*3+2]]:[xc+(rs[k*3]-xc)*S.gather,rs[k*3+1],S.z+.05*Math.sin(i*1.25)]);x.set(p,k*3);q.set(p,k*3)}S.fresh=0}
const gx=G[0]*d2,gy=G[1]*d2,gzz=G[2]*d2,zx=Z[0]*d2,zy=Z[1]*d2,zz=Z[2]*d2,xx=X[0]*d2,xy=X[1]*d2,xz=X[2]*d2;
for(let k=0;k<n;k++){const j=k*3;if(S.pin[k]&&k>=rel){fre[k]=fx[j]=0;x[j]=O[0]+X[0]*pp[j]+Y[0]*pp[j+1]+Z[0]*pp[j+2];x[j+1]=O[1]+X[1]*pp[j]+Y[1]*pp[j+1]+Z[1]*pp[j+2];x[j+2]=O[2]+X[2]*pp[j]+Y[2]*pp[j+1]+Z[2]*pp[j+2];continue}
fre[k]=fx[j]=1;const vx=(x[j]-q[j])*.992,vy=(x[j+1]-q[j+1])*.992,vz=(x[j+2]-q[j+2])*.992;q[j]=x[j];q[j+1]=x[j+1];q[j+2]=x[j+2];
const r0=rs[j],r1=rs[j+1]+rs[j+2],w=wd*(.3*Math.sin(t*1.3+r0*4+r1*2)+.25*Math.sin(t*2.1-r1*5+r0))+gz*(.6+.4*Math.sin(r0*3.1+r1*1.7+t*3)),sx=wd*.1*Math.sin(t*.7+r1*3);
x[j]+=vx+gx+zx*w+xx*sx;x[j+1]+=vy+gy+zy*w+xy*sx;x[j+2]+=vz+gzz+zz*w+xz*sx}
const ci=S.ci,cl=S.cl,cs=S.cs,m=cl.length,its=S.its;
for(let it=0;it<its;it++){
for(let c=0;c<m;c++){const a=ci[c*2],b=ci[c*2+1],fa=fx[a],fb=fx[b];if(!(fa|fb))continue;
const ex=x[b]-x[a],ey=x[b+1]-x[a+1],ez=x[b+2]-x[a+2],l=Math.sqrt(ex*ex+ey*ey+ez*ez)||1,k=(l-cl[c])/l*cs[c]*(fa&fb?.5:1),kx=ex*k,ky=ey*k,kz=ez*k;
if(fa){x[a]+=kx;x[a+1]+=ky;x[a+2]+=kz}if(fb){x[b]-=kx;x[b+1]-=ky;x[b+2]-=kz}}
// a floor (S.floor: how low): what reaches it lies on it and stays where it lies - the rows that were lowest undermost
if(flo!=null)for(let k=0;k<n;k++){if(!fre[k])continue;const j=k*3,d=flo+.0035*(ny-1-(k/nx|0))-((x[j]-O[0])*Y[0]+(x[j+1]-O[1])*Y[1]+(x[j+2]-O[2])*Y[2]);
if(d>0){x[j]+=Y[0]*d;x[j+1]+=Y[1]*d;x[j+2]+=Y[2]*d;if(it==its-1){q[j]+=(x[j]-q[j])*.7;q[j+1]+=(x[j+1]-q[j+1])*.7;q[j+2]+=(x[j+2]-q[j+2])*.7}}}
if(bx){const c=bx.c,R=bx.R,h0=bx.h[0]+bx.m,h1=bx.h[1]+bx.m,h2=bx.h[2]+bx.m,P=bx.Rp,cp=bx.cp,fr=it==its-1?bx.fr:0;
for(let k=0;k<n;k++){const j=k*3,wx=x[j]-O[0],wy=x[j+1]-O[1],wz=x[j+2]-O[2],sx=wx*X[0]+wy*X[1]+wz*X[2]-c[0],sy=wx*Y[0]+wy*Y[1]+wz*Y[2]-c[1],sz=wx*Z[0]+wy*Z[1]+wz*Z[2]-c[2];
let a=R[0]*sx+R[3]*sy+R[6]*sz,b=R[1]*sx+R[4]*sy+R[7]*sz,e=R[2]*sx+R[5]*sy+R[8]*sz;const pa=h0-Math.abs(a),pb=h1-Math.abs(b),pe=h2-Math.abs(e);
if(pa>0&&pb>0&&pe>0){if(pa<pb&&pa<pe)a=a<0?-h0:h0;else if(pb<pe)b=b<0?-h1:h1;else e=e<0?-h2:h2;
const ux=R[0]*a+R[1]*b+R[2]*e+c[0],uy=R[3]*a+R[4]*b+R[5]*e+c[1],uz=R[6]*a+R[7]*b+R[8]*e+c[2];
x[j]=O[0]+X[0]*ux+Y[0]*uy+Z[0]*uz;x[j+1]=O[1]+X[1]*ux+Y[1]*uy+Z[1]*uz;x[j+2]=O[2]+X[2]*ux+Y[2]*uy+Z[2]*uz;
if(fr){const vx=P[0]*a+P[1]*b+P[2]*e+cp[0],vy=P[3]*a+P[4]*b+P[5]*e+cp[1],vz=P[6]*a+P[7]*b+P[8]*e+cp[2];
q[j]+=(O[0]+X[0]*vx+Y[0]*vy+Z[0]*vz-q[j])*fr;q[j+1]+=(O[1]+X[1]*vx+Y[1]*vy+Z[1]*vz-q[j+1])*fr;q[j+2]+=(O[2]+X[2]*vx+Y[2]*vy+Z[2]*vz-q[j+2])*fr}}}}}};
// the sheet as the card is given it: two triangles to every square of the net, with the way the cloth faces and the way its weft runs at each corner
const putSheet=S=>{const x=S.x,nr=S.nr,tn=S.tn,nx=S.nx,ny=S.ny,a=S.mesh.a;
for(let j=0;j<ny;j++)for(let i=0;i<nx;i++){const k=(j*nx+i)*3,l=(j*nx+(i?i-1:i))*3,r=(j*nx+(i<nx-1?i+1:i))*3,u=((j?j-1:j)*nx+i)*3,d=((j<ny-1?j+1:j)*nx+i)*3,
tx=x[r]-x[l],ty=x[r+1]-x[l+1],tz=x[r+2]-x[l+2],bx=x[u]-x[d],by=x[u+1]-x[d+1],bz=x[u+2]-x[d+2];tn[k]=tx;tn[k+1]=ty;tn[k+2]=tz;nr[k]=ty*bz-tz*by;nr[k+1]=tz*bx-tx*bz;nr[k+2]=tx*by-ty*bx}
let o=0;const put=k=>{const j=k*3,l=1/(Math.hypot(nr[j],nr[j+1],nr[j+2])||1);a[o]=x[j];a[o+1]=x[j+1];a[o+2]=x[j+2];a[o+3]=nr[j]*l;a[o+4]=nr[j+1]*l;a[o+5]=nr[j+2]*l;a[o+6]=tn[j];a[o+7]=tn[j+1];a[o+8]=tn[j+2];
a[o+9]=S.uv[k*2];a[o+10]=S.uv[k*2+1];a[o+11]=a[o+12]=a[o+13]=1;a[o+14]=S.ty;a[o+15]=S.hue;a[o+16]=0;a[o+17]=0;o+=18};
for(let j=0;j<ny-1;j++)for(let i=0;i<nx-1;i++){const k=j*nx+i;put(k);put(k+nx);put(k+1);put(k+1);put(k+nx);put(k+nx+1)}
S.mesh.n=(nx-1)*(ny-1)*6;fill(S.mesh)};
/* ---------------- the five scenes, and what is inside the last cube ---------------- */
// Each scene is a room of its own - a wall at the back, a ceiling, a floor - with its own lamp (behind what it shows, unless it says
// otherwise) and its own light from our side. Places are in the scene's own directions: x to the right, y up, z toward us; the scene
// is come into at z = 0, and what it shows is about two units behind that.
// Every scene ends the same way: a cube of glass arrives in it (q), gets in the way of what is there, and comes to rest square on to
// us; the next scene is to be seen inside it, and the camera goes in. ga: the bar from which that cube may be seen.
// n: how many bars the scene lasts. fly: where the camera goes meanwhile - each [bar, where it is (three numbers), what it looks at
// (three), how far it leans over] - taken smoothly from one to the next. zin: how far in its flight begins, if not the usual.
// first: the piece opens inside it. off: not one of the row that are come into through each other's cubes. aim: something the camera keeps its eye on
// (where it is, and how much the camera's own flight gives way to it).
// init: once. reset: whenever the scene comes to life. step: sixty times a second. tick: once a frame. draw: the picture. face:
// only its cube (see the shader for how much of it). late: what of it is drawn after everything else there, being see-through.
// E: x, the bars since this scene's turn began; kick, how lately the bass drum; hit, what was struck on a new sixteenth; ce, where the camera is.
const SC=[],scene=o=>{o.e=SC.length;o.m=o.e;o.lamp=o.lamp||[0,.15,-3.3];o.lampC=o.lampC||[2.2,1.35,.62];o.key=nrm(o.key||[-.45,.55,.7]);o.keyC=o.keyC||[.95,1,1.15];o.amb=o.amb||[.02,.022,.03];
o.wall={z:-3.7,col:[.06,.045,.04],gl:1,yc:.93,yf:-1.5,xw:0,...o.wall};o.sets=[];o.fr=[0,0];o.fly=o.fly||[];o.face=function(md){drawSurf(this.U,this.q.m,0,36,{md})};if(!o.off)SC.push(o);return o},
NOBX=[0,0,0],I3=[1,0,0,0,1,0,0,0,1];
let CH=+UQ.get('ch')||30; // strands drawn to each one moved
// Strands are drawn twice: first only to find, at every pixel, which is in front; then for what they look like, where they are that one.
const strokes=(P,nv,cnt,solid)=>{gl.bindVertexArray(null);
if(solid){gl.colorMask(false,false,false,false);gl.depthMask(true);su(P,{uDp:1});gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,nv*2,cnt);gl.colorMask(true,true,true,true);su(P,{uDp:0})}
gl.depthMask(false);gl.drawArraysInstanced(gl.TRIANGLE_STRIP,0,nv*2,cnt)},
// o: ch strands to each guide (fewer when the window is small), nv corners along each, w width, spread, hc colour, nbr (lie between neighbouring guides), curl/turns, wave/wvn,
// cut (how much of its length), thn (thin as a thread), bd (drops on it: how many, how bright), ...
drawStr=(U,F,tex,np,row,n,o)=>{const c0=o.ch||1,ch=Math.max(1,Math.round(c0*(c0>3?U.q:1))),nv=o.nv||np;use(PHR);
su(PHR,{uBxH:NOBX,...U,uPts:tex,uOut:F.z,uNP:np,uNV:nv,uCH:ch,uG:n,uW:(o.w||.004)*Math.sqrt(c0/ch),uWX:o.wx||U.wx,uSpread:o.spread||0,uRow:row,uHC:o.hc||[.085,.045,.025],uTr:o.tr==null?.8:o.tr,uLay:o.lay==null?1:o.lay,
uLat:o.lat==null?1:o.lat,uSh:o.sh==null?1:o.sh,uHue:o.hue||0,uCurl:o.curl||0,uTurns:o.turns||0,uNbr:o.nbr?1:0,uWave:o.wave||0,uWvN:o.wvn||3,uRag:o.rag==null?1:o.rag,uTap:o.tap==null?.6:o.tap,uLenK:o.lenk==null?1:o.lenk,uCut:o.cut==null?1:o.cut,uThn:o.thn||0,uBd:o.bd||[0,0],uDp:0});
strokes(PHR,nv,(o.nbr?n-1:n)*ch,o.solid!==0)},
drawSurf=(U,m,first,count,o)=>{use(PSF);su(PSF,{uBxH:NOBX,...U,uTs:o&&o.ts||1,uMd:o&&o.md||0,uOp:o&&o.op||1});gl.depthMask(!(o&&o.nodepth));drawMesh(m,first,count)};
/* The cube that arrives in a scene, with the next scene inside it. hs: half its size.
put: where it is at bar x - at p, turning over as it goes; from the bar xs it settles, and by x2 it is at rest at r, square on to us.
gp: a place given as the scene inside it counts places, as the scene it stands in counts it (the scene inside is come into
through the face that is toward us); gi: the other way about; gd: a direction, the other way about.
fill: its corners, as the card is given them. sh: what a shader needs for its shadow. */
const inner=hs=>({hs,k:hs/HE,c:[0,-9,0],R:I3,cp:[0,-9,0],Rp:I3,m:mesh([3,3,3,2,3,4],36),
put(x,p,r,xs,x2){const e=ss(xs,x2,x),u=cl((x-x2)/3+1,0,1),a=u<=0?5.383+.5*(x-x2+3):5.383*(2*u*u*u-3*u*u+1)+1.5*(u*u*u-2*u*u+u)+6.2832*(3*u*u-2*u*u*u);
this.cp=this.c;this.Rp=this.R;this.c=mix3(p,r,e);this.R=mm(rot(nrm(mix3([.4,1,.25],[0,1,0],e)),a),rot([1,0,0],.62*(1-e)))},
gp(p){return add(this.c,mul(mv(this.R,[p[0],p[1],p[2]+HE]),this.k))},
gd(v){const R=this.R;return[R[0]*v[0]+R[3]*v[1]+R[6]*v[2],R[1]*v[0]+R[4]*v[1]+R[7]*v[2],R[2]*v[0]+R[5]*v[1]+R[8]*v[2]]},
gi(p){const d=mul(this.gd(sub(p,this.c)),1/this.k);d[2]-=HE;return d},
fill(F){const a=this.m.a,k=this.k;let o=0;
for(let i=0;i<6;i++){const f=IF[i],n=fDir(F,mv(this.R,f.n)),t=fDir(F,mv(this.R,f.e1));for(const j of[0,1,2,0,2,3]){const l=IV[f.v[j]],d=sub(l,f.c);a.set(fAt(F,add(this.c,mul(mv(this.R,l),k))),o);a.set(n,o+3);a.set(t,o+6);a[o+9]=dot(d,f.e1);a[o+10]=dot(d,f.e2);a[o+11]=a[o+12]=a[o+13]=1;a[o+14]=9;a[o+15]=0;a[o+16]=0;a[o+17]=0;o+=18}}
this.m.n=36;fill(this.m)},
sh(F){return{uBxC:fAt(F,this.c),uBxH:[this.hs,this.hs,this.hs],uBxR:{m:mm([F.x[0],F.y[0],F.z[0],F.x[1],F.y[1],F.z[1],F.x[2],F.y[2],F.z[2]],this.R)}}}});
// 1. Hair. The piece opens on it (first): a fall of hair from the ceiling, in waves, with the lamp behind it. It sways; when the drums
// come in the bass drum breathes on it; a slow wave crosses it from one side to the other. Then something comes between it and the
// lamp - its shadow on the hair shrinks as it comes nearer - and pushes through: the cube, turning over, the hair sliding off it.
scene({name:'hair',n:16,first:1,ga:9.2,keyC:[.9,.82,.7],lamp:[0,.15,-4.6],lampC:[8.8,5.4,2.5],wall:{z:-5.4,gl:.25,yc:.925},
fly:[[3,-.45,-.05,-.55,-.1,-.05,-2.05,.03],[6,-.75,-.15,-.85,.05,-.15,-2.05,.07],[9.3,.7,.05,-.6,0,-.05,-2.05,-.06],[12.3,.3,.08,.1,0,0,-1.9,0]],
init(){const R=[],r=rnd0(5);for(let i=0;i<130;i++)R.push([-1.75+3.5*(i+.6*r())/130,.94+.02*r(),-2-.12*r()]);this.q=inner(.3);
this.h=strands(R,1.8,{out:[0,-1,.15],damp:.975,wind:.5,col:[['b',this.q,.06],['s',0,9,0,.22]]});this.sets=[this.h]},
reset(E){this.pose(E.x,0);this.pose(E.x,0)},
// (it comes up through the floor as it comes forward)
pose(x,kick){this.q.put(x,[.2*Math.sin(x*.8),mix(-2,-.05,ss(9.5,11.7,x))+.08*Math.sin(x*.57),mix(-3.9,-1.15,ss(10.3,14,x))+.05*kick],[0,-.05,-1.15],12.5,15.5)},
step(F,dt,t,E){const x=E.x,wv=ss(5,6,x)*(1-ss(10,11,x)),c=this.h.col[1],e=E.ce,fr=this.h.fresh,W={gz:E.kick*.8,gx:wv?(sx=>.8*wv*Math.sin(sx*1.5-(x-5)*1.5)):null};
this.pose(x,E.kick);if(e&&x>13){c[1]=e[0];c[2]=e[1];c[3]=e[2]}else c[2]=9;
stepStrands(this.h,F,dt,t,W);if(fr)for(let i=0;i<90;i++)stepStrands(this.h,F,dt,t,W)},
draw(F,U0){const U=this.U={...U0,...this.q.sh(F)};this.q.fill(F);drawSurf(U,this.q.m,0,36);
drawStr(U,F,HT,NP,this.h.row,this.h.n,{ch:CH,nv:48,w:.0042,spread:.07,hc:[.075,.038,.02],nbr:1,wave:.009,wvn:2.6,sh:.75})}});
// 2. Braids. Five of them on a rail, plaited from the top down as the drums go, each a little after the one before it. Then the rail
// begins to move - from side to side, and turning, as a head turns - and they swing, each in its own time, being of different lengths:
// so they meet, and lean on each other, and part. Then the cube comes in from the right, along the row, and each braid in turn is
// pushed aside by it, rides over it and swings back.
const NBR=5,NCB=13,NLB=110;
scene({name:'braids',n:12,ga:5.9,lamp:[0,.1,-3.4],keyC:[.95,.88,.76],wall:{yc:.895},
fly:[[2.2,-.15,.4,-.85,0,.3,-2,0],[3.8,.35,-.05,-.8,.05,-.25,-2,-.05],[5.6,1.35,-.15,-1,0,-.25,-2,-.12],[7.1,.1,-.55,-.7,0,-.2,-2,0],[8.6,-1.1,-.05,-.75,0,-.2,-2,.1]],
init(){this.x0=[-.92,-.45,0,.47,.9];this.z0=[-.06,.07,-.03,.06,-.07];this.q=inner(.3);
this.ax=strands(this.x0.map((x,i)=>[x,.9,-2+this.z0[i]]),1.8,{stiff:.7,damp:.99,wind:.2,out:[0,-1,.04],col:[['b',this.q,.11]]});
this.ax.lens=[.8,.97,.88,1.02,.76];this.sz=[.86,1.04,.94,1.1,.8];this.rad=this.sz.map(s=>.105*s);
this.hc=[[.07,.036,.02],[.17,.09,.032],[.03,.021,.017],[.2,.07,.026],[.11,.056,.027]];this.sets=[this.ax];this.nt=0;this.nc=new Float32Array(NBR)},
reset(E){this.nt=E.x>4.5?99:0;this.nc.fill(E.x>4.5?NCB:0);this.pose(E.x);this.pose(E.x)},
pose(x){this.q.put(x,[mix(3,0,ss(6.2,9.2,x)),-.2+.05*Math.sin(x*.9),mix(-2,-1.2,ss(8.8,10.6,x))],[0,-.12,-1.2],9.6,11.4)},
tick(F,dt,t,E){if(E.hit&3&&E.x>.4)this.nt++;for(let i=0;i<NBR;i++)this.nc[i]+=(cl(this.nt-i*2,0,NCB)-this.nc[i])*(1-Math.exp(-dt*7))},
step(F,dt,t,E){const x=E.x,go=ss(3.6,6,x)*(1+.35*ss(7.8,8.8,x))*(1-ss(8.8,10.4,x)),a=go*.6*Math.sin(TAU*(x-3.6)/5),sx=go*.3*Math.sin(TAU*(x-3.6)/3.3+1),c=Math.cos(a),s=Math.sin(a);
for(let i=0;i<NBR;i++){const q=this.ax.root[i],px=this.x0[i],pz=this.z0[i];q[0]=px*c-pz*s+sx;q[2]=-2+px*s+pz*c}
this.pose(x);stepStrands(this.ax,F,dt,t,{gz:E.kick*.2});collide(this.ax,this.rad)},
draw(F,U0){const U=this.U={...U0,...this.q.sh(F)},nl=Math.max(24,Math.round(NLB*U.q));this.q.fill(F);drawSurf(U,this.q.m,0,36);use(PBR);for(let i=0;i<NBR;i++){
su(PBR,{...U,uPts:HT,uOut:F.z,uNP:NP,uNV:88,uNL:nl,uW:.0052*Math.sqrt(NLB/nl),uRow:this.ax.row+i,uNc:this.nc[i],uNC:NCB,uSz:this.sz[i],uWX:1e3,uHC:this.hc[i],uTr:.8,uLay:.35,uLat:0,uSh:.8,uHue:0,uDp:0});strokes(PBR,88,3*nl,1)}}});
// 3. Silk. A curtain of red satin hung from the ceiling, gathered; the bass drum fills it like a sail. Then something behind it comes
// forward and leans into it, a long way - a cube, by the look of the cloth, turning over slowly, and pushing at it on every bass drum - and the
// curtain is pulled tight over its corners, in ridges up to the rail, and slides across its faces. The camera goes round to the side to see that. Then the curtain lets go, from the left, and falls, and
// there the cube is.
scene({name:'silk',n:16,zin:-.15,ga:7.6,lamp:[.3,.25,-3.6],lampC:[9,5.2,2.2],key:[-.72,.42,.55],wall:{z:-3.95,gl:.26,yc:.945,yf:-1.45},
fly:[[2.6,-.15,0,-.2,0,0,-1.95,0],[4.6,1.05,.1,-.3,0,-.05,-1.85,-.06],[6.6,1.45,.08,-.45,0,-.05,-1.8,-.08],[8.2,.5,-.05,-.2,0,-.1,-1.8,0],[10.4,-.7,-.15,-.25,0,-.1,-1.8,.05],[12.6,-.9,.25,-.5,0,0,-1.76,.06]],
init(){this.s=sheet(50,30,{x0:-1.75,x1:1.75,y1:.95,h:1.85,z:-2,gather:.84,ty:2,hue:0,its:5,floor:-1.44});this.q=inner(.37)},
reset(E){this.s.fresh=1;this.s.rel=0;this.s.box=null;this.pose(E.x,0);this.pose(E.x,0)},
pose(x,kick){this.q.put(x,[.2*Math.sin(x*.8),-.02+.08*Math.sin(x*.57),mix(-2.95,-1.76,ss(1.6,4.6,x))+.07*kick],[0,-.02,-1.76],11.5,15.5)},
step(F,dt,t,E){const x=E.x,q=this.q,h=q.hs;this.pose(x,E.kick);
this.s.rel=ss(8,9.7,x);this.s.box={c:q.c,cp:q.cp,R:q.R,Rp:q.Rp,h:[h,h,h],m:.045,fr:.25};stepSheet(this.s,F,dt,t,{gz:.2+E.kick*1.3})},
draw(F,U0){const U=this.U={...U0,...this.q.sh(F)};this.q.fill(F);drawSurf(U,this.q.m,0,36);putSheet(this.s)},
// the curtain is half see-through: so it is drawn last of all - after the cube behind it, and after what is to be seen inside that cube - and does not hide itself
late(){drawSurf(this.U,this.s.mesh,0,undefined,{op:.5,nodepth:1})}});
/* Solids with flat faces and no dents, other than cubes. V: the corners; Fc: each face's corners, in order round it. Kept for each face: its corners
(counted anticlockwise seen from outside), its middle, the way it faces, how far out it lies; P: the same as planes, for what must stay outside it;
r2: how far (squared) anything can be from its middle and still be in its way. prism: one with n sides, r across, 2h high. */
const solid=(V,Fc)=>{const F=Fc.map(f=>{let v=f.slice(),c=[0,0,0];for(const i of v)c=add(c,mul(V[i],1/v.length));let n=nrm(cross(sub(V[v[1]],V[v[0]]),sub(V[v[2]],V[v[0]])));if(dot(n,c)<0){v.reverse();n=mul(n,-1)}return{v,c,n,d:dot(n,c)}});
return{V,F,P:F.map(f=>[f.n[0],f.n[1],f.n[2],f.d]),r2:(Math.sqrt(Math.max(...V.map(v=>dot(v,v))))+.12)**2}},
prism=(n,r,h)=>{const V=[],Fc=[[],[]];for(let i=0;i<n;i++){const a=TAU*i/n,j=(i+1)%n;V.push([r*Math.cos(a),h,r*Math.sin(a)],[r*Math.cos(a),-h,r*Math.sin(a)]);Fc[0].push(2*i);Fc[1].push(2*i+1);Fc.push([2*i,2*j,2*j+1,2*i+1])}return solid(V,Fc)};
// 4. Ringlets. Each is one moved strand with a lock of hair wound round it like a spring; the bass drum pulls them up short, and
// they bounce - the springs closing and opening. They hang in four rows, and the camera goes once right round them; then, facing
// the lamp, it goes in among them and out again, twice, turning right over as it does; what it comes near gets out of its way.
// Before that, while the camera goes round, other solids fly through them one after another, turning as they go - a cube, a tetrahedron, prisms of six, three
// and five sides, an octahedron - of glass, with only the faces toward us drawn; the ringlets are pushed aside by them and swing back.
// (S: those solids - each from where to where, from which bar of the scene (for two bars), and about what it turns.)
// Then the cube comes up through the floor into the middle of them, their ends lying over it, and forward out of them.
scene({name:'ringlets',n:16,ga:10,keyC:[.95,.88,.76],lamp:[0,.1,-5.4],lampC:[13,8,3.7],wall:{z:-6.2,gl:.17,yc:.88},
fly:[[2,0,-.1,-.2,0,-.2,-2.2,0],[2.8,1.77,-.05,-.79,0,-.2,-2.2,-.06],[3.6,2.5,0,-2.2,0,-.2,-2.2,-.1],[4.4,1.77,.08,-3.61,0,-.2,-2.2,-.05],[5.2,0,.12,-4.2,0,-.2,-2.2,0],[6,-1.77,.08,-3.61,0,-.2,-2.2,.05],[6.8,-2.5,0,-2.2,0,-.2,-2.2,.1],[7.6,-1.77,-.05,-.79,0,-.2,-2.2,.06],[8.4,0,-.1,-.2,0,-.2,-2.2,0],[9.4,-.35,-.15,-1.6,-.3,-.18,-5,1.6],[10.4,0,-.15,-.7,.05,-.18,-4,3.2],[11.4,.4,-.15,-1.85,.3,-.15,-5,4.8],[12.6,.15,-.1,-.8,.1,-.1,-4,6.2832],[13.7,.1,-.05,.2,0,-.1,-3,6.2832]],
init(){const R=[],r=rnd0(12),ZR=[-1.5,-2.3,-1.9,-2.7];for(let i=0;i<32;i++)R.push([-1.55+3.1*(i+.5+.5*(r()-.5))/32,.9,ZR[i&3]-.08*r()]);
this.q=inner(.3);this.h=strands(R,1.9,{out:[0,-1,0],stiff:.3,damp:.98,wind:.4,col:[['s',0,9,0,.2],['b',this.q,.1]]});this.h.lift=R.map(()=>.6+.8*r());this.sets=[this.h];this.y=this.v=0;
this.S=[[prism(4,.3,.212),[-3,.15,-1.7],[3,-.1,-2.3],1.5],[solid([[1,1,1],[1,-1,-1],[-1,1,-1],[-1,-1,1]].map(v=>mul(v,.23)),[[0,1,2],[0,1,3],[0,2,3],[1,2,3]]),[3,.4,-2.6],[-3,.15,-1.6],2.75],
[prism(6,.27,.2),[-3,-.35,-2.5],[3,.3,-1.7],4],[prism(3,.31,.24),[3,-.15,-1.8],[-3,.35,-2.5],5.25],[prism(5,.28,.2),[-3,.3,-2.1],[3,-.3,-2],6.5],
[solid([[1,0,0],[-1,0,0],[0,1,0],[0,-1,0],[0,0,1],[0,0,-1]].map(v=>mul(v,.34)),[[0,2,4],[0,4,3],[0,3,5],[0,5,2],[1,2,4],[1,4,3],[1,3,5],[1,5,2]]),[3,.1,-2.4],[-3,-.1,-1.7],7.75]]
.map(([g,a,b,t0],k)=>({...g,a,b,t0,k,ax:nrm([Math.sin(k*2.1),1,Math.cos(k*1.3)]),c:[0,-9,0],R:I3}));
for(const g of this.S)this.h.col.push(['c',g,.09]);this.SM=mesh([3,3,3,2,3,4],540)},
reset(E){this.pose(E.x);this.pose(E.x)},
pose(x){this.q.put(x,[.2*Math.sin(x*.7),mix(-2.3,-.15,ss(10.2,12.6,x)),mix(-2.1,-.85,ss(12.2,14.2,x))],[0,-.1,-.85],13,15.3)},
step(F,dt,t,E){const x=E.x,wv=ss(6,7,x)*(1-ss(9.6,10.4,x)),c=this.h.col[0],e=E.ce;
for(const g of this.S){const u=(x-g.t0)/2;if(u>0&&u<1){g.c=mix3(g.a,g.b,u);g.c[1]+=.12*Math.sin(u*9+g.t0);g.R=rot(g.ax,u*7+g.t0)}else g.c=[0,-9,0]}
if(e&&e[2]<0){c[1]=e[0];c[2]=e[1];c[3]=e[2]}else c[2]=9;this.pose(x);
this.v+=(-30*this.y-2.2*this.v)*dt+(E.hit&1?.8:0);this.y+=this.v*dt;stepStrands(this.h,F,dt,t,{gz:E.kick*.2,ry:.05*E.kick,gx:wv?(sx=>.5*wv*Math.sin(sx*1.3-(x-6)*1.6)):null})},
draw(F,U0){const U=this.U={...U0,...this.q.sh(F)};this.q.fill(F);drawSurf(U,this.q.m,0,36);drawStr(U,F,HT,NP,this.h.row,this.h.n,{ch:34,nv:220,w:.0048,spread:.014,hc:[.17,.08,.027],curl:.08,turns:8.5,lay:.6,lat:0,lenk:cl(.78-this.y,.5,1)});this.F0=F},
// the solids, last of all (they are see-through): each face as a fan from its middle, with how far from the face's edge every corner of the fan is; only the faces toward us
late(){const a=this.SM.a,F=this.F0;let o=0;for(const g of this.S)if(g.c[1]>-8)for(const f of g.F){const n=fDir(F,mv(g.R,f.n)),cw=fAt(F,add(g.c,mv(g.R,f.c))),m=f.v.length;
for(let i=0;i<m;i++){const p=g.V[f.v[i]],q=g.V[f.v[(i+1)%m]],e=sub(q,p),h=len(cross(e,sub(f.c,p)))/len(e),t=fDir(F,mv(g.R,nrm(e)));
for(const[w,d]of[[cw,h],[fAt(F,add(g.c,mv(g.R,p))),0],[fAt(F,add(g.c,mv(g.R,q))),0]]){a.set(w,o);a.set(n,o+3);a.set(t,o+6);a[o+9]=d;a[o+10]=0;a[o+11]=a[o+12]=a[o+13]=1;a[o+14]=12;a[o+15]=0;a[o+16]=g.k;a[o+17]=0;o+=18}}}
this.SM.n=o/18;if(o){fill(this.SM);gl.enable(gl.CULL_FACE);gl.cullFace(gl.BACK);drawSurf(this.U,this.SM,0,undefined,{nodepth:1});gl.disable(gl.CULL_FACE)}}});
// 5. Rope. Three strands laid round each other, each of them fibres twisted the other way, from one wall to the other. Somebody is
// twisting it, a turn to the bar, tighter and tighter, and flicking it: a hump runs along it like the crack of a whip, steeper the
// further it gets. The third flick, where it meets the twist in the middle, is too much - a rope twisted too far gets rid of a turn
// the only way it can - and it throws a loop. Then, once, the rope is pulled tight (XP, half a bar on): the loop is gone with a snap,
// the rope is hauled a hand's breadth through the wall, and is straight, thinner, and humming. On the tight rope the cube comes down,
// and is thrown up by it, less high each time (XL: it lands on the bass drum, and on three of the knocks after it) - and by the time it lies still the camera is in it.
const RPF=40,RL=6.6,RW=1.8,PV=8.6,XC=.8,XP=4.5,XL=[5,5.375,5.6875,5.875],FH=1.57,CG=6,CT=.12; // (PV: how fast a flick runs, in units a bar; XC: where along the rope the cube comes down; FH: from how high; CG: how fast things fall; CT: how long the rope has it each time)
scene({name:'rope',n:6,ga:4.4,lamp:[-.7,.45,-3.2],key:[-.3,.6,.75],wall:{xw:1.9},
fly:[[2.2,-.7,-.1,-.45,.25,-.3,-1.75,.06],[3.6,.1,-.15,-.5,0,-.2,-1.75,0],[4.45,.4,0,-.36,.15,-.25,-1.75,-.02],[5,.95,.1,-.12,.7,-.05,-1.75,-.03],[5.6,1.25,.3,-.4,.8,.05,-1.75,.03]],
// the cube falls into the picture; once it has landed the camera goes with it, wherever the rope throws it (tc: where the camera takes the cube to be - a little behind where it is, as a hand would follow it)
aim(x){return x>4.9&&this.tc?[this.tc,.85*ss(4.9,5.1,x)]:null},
// the flicks: [the bar (of the scene's own) at which it passes the middle; which way it runs (1: from the left); how hard (and which way up); 1 if it gets no further than the middle]
fl:[[1.97,1,.9,0],[2.97,-1,-.8,0],[3.97,1,1.1,1]],
init(){this.row=rows(3*RPF);this.fr=[this.row,this.row+3*RPF];const q=rnd0(41);this.fb=[];for(let i=0;i<3*RPF;i++){const st=q()>.96;this.fb.push([st?1.15+.25*q():Math.pow(q(),.35),q()*TAU,q(),st?1:0])}this.th=this.tv=0;this.cx=new Float32Array(NF*3);this.q=inner(.2)},
reset(E){this.tc=null;this.th=E.x>XP?0:E.x>4?2.85:0;this.tv=0;this.sc=XC},
// The cube at x - worked out, not moved along step by step, so that it lands when the music has it land: how high above lying on the rope
// (less than nothing: the rope has given that much under it), how far down the rope is where it lies, and how it is tipped. It waits out of
// sight, falls, and each time it lands the rope takes it down a little way and throws it up again, not as hard as it came - the last time not at all.
cb(x){const t=x*BAR,h=CT/2,T0=XL[0]*BAR-h,tf=Math.sqrt(2*FH/CG),n=XL.length-1;if(t<T0-tf)return[FH,0,.4];if(t<T0){const u=t-T0+tf;return[FH-CG*u*u/2,0,.4*(1-u/tf)]}
let k=0;while(k<n&&t>=XL[k+1]*BAR-h)k++;
const a=XL[k]*BAR-h,D=k<n?(XL[k+1]-XL[k])*BAR-CT:0,v=CG*D/2,vi=k?CG*((XL[k]-XL[k-1])*BAR-CT)/2:Math.sqrt(2*FH*CG),u=(t-a)/CT;
if(u<1){const d=(vi*u*(1-u)*(1-u)+v*u*u*(1-u))*CT;return[-d,d,0]}
const w=t-a-CT;return k<n?[v*w-CG*w*w/2,-v/56.5*Math.exp(-w/.16)*Math.sin(56.5*w),[.35,-.25,.1][k]*Math.sin(Math.PI*Math.min(1,w/D))]:[0,0,0]},
tick(F,dt,t,E){const x=E.x,thrown=x>=3.97,tp=(x-XP)*BAR,pl=tp>=0,tt=pl?1-Math.exp(-tp/.06):0,tg=pl?0:thrown?2.85:.1*ss(1.5,3.9,x)*(1+.5*Math.sin(t*7)),P=[],cb=this.cb(x);
for(const f of this.fl){const s0=f[1]*(x-f[0])*PV,w=cl((f[1]*s0+1.9)/3.8,0,1),sg=.22-.06*w;if(Math.abs(s0)<RL/2+1)P.push([s0,f[2]*(.45+.35*w)*sg*1.65*(f[3]?1-ss(-.2,.2,f[1]*s0):1),sg])}
if(cb[1])P.push([this.sc,-cb[1],.42]); // (and where the cube is on it, it gives)
// how far the loop is thrown: a spring, let go on the bar line, that overshoots and settles - and a far stiffer one, back to nothing, when the rope is pulled
for(let k=0,n=Math.max(1,Math.round(dt*240));k<n;k++){this.tv+=((tg-this.th)*(pl?300:20)-this.tv*(pl?21:3.4))/240;this.th+=this.tv/240}
const Th=this.th,lp=ss(1.4,2.4,Th),twr=(9+6*ss(0,3.9,x))*(1-.3*lp),spin=TAU*Math.min(x,3.97)*.55+(thrown?TAU*.16*(Math.min(x,XP)-3.97):0)+.3*E.kick*(1-tt),
// pulled: how far it has been hauled along (sl), how it hums (tw: the middle of it up and down, less and less) - and it is thinner
sl=pl?-.4*(1-Math.exp(-tp/.17)):0,tw=pl?.07*Math.exp(-tp/.28)*Math.sin(TAU*8*tp):0,
ds=RL/(NF-1),C=this.cx,a=.062*(1-.12*tt),rho=.058*(1-.1*tt),zc=.17;
// the rope's middle line: straight, but for a stretch in the middle where it turns up, over and down again - far enough, and its feet
// cross - and for wherever a flick is on its way along it
let px=0,py=0,mx=0,bd=9,sa=XC;
for(let i=0;i<NF;i++){const sg=(i-(NF-1)/2)*ds,w=sg/RW;let th=Math.abs(w)<.5?-Th*Math.sin(TAU*w):0;
for(const p of P){const z=(sg-p[0])/p[2];if(z*z<16)th-=p[1]*z/p[2]*Math.exp(-.5*z*z)}
C[i*3]=px;C[i*3+1]=py;C[i*3+2]=th;if(i==(NF>>1))mx=px;px+=Math.cos(th)*ds;py+=Math.sin(th)*ds}
const sag=.05*(1-tt),tilt=-.1,ct=Math.cos(tilt),st=Math.sin(tilt),y0=-.3-.1*lp;
for(let i=0;i<NF;i++){const sg=(i-(NF-1)/2)*ds,so=sg-sl,w=cl(sg/RW,-.5,.5),th=C[i*3+2],lx=C[i*3]-mx,ly=C[i*3+1]-sag*(1-lp)*Math.cos(Math.PI*cl(sg/RL*1.2,-.5,.5))+tw*Math.cos(Math.PI*cl(lx/3.8,-.5,.5)),
cz=zc*lp*Math.sin(Math.PI*w),tx=Math.cos(th),ty=Math.sin(th),nx=-ty,ny=tx;
// the middle line as the scene counts places (a little off level), the direction across it in the plane of the loop, and the one out of that plane
const sx=lx*ct-ly*st,sy=y0+lx*st+ly*ct,c=fAt(F,[sx,sy,-1.75+cz]),N=fDir(F,[nx*ct-ny*st,nx*st+ny*ct,0]),B=F.z;if(sg>0&&Math.abs(sx-XC)<bd){bd=Math.abs(sx-XC);sa=sg}
for(let k=0;k<3;k++){const ph=TAU*k/3+twr*so+spin,cp=Math.cos(ph),sp=Math.sin(ph),e1=[N[0]*cp+B[0]*sp,N[1]*cp+B[1]*sp,N[2]*cp+B[2]*sp],e2=[-N[0]*sp+B[0]*cp,-N[1]*sp+B[1]*cp,-N[2]*sp+B[2]*cp];
for(let j=0;j<RPF;j++){const f=this.fb[k*RPF+j],ps=f[1]-twr*.75*so+(f[3]?.4*Math.sin(so*7+f[2]*40):0),r=rho*f[0],c1=Math.cos(ps),s1=Math.sin(ps),o=((this.row+k*RPF+j)*NF+i)*4;
FP[o]=c[0]+e1[0]*(a+r*c1)+e2[0]*r*s1;FP[o+1]=c[1]+e1[1]*(a+r*c1)+e2[1]*r*s1;FP[o+2]=c[2]+e1[2]*(a+r*c1)+e2[2]*r*s1;
// a fibre down in the groove between two strands is in their shade
FP[o+3]=f[3]?1:(.25+.75*ss(-.75,.35,c1))*(.55+.45*f[0])}}}
this.sc=sa;
{const q=this.q;q.c=[XC,-.3+XC*Math.tan(tilt)+.112+q.hs+cb[0],-1.75];q.R=rot([0,0,1],cb[2]);this.tc=this.tc?mix3(this.tc,q.c,1-Math.exp(-dt/.14)):q.c}},
draw(F,U0){const U=this.U={...U0,...this.q.sh(F)};this.q.fill(F);drawSurf(U,this.q.m,0,36);drawStr(U,F,FT,NF,this.row,3*RPF,{ch:3,nv:400,wx:1e3,w:.0062,spread:.004,hc:[.34,.24,.13],lay:.5,lat:0,sh:.3,tr:.5,rag:0,tap:0})}});
/* What is inside the last cube: a solid of twenty faces, dark, covered in long fur - which hangs with its own weight, and does
nothing of itself: it is thrown about by what it grows on.
How the solid is thrown about (sim): every bass drum is a blow that sets it turning - each time about another line, and the other
way from the last - and knocks it sideways; in the run of claps before a new phrase every clap turns it back the other way, as a
head is shaken. Between blows it slows down and stops. (lv: how hard the music is going, 0 to 4. R: how it is turned; w: how fast
it is turning, and about what; p, pv: how far it has been knocked off its place, and how fast it is going.)
From that follows how every point of it is being hurried along at this moment: so much for the solid as a whole, and so much more
(three numbers by three) according to where the point is. A hair's tip, hung on its root as on a spring, is left behind by just
that; and since what a spring does with two pushes at once is what it does with each added together, it is enough to keep twelve
numbers swinging - three and nine - for the shader to work out any hair's tip from. That is done twice, for a slack spring and a
stiff one (SA, SB: each the twelve, then how fast each is changing).
When it has been shaken long enough the fur lets go of it (see the spiders), and the cube is thrown about less and less. */
const NFUR=30000,NPF=12,FUR={RF:.155,R:I3,w:[0,0,0],p:[0,0,0],pv:[0,0,0],kc:0,cc:0,SA:new Float64Array(24),SB:new Float64Array(24),
AX:[[0,1,0],[1,.1,.25],[.25,.35,1],[-.6,1,.3],[1,.5,-.5],[.1,1,-.2]].map(nrm),
init(){const A=new Float32Array(256*256*4),r=rnd0(77),RF=this.RF,V=[],Fc=[],PH=(1+Math.sqrt(5))/2,s=1/Math.hypot(1,PH);
for(const a of[-1,1])for(const b of[-1,1])V.push([0,a*s,b*PH*s],[a*s,b*PH*s,0],[b*PH*s,0,a*s]);
const ed=Math.min(...V.slice(1).map(v=>len(sub(v,V[0]))));
for(let i=0;i<12;i++)for(let j=i+1;j<12;j++)for(let k=j+1;k<12;k++){const d=(a,b)=>Math.abs(len(sub(V[a],V[b]))-ed)<1e-6;
if(d(i,j)&&d(j,k)&&d(i,k)){const c=mul(add(add(V[i],V[j]),V[k]),1/3),up=dot(cross(sub(V[j],V[i]),sub(V[k],V[i])),c)>0;Fc.push({v:up?[i,j,k]:[i,k,j],n:nrm(c)})}}
this.V=V;this.F=Fc;
for(let i=0;i<NFUR;i++){const f=Fc[i%20],a=Math.sqrt(r()),b=r(),u=1-a,v=a*(1-b),w=a*b,P=V[f.v[0]],Q=V[f.v[1]],S=V[f.v[2]],p=[P[0]*u+Q[0]*v+S[0]*w,P[1]*u+Q[1]*v+S[1]*w,P[2]*u+Q[2]*v+S[2]*w],n=nrm(mix3(f.n,nrm(p),.45));
A.set([p[0]*RF,p[1]*RF,p[2]*RF,r()],i*4);A.set([n[0],n[1],n[2],(i%20)/20],(i+128*256)*4)}
this.T=data(256,256,A);this.M=mesh([3,3,3,2,3,4],60)},
// calm: how far the blows have been taken out of it (it then turns slowly, of itself); al: how far it has been brought round to stand
// square to us, and to rest (A: from how it stood then, R0, to the nearest way of standing square, T - a turn of an about ax)
sim(dt,hit,lv,turn,calm,al,t){const w=this.w,g=1-.82*calm,J=([0,3.5,6,7.5,9][lv]||0)*g,n=4,h=dt/n,wt=mul(nrm([Math.sin(t*.31),1,Math.cos(t*.23)]),.5*calm);let jw=[0,0,0],jp=[0,0,0];
if(hit&1&&J){const a=this.AX[this.kc%6],s=this.kc&1?-1:1;jw=mul(a,J*s);jp=mul(nrm(cross(a,[.3,.2,1])),.45*s*g);this.kc++}
if(hit&2&&turn){const s=this.cc++&1?-1:1;jw=add(jw,[1.5*s,9*s,0]);jp=add(jp,[.3*s,0,0])}
if(al>0){
if(!this.A){const R=this.R,T=[0,0,0,0,0,0,0,0,0],ur=[0,1,2],uc=[0,1,2];let li=0;
for(let m=0;m<3;m++){let bi=ur[0],bj=uc[0],bv=-1;for(const i of ur)for(const j of uc)if(Math.abs(R[i*3+j])>bv){bv=Math.abs(R[i*3+j]);bi=i;bj=j}
T[li=bi*3+bj]=R[bi*3+bj]<0?-1:1;ur.splice(ur.indexOf(bi),1);uc.splice(uc.indexOf(bj),1)}
if(T[0]*(T[4]*T[8]-T[5]*T[7])-T[1]*(T[3]*T[8]-T[5]*T[6])+T[2]*(T[3]*T[7]-T[4]*T[6])<0)T[li]=-T[li];
const D=mm(T,[R[0],R[3],R[6],R[1],R[4],R[7],R[2],R[5],R[8]]),an=Math.acos(cl((D[0]+D[4]+D[8]-1)/2,-1,1));
this.A={T,R0:R,an,ax:an>1e-4?nrm([D[7]-D[5],D[2]-D[6],D[3]-D[1]]):[0,1,0]}}
const A=this.A;this.R=al<1?mm(rot(A.ax,A.an*ss(0,1,al)),A.R0):A.T;w[0]=w[1]=w[2]=0}
else this.A=null;
for(let k=0;k<n;k++){const w0=[w[0],w[1],w[2]],d=Math.exp(-h/.2),pa=[0,0,0];
for(let i=0;i<3;i++){if(!this.A)w[i]=wt[i]+(w[i]-wt[i]+(k?0:jw[i]))*d;pa[i]=-170*this.p[i]-11*this.pv[i]+(k?0:jp[i]/h)}
const al2=mul(sub(w,w0),1/h),ww=dot(w,w),Bm=mm([w[0]*w[0]-ww,w[0]*w[1]-al2[2],w[0]*w[2]+al2[1],w[1]*w[0]+al2[2],w[1]*w[1]-ww,w[1]*w[2]-al2[0],w[2]*w[0]-al2[1],w[2]*w[1]+al2[0],w[2]*w[2]-ww],this.R);
for(const[S,kk,c]of[[this.SA,36,1.7],[this.SB,121,4.8]])for(let i=0;i<12;i++){S[12+i]+=(-kk*S[i]-c*S[12+i]-(i<3?pa[i]:Bm[i-3]))*h;S[i]+=S[12+i]*h}
for(let i=0;i<3;i++){this.pv[i]+=pa[i]*h;this.p[i]+=this.pv[i]*h}
const wl=len(w);if(wl>1e-6&&!this.A)this.R=mm(rot(mul(w,1/wl),wl*h),this.R)}
// (and it is kept true: its three directions at right angles, each of length one)
{const R=this.R,x=nrm([R[0],R[1],R[2]]),y0=[R[3],R[4],R[5]],y=nrm(sub(y0,mul(x,dot(x,y0))));this.R=[...x,...y,...cross(x,y)]}},
// a place given as it will be when the cube stands square (counted from its middle): where on the cube that is
gi(v){const T=this.A.T;return[T[0]*v[0]+T[3]*v[1]+T[6]*v[2],T[1]*v[0]+T[4]*v[1]+T[7]*v[2],T[2]*v[0]+T[5]*v[1]+T[8]*v[2]]},
// the solid itself (c: where its middle is; M: how it is turned)
core(c,M,U){const a=this.M.a;let o=0;
for(const f of this.F){const n=mv(M,f.n),t=mv(M,nrm(sub(this.V[f.v[1]],this.V[f.v[0]])));for(let j=0;j<3;j++){a.set(add(mul(mv(M,this.V[f.v[j]]),this.RF*.985),c),o);a.set(n,o+3);a.set(t,o+6);a[o+9]=a[o+10]=0;a[o+11]=a[o+12]=a[o+13]=1;a[o+14]=7;a[o+15]=0;a[o+16]=0;a[o+17]=0;o+=18}}
this.M.n=60;fill(this.M);drawSurf(U,this.M)},
// its fur. o.mo: some of it may have let go, and be spiders; o.one: all of it (o.n hairs) is the one spider of that row; o.w: how wide a hair; o.sc: how far its colour has gone from the fur's to a spider's brown
fur(c,M,U,o){o=o||{};const n=Math.round((o.n||NFUR)*Math.max(.3,U.q)),A=this.SA,B=this.SB,sc=o.sc||0,m3=S=>({m:[S[3],S[6],S[9],S[4],S[7],S[10],S[5],S[8],S[11]]});use(PFU);
su(PFU,{...U,uPts:this.T,uSp:SPI.T,uFR:{m:[M[0],M[3],M[6],M[1],M[4],M[7],M[2],M[5],M[8]]},uFO:c,uGrav:[0,-1,0],uNP:NPF,uW:o.w||.0016*Math.sqrt(NFUR/n),uWX:U.wx,uLen:.24,uGrow:1,
uOne:o.one==null?-1:o.one,uMo:o.mo||o.one!=null?1:0,uShl:1,
uD0a:[A[0],A[1],A[2]],uDa:m3(A),uD0b:[B[0],B[1],B[2]],uDb:m3(B),uHC:mix3([.46,.33,.19],[.4,.19,.085],sc),uTr:.9,uLay:1,uLat:0,uSh:.5-.2*sc,uHue:.1,uDp:0});strokes(PFU,NPF,n,0)}};
/* The spiders. When the solid has been shaken long enough its fur lets go of it: a tuft at a time - the hair of one of its twenty
faces - on the bass drum; it bursts out through the cube and comes down on the outside of it as a spider, every hair of the tuft a
hair of one of two spiders' legs or of its back. Forty of them, big and small - tarantulas: thick in the leg, the body carried high, the
knees higher still - and they walk about the cube, round its edges from face to face, faster when the bass drum has just gone, keeping
out of each other's way; a run of claps stops them where they are, and they rear: the front of the body up, and the front legs in the air.
A spider is moved here as a place on the cube's surface (p, on the face that faces n), a heading (d), and eight feet. A foot stays
where it was put, on the cube, until its turn comes to be lifted and put down further on - four at a time, as spiders do: on
each side the first and third with the other side's second and fourth. How far a step has got depends on how far the spider has
got, so a foot on the ground does not slide. From the body and a foot follow the hip and the knee (a leg is two lengths; taking
the knee to be up, there is one place for it; the joint below the knee the shader adds). All that is written into a sheet of numbers, a row to a spider, as the world counts
places, for the shader, which lays the hairs along the legs (see there). Row NSP is the one that sits in the web.
T: the bar at which a spider's tuft lets go; m: how far it has got since (1: every hair has arrived). */
const SPW=27,SPD=new Float32Array(SPW*(NSP+1)*4),SPM=.85,LA=[.55,1.12,1.85,2.5],LR=[.122,.108,.104,.12],KF=[.5,.22,-.25,-.55],LAM=.115,SWF=.42,
// into row k of the sheet, at i: the place v (or with dr the direction v) of a thing that is turned M and stands at c; w: a number to go with it
spPut=(k,M,c,i,v,w,dr)=>{const o=(k*SPW+i)*4;SPD[o]=(dr?0:c[0])+M[0]*v[0]+M[1]*v[1]+M[2]*v[2];SPD[o+1]=(dr?0:c[1])+M[3]*v[0]+M[4]*v[1]+M[5]*v[2];SPD[o+2]=(dr?0:c[2])+M[6]*v[0]+M[7]*v[1]+M[8]*v[2];SPD[o+3]=w||0},
// leg l of a spider whose body is at b - up u, ahead f, to its left s, size z - with its foot at ft: the hip and the knee (the knee is up -
// and, KF, the front legs' ahead of the straight line from hip to foot, the back legs' behind it, so that seen from above a leg is bent too)
legIK=(b,u,f,s,z,l,ft)=>{const al=LA[l>>1],sg=l&1?-1:1,c=(Math.cos(al)*.024+.016)*z,si=Math.sin(al)*.024*z*sg,dn0=-.006*z,hp=[b[0]+f[0]*c+s[0]*si+u[0]*dn0,b[1]+f[1]*c+s[1]*si+u[1]*dn0,b[2]+f[2]*c+s[2]*si+u[2]*dn0],
D=sub(ft,hp),a1=.08*z,a2=.1*z,dl=cl(len(D),a2-a1+1e-4,a1+a2-1e-4),dn=nrm(D),a=(a1*a1-a2*a2+dl*dl)/(2*dl),hg=Math.sqrt(Math.max(a1*a1-a*a,0)),ku=add(u,mul(f,KF[l>>1])),up=nrm(sub(ku,mul(dn,dot(ku,dn))));
return[hp,[hp[0]+dn[0]*a+up[0]*hg,hp[1]+dn[1]*a+up[1]*hg,hp[2]+dn[2]*a+up[2]*hg]]},
SPI={S:[],pb:0,
// KS: the bass drums on which tufts let go. Of each tuft's two spiders one comes down where the cube is nearest it (for the eight that would be a corner: a little way off
// it), the other somewhere on the same face
init(KS){this.T=data(SPW,NSP+1,SPD);this.M=mesh([3,3,3,2,3,4],(NSP+1)*120);const r=rnd0(9),h=this.h=HE*KL;let nc=0;
for(let k=0;k<NSP;k++){const f=FUR.F[k%20],a=f.n.map(Math.abs),mx=Math.max(...a);let ax=a.indexOf(mx),p;
if(mx<.7){ax=nc++%3;p=f.n.map(v=>(v<0?-1:1)*h*(.25+.4*r()))}else p=f.n.map(v=>k<20?v/mx*h:(r()-.5)*1.5*h);
const n=[0,0,0],t1=[0,0,0];n[ax]=f.n[ax]<0?-1:1;p[ax]=n[ax]*h;t1[(ax+1)%3]=1;const an=r()*TAU;
this.S.push({k,p0:p,n0:n,d0:add(mul(t1,Math.cos(an)),mul(cross(n,t1),Math.sin(an))),sz:k<20?.8+.35*r():.5+.3*r(),v:.2+.1*r(),T:KS[(k%20)*KS.length/20|0],sd:r()})}
this.reset()},
reset(){for(const s of this.S){s.p=[...s.p0];s.n=[...s.n0];s.d=[...s.d0];s.B=[...s.p0];s.ph=s.sd;s.fl=s.m=s.re=0;s.L=[]}},
// b: the bar; E: what the music is doing; M, c: how the cube is turned and where it is; av: a place on it that they keep clear of (or none)
step(dt,b,E,M,c,av,t){const h=this.h,S=this.S;if(b<this.pb)this.reset();this.pb=b; // (if the piece has gone back, they begin again)
for(const s of S){const m=s.m=(b-s.T)/SPM,z=s.sz;SPD[(s.k*SPW+1)*4+3]=m;if(m<=0)continue;let n=s.n,p=s.p,d=s.d;
const go=ss(1.2,1.5,m);s.fl=Math.max(s.fl*Math.exp(-dt/.1),E.hit&2&&E.turn?1:0);
if(go>0){
// it wanders; it keeps off the others, and off where it is not wanted
let tw=1.5*Math.sin(t*.9+s.k*1.7)+1.1*Math.sin(t*2.1+s.k*4.1);
const off=(q,r,g)=>{const e=sub(q,s.B),l=len(e);if(l<r)tw+=(dot(cross(d,e),n)>0?-1:1)*(1-l/r)*g};
for(const o of S)if(o!==s&&o.m>1)off(o.B,.115*(z+o.sz),7);if(av)off(av,.36,18);
const a=tw*dt,cn=cross(n,d),v=s.v*z*(.45+1.6*E.kick)*go*(1-s.fl);d=nrm(add(mul(d,Math.cos(a)),mul(cn,Math.sin(a))));p=add(p,mul(d,v*dt));s.ph+=v*dt/(LAM*z);
// over an edge: round it, and on down the next face
for(let a=0;a<3;a++)if(!n[a]){const e=Math.abs(p[a])-h;if(e>0){const sg=p[a]<0?-1:1,cc=d[a]*sg,nn=[0,0,0];nn[a]=sg;p[a]=sg*h;d[a]=0;for(let i=0;i<3;i++){p[i]-=n[i]*e;d[i]-=n[i]*cc}n=nn;d=nrm(d);break}}
s.p=p;s.d=d;s.n=n}
// which way is up where it stands (near an edge, partly the next face's way), ahead, and to its left; and its body, which the bass drum lifts a little.
// Stopped by the claps it rears (ru: how far) - the body tipped up in front (Ub, Fb), the first two pairs of feet off the ground, the first highest
const u=[n[0],n[1],n[2]],re=.09*z;for(let a=0;a<3;a++)if(!n[a]){const dl=h-Math.abs(p[a]);if(dl<re)u[a]=(p[a]<0?-1:1)*(re-dl)/(re+dl)}
const U=nrm(u),F=nrm(sub(d,mul(U,dot(d,U)))),L=cross(U,F),cn=cross(n,d),uf=ss(.7,1.25,m),ru=(s.re+=((s.fl>.15?1:0)-s.re)*(1-Math.exp(-dt/.09)))*go,ra=.42*ru,cr=Math.cos(ra),sr=Math.sin(ra),
Ub=add(mul(U,cr),mul(F,-sr)),Fb=add(mul(F,cr),mul(U,sr)),B=s.B=add(add(p,mul(U,z*(.06*mix(.6,1,uf)+.026*ru+.008*E.kick*go))),mul(F,-.012*z*ru));
for(let l=0;l<8;l++){const j=l>>1,sg=l&1?-1:1,ro=LR[j]*z*mix(.35,1,uf),ca=Math.cos(LA[j])*ro,sa=Math.sin(LA[j])*ro*sg,
// where the foot would stand if the spider stood still (q: so much further on) - a place in the plane of its face, and if that is beyond the face's edge, round the edge
hm=q=>{const o=[p[0]+d[0]*(ca+q)+cn[0]*sa,p[1]+d[1]*(ca+q)+cn[1]*sa,p[2]+d[2]*(ca+q)+cn[2]*sa];
for(let a=0;a<3;a++)if(!n[a]){const e=Math.abs(o[a])-h;if(e>0){o[a]=o[a]<0?-h:h;for(let i=0;i<3;i++)o[i]-=n[i]*e}}return o};
let g=s.L[l];if(!g||go<=0)g=s.L[l]={p:hm(0),c:-1,sw:0};
else{const ps=s.ph+((j+(l&1))&1)*.5+.04*j,cy=Math.floor(ps),ph=ps-cy;
if(!g.sw&&cy!=g.c&&ph<SWF){g.sw=1;g.c=cy;g.f=g.p;g.t=hm(LAM*z*(1+SWF)/2);g.t0=ph/SWF}
if(g.sw){if(cy!=g.c||ph>=SWF){g.sw=0;g.p=g.t}
else{const tu=cl((ph/SWF-g.t0)/(1-g.t0+1e-6),0,1),e=tu*tu*(3-2*tu),li=.042*z*Math.sin(Math.PI*tu),q=[0,1,2].map(i=>mix(g.f[i],g.t[i],e)+U[i]*li),mx=Math.max(Math.abs(q[0]),Math.abs(q[1]),Math.abs(q[2]));
if(mx<h){const a=Math.abs(q[0])==mx?0:Math.abs(q[1])==mx?1:2;q[a]=q[a]<0?-h:h}g.p=q}}}
const fp=ru>0&&j<2?add(g.p,add(mul(U,z*ru*(j?.04:.115)),mul(F,z*ru*(j?.01:.04)))):g.p,hk=legIK(B,Ub,Fb,L,z,l,fp);spPut(s.k,M,c,3+3*l,hk[0]);spPut(s.k,M,c,4+3*l,hk[1]);spPut(s.k,M,c,5+3*l,fp)}
spPut(s.k,M,c,0,B,z);spPut(s.k,M,c,1,Ub,m,1);spPut(s.k,M,c,2,Fb,s.sd,1)}
redata(this.T,SPD,0,NSP)},
// their bodies, under the hair: two dark brown lumps each, a little smaller than their coats, there as soon as half the hair is (rows k0 to k1)
bodies(U,k0,k1){const a=this.M.a,V=FUR.V,D=SPD;let o=0;
for(let k=k0;k<k1;k++){const r=k*SPW*4,g=ss(.5,1,D[r+7])*D[r+3];if(g<=0)continue;
const B=[D[r],D[r+1],D[r+2]],Uu=[D[r+4],D[r+5],D[r+6]],Ff=[D[r+8],D[r+9],D[r+10]],Ss=cross(Uu,Ff);
for(const[cf,cu,rx,ry,rz]of[[-.056,.012,.032,.03,.044],[.016,0,.024,.0145,.0275]]){const ce=add(B,add(mul(Ff,cf*g),mul(Uu,cu*g)));
for(const f of FUR.F){
for(let j=0;j<3;j++){const v=V[f.v[j]],x=v[0]*rx*g,y=v[1]*ry*g,w=v[2]*rz*g,nn=nrm(add(add(mul(Ss,v[0]/rx),mul(Uu,v[1]/ry)),mul(Ff,v[2]/rz)));a[o]=ce[0]+Ss[0]*x+Uu[0]*y+Ff[0]*w;a[o+1]=ce[1]+Ss[1]*x+Uu[1]*y+Ff[1]*w;a[o+2]=ce[2]+Ss[2]*x+Uu[2]*y+Ff[2]*w;
a.set(nn,o+3);a.set(Ff,o+6);a[o+9]=a[o+10]=0;a[o+11]=3.2;a[o+12]=1.9;a[o+13]=1.1;a[o+14]=7;a[o+15]=a[o+16]=a[o+17]=0;o+=18}}}}
this.M.n=o/18;if(o){fill(this.M);drawSurf(U,this.M)}}};
/* The web. What is in the black of the tile: one spider, sitting head down in the middle of nothing, the lamp behind it. Then its
web is there, thread by thread, in time: the spokes out from the middle, a sixteenth each; then the spiral, round and round and
outward. The bass drum is a blow on the middle of it from behind, which the whole web takes and gives back; and when the music lets
it be, it comes to rest - and the picture goes dim, and draws back, as if that were all. Then, six bars in, the spider jumps: at
whoever is looking, turning head on as it comes, its legs spread, until it is all there is to see; and that is the end.
The web is moved as a net: WSP spokes of nineteen points each, held where they meet the frame, every point tied to the next along
its spoke and to its neighbours on the spokes either side - by threads, which pull and do not push. The spiral is laid across the
spokes wherever it comes, between their points; its turns join end to end. ord: the order the spokes come in (those the spider's
feet are on - ft: which, and how far out - are there from the first); tn: for each turn of the spiral, how far out it begins, how
much further out it gets in going round, the bar it begins at, how long it takes. (The spiral is round, whatever shape the frame is.) */
const WSP=19,WCO=[[1.38,.42],[.2,1.05],[-1.3,.78],[-1.12,-.82],[.72,-1]],WEB=scene({name:'web',off:1,n:8,lamp:[.5,.32,-3.5],lampC:[5,4,3.2],key:[-.4,.5,.75],keyC:[.7,.8,1.05],amb:[.012,.014,.02],
wall:{z:-5,col:[.012,.013,.017],gl:.1,yc:2.4,yf:-2.4},
fly:[[0,.03,.1,-1.3,0,.08,-2,0],[1.3,.12,.03,-1.05,0,.07,-2,.05],[4,-.22,-.04,-.6,0,.05,-2,-.04],[6,.1,.06,-.05,0,.06,-2,.02],[6.16,.04,.07,-.75,0,.08,-2,0],[8,.04,.07,-.75,0,.08,-2,0]],
init(){const r=rnd0(31),N=this.N=1+WSP*19,x=this.x=new Float32Array(N*3),H=this.hub=[0,.08,-2],C=this.C=[],z=this.z=1.25,used=[];
this.q=new Float32Array(N*3);this.pin=new Uint8Array(N);this.rm=[];this.an=[];this.ft=[];this.tn=[];
for(let i=0;i<WSP;i++){const th=TAU*(i+.5+.4*(r()-.5))/WSP,c=Math.cos(th),s=Math.sin(th);let R=1;
// (how far along its own direction a spoke meets the frame)
for(let k=0;k<5;k++){const A=WCO[k],B=WCO[(k+1)%5],ex=B[0]-A[0],ey=B[1]-A[1],det=s*ex-c*ey,l=(A[1]*ex-A[0]*ey)/det,t=(c*A[1]-s*A[0])/det;if(l>0&&t>=0&&t<=1)R=l}
this.rm.push(R);this.an.push(th);
for(let j=1;j<20;j++){const k=(i*19+j)*3,d=R*j/19;x[k]=H[0]+c*d;x[k+1]=H[1]+s*d;x[k+2]=H[2]}this.pin[i*19+19]=1}
x.set(H,0);this.r0=x.slice();
for(let i=0;i<WSP;i++)for(let j=1;j<20;j++){const a=i*19+j,e=((i+1)%WSP)*19+j;C.push(a,j>1?a-1:0,.965*this.rm[i]/19);if(j<19)C.push(a,e,Math.hypot(x[a*3]-x[e*3],x[a*3+1]-x[e*3+1]))}
for(let l=0;l<8;l++){const al=LA[l>>1],th=Math.atan2(-Math.cos(al),(l&1?-1:1)*Math.sin(al));let bi=0,bd=9;
for(let i=0;i<WSP;i++){const d=Math.abs(((this.an[i]-th)%TAU+TAU+Math.PI)%TAU-Math.PI);if(d<bd&&!used.includes(i)){bd=d;bi=i}}used.push(bi);this.ft.push([bi,LR[l>>1]*z*1.45])}
this.ord=[...used];for(let i=0;i<WSP;i++)if(!used.includes(i*7%WSP))this.ord.push(i*7%WSP);
for(let k=0;k<4;k++)this.tn.push([.012+.014*k,.014,1.12+.06*k,.06]);for(let k=0;k<26;k++)this.tn.push([.17+.6*k/26,.6/26,1.4+2.5*k/26,2.5/26]);
this.nr=WSP+this.tn.length+10;this.row=hrow;hrow+=this.nr},
reset(){this.x.set(this.r0);this.q.set(this.r0);this.imp=this.jd=0},
// point j of spoke i (0: the middle); and the place on spoke i that is so far out (f, as the scene counts lengths)
nd(i,j){const k=(j?i*19+j:0)*3,x=this.x;return[x[k],x[k+1],x[k+2]]},
on(i,f){const g=cl(f/this.rm[i]*19,0,18.999),j=g|0;return mix3(this.nd(i,j),this.nd(i,j+1),g-j)},
step(F,dt,t,E){const x=this.x,q=this.q,N=this.N,C=this.C,H=this.hub,pin=this.pin,im=this.imp;this.imp=0;
for(let k=0;k<N;k++){if(pin[k])continue;const j=k*3,vx=(x[j]-q[j])*.987,vy=(x[j+1]-q[j+1])*.987,vz=(x[j+2]-q[j+2])*.987,dx=x[j]-H[0],dy=x[j+1]-H[1],r2=(dx*dx+dy*dy)/.8;
q[j]=x[j];q[j+1]=x[j+1];q[j+2]=x[j+2];x[j]+=vx;x[j+1]+=vy+G[1]*dt*dt*.5;x[j+2]+=vz+(r2<1?im*(1-r2)*(1-r2):0)+2e-5*Math.sin(t*1.3+dx*3+dy*2)}
for(let it=0;it<12;it++)for(let c=0;c<C.length;c+=3){const a=C[c]*3,b=C[c+1]*3,ex=x[b]-x[a],ey=x[b+1]-x[a+1],ez=x[b+2]-x[a+2],l=Math.sqrt(ex*ex+ey*ey+ez*ez);if(l<=C[c+2])continue;
const pa=pin[C[c]],pb=pin[C[c+1]],k=(l-C[c+2])/l*(pa|pb?1:.5);if(!pa){x[a]+=ex*k;x[a+1]+=ey*k;x[a+2]+=ez*k}if(!pb){x[b]-=ex*k;x[b+1]-=ey*k;x[b+2]-=ez*k}}},
// once a frame: the blow, if the bass drum has gone; every thread's line, for the card; and the spider, as the web has moved under it
tick(F,dt,t,E){if(E.hit&1)this.imp=.0016+.0009*E.lv;
const H=this.nd(0,0),ns=this.tn.length,P=(row,i,v)=>{const w=fAt(F,v),o=((this.row+row)*NP+i)*4;HP[o]=w[0];HP[o+1]=w[1];HP[o+2]=w[2];HP[o+3]=1};
for(let k=0;k<WSP;k++)for(let j=0;j<20;j++)P(k,j,this.nd(this.ord[k],j));
this.tn.forEach((T,k)=>{for(let i=0;i<20;i++)P(WSP+k,i,this.on(i%WSP,T[0]+T[1]*i/19))});
// (the frame, corner to corner; and from each corner a thread away to the walls)
for(let k=0;k<5;k++){const A=WCO[k],B=WCO[(k+1)%5],h=this.hub;for(let i=0;i<20;i++){const u=i/19;P(WSP+ns+k,i,[h[0]+mix(A[0],B[0],u),h[1]+mix(A[1],B[1],u),h[2]]);P(WSP+ns+5+k,i,[h[0]+A[0]*(1+1.7*u),h[1]+A[1]*(1+1.7*u),h[2]-.35*u])}}
redata(HT,HP,this.row,this.row+this.nr);
const n0=nrm(cross(sub(this.on(0,.12),H),sub(this.on(5,.12),H))),z=this.z,M=[F.x[0],F.y[0],F.z[0],F.x[1],F.y[1],F.z[1],F.x[2],F.y[2],F.z[2]],jx=cl((E.x-6)/.14,0,1);
let U=n0[2]<0?mul(n0,-1):n0,Fd=nrm(add([0,-1,0],mul(U,U[1]))),B=add(H,mul(U,z*.034)),kn=mul(U,.45); // (on its web its legs are stretched out further than a walking spider's, and its knees less high: seen from in front, its legs are all there)
// the jump (jx: how far gone): from where it sat to just in front of the camera (E.cam), a little up on the way; it turns to come head
// first, its knees out to the sides and its feet ahead of it, closing in; and the web is pushed back by its going
if(jx>0&&E.cam){const vd=nrm(sub(H,E.cam)),e=Math.pow(jx,1.7),s=ss(0,.5,jx);if(!this.jd){this.jd=1;this.imp=-.02}
B=add(mix3(B,add(E.cam,mul(vd,.08)),e),[0,.05*Math.sin(Math.PI*e),0]);Fd=nrm(mix3(Fd,mul(vd,-1),s));U=nrm(mix3(U,[0,1,0],s));U=nrm(sub(U,mul(Fd,dot(U,Fd))));kn=nrm(mix3(U,Fd,s))}
const L=cross(U,Fd);
for(let l=0;l<8;l++){let ft=this.on(this.ft[l][0],this.ft[l][1]),kd=kn;
if(jx>0){const j=l>>1,a=[.96,.35,-.35,-.96][j],d=add(mul(L,(l&1?-1:1)*Math.cos(a)),mul(U,Math.sin(a))),s=ss(0,.3,jx);ft=mix3(ft,add(add(B,mul(d,.1*z)),mul(Fd,[.135,.11,.08,.045][j]*z)),s);kd=nrm(mix3(kn,d,s))}
const hk=legIK(B,kd,Fd,L,z,l,ft);spPut(NSP,M,F.o,3+3*l,hk[0]);spPut(NSP,M,F.o,4+3*l,hk[1]);spPut(NSP,M,F.o,5+3*l,ft)}
spPut(NSP,M,F.o,0,B,z);spPut(NSP,M,F.o,1,U,9,1);spPut(NSP,M,F.o,2,Fd,.5,1);redata(SPI.T,SPD,NSP,NSP+1)},
draw(F,U0,E){const U={...U0,uBxH:NOBX},x=E.x,ns=this.tn.length,o={ch:1,w:.001,spread:0,hc:[.3,.32,.37],tr:.3,lay:0,lat:0,sh:.7,rag:0,tap:0,thn:1,solid:0},
D=(row,n,nv,cut,bd)=>{if(n>0)drawStr(U,F,HT,NP,this.row+row,n,{...o,nv,cut,bd})};
// what has been spun so far: the frame; the spokes; the spiral (whatever is still on its way is drawn as far as it has got)
D(WSP+ns,10,24,1);
let c=0;for(let k=0;k<WSP;k++){const g=k<8?1:cl((x-.375-(k-8)/16)/.14,0,1);if(g>=1)c=k+1;else if(g>0)D(k,1,40,g)}D(0,c,40,1);
c=0;this.tn.forEach((T,k)=>{const g=cl((x-T[2])/T[3],0,1);if(g>=1)c=k+1;else if(g>0)D(WSP+k,1,100,g,[78,2.2])});D(WSP,c,100,1,[78,2.2]);
SPI.bodies(U,NSP,NSP+1);FUR.fur(null,I3,U,{n:5200,one:NSP,w:.00085,sc:1})}});
/* The piece's name, written in fur: a brown bear's - long, coarse, shaggy, darker at the root and lighter at the tip - hanging, on nothing. It stands before the piece is started, and again at the very end. The
word is drawn on the page in heavy letters, and thirty thousand hairs are given places on what is drawn (and a number each, by how far
along the word it stands: the fur grows from the left, as if the word were being written). It is the same fur as the solid's and
drawn by the same shader - only it grows on a flat thing that does not move, so what stirs it is given here: now and then a breath
of air, at the start a gust, at the end the last claps (dA, dB: how far its tips have been left behind, slack and stiff; vA, vB: how
fast). g: how much of it has grown. */
const Z3=[0,0,0],Z9=[0,0,0,0,0,0,0,0,0],LOGO={LW:3.2,g:0,rn:0,T:null,dA:[0,0,0],vA:[0,0,0],dB:[0,0,0],vB:[0,0,0],
init(){const W=1536,H=448,c=document.createElement('canvas'),g=c.getContext('2d'),tx=[...'FURTILE'],r=rnd0(404),A=new Float32Array(256*256*4),P=[];c.width=W;c.height=H;
let fs=300,ws,w;for(let k=0;k<2;k++){g.font='bold '+fs+'px "Helvetica Neue",Helvetica,Arial,sans-serif';ws=tx.map(ch=>g.measureText(ch).width);w=ws.reduce((a,b)=>a+b,0)+fs*.09*(tx.length-1);if(w>W*.95)fs*=W*.95/w}
g.textBaseline='middle';g.fillStyle='#fff';let x=(W-w)/2;tx.forEach((ch,i)=>{g.fillText(ch,x,H/2);x+=ws[i]+fs*.09});
const d=g.getImageData(0,0,W,H).data;for(let i=0;i<W*H;i++)if(d[i*4+3]>128)P.push(i);
if(P.length<1000)return; // (nothing was drawn: then the name stays as the page has it)
for(let i=0;i<NFUR;i++){const p=P[r()*P.length|0],px=p%W+r(),py=(p/W|0)+r();A.set([(px/W-.5)*this.LW,(.5-py/H)*this.LW*H/W,50,r()],i*4);A.set([0,0,1,cl(px/W*.9+.05*r(),0,1)],(i+128*256)*4)}
this.T=data(256,256,A)},
// im: a push given to all of it at once
step(dt,im){const n=4,h=dt/n;for(let k=0;k<n;k++)for(const[d,v,kk,c]of[[this.dA,this.vA,36,1.7],[this.dB,this.vB,121,4.8]])for(let i=0;i<3;i++){v[i]+=(-kk*d[i]-c*v[i])*h+(k?0:im[i]);d[i]+=v[i]*h}},
// as the picture is as wide as it is (as), the word takes up six tenths of its width
draw(as,t,S,q,wx){const D=9.7/as,n=Math.round(NFUR*Math.max(.3,q));use(PFU);
su(PFU,{uVP:{m:m4(persp(FY,as,.05,40),look([0,0,D],[0,0,0],[0,1,0]))},uCam:[0,0,D],uLamp:[.7,1.5,2.4],uLampC:[3.4,2.9,2.3],uKey:nrm([-.4,.6,.7]),uKeyC:[1.1,1.02,.92],uAmb:[.04,.034,.028],uT:t,uPS:S.h/2/TAN,uBxH:NOBX,
uPts:this.T,uSp:SPI.T,uFR:{m:I3},uFO:[0,0,-50],uGrav:[0,-1,0],uNP:NPF,uW:.0058*Math.sqrt(NFUR/n),uWX:wx,uLen:.19,uGrow:this.g,uShl:0,uOne:-1,uMo:0,
uD0a:this.dA,uDa:{m:Z9},uD0b:this.dB,uDb:{m:Z9},uHC:[.17,.09,.042],uTr:.9,uLay:1,uLat:0,uSh:.3,uHue:0,uDp:0});strokes(PFU,NPF,n,0)}};
/* ---------------- the piece ---------------- */
const META=SYN(0),BPM=META.BPM,BAR=240/BPM,NBAR=META.NBAR,LEN=NBAR*BAR,NS=SC.length,SB=[],ENDC=[],KT=[],KS=[],KC=[];
{let b=0;for(const s of SC){SB.push(b);b+=s.n}SB.push(b)}
// SB: the bar each scene begins at; RE: the bar at which we are inside the last cube's room - two bars before the music takes up again (FB), from which
// everything in that room is counted; WB: the bar at which we are in the black of one of its tiles, where the web is
const RE=SB[NS],FB=RE+2,WB=FB+16;
for(let k=8;k<16;k++)if(META.hit((NBAR-1)*16+k)&2)ENDC.push(NBAR-1+k/16); // (the claps of the very last half bar: the steps the shutter closes in)
// the bass drums of the room: those on which the last cube's tiles turn out of the way (KT), those on which the fur lets go of the
// solid, a tuft or two to each (KS: from the bar at which the music goes back to its first kit), and those on which the tiles turn back (KC)
for(let k=FB*16;k<WB*16;k++)if(META.hit(k)&1){const b=k/16;if(b<FB+6)KT.push(b);if(b>=FB+8&&b<FB+9.7)KS.push(b);if(b>=FB+8&&b<FB+10.2)KC.push(b)}
const KW=[...KT,...KC];
/* What is inside the cube that arrives in the fifth scene: a dark room, and in it the last cube, the only one not of glass, with the
furred solid shut up in it. FIN stands for that room where a scene is expected (it is come into ZF in front of the cube; KR: the
camera's flight in it, counted from the cube's place C0). The cube is KL times the size of the others, and is thrown about with
what is in it. Every bass drum of the room's first six bars turns some more of its tiles edge on for good - and a wave of
tiles turning and turning back crosses it, each time another way - so that by the sixth it is all open, and only its edges left.
Then the fur lets go of the solid and comes down on the cube as spiders, and the tiles turn back, the same way, for them to walk
on. At the last the cube is brought square to us, and the camera goes down (from the bar XD of the room) to one tile of it - the one
TXY to the right of its middle and as much above - until that tile's black is all there is.
As the card is given them: its six faces; and after them, when it is wanted, the shutter that opens and closes the whole piece -
a square held up close in front of the camera. */
const ZF=2.3,KL=1.6,XD=14.9,TXY=HE*KL/8,C0=add(SPOT[NS-1],[0,0,-HE]),FIN={K:[],KR:[],F:{o:add(SPOT[NS-1],[0,0,ZF]),x:[1,0,0],y:[0,1,0],z:[0,0,1]}},FM=mesh([3,3,3,2,3,4],42),
WD=[[1,.3,.2],[-.4,1,.3],[.2,-.3,1],[-1,-.2,.4],[.3,1,-.6],[-.2,-1,-.3]].map(nrm);
SC.forEach(s=>{s.init&&s.init();s.F=frameOf(s.m,TRAIL[s.e])});FUR.init();SPI.init(KS);WEB.init();WEB.F=frameOf(5,TRAIL[5]);LOGO.init();
const putFaces=(sh,eye,cx,cy,cz)=>{const a=FM.a,P={r:FUR.R,t:add(C0,FUR.p)};let o=0;
const put=(p,n,t,u,v,ty,f,w)=>{a.set(p,o);a.set(n,o+3);a.set(t,o+6);a[o+9]=u;a[o+10]=v;a[o+11]=a[o+12]=a[o+13]=1;a[o+14]=ty;a[o+15]=0;a[o+16]=f;a[o+17]=w;o+=18};
for(let f=0;f<6;f++){const F=IF[f];for(const k of[0,1,2,0,2,3]){const l=IV[F.v[k]],d=sub(l,F.c);put(pAt(P,mul(l,KL)),pDir(P,F.n),pDir(P,F.e1),dot(d,F.e1),dot(d,F.e2),11,f,0)}}
if(sh!=null)for(const[u,v]of[[-1,-1],[1,-1],[1,1],[-1,-1],[1,1],[-1,1]])put(add(add(sub(eye,mul(cz,DN)),mul(cx,u*HE)),mul(cy,v*HE)),cz,cx,u*HE,v*HE,10,0,sh);
FM.n=o/18;fill(FM)};
/* How fine the picture is made depends on how fast this machine turns out to be. Five steps: fewer strands drawn to each one moved,
then a smaller picture as well. The first frames draw nothing and only time the display (so that a slow display is not mistaken
for a slow card: the middle third of three dozen frames, so that neither a stall nor a hurry counts); after that, two half-seconds running in which frames come late take it a step down, and a quiet spell lets it try
a step up again - waiting twice as long each time that turns out to have been too much. */
const QL=[1,.6,.45,.33,.25],RS=[1,1,.8,.64,.5],FIXL=UQ.has('lvl'),LOAD=+UQ.get('load')||1,snapT=x=>{for(const r of[120,144,90,60,75,50,30])if(Math.abs(x*r-1)<.1)return 1/r;return x};
let lvl=Math.min(4,+UQ.get('lvl')||0),tdisp=0,pbuf=[],wN=0,wT=0,lateW=0,okT=0,upWait=6,tried=0,cool=0;
// targets: the picture, and the glow's smaller copies
let S=null,BL=[],sc=1;
const resize=()=>{const d=Math.min(devicePixelRatio||1,2);W=C.width=Math.max(2,(C.clientWidth||innerWidth)*d|0);H=C.height=Math.max(2,(C.clientHeight||innerHeight)*d|0);
sc=Math.min(1,Math.sqrt((+UQ.get('mp')||3)*1e6/(W*H)))*RS[lvl];free(S);BL.forEach(free);BL=[];
let w=Math.max(2,Math.round(W*sc)),h=Math.max(2,Math.round(H*sc));S=fld(w,h,F4,1,1);
for(let i=0;i<5;i++){w=Math.max(1,w>>1);h=Math.max(1,h>>1);BL.push(fld(w,h,F4,1))}};
addEventListener('resize',resize);resize();
/* Where everything is at bar b. The piece opens inside its first scene - a shutter over the whole picture flying open - and from
then on goes inward and never comes out: every scene ends with a cube of glass arriving in it, with the next scene to be seen
inside it, and the camera goes up to that cube and in (see the scenes). Inside the fifth cube is a dark room with the last cube
in it, being thrown about by the bass drum; its tiles turn out of the way, and what was shut up in it is seen, thrown about with
it, its fur flying - until the fur lets go, and is spiders, walking on the cube. The camera goes down into the black of one tile;
and in the black is a spider's web, being spun (WB). When the music lets it be it comes to rest, and the picture goes dim and draws
back as if that were the end - until the spider jumps at the camera, and there is nothing but black, and the piece's name on it.
Then the shutter closes over the picture on the last claps, and the piece begins again.
e: which scene (NS: the last cube's room, and what is in its black); x: bars into it (in that room: since FB, so less than nothing at first); n: how many it has. */
const where=b=>{const fin=b>=RE;let e=0;while(e<NS-1&&b>=SB[e+1])e++;return fin?{e:NS,x:b-FB,n:NBAR-FB,fin}:{e,x:b-SB[e],n:SC[e].n,fin}};
/* The camera's flight through a scene: it comes in close up to the face of the cube the scene is inside (DN off it: so close that
the face is the whole picture, whatever the picture's shape), goes on in, and then through the places the scene names. Keys are
laid out afresh when the picture's shape changes. */
const FY=.52,TAN=Math.tan(FY/2),KEY=nrm([-.45,.55,.7]),RA=HE*.81;let KA=0,DN=.3,CAMO=UQ.has('cam')?[0,...UQ.get('cam').split(',').map(Number)]:null;
const lay=as=>{KA=as;DN=.9*RA/(TAN*Math.hypot(as,1));FIN.K=[[0,0,0,DN,0,0,-2,0]];WEB.K=WEB.fly;
FIN.KR=[[RE-FB,0,0,HE+ZF+DN,0,0,0,0],[.6,.5,.28,2.25,0,0,0,0],[3.4,-.3,.22,2.3,0,0,0,0],[6,-.42,.14,2,0,0,0,0],[7.8,.3,.16,1.75,0,-.03,0,0],[9.4,-.3,.1,2.05,0,-.02,0,0],[11.4,.45,.22,2.2,0,0,0,0],[13.2,-.35,.15,2.05,0,0,0,0],[XD,.1,.12,1.95,.02,.02,.2,0]];
for(const s of SC){const zi=s.zin||-.35;s.cr=[s.fly[0][0],s.fly[s.fly.length-1][0]];
s.K=[...(s.first?[[0,0,0,DN+.06,0,0,-2,0]]:[[0,0,0,DN,0,0,-2,0],[.55,0,0,zi,0,0,-2.1,0]]),...s.fly]}},
flight=(K,x)=>{const n=K.length;if(x<=K[0][0])return K[0];if(x>=K[n-1][0])return K[n-1];let i=0;while(K[i+1][0]<x)i++;
const a=K[Math.max(i-1,0)],b=K[i],c=K[i+1],d=K[Math.min(i+2,n-1)],h=c[0]-b[0],u=(x-b[0])/h,u2=u*u,u3=u2*u,o=[x];
for(let k=1;k<8;k++){const m0=i?(c[k]-a[k])/(c[0]-a[0])*h:0,m1=i<n-2?(d[k]-b[k])/(d[0]-b[0])*h:0;o[k]=(2*u3-3*u2+1)*b[k]+(u3-2*u2+u)*m0+(3*u2-2*u3)*c[k]+(u3-u2)*m1}return o};
/* Cuts. Where the music has claps the picture is cut on them: to where the camera will be (or was) at another time in the same
scene - half its flight away from now - and back again; what is in the scene goes on meanwhile as it was going. In every bar from its
second clap to the bar line; in every other bar on its first clap as well, for half a beat; and in the bar that ends a phrase, to
and fro through the run. Not while a scene is being come into or left, nor where the music has no claps.
CUT: whether, on this sixteenth, it is the other time that is shown; other: that time, for x in a flight that is free from a to c. */
const NOCUT=UQ.has('nocut'),CUT=st=>{const s=st&15,bar=st>>4;return META.lv(bar)<2?0:(bar&3)==3?s==4||s==5||s==8||s==9||s==12||s==13:s>=12||(bar&1&&(s==4||s==5))},
other=(x,a,c)=>a+((x-a+(c-a)/2)%(c-a)+(c-a))%(c-a);
/* ---------------- measuring ---------------- */
const HU=document.getElementById('h'),TI=document.getElementById('t'),M_={t:0,n:0,w:0};let hud=1;
const hset=v=>{hud=v;HU.textContent='';HU.style.background=v?'rgba(0,0,0,.42)':'none'};
HU.addEventListener('pointerdown',e=>{hset(hud?0:1);e.stopPropagation()});
if(LOGO.T){TI.style.color='transparent';TI.style.textShadow='none'} // (the name is in fur: of what the page has, only the line under it is to be seen)
let tio=-1,seekB=UQ.has('b')?+UQ.get('b'):null,free0=MUTE?0:null,lt=0,ck=0,gC=0,frames=0,simT=0,lat=0,fade=0,pst=-1,curB=0,SN=null;
const jump=b=>{if(ctx&&t0!=null&&ctx.state=='running'){t0+=(curB-b)*BAR;ck=b*BAR}else{gC=b*BAR;if(free0==null)free0=0}};
addEventListener('keydown',e=>{const c=e.key.toLowerCase();
if(c=='f'){try{(document.fullscreenElement?document.exitFullscreen():document.documentElement.requestFullscreen()).catch(()=>{})}catch(e){}}
if(c=='m')hset(hud?0:1);
if(e.key=='ArrowRight'||e.key=='ArrowLeft'){const J=[...SB,FB+8,WB],d=e.key=='ArrowRight'?1:-1;let i=0;while(i<J.length-1&&curB>=J[i+1])i++;jump(J[(i+d+J.length)%J.length]+.02)}
start()});
const frame=now=>{
requestAnimationFrame(frame);
const t=now/1e3,rdt=t-lt,dt=Math.min(.1,Math.max(.001,rdt)),j0=performance.now();lt=t;frames++;
// (timing the display)
if(!tdisp){if(frames>3&&rdt<.2)pbuf.push(rdt);if(pbuf.length<36)return;pbuf.sort((a,b)=>a-b);let m=0;for(let i=12;i<24;i++)m+=pbuf[i]/12;tdisp=snapT(m);return}
// the clock: the sound's, when there is sound (the frames' own clock runs on between its jumps and is drawn slowly towards it)
let run=0,mt=0;
if(ctx&&t0!=null&&ctx.state=='running'){lat+=((ctx.outputLatency||0)-lat)*(lat?.02:1);const a=ctx.currentTime-t0-lat,e=a-(ck+=rdt);ck=Math.abs(e)>.25?a:ck+e*(1-Math.exp(-rdt/.7));mt=(Math.max(0,ck)%LEN+LEN)%LEN;run=1}
else if(free0!=null){gC+=rdt<1?rdt:0;mt=(gC%LEN+LEN)%LEN;run=1}
if(seekB!=null){mt=seekB*BAR;run=1}
const b=mt/BAR;curB=b;
// (how to start, under the name, until it is started)
{const o=run?0:1;if(o!=tio){tio=o;TI.style.opacity=o;TI.style.transition=run?'none':''}}
// (how fine: see above)
if(run&&rdt<.25&&!FIXL){wT+=rdt;wN++;cool-=rdt;okT+=rdt;
if(wN>=30){const late=wT/wN>tdisp*1.15;wT=wN=0;
if(cool>0);else if(late){if(++lateW>1){lateW=0;if(lvl<4){if(tried)upWait=Math.min(48,upWait*2);lvl++;cool=1;resize()}okT=0;tried=0}}
else{lateW=0;if(tried&&okT>3)tried=0;if(lvl>0&&okT>upWait){lvl--;tried=1;okT=0;cool=1;resize()}}}}
const as=W/H;if(as!=KA)lay(as);
const sn=SN=where(b),wb=b>=WB,room=sn.fin&&!wb,cur=sn.fin?null:SC[sn.e],nxt=cur?SC[sn.e+1]||FIN:null,tan=TAN;
// what is struck on this sixteenth, and how lately the bass drum
const st=Math.floor(b*16),hit=st!=pst&&run?META.hit(st):0;pst=st;
let kick=0;if(run)for(let s=st;s>st-8;s--)if(s>=0&&META.hit(s)&1){kick=Math.exp(-(b*16-s)*.45);break}
simT+=dt;let nst=0;while(simT>1/60&&nst<3){simT-=1/60;nst++}if(simT>.1)simT=0;
// The name, in fur: grown before the piece is started - it grows as the page opens, and is gone, in a gust, as the piece begins - and again at the very
// end, after the spider has jumped: it grows out of the black, and goes with the last claps. Now and then, while it waits, a breath of air goes through it.
{const x=b-WB,tg=!run?1:wb&&x>6.2?ss(6.25,7.1,x)*(1-ENDC.reduce((a,c)=>a+ss(c-.01,c+.04,b),0)/Math.max(1,ENDC.length)):0,g=LOGO.g;LOGO.g=tg>g?Math.min(tg,g+dt*.8):Math.max(tg,g-dt*5);
LOGO.step(dt,run&&!LOGO.rn?[.6,.2,.4]:!run&&Math.floor(t/2.6)!=Math.floor((t-dt)/2.6)?[.22*Math.sin(t*1.7),.05,.14]:wb&&hit&2?[st&1?.5:-.5,.12,.3]:Z3);LOGO.rn=run}
// the last cube and what is in it, thrown about (also while they are only to be seen inside the fifth cube) - less and less once the
// fur has let go, and at the last brought square to us and to rest; and the spiders on it, who get out of the way of where we are going
const br=Math.floor(b),clp=(br&3)==3&&b-br>=.5;
if(room||cur&&nxt==FIN&&sn.x>=cur.ga)FUR.sim(dt,hit,META.lv(br),clp&&b<FB+8,room?ss(FB+9.4,FB+10.6,b):0,room?cl((b-FB-14.3)/.8,0,1):0,b*BAR);
if(room&&b>KS[0]-.1)SPI.step(dt,b,{kick,hit,turn:clp},FUR.R,add(C0,FUR.p),FUR.A?FUR.gi([TXY,TXY,HE*KL]):null,b*BAR);
// The camera: on its flight through the scene (ca: where it is, as the scene counts places) - toward the scene's end going up to
// the cube that has arrived in it, to where the flight of the scene inside that cube begins (gw: how far gone); or, in the last
// cube's room or in the web, on its flight there. And where there is a cut, where it is at that other time instead (ce: where the
// picture is taken from; what is in the scene still makes way for it where it is on its flight).
let eye,tgt,rl=0,ca=null,ce=null,gw=0;const cut=!NOCUT&&!CAMO&&run&&CUT(st);
if(cur){const k=CAMO||flight(cur.K,sn.x),k0=nxt.K[0],q=cur.q;ce=ca=[k[1],k[2],k[3]];let c=[k[4],k[5],k[6]];rl=k[7];gw=Math.pow(cl((sn.x-sn.n)/1.7+1,0,1),2.6);
if(!gw&&cut&&sn.x>1.2&&sn.x<sn.n-1.9){const kb=flight(cur.K,other(sn.x,cur.cr[0],cur.cr[1]));ce=[kb[1],kb[2],kb[3]];c=[kb[4],kb[5],kb[6]];rl=kb[7]}
// (a scene may have the camera keep its eye on something that moves: see the rope's)
{const a=!CAMO&&cur.aim&&cur.aim(sn.x);if(a)c=mix3(c,a[0],a[1])}
if(gw>0){ce=mix3(ce,q.gp([k0[1],k0[2],k0[3]]),gw);c=mix3(c,q.gp([k0[4],k0[5],k0[6]]),gw);rl=(rl-TAU*Math.round(rl/TAU))*(1-gw)}
eye=add(SPOT[sn.e],ce);tgt=add(SPOT[sn.e],c)}
else if(wb){const x=b-WB,k=CAMO||flight(WEB.K,cut&&x>.6?other(x,.5,7.5):x);eye=add(SPOT[5],[k[1],k[2],k[3]]);tgt=add(SPOT[5],[k[4],k[5],k[6]]);rl=k[7]}
else{const x=sn.x,k=CAMO||flight(FIN.KR,cut&&x>1.2&&x<7.9?other(x,1.2,7.9):cut&&x>9.7&&x<14.7?other(x,9.7,14.7):x);eye=add(C0,[k[1],k[2],k[3]]);tgt=add(C0,[k[4],k[5],k[6]]);
// and at the last, down to the one tile of the face that is toward us, until there is nothing else to be seen
const dv=cl((x-XD)/(16-XD),0,1);if(dv>0&&!CAMO){const tp=add(add(C0,FUR.p),[TXY,TXY,HE*KL]),o=sub(eye,tp),l=len(o),u=Math.pow(dv,1.2);
eye=add(tp,mul(nrm(mix3(mul(o,1/l),[0,0,1],ss(0,.75,u))),l*Math.pow(.058/l,u)));tgt=mix3(tgt,tp,ss(0,.5,u))}}
const z0=nrm(sub(eye,tgt)),x0=nrm(cross([0,1,0],z0)),VM=look(eye,tgt,add(mul(cross(z0,x0),Math.cos(rl)),mul(x0,Math.sin(rl)))),VP=m4(persp(FY,as,.05,40),VM),
cx=[VM[0],VM[4],VM[8]],cy=[VM[1],VM[5],VM[9]],cz=[VM[2],VM[6],VM[10]],ps=S.h/2/tan,CA={eye,VP,cx,cy,cz,ps};
// The same camera as the scene inside the cube has it. That scene is its own size and stands in its own place; seen through the cube
// it is as if it were the size of the cube and stood where the cube stands, turned as the cube is turned: so it is drawn with the
// camera's matrix times that change of place, from where the camera is when that change is undone.
let CI=null;
if(cur&&sn.x>=cur.ga){const q=cur.q,R=q.R,k=q.k,o=nxt.F.o,t0=add(add(SPOT[sn.e],q.c),mul(mv(R,sub([0,0,HE],o)),k));
CI={eye:add(o,q.gi(ce)),VP:m4(VP,[k*R[0],k*R[3],k*R[6],0,k*R[1],k*R[4],k*R[7],0,k*R[2],k*R[5],k*R[8],0,t0[0],t0[1],t0[2],1]),cx:q.gd(cx),cy:q.gd(cy),cz:q.gd(cz),ps:ps*k}}
// which scenes are lived in just now: the one we are in, and the next from a little before its cube arrives (so that what is in it
// has come to rest by then). One coming to life starts afresh.
for(const s of SC){const l=cur&&(s==cur||s==nxt&&sn.x>cur.ga-1.3)?1:0,E=s.E={x:b-SB[s.e],kick,hit,st,ce:s==cur?ca:null};
if(l&&!s.live){for(const q of s.sets)q.fresh=1;if(s.reset)s.reset(E)}s.live=l;if(!l)continue;
const F=s.F;
if(s.step)for(let k=0;k<nst;k++)s.step(F,1/60,t,E);
if(s.tick)s.tick(F,dt,t,E);
for(const q of s.sets)redata(HT,HP,q.row,q.row+q.n);if(s.fr[1]>s.fr[0])redata(FT,FP,s.fr[0],s.fr[1])}
// (the web is there a little before it is come to, in the black, so that it hangs as it will)
{const l=b>=WB-.3?1:0,E=WEB.E={x:b-WB,kick,hit,lv:META.lv(br),cam:wb?sub(eye,SPOT[5]):null};if(l&&!WEB.live)WEB.reset(E);WEB.live=l;if(l){for(let k=0;k<nst;k++)WEB.step(WEB.F,1/60,t,E);WEB.tick(WEB.F,dt,t,E)}}
// the last cube's tiles: how many are turned out of the way for good - more with every bass drum of the first six bars, fewer again
// with every one on which the fur lets go; and how far the wave from the latest of those has got, and which way that one runs
let TL=[0,-1,KL,0],TD=WD[0];if(room){let i=-1;while(i<KW.length-1&&KW[i+1]<=b)i++;const f=K=>K.reduce((a,k)=>a+ss(k,k+.1,b),0)/K.length;
TL=[(1-Math.pow(1-f(KT),1.8))*(1-Math.pow(f(KC),1.5)),i<0?-1:(b-KW[i])/.3,KL,0];TD=WD[(i+6)%6]}
// the shutter over the whole picture: open but for the first moments of the piece, and for the last claps of it
const qual=QL[lvl],wx=Math.max(3,S.h*.0045),sho=b<1?1-Math.pow(1-cl(b/.4,0,1),3):b>NBAR-1?Math.max(0,1-ENDC.reduce((a,c)=>a+ss(c-.012,c+.035,b),0)/Math.max(1,ENDC.length)):1,
BLN=()=>{gl.enable(gl.BLEND);gl.blendFuncSeparate(gl.SRC_ALPHA,gl.ONE_MINUS_SRC_ALPHA,gl.ONE,gl.ONE_MINUS_SRC_ALPHA)};
putFaces(sho<1?sho:null,eye,cx,cy,cz);
fade=Math.min(1,fade+dt*1.2);let wi=0;
const blk=wb&&b-WB>=6.15; // (after the jump there is nothing: black, and the name)
for(let rep=0;rep<LOAD;rep++){
to(S);gl.depthMask(true);gl.clearDepth(1);gl.clearStencil(0);gl.clear(gl.DEPTH_BUFFER_BIT|gl.STENCIL_BUFFER_BIT);gl.disable(gl.BLEND);gl.disable(gl.CULL_FACE);gl.disable(gl.STENCIL_TEST);wi=0;
// a scene, as the camera c sees it: its room first - which also says how far off the room is at each place - and then what stands in it
const scn=(s,c)=>{const F=s.F,lc=mul(s.lampC,1+.3*kick),w=s.wall;wi++;
gl.enable(gl.DEPTH_TEST);gl.depthFunc(gl.ALWAYS);gl.depthMask(true);gl.disable(gl.BLEND);
use(PSB);su(PSB,{uVP:{m:c.VP},uCam:c.eye,uCx:c.cx,uCy:c.cy,uCz:c.cz,uR:[S.w,S.h],uTan:tan,uSO:F.o,uSX:F.x,uSY:F.y,uSZ:F.z,uLS:s.lamp,uLC:lc,uWC:w.col,uZW:w.z,uRm:[w.yc,w.yf,w.xw],uGl:w.gl});gl.bindVertexArray(null);gl.drawArrays(gl.TRIANGLES,0,3);
gl.depthFunc(gl.LEQUAL);BLN();
s.draw(F,{uVP:{m:c.VP},uCam:c.eye,uLamp:fAt(F,s.lamp),uLampC:lc,uKey:nrm(fDir(F,s.key)),uKeyC:s.keyC,uAmb:s.amb,uT:t,uPS:c.ps,q:qual,wx},s.E)},
// the last cube's room: the wall behind it; the cube's far faces, from inside; what is in the cube; and its near faces. (Once the fur
// has begun to let go there are hairs outside the cube as well as in it: from then on the hair is drawn last, and the spiders' bodies before it.)
world=c=>{const cb=add(C0,FUR.p),lamp=add(cb,[.1,.3,.12]),lampC=[.42,.3,.17],UW={uVP:{m:c.VP},uCam:c.eye,uLamp:lamp,uLampC:lampC,uKey:KEY,uKeyC:[.95,1,1.15],uAmb:[.02,.022,.03],uT:t,uPS:c.ps,q:qual,wx},
US={...UW,uBxH:NOBX,uKeyC:[.8,.84,.95],uTs:5,uMd:0,uTl:TL,uTd:TD,uTc:cb},out=room&&b>KS[0]-.05;wi++;
gl.enable(gl.DEPTH_TEST);gl.depthFunc(gl.ALWAYS);gl.depthMask(true);gl.disable(gl.BLEND);
use(PBG);su(PBG,{uCam:c.eye,uCx:c.cx,uCy:c.cy,uCz:c.cz,uLamp:lamp,uLampC:lampC,uR:[S.w,S.h],uTan:tan,uT:t});gl.bindVertexArray(null);gl.drawArrays(gl.TRIANGLES,0,3);
gl.depthFunc(gl.LEQUAL);BLN();gl.enable(gl.CULL_FACE);gl.cullFace(gl.FRONT);use(PSF);su(PSF,US);drawMesh(FM,0,36);gl.disable(gl.CULL_FACE);
FUR.core(cb,FUR.R,US);if(!out)FUR.fur(cb,FUR.R,UW);
gl.depthMask(true);gl.enable(gl.CULL_FACE);gl.cullFace(gl.BACK);use(PSF);su(PSF,US);drawMesh(FM,0,36);gl.disable(gl.CULL_FACE);
if(out){SPI.bodies(US,0,NSP);FUR.fur(cb,FUR.R,UW,{mo:1,sc:ss(KS[0]+.4,KS[0]+2.6,b)})}};
if(blk){gl.clearColor(0,0,0,1);gl.clear(gl.COLOR_BUFFER_BIT)}
else if(cur){scn(cur,CA);
// and the scene inside its cube: where the cube is seen is marked out, the scene drawn only there, and over it what the glass adds
if(CI){const om=1-ss(.8,1,gw);gl.enable(gl.STENCIL_TEST);gl.colorMask(false,false,false,false);gl.stencilFunc(gl.ALWAYS,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.REPLACE);gl.enable(gl.CULL_FACE);gl.cullFace(gl.BACK);cur.face(0);
gl.colorMask(true,true,true,true);gl.stencilFunc(gl.EQUAL,1,255);gl.stencilOp(gl.KEEP,gl.KEEP,gl.KEEP);gl.disable(gl.CULL_FACE);
if(nxt==FIN)world(CI);else{scn(nxt,CI);if(nxt.late)nxt.late()}
if(om>0){gl.depthFunc(gl.ALWAYS);gl.enable(gl.CULL_FACE);gl.cullFace(gl.BACK);cur.face(om);gl.disable(gl.CULL_FACE);gl.depthFunc(gl.LEQUAL)}gl.disable(gl.STENCIL_TEST)}
if(cur.late){gl.enable(gl.DEPTH_TEST);gl.depthFunc(gl.LEQUAL);gl.disable(gl.CULL_FACE);BLN();cur.late()}}
else if(wb)scn(WEB,CA);
else world(CA);
if(sho<1){gl.disable(gl.DEPTH_TEST);gl.disable(gl.CULL_FACE);BLN();use(PSF);su(PSF,{uVP:{m:VP},uCam:eye,uLamp:eye,uLampC:[0,0,0],uKey:cz,uKeyC:[.9,.94,1.05],uAmb:[.02,.022,.03],uBxH:NOBX,uTs:1,uMd:0});drawMesh(FM,36,6)}
if(LOGO.T&&LOGO.g>.003){gl.disable(gl.DEPTH_TEST);gl.disable(gl.CULL_FACE);BLN();LOGO.draw(as,t,S,qual,wx)}
gl.disable(gl.DEPTH_TEST);gl.depthMask(false);gl.disable(gl.BLEND);
// the glow, and out - dark as the camera reaches the tile, coming up out of the dark on the web, dimmer as the web is left, and black when the spider has jumped
let src=S;for(let j=0;j<BL.length;j++){pass(PDN,BL[j],{uC:src,uI:[1/src.w,1/src.h],uTh:j?0:1.1});src=BL[j]}
gl.enable(gl.BLEND);gl.blendFunc(gl.ONE,gl.ONE);for(let j=BL.length-1;j>0;j--)pass(PUP,BL[j-1],{uC:BL[j],uI:[1/BL[j].w,1/BL[j].h]},1);gl.disable(gl.BLEND);
pass(PCM,null,{uC:S,uB:BL[0],uR:[W,H],uT:t%100,uF:fade*(blk?1:wb?ss(.02,.45,b-WB)*(b<WB+6?1-.5*ss(4.4,6,b-WB):1-ss(6.105,6.15,b-WB)):room?1-ss(WB-.085,WB-.012,b):1),uBm:.5})}
// the readout
const m=M_;if(rdt<1){m.t+=rdt;m.n++;m.w=Math.max(m.w,rdt);m.js=(m.js||0)+performance.now()-j0;
if(m.t>=1){if(hud)HU.textContent=Math.round(m.n/m.t)+' fps · longest '+(m.w*1e3).toFixed(1)+' ms · script '+(m.js/m.n).toFixed(1)+' ms · bar '+b.toFixed(1)+' · '+(wb?'web':sn.fin?'fur':SC[sn.e].name)+' · '+wi+' window'+(wi==1?'':'s')+' · '+S.w+'×'+S.h+' · step '+lvl+' · display '+Math.round(1/tdisp)+' Hz';m.t=m.n=m.w=m.js=0}}
};
// It begins when it is clicked or a key is pressed, not of itself (but for ?mute, which is for measuring). The meter is there from the first only on the
// machine the piece is made on; elsewhere M or a tap in the corner brings it up (?hud=1 or ?hud=0 says which, whatever the place).
hset(UQ.has('hud')?+UQ.get('hud'):/^(127\.|localhost$|\[::1\])/.test(location.hostname)?1:0);requestAnimationFrame(frame);
window.ICO={errs,HP,FP,SC,SB,FUR,SPI,WEB,LOGO,FIN,TRAIL,SPOT,set cam(v){CAMO=v&&[0,...v]},check:()=>{[PBG,PSB,PSF,PHR,PBR,PFU,PDN,PUP,PCM].forEach(p=>{const c=CP;use(p);if(c)use(c)});return errs.length},get sn(){return SN},get state(){return{b:curB,frames,W,H,sw:S.w,sh:S.h,ctx:ctx&&ctx.state,hrow,frow,lvl,tdisp,upWait}},set seek(v){seekB=v},jump:b=>jump(b)};
})();
</script>
</body>
</html>