live.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. const D = __DATA__;
  2. const root = document.querySelector(".rp");
  3. const stage = root.querySelector(".rp-stage");
  4. const front = stage.querySelector(".rp-front");
  5. const engine = front.querySelector(".rp-engine");
  6. const canvas = stage.querySelector(".rp-plume");
  7. const veilCanvas = stage.querySelector(".rp-veil");
  8. const glowGroup = stage.querySelector(".rp-glow-g");
  9. const flashVeil = stage.querySelector(".rp-flash");
  10. const live = root.querySelector("[data-live]");
  11. const readout = root.querySelector("[data-readout]");
  12. const fireButton = root.querySelector("[data-fire]");
  13. const fireLabel = root.querySelector("[data-fire-label]");
  14. const partButtons = [...root.querySelectorAll(".rp-part")];
  15. const still = matchMedia("(prefers-reduced-motion: reduce)");
  16. const nodes = [...engine.querySelectorAll(".it")].map((el) => ({
  17. el,
  18. piece: el.dataset.piece,
  19. ride: el.dataset.ride || el.dataset.piece,
  20. key: Number(el.dataset.k),
  21. m: el.dataset.m.split(",").map(Number),
  22. index: Number(el.dataset.i),
  23. shown: "",
  24. }));
  25. for (const node of nodes) node.lift = node.m[0] * D.V[0] + node.m[1] * D.V[1] + node.m[2] * D.V[2];
  26. let order = nodes.map((_, index) => index);
  27. const IDS = Object.keys(D.pieces);
  28. const STAGGER = D.stagger;
  29. const SPRING = D.spring;
  30. const piece = Object.fromEntries(IDS.map((id) => [id, { x: 0, v: 0, target: 0, pending: null }]));
  31. const clamp = (value, low, high) => Math.min(high, Math.max(low, value));
  32. const fmt = (value) => Math.round(value).toLocaleString("en-US");
  33. const pct = (value) => `${(value * 100).toFixed(2)}%`;
  34. stage.style.setProperty("--gx", pct(D.exit[0] / D.W));
  35. stage.style.setProperty("--gy", pct((D.exit[1] + 6) / D.H));
  36. let clock = 0;
  37. let apart = false;
  38. let selected = null;
  39. let inside = false;
  40. let leaveTimer = 0;
  41. let touring = !still.matches;
  42. let tourClock = -1;
  43. let idleTimer = 0;
  44. let visible = false;
  45. let frame = 0;
  46. let last = 0;
  47. let jitter = [0, 0];
  48. const VENT = 0.7;
  49. const MAIN = 1.25;
  50. const STEADY = 9;
  51. const PREBURNER_PC = 0.13;
  52. const LONGEST = D.plumeLength + 60;
  53. const glide = () => ({ x: 0, v: 0 });
  54. const burn = {
  55. on: false,
  56. armed: false,
  57. tour: false,
  58. t: -1,
  59. stopAt: Infinity,
  60. stopping: -1,
  61. pc: 0,
  62. pcRate: 0,
  63. hue: glide(),
  64. glowStop: null,
  65. lastGlow: 0,
  66. popped: false,
  67. phase: "",
  68. pre: glide(),
  69. vent: glide(),
  70. smoke: glide(),
  71. reach: glide(),
  72. splash: glide(),
  73. mainAt: -99,
  74. popAt: -99,
  75. };
  76. function say(text) {
  77. if (readout.textContent !== text) readout.textContent = text;
  78. }
  79. function describe() {
  80. const text = selected ? D.pieces[selected].say : burn.on ? "firing" : apart ? "apart · 11 assemblies" : "Assembled, cold";
  81. if (stage.getAttribute("aria-valuetext") !== text) stage.setAttribute("aria-valuetext", text);
  82. }
  83. function setApart(on) {
  84. if (apart === on) return;
  85. apart = on;
  86. const goal = on ? 1 : 0;
  87. let index = 0;
  88. for (const id of on ? STAGGER : [...STAGGER].reverse()) {
  89. if (!piece[id]) continue;
  90. if (piece[id].target === goal) piece[id].pending = null;
  91. else piece[id].pending = { value: goal, at: clock + (still.matches ? 0 : index++ * SPRING.stagger) };
  92. }
  93. describe();
  94. run();
  95. }
  96. function select(id) {
  97. if (id === "stand") id = null;
  98. if (selected === id) return;
  99. selected = id;
  100. for (const node of nodes) {
  101. if (id !== null && node.piece === id) node.el.setAttribute("data-on", "");
  102. else node.el.removeAttribute("data-on");
  103. }
  104. if (id) stage.setAttribute("data-focus", "");
  105. else stage.removeAttribute("data-focus");
  106. partButtons.forEach((button) => (button.dataset.part === id ? button.setAttribute("data-on", "") : button.removeAttribute("data-on")));
  107. stage.setAttribute("aria-valuenow", String(id ? D.order.indexOf(id) + 1 : 0));
  108. describe();
  109. }
  110. function steerSpring(state, dt, calm) {
  111. if (state.pending && clock >= state.pending.at) {
  112. state.target = state.pending.value;
  113. state.pending = null;
  114. }
  115. if (calm) {
  116. state.x = state.target;
  117. state.v = 0;
  118. return false;
  119. }
  120. for (let index = 0; index < SPRING.substeps; index++) {
  121. const h = dt / SPRING.substeps;
  122. state.v += ((state.target - state.x) * SPRING.stiffness - state.v * SPRING.damping) * h;
  123. state.x += state.v * h;
  124. }
  125. if (Math.abs(state.target - state.x) < 2e-4 && Math.abs(state.v) < 2e-4) {
  126. state.x = state.target;
  127. state.v = 0;
  128. return state.pending !== null;
  129. }
  130. return true;
  131. }
  132. function place() {
  133. for (const node of nodes) {
  134. const share = piece[node.ride].x;
  135. const tx = (D.EX[0] * node.m[0] + D.EY[0] * node.m[1] + D.EZ[0] * node.m[2]) * share;
  136. const ty = (D.EX[1] * node.m[0] + D.EY[1] * node.m[1] + D.EZ[1] * node.m[2]) * share;
  137. const text = share === 0 ? "" : `translate(${tx.toFixed(2)} ${ty.toFixed(2)})`;
  138. if (text !== node.shown) {
  139. node.shown = text;
  140. if (text) node.el.setAttribute("transform", text);
  141. else node.el.removeAttribute("transform");
  142. }
  143. }
  144. const next = nodes.map((_, index) => index).sort((a, b) => nodes[a].key + nodes[a].lift * piece[nodes[a].ride].x - (nodes[b].key + nodes[b].lift * piece[nodes[b].ride].x));
  145. if (next.some((index, at) => index !== order[at])) {
  146. const fragment = document.createDocumentFragment();
  147. for (const index of next) fragment.appendChild(nodes[index].el);
  148. engine.appendChild(fragment);
  149. order = next;
  150. }
  151. const moved = Math.abs(jitter[0]) > 0.005 || Math.abs(jitter[1]) > 0.005;
  152. const shift = moved ? `translate(${jitter[0].toFixed(2)} ${jitter[1].toFixed(2)})` : "";
  153. engine.setAttribute("transform", shift);
  154. glowGroup.setAttribute("transform", shift);
  155. }
  156. const COMMON = `
  157. uniform vec2 uJit;
  158. uniform float uPc;
  159. uniform float uHue;
  160. uniform float uSmoke;
  161. uniform float uVent;
  162. uniform float uShockR;
  163. uniform float uShockA;
  164. uniform float uLight;
  165. const float ZE=${D.zExit.toFixed(1)};
  166. const float R0=${D.exitR.toFixed(1)};
  167. const float PL=${D.plumeLength.toFixed(1)};
  168. const float RB=132.;
  169. const vec2 COL0=vec2(${D.columns[0][0].toFixed(1)},${D.columns[0][1].toFixed(1)});
  170. const vec2 COL1=vec2(${D.columns[1][0].toFixed(1)},${D.columns[1][1].toFixed(1)});
  171. const float COLH=${D.columnHalf.toFixed(1)};
  172. const float COLTOP=${D.columnTop.toFixed(1)};
  173. float ground(float r){
  174. if(r>56.)return 0.;
  175. if(r>48.)return 4.;
  176. if(r>43.)return mix(4.,8.,(48.-r)/5.);
  177. if(r>35.)return mix(8.,13.,(43.-r)/8.);
  178. if(r>25.)return mix(13.,19.,(35.-r)/10.);
  179. if(r>14.5)return mix(19.,25.,(25.-r)/10.5);
  180. if(r>6.)return mix(25.,29.5,(14.5-r)/8.5);
  181. if(r>2.)return mix(29.5,32.,(6.-r)/4.);
  182. return 32.;
  183. }
  184. bool solid(vec3 p){return p.z<ground(length(p.xy));}
  185. float ringField(vec3 p){return length(vec2(length(p.xy)-68.,p.z-6.4))-2.1;}
  186. vec2 groundHit(vec3 o,vec3 V){
  187. float tf=-o.z/V.z;
  188. float best=tf;
  189. if(length(o.xy)<56.5){
  190. float ta=(36.-o.z)/V.z;
  191. float prev=ta;
  192. for(int i=1;i<=28;i++){
  193. float t=mix(ta,tf,float(i)/28.);
  194. if(solid(o+t*V)){
  195. float lo=t;float hi=prev;
  196. for(int j=0;j<7;j++){float m=(lo+hi)*.5;if(solid(o+m*V))lo=m;else hi=m;}
  197. best=hi;
  198. break;
  199. }
  200. prev=t;
  201. }
  202. }
  203. float t6=(6.4-o.z)/V.z;
  204. if(abs(length((o+t6*V).xy)-68.)<12.){
  205. float t=(9.-o.z)/V.z;
  206. for(int i=0;i<14;i++){
  207. float d=ringField(o+t*V);
  208. if(d<.02)break;
  209. t-=d;
  210. }
  211. if(ringField(o+t*V)<.06&&t>best)return vec2(t,1.);
  212. }
  213. return vec2(best,0.);
  214. }
  215. float columnNear(vec3 o,vec3 V,vec2 c){
  216. vec3 lo=vec3(c-COLH,0.);
  217. vec3 hi=vec3(c+COLH,COLTOP);
  218. vec3 a=(lo-o)/V;
  219. vec3 b=(hi-o)/V;
  220. vec3 mn=min(a,b);
  221. vec3 mx=max(a,b);
  222. float tIn=max(mn.x,max(mn.y,mn.z));
  223. float tOut=min(mx.x,min(mx.y,mx.z));
  224. return tOut>tIn?tOut:-1e9;
  225. }
  226. float columnsNear(vec3 o,vec3 V){return max(columnNear(o,V,COL0),columnNear(o,V,COL1));}
  227. float engineR(float z){
  228. float s=z-ZE;
  229. float b=36.+29.*(1.-pow(clamp(s,0.,120.)/120.,1.6))+3.;
  230. return mix(b,48.,smoothstep(108.,124.,s))*smoothstep(-6.,0.,s)*(1.-smoothstep(318.,330.,s));
  231. }
  232. float inFront(vec3 p,vec3 V){
  233. vec2 h=V.xy;
  234. float hl=length(h);
  235. float tau=-dot(p.xy,h)/(hl*hl);
  236. float d=length(p.xy+tau*h);
  237. float re=engineR(p.z+tau*V.z);
  238. float inside=1.-smoothstep(re-3.,re+3.,d);
  239. float surface=tau*hl+sqrt(max(re*re-d*d,0.));
  240. return inside*smoothstep(0.,5.,-surface);
  241. }
  242. float side(float front,float veil){return mix(1.-front,front,veil);}
  243. float beyond(float t0,float tc,float sigma){return 1./(1.+exp(clamp(2.41*(t0-tc)/sigma,-30.,30.)));}
  244. vec3 steamOf(vec3 g0,vec3 V,float tA,float u,float z0,float pf,float T,float veil){
  245. if(uSmoke<.0004&&uVent<.0004)return vec3(0.);
  246. float sE=uCam.z;
  247. float Hs=70.+60.*uSmoke;
  248. vec3 start=g0+tA*V;
  249. float steam=0.;
  250. float vapour=0.;
  251. for(int i=0;i<6;i++){
  252. float w=(float(i)+.5)/6.;
  253. vec3 p=start+(-Hs*log(w)/sE)*V;
  254. float r=length(p.xy);
  255. float lay=side(inFront(p,V),veil);
  256. float box=1.-smoothstep(120.,178.,max(abs(p.x),abs(p.y)));
  257. steam+=box*(.4+.6*exp(-pow((r-90.)/80.,2.)))*(1.-smoothstep(170.,330.,p.z))*lay;
  258. vapour+=exp(-pow(r/96.,2.))*(1.-smoothstep(40.,140.,p.z))*lay;
  259. }
  260. float layer=exp(-max(start.z,0.)/Hs)/6.;
  261. float b1=fbm2(vec2(u*.018+T*.03,z0*.016-T*.26+7.));
  262. float b2=fbm2(vec2(u*.045-T*.05,z0*.04-T*.5+2.));
  263. vec3 lit=mix(vec3(.3,.3,.33),vec3(.72,.4,.2),pf*exp(-max(z0,0.)/150.));
  264. return lit*(b1*.75+b2*.4)*smoothstep(.05,.6,b1)*steam*layer*uSmoke*1.05+vec3(.42,.47,.54)*(b2*.6+.4)*vapour*layer*uVent*.5;
  265. }
  266. float shellOf(float dist,float tc,float tAir,float rs,float w,vec3 e0,vec3 V,float veil){
  267. if(rs<.5)return 0.;
  268. float m=sqrt(max(rs*rs-dist*dist,0.));
  269. float edge=exp(-pow(max(dist-rs,0.)/w,2.));
  270. float limb=rs*w/sqrt(m*m+2.*rs*w)/sqrt(rs*w*.5);
  271. limb*=limb;
  272. float soft=w*2.+2.;
  273. float far=tc-m;
  274. float near=tc+m;
  275. float aFar=smoothstep(tAir-soft*.5,tAir+soft,far)*side(inFront(e0+far*V,V),veil);
  276. float aNear=smoothstep(tAir-soft*.5,tAir+soft,near)*side(inFront(e0+near*V,V),veil);
  277. return edge*limb*(aFar+aNear)*.5;
  278. }
  279. vec3 shockOf(float dist,float tc,float tAir,vec3 e0,vec3 V,float veil){
  280. if(uShockA<.0004)return vec3(0.);
  281. float rs=uShockR;
  282. float sw=1.8+max(rs-26.,0.)*.01;
  283. float lead=shellOf(dist,tc,tAir,rs,sw,e0,V,veil);
  284. float trail=shellOf(dist,tc,tAir,max(rs-sw*2.6,0.),sw*2.2,e0,V,veil)*.3;
  285. return vec3(.84,.88,1.)*(lead+trail)*.42*uShockA*(1.-smoothstep(170.,270.,rs));
  286. }
  287. vec4 inkOf(vec3 col){
  288. col=tonemap(col,1.15);
  289. float m=max(col.r,max(col.g,col.b));
  290. float n=(hash12(gl_FragCoord.xy+fract(uTime*.61)*91.)+hash12(gl_FragCoord.yx*1.37+fract(uTime*.37)*57.)-1.)/255.;
  291. col=max(col+n*smoothstep(0.,1.5/255.,m),0.);
  292. m=max(col.r,max(col.g,col.b));
  293. vec3 ink=col/max(m,1e-5)*vec3(.62,.34,.3);
  294. vec4 light=vec4(ink*m*.92,m*.92);
  295. return mix(vec4(col,m),light,uLight);
  296. }
  297. struct Scene{vec3 V;vec3 g0;vec3 e0;vec3 hit;float tLow;float ring;float tCol;float tAir;float tAirE;float tLowE;float u;float z0;float s0;float dist;float tc;};
  298. Scene sceneOf(vec2 vb){
  299. Scene S;
  300. S.V=towardViewer();
  301. float sE=uCam.z;
  302. S.g0=rayOf(vb);
  303. vec2 gh=groundHit(S.g0,S.V);
  304. S.tLow=gh.x;
  305. S.ring=gh.y;
  306. S.tCol=columnsNear(S.g0,S.V);
  307. S.tAir=max(S.tLow,S.tCol);
  308. S.hit=S.g0+S.tLow*S.V;
  309. S.e0=rayOf(vb-uJit);
  310. S.u=(vb.x-uJit.x-uOrigin.x)/uK;
  311. S.z0=S.e0.z;
  312. S.s0=ZE-S.z0;
  313. S.tLowE=S.tLow+(S.g0.z-S.e0.z)/sE;
  314. S.tAirE=S.tAir+(S.g0.z-S.e0.z)/sE;
  315. vec3 C=vec3(0.,0.,ZE-6.);
  316. S.tc=dot(C-S.e0,S.V);
  317. S.dist=length(S.e0+S.tc*S.V-C);
  318. return S;
  319. }
  320. `;
  321. const PLUME = `${COMMON}
  322. uniform float uGlow;
  323. uniform float uPre;
  324. uniform float uReach;
  325. uniform float uSplash;
  326. uniform float uFlash;
  327. uniform float uRingR;
  328. uniform float uRingA;
  329. uniform float uPop;
  330. uniform float uPopR;
  331. float onDeck(vec2 p){return 1.-smoothstep(134.,150.,max(abs(p.x),abs(p.y)));}
  332. float radiusAt(float s,float pf,float warm){
  333. float spread=.045+.12*(1.-pf);
  334. return R0*(1.-.07*pf*smoothstep(0.,40.,s)-.42*warm*smoothstep(0.,80.,s))+spread*max(s,0.);
  335. }
  336. void main(){
  337. vec2 vb=viewOf(gl_FragCoord.xy);
  338. Scene S=sceneOf(vb);
  339. vec3 V=S.V;
  340. float sE=uCam.z;
  341. float cE=uCam.w;
  342. float tanE=sE/cE;
  343. float T=uTime;
  344. float pc=clamp(uPc,0.,1.);
  345. float pf=pc;
  346. float warm=1.-clamp(uHue,0.,1.);
  347. float glow=uGlow;
  348. float dith=hash12(gl_FragCoord.xy);
  349. vec3 col=vec3(0.);
  350. vec3 g0=S.g0;
  351. vec3 e0=S.e0;
  352. float u=S.u;
  353. float z0=S.z0;
  354. float s0=S.s0;
  355. vec3 hit=S.hit;
  356. float rho=length(hit.xy);
  357. vec2 dirH=hit.xy/max(rho,1e-3);
  358. float tongue=fbm2(vec2(u*.07,s0*.06-T*8.));
  359. float reach=uReach*(1.+(tongue-.5)*.55*warm);
  360. float rag=1.+1.6*warm;
  361. float lam=29.+7.*pf;
  362. float v0=19.+6.*pf;
  363. float cellOn=smoothstep(.4,.95,pf);
  364. if((glow>.0004||uVent>.0004)&&abs(u)<RB){
  365. float e=fbm2(vec2(u*.06+7.,s0*.03-T*4.6));
  366. float e2=fbm2(vec2(u*.16-3.,s0*.08-T*7.3));
  367. float streak=.86+.3*fbm2(vec2(u*.22,s0*.012-T*9.));
  368. float flick=(.94+.06*sin(T*97.+s0*.23))*(1.+uPre*warm*(tongue-.45)*.9);
  369. float mist=fbm2(vec2(u*.035+.4*T,s0*.022-T*.7));
  370. float halfChord=sqrt(max(RB*RB-u*u,0.))/cE;
  371. float ta=max(S.tAirE,-halfChord);
  372. float tb=min((ZE+2.-z0)/sE,halfChord);
  373. float dt=max(tb-ta,0.)/28.;
  374. vec3 sum=vec3(0.);
  375. vec3 cold=vec3(0.);
  376. for(int i=0;i<28;i++){
  377. float t=ta+(float(i)+dith)*dt;
  378. vec3 p=e0+t*V;
  379. float s=ZE-p.z;
  380. float r=length(p.xy);
  381. float keep=(1.-smoothstep(RB*.8,RB,r))*smoothstep(-.5,4.,s);
  382. float Rv=radiusAt(s,pf,warm);
  383. float Re=Rv*(1.+(e-.5)*.42*rag*smoothstep(4.,120.,s)+(e2-.5)*.1*rag*smoothstep(0.,40.,s));
  384. float rn=r/Re;
  385. float fade=(1.-smoothstep(reach-46.,reach,s))*keep;
  386. vec3 violet=mix(vec3(.86,.44,1.),vec3(1.,.5,.62),smoothstep(0.,150.,s)*.7);
  387. vec3 ember=mix(vec3(1.,.36,.07),vec3(1.,.7,.32),tongue);
  388. vec3 bodyCol=mix(violet,ember,warm);
  389. float fill=smoothstep(1.06,.72,rn);
  390. float wBody=dt*cE/(2.*Re);
  391. sum+=bodyCol*fill*wBody*(.26+.3*exp(-max(s,0.)/110.))*streak*flick*fade*2.;
  392. sum+=bodyCol*exp(-rn*rn*2.)*(dt*cE/(Re*1.25))*.22*fade;
  393. float rc=Rv*.34;
  394. sum+=mix(vec3(1.,.74,.9),vec3(1.,.8,.45),warm)*exp(-(r*r)/(rc*rc))*(dt*cE/(rc*1.772))*.34*(1.-.5*smoothstep(PL-30.,PL+10.,s))*fade;
  395. float rb=Rv*.62;
  396. sum+=mix(vec3(1.,.9,.76),vec3(1.,.78,.46),warm)*exp(-(r*r)/(rb*rb))*exp(-max(s,0.)/24.)*(dt*cE/(rb*1.772))*1.6*keep;
  397. float sw=Re*.24;
  398. float shell=exp(-pow((r-Re*.9)/sw,2.))*smoothstep(8.,150.,s)*(dt*cE/(sw*3.545));
  399. sum+=vec3(1.,.42,.12)*shell*.8*(.35+1.1*e)*fade;
  400. sum+=vec3(1.,.8,.52)*smoothstep(PL-30.,PL+4.,s)*fill*wBody*.8*fade;
  401. for(int c=0;c<4;c++){
  402. float fc=float(c);
  403. float vc=v0+fc*lam+sin(T*47.+fc*2.3)*.6;
  404. float Rc=R0*(1.-.07*pf)+(.045+.12*(1.-pf))*vc;
  405. float amp=exp(-fc*.28)*cellOn*(1.-smoothstep(reach-70.,reach-10.,vc))*(1.-smoothstep(PL-24.,PL-6.,vc))*keep;
  406. float rm=Rc*.33;
  407. float ph=(s-vc)/(lam*.62);
  408. float taper=rm*(1.06-clamp(ph,0.,1.)*.8)+.6;
  409. float bead=exp(-pow(r/taper,2.)*2.)*exp(-max(ph,0.)*2.2)*smoothstep(-.16,.02,ph)*(1.-smoothstep(.7,1.,ph));
  410. sum+=vec3(1.,.82,.46)*bead*(dt*cE/(taper*1.25))*2.1*amp;
  411. float d=r/(Rc*.9)+abs(s-vc+lam*.08)/(lam*.5);
  412. sum+=vec3(1.,.5,.64)*pow(clamp(1.-d,0.,1.),2.2)*(dt*cE/(Rc*.9*.62))*1.05*amp;
  413. }
  414. float sigma=R0*(.85+max(s,0.)*.012);
  415. cold+=vec3(.5,.56,.64)*exp(-(r*r)/(sigma*sigma))*(dt*cE/(sigma*1.772))*smoothstep(0.,16.,s)*(1.-smoothstep(50.,170.,s))*keep;
  416. }
  417. col+=sum*glow;
  418. col+=cold*mist*uVent*.62;
  419. }
  420. if(glow>.0004){
  421. float sh=radiusAt(clamp(s0,0.,PL),pf,warm)*2.2;
  422. float span=2.6*sh/cE;
  423. float h0=max(S.tAirE,-span);
  424. float dh=max(span-h0,0.)/8.;
  425. float halo=0.;
  426. for(int i=0;i<8;i++){
  427. float t=h0+(float(i)+dith)*dh;
  428. float z=z0+t*sE;
  429. float rr=u*u+t*t*cE*cE;
  430. halo+=exp(-rr/(sh*sh))*exp(-max(ZE-z,0.)/240.)*smoothstep(0.,30.,z)*smoothstep(ZE+25.,ZE-15.,z);
  431. }
  432. col+=mix(vec3(.9,.42,.62),vec3(1.,.45,.16),warm)*halo*(dh*cE/(sh*1.772))*.16*glow;
  433. }
  434. for(int c=0;c<4;c++){
  435. float fc=float(c);
  436. float vc=v0+fc*lam+sin(T*47.+fc*2.3)*.6;
  437. float Rc=R0*(1.-.07*pf)+(.045+.12*(1.-pf))*vc;
  438. float amp=exp(-fc*.28)*cellOn*(1.-smoothstep(reach-70.,reach-10.,vc))*(1.-smoothstep(PL-24.,PL-6.,vc));
  439. float rm=Rc*.33;
  440. float q=length(vec2(u/rm,(s0-vc)/(rm*tanE+1.2)));
  441. col+=vec3(1.,.98,.9)*exp(-q*q*2.4)*2.6*amp*glow;
  442. }
  443. float dist=S.dist;
  444. col+=vec3(1.,.84,.64)*uFlash*(exp(-pow(dist/120.,2.))*.36*beyond(S.tAirE,S.tc,120.)+exp(-dist/30.)*1.5*beyond(S.tAirE,S.tc,30.));
  445. col+=shockOf(dist,S.tc,S.tAirE,e0,V,0.);
  446. float floorSeen=S.tCol>S.tLow?0.:1.;
  447. float deck=onDeck(hit.xy)*floorSeen;
  448. vec3 tube=hit-vec3(dirH*68.,6.4);
  449. float tubeLit=max(dot(normalize(tube),normalize(vec3(-dirH*68.,26.))),0.);
  450. float facing=mix(1.,.15+1.3*tubeLit,S.ring);
  451. float splash=uSplash*pf;
  452. if(splash>.0004||uRingA>.0004){
  453. float flow=fbm3(vec3(dirH*2.4,rho*.035-T*3.8));
  454. float flow2=noise3(vec3(dirH*5.1+9.,rho*.07-T*6.1));
  455. float reachS=56.+80.*splash;
  456. float sheet=exp(-pow(rho/reachS,1.5))*deck*facing;
  457. vec3 hot=mix(vec3(1.,.38,.1),vec3(1.,.82,.55),exp(-rho/26.));
  458. col+=hot*sheet*(.26+.56*flow+.22*flow2)*splash*.62;
  459. col+=vec3(1.,.88,.7)*exp(-pow(rho/40.,2.))*splash*.9*floorSeen;
  460. float dust=noise3(vec3(dirH*7.,rho*.05));
  461. float gw=4.+uRingR*.034;
  462. col+=vec3(1.,.8,.6)*exp(-pow((rho-uRingR)/gw,2.))*(.45+dust)*deck*facing*uRingA*.5;
  463. if(splash>.0004){
  464. float tS=(70.-g0.z)/sE;
  465. float dS=max(tS-S.tAir,0.)/10.;
  466. float curtain=0.;
  467. for(int i=0;i<10;i++){
  468. vec3 p=g0+(S.tAir+(float(i)+dith)*dS)*V;
  469. float r=length(p.xy);
  470. float h=max(p.z-ground(r),0.);
  471. curtain+=exp(-pow((r-reachS*.8)/(reachS*.34),2.))*exp(-h/26.)*onDeck(p.xy)*dS*sE/26.;
  472. }
  473. float lick=fbm2(vec2(u*.05+T*.3,z0*.06-T*1.4));
  474. col+=vec3(1.,.46,.16)*curtain*lick*.55*splash;
  475. }
  476. }
  477. col+=steamOf(g0,V,S.tAir,u,z0,pf,T,0.);
  478. if(uPop>.0004){
  479. vec3 Cp=vec3(0.,0.,ZE-uPopR*.55);
  480. float tp=dot(Cp-e0,V);
  481. float pd=length(e0+tp*V-Cp);
  482. float pn=fbm2(vec2(u*.045,s0*.045-T*2.4));
  483. col+=vec3(1.,.46,.15)*exp(-pow(pd/uPopR,2.))*(.55+.7*pn)*smoothstep(ZE+24.,ZE-12.,z0)*beyond(S.tAirE,tp,uPopR)*uPop*.95;
  484. }
  485. gl_FragColor=inkOf(col);
  486. }`;
  487. const VEIL = `${COMMON}
  488. void main(){
  489. vec2 vb=viewOf(gl_FragCoord.xy);
  490. Scene S=sceneOf(vb);
  491. float pf=clamp(uPc,0.,1.);
  492. vec3 col=steamOf(S.g0,S.V,S.tAir,S.u,S.z0,pf,uTime,1.);
  493. col+=shockOf(S.dist,S.tc,S.tAirE,S.e0,S.V,1.);
  494. gl_FragColor=inkOf(col);
  495. }`;
  496. const SHARED = ["uJit", "uPc", "uHue", "uSmoke", "uVent", "uShockR", "uShockA", "uLight"];
  497. const layer = glLayer({
  498. canvas,
  499. fragment: PLUME,
  500. view: [D.W, D.H],
  501. camera: D.camera,
  502. uniforms: [...SHARED, "uGlow", "uPre", "uReach", "uSplash", "uFlash", "uRingR", "uRingA", "uPop", "uPopR"],
  503. });
  504. const veil = layer
  505. ? glLayer({
  506. canvas: veilCanvas,
  507. fragment: VEIL,
  508. view: [D.W, D.H],
  509. camera: D.camera,
  510. uniforms: SHARED,
  511. })
  512. : null;
  513. if (layer) stage.setAttribute("data-gl", "");
  514. const LIGHT = document.body.dataset.theme === "light" || root.closest('[data-theme="light"]') ? 1 : 0;
  515. let lastKey = "";
  516. function paintPlume(values, active, veiled) {
  517. if (!layer) return;
  518. if (!active) {
  519. layer.clear();
  520. if (veil) veil.clear();
  521. lastKey = "";
  522. return;
  523. }
  524. const time = still.matches ? 0.8 : clock;
  525. const key = still.matches ? JSON.stringify(values) + veiled : "";
  526. if (key && key === lastKey) return;
  527. lastKey = key;
  528. layer.draw(values, time);
  529. if (!veil) return;
  530. if (veiled) veil.draw(values, time);
  531. else veil.clear();
  532. }
  533. function announce(phase) {
  534. if (burn.phase === phase) return;
  535. burn.phase = phase;
  536. if (live) live.textContent = phase;
  537. }
  538. function fireState(state) {
  539. if (state) fireButton.dataset.state = state;
  540. else delete fireButton.dataset.state;
  541. }
  542. function ignite(fromTour = false) {
  543. if (burn.on) return;
  544. setApart(false);
  545. select(null);
  546. burn.on = true;
  547. describe();
  548. burn.armed = true;
  549. burn.tour = fromTour;
  550. burn.t = 0;
  551. burn.stopping = -1;
  552. burn.glowStop = null;
  553. burn.popped = false;
  554. burn.stopAt = fromTour ? MAIN + 4.2 : MAIN + STEADY;
  555. fireState("armed");
  556. fireLabel.textContent = "Shut down";
  557. run();
  558. }
  559. function shutdown() {
  560. if (!burn.on || burn.stopping >= 0) return;
  561. burn.stopping = burn.t;
  562. }
  563. function pcTarget() {
  564. if (!burn.on || burn.armed || burn.stopping >= 0) return 0;
  565. if (burn.t >= MAIN) return 1;
  566. if (burn.t >= VENT) return PREBURNER_PC;
  567. return 0;
  568. }
  569. function stepBurn(dt, calm) {
  570. if (burn.on && burn.armed && IDS.every((id) => piece[id].x < 0.002 && !piece[id].pending)) {
  571. burn.armed = false;
  572. fireState("on");
  573. if (calm) burn.t = MAIN + 0.5;
  574. }
  575. const before = burn.t;
  576. if (burn.on && !burn.armed) burn.t += dt;
  577. const t = burn.t;
  578. if (burn.on && burn.stopping < 0 && t >= burn.stopAt) burn.stopping = t;
  579. const lit = burn.on && !burn.armed && burn.stopping < 0;
  580. if (lit && t >= MAIN && before < MAIN && !calm) burn.mainAt = clock;
  581. const target = pcTarget();
  582. if (calm) {
  583. burn.pc = target;
  584. burn.pcRate = 0;
  585. } else {
  586. const falling = burn.stopping >= 0;
  587. const stiff = falling ? 16 : target > PREBURNER_PC ? 20 : 64;
  588. const drag = falling ? 8.2 : 2 * Math.sqrt(stiff);
  589. for (let index = 0; index < 4; index++) {
  590. const h = dt / 4;
  591. burn.pcRate += ((target - burn.pc) * stiff - burn.pcRate * drag) * h;
  592. burn.pc = clamp(burn.pc + burn.pcRate * h, 0, 1);
  593. }
  594. }
  595. settle(burn.hue, lit && burn.pc > 0.2 ? 1 : 0, dt, lit ? 0.32 : 0.5, calm);
  596. const onset = smooth(0, 0.1, burn.pc);
  597. let glow = onset * (1.15 - 0.15 * burn.hue.x);
  598. if (burn.stopping >= 0 && !calm) {
  599. if (!burn.glowStop) burn.glowStop = { glow: burn.lastGlow ?? glow, onset: Math.max(onset, 1e-3), pc: Math.max(burn.pc, 1e-3) };
  600. const stop = burn.glowStop;
  601. glow = Math.min(stop.glow, stop.glow * Math.min(1, onset / stop.onset) * Math.sqrt(Math.min(1, burn.pc / stop.pc)));
  602. }
  603. if (!burn.on) glow = 0;
  604. burn.lastGlow = glow;
  605. settle(burn.pre, lit && t > VENT && t < MAIN + 0.1 ? 1 : 0, dt, lit && t < MAIN ? 0.06 : 0.12, calm);
  606. settle(burn.vent, lit && t < MAIN ? 1 : 0, dt, lit && t < MAIN ? 0.16 : 0.24, calm);
  607. settle(burn.reach, 72 * smooth(0, 0.12, burn.pc) + (LONGEST - 72) * smooth(0.16, 0.75, burn.pc), dt, burn.stopping >= 0 ? 0.12 : 0.07, calm);
  608. settle(burn.splash, smooth(0.35, 0.9, burn.pc) * smooth(D.plumeLength - 10, D.plumeLength + 40, burn.reach.x), dt, 0.16, calm);
  609. settle(burn.smoke, lit && t > MAIN + 0.4 ? 1 : 0, dt, lit ? 0.9 : 1.8, calm);
  610. if (burn.on && burn.stopping >= 0 && burn.pc < 0.2 && !burn.popped && !calm) {
  611. burn.popAt = clock;
  612. burn.popped = true;
  613. }
  614. if (burn.on && burn.stopping >= 0 && burn.pc < 0.003 && Math.abs(burn.pcRate) < 0.01 && t - burn.stopping > 0.9) {
  615. burn.on = false;
  616. burn.t = -1;
  617. burn.pc = 0;
  618. burn.pcRate = 0;
  619. burn.glowStop = null;
  620. fireState("");
  621. fireLabel.textContent = "Ignite";
  622. announce("cold");
  623. describe();
  624. if (burn.tour) tourClock = 0;
  625. }
  626. const sinceMain = clock - burn.mainAt;
  627. const sincePop = burn.popAt >= 0 ? clock - burn.popAt : -1;
  628. const flash = calm ? 0 : burst(sinceMain, 0.08, 0.13);
  629. const shockA = calm ? 0 : burst(sinceMain, 0.05, 0.12);
  630. const ringA = calm ? 0 : burst(sinceMain - 0.08, 0.08, 0.16);
  631. const pop = calm || sincePop < 0 ? 0 : burst(sincePop, 0.09, 0.2);
  632. const shake = calm ? 0 : 3.2 * burst(sinceMain, 0.05, 0.12);
  633. const hum = calm ? 0 : 0.32 * burn.pc * smooth(0.02, 0.12, burn.pc);
  634. jitter = [Math.sin(clock * 91) * hum + Math.sin(clock * 53) * shake, Math.cos(clock * 77) * hum * 0.7 + Math.cos(clock * 61) * shake];
  635. const values = {
  636. uJit: jitter,
  637. uPc: burn.pc,
  638. uHue: burn.hue.x,
  639. uGlow: glow,
  640. uLight: LIGHT,
  641. uPre: burn.pre.x,
  642. uVent: burn.vent.x,
  643. uSmoke: burn.smoke.x,
  644. uReach: burn.reach.x,
  645. uSplash: burn.splash.x,
  646. uFlash: flash,
  647. uShockR: 26 + 900 * Math.max(0, sinceMain),
  648. uShockA: shockA,
  649. uRingR: 600 * Math.max(0, sinceMain - 0.08),
  650. uRingA: ringA,
  651. uPop: pop,
  652. uPopR: 24 + 70 * (1 - Math.exp(-Math.max(0, sincePop) / 0.25)),
  653. };
  654. const active = burn.pc > 1e-3 || burn.pre.x > 1e-3 || burn.vent.x > 1e-3 || burn.smoke.x > 1e-3 || flash > 1e-3 || shockA > 1e-3 || ringA > 1e-3 || pop > 1e-3 || (sinceMain > 0 && sinceMain < 0.6);
  655. paintPlume(values, active, burn.smoke.x > 1e-3 || burn.vent.x > 1e-3 || shockA > 1e-3);
  656. const rim = clamp(burn.pc * 0.85 + burn.pre.x * 0.45 * smooth(0, 0.05, burn.pc) + flash * 0.6 + pop * 0.7, 0, 1);
  657. stage.style.setProperty("--rim", rim.toFixed(3));
  658. stage.style.setProperty("--deck-glow", clamp(burn.pc * 0.9 * burn.splash.x + flash * 0.5 + burn.pre.x * 0.12 * smooth(0, 0.05, burn.pc) + pop * 0.25, 0, 1).toFixed(3));
  659. flashVeil.style.opacity = (flash * 0.32).toFixed(3);
  660. stage.style.transform = shake > 0.01 ? `translate(${(Math.sin(clock * 47) * shake * 0.5).toFixed(2)}px, ${(Math.cos(clock * 59) * shake * 0.5).toFixed(2)}px)` : "";
  661. if (burn.on) {
  662. const bar = fmt(burn.pc * D.chamberBar);
  663. const thrust = fmt(Math.max(0, D.vacuumTf * burn.pc - D.backTf));
  664. if (burn.armed) {
  665. say("closing up · arming");
  666. announce("arming");
  667. } else if (burn.stopping >= 0) {
  668. say(burn.pc > 0.01 ? `shutdown · ${bar} bar` : "shutdown · purging");
  669. announce("shutdown");
  670. } else if (t < VENT) {
  671. say("chill-down · venting");
  672. announce("chill-down, venting");
  673. } else if (t < MAIN) {
  674. say(`spin-up · preburners lit · ${bar} bar`);
  675. announce("preburners lit");
  676. } else {
  677. say(`Pc ${bar} bar · ${thrust} tf`);
  678. announce("main stage");
  679. }
  680. }
  681. return burn.on || active;
  682. }
  683. const TOUR_STEP = 1.5;
  684. function tourAt(time) {
  685. const apartFrom = 0.4;
  686. const listFrom = apartFrom + 0.9;
  687. const listTo = listFrom + D.order.length * TOUR_STEP;
  688. const fire = listTo + 1.4;
  689. if (time < apartFrom) return { apart: false, part: null, fire: false };
  690. if (time < listFrom) return { apart: true, part: null, fire: false };
  691. if (time < listTo) return { apart: true, part: D.order[Math.floor((time - listFrom) / TOUR_STEP)], fire: false };
  692. if (time < fire) return { apart: false, part: null, fire: false };
  693. return { apart: false, part: null, fire: true };
  694. }
  695. function quietTour() {
  696. touring = false;
  697. clearTimeout(idleTimer);
  698. idleTimer = setTimeout(() => {
  699. if (inside || burn.on) return quietTour();
  700. touring = !still.matches;
  701. tourClock = -0.6;
  702. run();
  703. }, 3600);
  704. }
  705. function idleText() {
  706. if (selected) return D.pieces[selected].say;
  707. const moving = IDS.some((id) => Math.abs(piece[id].x - piece[id].target) > 0.002 || piece[id].pending);
  708. if (moving) return apart ? "taking apart" : "closing up";
  709. return apart ? "apart · 11 assemblies" : "1,630 kg · cold";
  710. }
  711. function tick(now) {
  712. const dt = last ? Math.min((now - last) / 1000, 1 / 30) : 1 / 60;
  713. last = now;
  714. clock += dt;
  715. const calm = still.matches;
  716. if (touring && !burn.on) {
  717. tourClock += dt;
  718. const step = tourAt(tourClock);
  719. setApart(step.apart);
  720. select(step.part);
  721. if (step.fire) ignite(true);
  722. }
  723. let moving = false;
  724. for (const id of IDS) if (steerSpring(piece[id], dt, calm)) moving = true;
  725. const burning = stepBurn(dt, calm);
  726. place();
  727. if (!burn.on) say(idleText());
  728. frame = visible && (moving || burning || touring) ? requestAnimationFrame(tick) : 0;
  729. }
  730. function run() {
  731. if (!frame && visible) {
  732. last = 0;
  733. frame = requestAnimationFrame(tick);
  734. }
  735. }
  736. new IntersectionObserver(
  737. (entries) => {
  738. visible = entries[entries.length - 1].isIntersecting;
  739. if (visible) run();
  740. },
  741. { rootMargin: "120px 0px" },
  742. ).observe(stage);
  743. function pieceAt(event) {
  744. const hit = document.elementFromPoint(event.clientX, event.clientY);
  745. const item = hit && hit.closest ? hit.closest(".it") : null;
  746. return item ? item.dataset.piece : null;
  747. }
  748. stage.addEventListener("pointermove", (event) => {
  749. if (event.pointerType === "touch") return;
  750. inside = true;
  751. clearTimeout(leaveTimer);
  752. quietTour();
  753. if (burn.on) return;
  754. setApart(true);
  755. select(pieceAt(event));
  756. run();
  757. });
  758. stage.addEventListener("pointerleave", () => {
  759. inside = false;
  760. clearTimeout(leaveTimer);
  761. leaveTimer = setTimeout(() => {
  762. if (inside) return;
  763. select(null);
  764. setApart(false);
  765. }, 260);
  766. });
  767. stage.addEventListener("pointerdown", (event) => {
  768. if (event.pointerType !== "touch" || burn.on) return;
  769. quietTour();
  770. const id = pieceAt(event);
  771. if (!id || id === "stand") {
  772. select(null);
  773. setApart(!apart);
  774. } else {
  775. setApart(true);
  776. select(id);
  777. }
  778. run();
  779. });
  780. fireButton.addEventListener("click", () => {
  781. quietTour();
  782. if (burn.on) shutdown();
  783. else ignite(false);
  784. run();
  785. });
  786. partButtons.forEach((button) => {
  787. const show = () => {
  788. if (burn.on) return;
  789. quietTour();
  790. clearTimeout(leaveTimer);
  791. setApart(true);
  792. select(button.dataset.part);
  793. run();
  794. };
  795. const hide = () => {
  796. clearTimeout(leaveTimer);
  797. leaveTimer = setTimeout(() => {
  798. if (inside || partButtons.some((other) => other.matches(":hover, :focus-visible"))) return;
  799. select(null);
  800. setApart(false);
  801. }, 260);
  802. };
  803. button.addEventListener("pointerenter", show);
  804. button.addEventListener("focus", show);
  805. button.addEventListener("pointerleave", hide);
  806. button.addEventListener("blur", hide);
  807. button.addEventListener("click", show);
  808. });
  809. stage.addEventListener("keydown", (event) => {
  810. const index = selected ? D.order.indexOf(selected) : -1;
  811. let next = null;
  812. if (event.key === "ArrowRight" || event.key === "ArrowDown") next = Math.min(D.order.length - 1, index + 1);
  813. else if (event.key === "ArrowLeft" || event.key === "ArrowUp") next = Math.max(0, index - 1);
  814. else if (event.key === "Home") next = 0;
  815. else if (event.key === "End") next = D.order.length - 1;
  816. else if (event.key === "Escape") {
  817. event.preventDefault();
  818. quietTour();
  819. if (burn.on) shutdown();
  820. select(null);
  821. setApart(false);
  822. run();
  823. return;
  824. } else if (event.key === "i" || event.key === "I" || event.key === "Enter") {
  825. event.preventDefault();
  826. quietTour();
  827. if (burn.on) shutdown();
  828. else ignite(false);
  829. run();
  830. return;
  831. } else if (event.key === " ") {
  832. event.preventDefault();
  833. quietTour();
  834. if (!burn.on) setApart(!apart);
  835. if (!apart) select(null);
  836. run();
  837. return;
  838. }
  839. if (next === null || burn.on) return;
  840. event.preventDefault();
  841. quietTour();
  842. setApart(true);
  843. select(D.order[next]);
  844. run();
  845. });
  846. if (still.matches) touring = false;
  847. const narrow = matchMedia("(hover: none), (max-width: 480px)");
  848. const hint = root.querySelector(".rp-hint");
  849. const wording = () => hint && (hint.textContent = narrow.matches ? "Tap to take apart" : "Hover the engine to take it apart");
  850. wording();
  851. narrow.addEventListener("change", wording);
  852. window.addEventListener("resize", () => {
  853. lastKey = "";
  854. run();
  855. });
  856. place();
  857. say(idleText());