moodi 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
'use strict';var three=require('three');var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Bt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Ot={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},de=e=>Ue.includes(e),ue=e=>Je.includes(e);var O=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return Fe(t[1]);if(O||(O=document.createElement("canvas").getContext("2d")),!O)return o;O.fillStyle="#010203",O.fillStyle=e;let s=String(O.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return Fe(s.slice(1));O.canvas.width=O.canvas.height=1,O.clearRect(0,0,1,1),O.fillRect(0,0,1,1);let i=O.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Fe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var L=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],pe=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),me=e=>.2126*pe(e[0])+.7152*pe(e[1])+.0722*pe(e[2]),Ze=[.07,.06,.08],Qe=[1,.97,.93],et=[1,.42,.48],ee=e=>me(e)>.06?x(Ze,e,.06):Qe,te=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],G={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},oe={idle:{...G},thinking:{...G,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...G,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...G,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...G,mouthW:.09,mouthCurve:.25},happy:{...G,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...G,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...G,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...G,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...G,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...G,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Be={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ge=e=>({...e}),tt=.31,ot=-0.04,st=.098,fe=.128,it=.2,be=.05;function Oe(e,o,t,s,i,r,d,a,l,u){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let h=o.lookX*.075,p=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h,f=.16+p,c=e.createRadialGradient(v,f,0,v,f,.16);c.addColorStop(0,i),c.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=c,e.beginPath(),e.ellipse(v,f,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let g=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(g>.01){e.save(),e.globalAlpha=g*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let y of [-1,1]){let v=y*.52+h,f=.16+p;for(let c=-1;c<=1;c++){let T=v+c*.045;e.beginPath(),e.moveTo(T+.018,f-.035),e.lineTo(T-.018,f+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=be;let b=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),E=1-b;for(let m of [-1,1]){let g=m*tt+h,y=ot+p,v=st*o.eyeSize;if(E>.01){e.globalAlpha=E;let f=Math.max(.06,o.eyeOpen*t),c=fe*o.eyeSize*f;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-fe*o.eyeSize+2*fe*o.eyeSize*o.lidCover;e.translate(g,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-g,-W);}r>.5&&ze(e,g,y,v*2.1,c*1.9,s,.35*E);let T=e.createRadialGradient(g,y-c*.2,0,g,y,Math.max(v,c));if(T.addColorStop(0,d),T.addColorStop(1,s),e.fillStyle=T,e.beginPath(),e.ellipse(g,y,v,c,0,0,Math.PI*2),e.fill(),e.fillStyle=s,f>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ce=a==="#fff"?.3:.34;e.globalAlpha=E*Math.min(1,(f-.35)*3),e.fillStyle=a,e.beginPath(),e.arc(g-v*.1+R*v*.5,y-c*.16+W*c*.42,v*ce,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&b>.01&&ze(e,g,y,v*2,v*1.8,s,.3*b),e.fillStyle=s,o.happyEye>.01){let f=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(g,y+.045,v*1.15*f,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let f=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(g-v*1.15*f,y+.01),e.quadraticCurveTo(g,y+.07*f,g+v*1.15*f,y+.01),e.stroke();}if(o.xEye>.01){let f=v*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(g-f,y-f),e.lineTo(g+f,y+f),e.moveTo(g+f,y-f),e.lineTo(g-f,y+f),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ye(e,g,y,v*1.35*(.5+.5*o.heartEye)),e.fill());}let S=h+o.mouthSkew*.07,k=it+p,C=Math.min(1,Math.max(0,o.mouthRound)),A=o.mouthW,B=e.createLinearGradient(S-A-.02,k,S+A+.02,k);B.addColorStop(0,u),B.addColorStop(.5,d),B.addColorStop(1,u);let q=(m,g,y,v)=>{e.fillStyle=l,m(),e.fill(),g>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(g-.02)*12),e.beginPath(),e.ellipse(S,y,v,g*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let f=e.lineWidth;e.lineWidth=f*.6,m(),e.stroke(),e.lineWidth=f;};if(C<.99){e.globalAlpha=1-C;let m=18,g=c=>o.mouthCurve*.075*(1-c*c),y=(c,T)=>{let R=c/m*2-1,W=S+R*A,ce=k+g(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+T*(1-R*R);return [W,ce]},v=Math.max(0,o.mouthOpen),f=()=>{for(let c=0;c<=m;c++){let[T,R]=y(c,-v*.15);c?e.lineTo(T,R):e.moveTo(T,R);}};v>.003?q(()=>{e.beginPath(),f();for(let T=m;T>=0;T--){let[R,W]=y(T,v);e.lineTo(R,W);}e.closePath();},v,y(m/2,v)[1],A*.55):(e.strokeStyle=B,e.beginPath(),f(),e.stroke());}if(C>.01){e.globalAlpha=C;let m=Math.max(.02,o.mouthOpen*.55+.01),g=()=>{e.beginPath(),e.ellipse(S,k+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=be*.8,q(g,o.mouthOpen,k+.01+m*.8,m*.6),e.lineWidth=be;}e.globalAlpha=1;}function ze(e,o,t,s,i,r,d){let a=e.createRadialGradient(o,t,0,o,t,1);a.addColorStop(0,r),a.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=d,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=a,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ye(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var J=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:w,cos:nt,abs:rt,max:se,exp:at,PI:ie}=Math;function ne(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=w(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*w(o*1.1),s.y=.02*w(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*w(i),s.rz=.12*nt(i),s.ry=.22*w(i),s.y=.02*w(i*2),s.lookX=.55*w(i);break}case "listening":{let i=se(0,w(o*3.2));s.rx=.09*i*i;let r=rt(w(o*7.1)*.5+w(o*11.3)*.3+w(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=se(0,w(o*9)*.6+w(o*14.3)*.4)*(.65+.35*w(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*w(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*w(ie*i);let r=se(0,1-i/.14)+se(0,(i-.9)/.1);s.st=.08*w(ie*i)-.14*r,s.rz=.05*w(o*1.35*ie);break}case "surprised":{if(t<.45){let i=w(ie*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*w(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=w(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*w(i*42)*r,s.rz=.05*w(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=w(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*w(o*1.6),s.rz=.05*w(o*.8),s.st=.015*w(o*1.6+1);break}}return s}function ve(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Le={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,P=null,$=0,V=0,re=0,we,Ge=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Ge)return;Ge=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return P||(P=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),P.setPixelRatio(1),P.setClearColor(0,0),z.lowPower=ct(P.getContext()),P.setScissorTest(true),P.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),P?.dispose(),P=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),P.setSize($,V,false)),P}var Ye=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function We(e,o){if(H==="ready")return true;if(H==="idle"&&P){H="compiling";let t=P;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===P&&(H="ready"),_();});}return false}function De(e){lt(),clearTimeout(we),j.add(e),_();}function _e(e){j.delete(e),j.size===0&&(clearTimeout(we),we=setTimeout(()=>{j.size||!P||(P.dispose(),P.forceContextLoss(),P=null,$=V=0,H="idle");},1500));}function _(){if(!re){for(let e of j)if(e.active){re=requestAnimationFrame(Ne);return}}}var dt=10,Ie=new WeakMap,Me=0;function Ne(e){re=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(Me+i)%s],d=Ie.get(r)??e-16;if(Ie.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-d)/1e3)),e),performance.now()-t>dt){i++;break}}Me=(Me+i)%s,re=requestAnimationFrame(Ne);}var Z={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ve(e){let o=new three.SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],d=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let h=t.getX(n),p=t.getY(n),b=t.getZ(n),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(b*1e4)}`,S=i.get(E);if(S===void 0){S=r.length/3,i.set(E,S);let k=Math.hypot(h,p,b)||1;r.push(h/k,p/k,b/k);}d[n]=S;}let a=[];for(let n=0;n<s.count;n+=3){let h=d[s.getX(n)],p=d[s.getX(n+1)],b=d[s.getX(n+2)];h!==p&&p!==b&&h!==b&&a.push(h,p,b);}o.dispose();let l=new three.BufferGeometry,u=new Float32Array(r);return l.setAttribute("position",new three.BufferAttribute(new Float32Array(u),3)),l.setIndex(a),{geometry:l,dirs:u}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function ke(e,o,t,s,i,r,d){let a=o,l=t,u=s;switch(e){case "pebble":{a=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,u=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);a=o*n,l=t*n,u=s*n;break}case "drop":{let n=Math.max(t,0),h=1-.8*Math.pow(n,1.6);a=o*h*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,u=s*h*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);a=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,u=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);a=o*h*.84,l=t*h*.82,u=s*h*.84;break}case "ghost":{if(t>=0)a=o,l=t,u=s;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,s)||1,b=o/p,E=s/p,S=Math.atan2(s,o),k=.085*Math.sin(5*S+2.2*i),C=1.04,A=.13,B=-1.05+k,q;if(h<.45){let m=h/.45;q=1+.04*m,l=-m*(1.05-A)+k*ft(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);q=C-A+A*Math.cos(m),l=B+A-A*Math.sin(m);}else {let m=(h-.75)/.25;q=(C-A)*(1-m),l=B+.22*m*m*(3-2*m);}a=b*q,u=E*q;}a*=.9,l=(l+.1)*.9,u*=.9;break}}r[d]=a,r[d+1]=l,r[d+2]=u;}var I=null,ae=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,ae=I.createGain(),ae.gain.value=.55,ae.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:d=.1,wobble:a=0}){let l=e.currentTime+s,u=e.createOscillator(),n=e.createGain();if(u.type=r,u.frequency.setValueAtTime(o,l),t&&u.frequency.exponentialRampToValueAtTime(t,l+i),a){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=a,h.connect(p).connect(u.frequency),h.start(l),h.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(d,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),u.connect(n).connect(ae),u.start(l),u.stop(l+i+.02);}var Se={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Xe=false;function gt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function he(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,Se[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](t),Y=null;});}}var Ee=-0.85,je=new Map;function Tt(e){let o=je.get(e);if(!o){let{geometry:t,dirs:s}=Ve(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),je.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),Te=e=>1-Math.pow(1-e,3),le=class{constructor(){this.scene=new three.Scene;this.camera=new three.PerspectiveCamera(30,1,.1,50);this.group=new three.Group;this.v=new three.Vector3;this.geometry=new three.BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=J();this.poseIdle=J();this.moodTime=0;this.poseA=J();this.poseB=J();this.face=ge(oe.idle);this.target=ge(oe.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=ee(this.body);this.faceCT=this.faceC;this.cheek=te(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new three.CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=three.SRGBColorSpace,this.faceTex.minFilter=three.LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new three.Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ee,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new three.HemisphereLight(16774120,7234140,1.7));let o=new three.DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new three.DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new three.DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new three.MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,three.SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
|
|
1
|
+
'use strict';var three=require('three');var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Bt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Ot={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},de=e=>Ue.includes(e),ue=e=>Je.includes(e);var O=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return Fe(t[1]);if(O||(O=document.createElement("canvas").getContext("2d")),!O)return o;O.fillStyle="#010203",O.fillStyle=e;let s=String(O.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return Fe(s.slice(1));O.canvas.width=O.canvas.height=1,O.clearRect(0,0,1,1),O.fillRect(0,0,1,1);let i=O.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Fe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var L=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],pe=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),me=e=>.2126*pe(e[0])+.7152*pe(e[1])+.0722*pe(e[2]),Ze=[.07,.06,.08],Qe=[209/255,190/255,173/255],et=[1,.42,.48],ee=e=>me(e)>.06?x(Ze,e,.06):Qe,te=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],G={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},oe={idle:{...G},thinking:{...G,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...G,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...G,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...G,mouthW:.09,mouthCurve:.25},happy:{...G,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...G,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...G,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...G,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...G,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...G,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Be={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ge=e=>({...e}),tt=.31,ot=-0.04,st=.098,fe=.128,it=.2,be=.05;function Oe(e,o,t,s,i,r,d,a,l,u){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let h=o.lookX*.075,p=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h,f=.16+p,c=e.createRadialGradient(v,f,0,v,f,.16);c.addColorStop(0,i),c.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=c,e.beginPath(),e.ellipse(v,f,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let g=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(g>.01){e.save(),e.globalAlpha=g*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let y of [-1,1]){let v=y*.52+h,f=.16+p;for(let c=-1;c<=1;c++){let T=v+c*.045;e.beginPath(),e.moveTo(T+.018,f-.035),e.lineTo(T-.018,f+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=be;let b=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),E=1-b;for(let m of [-1,1]){let g=m*tt+h,y=ot+p,v=st*o.eyeSize;if(E>.01){e.globalAlpha=E;let f=Math.max(.06,o.eyeOpen*t),c=fe*o.eyeSize*f;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-fe*o.eyeSize+2*fe*o.eyeSize*o.lidCover;e.translate(g,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-g,-W);}r>.5&&ze(e,g,y,v*2.1,c*1.9,s,.35*E);let T=e.createRadialGradient(g,y-c*.2,0,g,y,Math.max(v,c));if(T.addColorStop(0,d),T.addColorStop(1,s),e.fillStyle=T,e.beginPath(),e.ellipse(g,y,v,c,0,0,Math.PI*2),e.fill(),e.fillStyle=s,f>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ce=a==="#fff"?.3:.34;e.globalAlpha=E*Math.min(1,(f-.35)*3),e.fillStyle=a,e.beginPath(),e.arc(g-v*.1+R*v*.5,y-c*.16+W*c*.42,v*ce,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&b>.01&&ze(e,g,y,v*2,v*1.8,s,.3*b),e.fillStyle=s,o.happyEye>.01){let f=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(g,y+.045,v*1.15*f,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let f=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(g-v*1.15*f,y+.01),e.quadraticCurveTo(g,y+.07*f,g+v*1.15*f,y+.01),e.stroke();}if(o.xEye>.01){let f=v*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(g-f,y-f),e.lineTo(g+f,y+f),e.moveTo(g+f,y-f),e.lineTo(g-f,y+f),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ye(e,g,y,v*1.35*(.5+.5*o.heartEye)),e.fill());}let S=h+o.mouthSkew*.07,k=it+p,C=Math.min(1,Math.max(0,o.mouthRound)),A=o.mouthW,B=e.createLinearGradient(S-A-.02,k,S+A+.02,k);B.addColorStop(0,u),B.addColorStop(.5,d),B.addColorStop(1,u);let q=(m,g,y,v)=>{e.fillStyle=l,m(),e.fill(),g>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(g-.02)*12),e.beginPath(),e.ellipse(S,y,v,g*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let f=e.lineWidth;e.lineWidth=f*.6,m(),e.stroke(),e.lineWidth=f;};if(C<.99){e.globalAlpha=1-C;let m=18,g=c=>o.mouthCurve*.075*(1-c*c),y=(c,T)=>{let R=c/m*2-1,W=S+R*A,ce=k+g(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+T*(1-R*R);return [W,ce]},v=Math.max(0,o.mouthOpen),f=()=>{for(let c=0;c<=m;c++){let[T,R]=y(c,-v*.15);c?e.lineTo(T,R):e.moveTo(T,R);}};v>.003?q(()=>{e.beginPath(),f();for(let T=m;T>=0;T--){let[R,W]=y(T,v);e.lineTo(R,W);}e.closePath();},v,y(m/2,v)[1],A*.55):(e.strokeStyle=B,e.beginPath(),f(),e.stroke());}if(C>.01){e.globalAlpha=C;let m=Math.max(.02,o.mouthOpen*.55+.01),g=()=>{e.beginPath(),e.ellipse(S,k+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=be*.8,q(g,o.mouthOpen,k+.01+m*.8,m*.6),e.lineWidth=be;}e.globalAlpha=1;}function ze(e,o,t,s,i,r,d){let a=e.createRadialGradient(o,t,0,o,t,1);a.addColorStop(0,r),a.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=d,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=a,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ye(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var J=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:w,cos:nt,abs:rt,max:se,exp:at,PI:ie}=Math;function ne(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=w(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*w(o*1.1),s.y=.02*w(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*w(i),s.rz=.12*nt(i),s.ry=.22*w(i),s.y=.02*w(i*2),s.lookX=.55*w(i);break}case "listening":{let i=se(0,w(o*3.2));s.rx=.09*i*i;let r=rt(w(o*7.1)*.5+w(o*11.3)*.3+w(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=se(0,w(o*9)*.6+w(o*14.3)*.4)*(.65+.35*w(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*w(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*w(ie*i);let r=se(0,1-i/.14)+se(0,(i-.9)/.1);s.st=.08*w(ie*i)-.14*r,s.rz=.05*w(o*1.35*ie);break}case "surprised":{if(t<.45){let i=w(ie*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*w(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=w(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*w(i*42)*r,s.rz=.05*w(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=w(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*w(o*1.6),s.rz=.05*w(o*.8),s.st=.015*w(o*1.6+1);break}}return s}function ve(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Le={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,P=null,$=0,V=0,re=0,we,Ge=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Ge)return;Ge=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return P||(P=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),P.setPixelRatio(1),P.setClearColor(0,0),z.lowPower=ct(P.getContext()),P.setScissorTest(true),P.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),P?.dispose(),P=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),P.setSize($,V,false)),P}var Ye=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function We(e,o){if(H==="ready")return true;if(H==="idle"&&P){H="compiling";let t=P;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===P&&(H="ready"),_();});}return false}function De(e){lt(),clearTimeout(we),j.add(e),_();}function _e(e){j.delete(e),j.size===0&&(clearTimeout(we),we=setTimeout(()=>{j.size||!P||(P.dispose(),P.forceContextLoss(),P=null,$=V=0,H="idle");},1500));}function _(){if(!re){for(let e of j)if(e.active){re=requestAnimationFrame(Ne);return}}}var dt=10,Ie=new WeakMap,Me=0;function Ne(e){re=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(Me+i)%s],d=Ie.get(r)??e-16;if(Ie.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-d)/1e3)),e),performance.now()-t>dt){i++;break}}Me=(Me+i)%s,re=requestAnimationFrame(Ne);}var Z={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ve(e){let o=new three.SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],d=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let h=t.getX(n),p=t.getY(n),b=t.getZ(n),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(b*1e4)}`,S=i.get(E);if(S===void 0){S=r.length/3,i.set(E,S);let k=Math.hypot(h,p,b)||1;r.push(h/k,p/k,b/k);}d[n]=S;}let a=[];for(let n=0;n<s.count;n+=3){let h=d[s.getX(n)],p=d[s.getX(n+1)],b=d[s.getX(n+2)];h!==p&&p!==b&&h!==b&&a.push(h,p,b);}o.dispose();let l=new three.BufferGeometry,u=new Float32Array(r);return l.setAttribute("position",new three.BufferAttribute(new Float32Array(u),3)),l.setIndex(a),{geometry:l,dirs:u}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function ke(e,o,t,s,i,r,d){let a=o,l=t,u=s;switch(e){case "pebble":{a=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,u=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);a=o*n,l=t*n,u=s*n;break}case "drop":{let n=Math.max(t,0),h=1-.8*Math.pow(n,1.6);a=o*h*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,u=s*h*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);a=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,u=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);a=o*h*.84,l=t*h*.82,u=s*h*.84;break}case "ghost":{if(t>=0)a=o,l=t,u=s;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,s)||1,b=o/p,E=s/p,S=Math.atan2(s,o),k=.085*Math.sin(5*S+2.2*i),C=1.04,A=.13,B=-1.05+k,q;if(h<.45){let m=h/.45;q=1+.04*m,l=-m*(1.05-A)+k*ft(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);q=C-A+A*Math.cos(m),l=B+A-A*Math.sin(m);}else {let m=(h-.75)/.25;q=(C-A)*(1-m),l=B+.22*m*m*(3-2*m);}a=b*q,u=E*q;}a*=.9,l=(l+.1)*.9,u*=.9;break}}r[d]=a,r[d+1]=l,r[d+2]=u;}var I=null,ae=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,ae=I.createGain(),ae.gain.value=.55,ae.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:d=.1,wobble:a=0}){let l=e.currentTime+s,u=e.createOscillator(),n=e.createGain();if(u.type=r,u.frequency.setValueAtTime(o,l),t&&u.frequency.exponentialRampToValueAtTime(t,l+i),a){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=a,h.connect(p).connect(u.frequency),h.start(l),h.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(d,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),u.connect(n).connect(ae),u.start(l),u.stop(l+i+.02);}var Se={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Xe=false;function gt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function he(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,Se[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](t),Y=null;});}}var Ee=-0.85,je=new Map;function Tt(e){let o=je.get(e);if(!o){let{geometry:t,dirs:s}=Ve(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),je.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),Te=e=>1-Math.pow(1-e,3),le=class{constructor(){this.scene=new three.Scene;this.camera=new three.PerspectiveCamera(30,1,.1,50);this.group=new three.Group;this.v=new three.Vector3;this.geometry=new three.BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=J();this.poseIdle=J();this.moodTime=0;this.poseA=J();this.poseB=J();this.face=ge(oe.idle);this.target=ge(oe.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=ee(this.body);this.faceCT=this.faceC;this.cheek=te(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new three.CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=three.SRGBColorSpace,this.faceTex.minFilter=three.LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new three.Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ee,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new three.HemisphereLight(16774120,7234140,1.7));let o=new three.DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new three.DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new three.DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new three.MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,three.SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
|
|
2
2
|
varying vec3 vFaceP;
|
|
3
3
|
varying vec3 vFaceN;`).replace("#include <begin_vertex>",`#include <begin_vertex>
|
|
4
4
|
vFaceP = position;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Scene,PerspectiveCamera,Group,Vector3,BufferGeometry,CanvasTexture,SRGBColorSpace,LinearMipmapLinearFilter,Mesh,HemisphereLight,DirectionalLight,MeshStandardMaterial,BufferAttribute,WebGLRenderer,SphereGeometry}from'three';var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Bt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Ot={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},de=e=>Ue.includes(e),ue=e=>Je.includes(e);var O=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return Fe(t[1]);if(O||(O=document.createElement("canvas").getContext("2d")),!O)return o;O.fillStyle="#010203",O.fillStyle=e;let s=String(O.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return Fe(s.slice(1));O.canvas.width=O.canvas.height=1,O.clearRect(0,0,1,1),O.fillRect(0,0,1,1);let i=O.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Fe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var L=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],pe=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),me=e=>.2126*pe(e[0])+.7152*pe(e[1])+.0722*pe(e[2]),Ze=[.07,.06,.08],Qe=[1,.97,.93],et=[1,.42,.48],ee=e=>me(e)>.06?x(Ze,e,.06):Qe,te=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],G={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},oe={idle:{...G},thinking:{...G,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...G,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...G,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...G,mouthW:.09,mouthCurve:.25},happy:{...G,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...G,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...G,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...G,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...G,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...G,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Be={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ge=e=>({...e}),tt=.31,ot=-0.04,st=.098,fe=.128,it=.2,be=.05;function Oe(e,o,t,s,i,r,d,a,l,u){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let h=o.lookX*.075,p=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h,f=.16+p,c=e.createRadialGradient(v,f,0,v,f,.16);c.addColorStop(0,i),c.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=c,e.beginPath(),e.ellipse(v,f,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let g=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(g>.01){e.save(),e.globalAlpha=g*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let y of [-1,1]){let v=y*.52+h,f=.16+p;for(let c=-1;c<=1;c++){let T=v+c*.045;e.beginPath(),e.moveTo(T+.018,f-.035),e.lineTo(T-.018,f+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=be;let b=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),E=1-b;for(let m of [-1,1]){let g=m*tt+h,y=ot+p,v=st*o.eyeSize;if(E>.01){e.globalAlpha=E;let f=Math.max(.06,o.eyeOpen*t),c=fe*o.eyeSize*f;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-fe*o.eyeSize+2*fe*o.eyeSize*o.lidCover;e.translate(g,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-g,-W);}r>.5&&ze(e,g,y,v*2.1,c*1.9,s,.35*E);let T=e.createRadialGradient(g,y-c*.2,0,g,y,Math.max(v,c));if(T.addColorStop(0,d),T.addColorStop(1,s),e.fillStyle=T,e.beginPath(),e.ellipse(g,y,v,c,0,0,Math.PI*2),e.fill(),e.fillStyle=s,f>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ce=a==="#fff"?.3:.34;e.globalAlpha=E*Math.min(1,(f-.35)*3),e.fillStyle=a,e.beginPath(),e.arc(g-v*.1+R*v*.5,y-c*.16+W*c*.42,v*ce,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&b>.01&&ze(e,g,y,v*2,v*1.8,s,.3*b),e.fillStyle=s,o.happyEye>.01){let f=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(g,y+.045,v*1.15*f,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let f=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(g-v*1.15*f,y+.01),e.quadraticCurveTo(g,y+.07*f,g+v*1.15*f,y+.01),e.stroke();}if(o.xEye>.01){let f=v*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(g-f,y-f),e.lineTo(g+f,y+f),e.moveTo(g+f,y-f),e.lineTo(g-f,y+f),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ye(e,g,y,v*1.35*(.5+.5*o.heartEye)),e.fill());}let S=h+o.mouthSkew*.07,k=it+p,C=Math.min(1,Math.max(0,o.mouthRound)),A=o.mouthW,B=e.createLinearGradient(S-A-.02,k,S+A+.02,k);B.addColorStop(0,u),B.addColorStop(.5,d),B.addColorStop(1,u);let q=(m,g,y,v)=>{e.fillStyle=l,m(),e.fill(),g>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(g-.02)*12),e.beginPath(),e.ellipse(S,y,v,g*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let f=e.lineWidth;e.lineWidth=f*.6,m(),e.stroke(),e.lineWidth=f;};if(C<.99){e.globalAlpha=1-C;let m=18,g=c=>o.mouthCurve*.075*(1-c*c),y=(c,T)=>{let R=c/m*2-1,W=S+R*A,ce=k+g(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+T*(1-R*R);return [W,ce]},v=Math.max(0,o.mouthOpen),f=()=>{for(let c=0;c<=m;c++){let[T,R]=y(c,-v*.15);c?e.lineTo(T,R):e.moveTo(T,R);}};v>.003?q(()=>{e.beginPath(),f();for(let T=m;T>=0;T--){let[R,W]=y(T,v);e.lineTo(R,W);}e.closePath();},v,y(m/2,v)[1],A*.55):(e.strokeStyle=B,e.beginPath(),f(),e.stroke());}if(C>.01){e.globalAlpha=C;let m=Math.max(.02,o.mouthOpen*.55+.01),g=()=>{e.beginPath(),e.ellipse(S,k+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=be*.8,q(g,o.mouthOpen,k+.01+m*.8,m*.6),e.lineWidth=be;}e.globalAlpha=1;}function ze(e,o,t,s,i,r,d){let a=e.createRadialGradient(o,t,0,o,t,1);a.addColorStop(0,r),a.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=d,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=a,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ye(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var J=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:w,cos:nt,abs:rt,max:se,exp:at,PI:ie}=Math;function ne(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=w(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*w(o*1.1),s.y=.02*w(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*w(i),s.rz=.12*nt(i),s.ry=.22*w(i),s.y=.02*w(i*2),s.lookX=.55*w(i);break}case "listening":{let i=se(0,w(o*3.2));s.rx=.09*i*i;let r=rt(w(o*7.1)*.5+w(o*11.3)*.3+w(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=se(0,w(o*9)*.6+w(o*14.3)*.4)*(.65+.35*w(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*w(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*w(ie*i);let r=se(0,1-i/.14)+se(0,(i-.9)/.1);s.st=.08*w(ie*i)-.14*r,s.rz=.05*w(o*1.35*ie);break}case "surprised":{if(t<.45){let i=w(ie*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*w(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=w(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*w(i*42)*r,s.rz=.05*w(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=w(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*w(o*1.6),s.rz=.05*w(o*.8),s.st=.015*w(o*1.6+1);break}}return s}function ve(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Le={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,P=null,$=0,V=0,re=0,we,Ge=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Ge)return;Ge=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return P||(P=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),P.setPixelRatio(1),P.setClearColor(0,0),z.lowPower=ct(P.getContext()),P.setScissorTest(true),P.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),P?.dispose(),P=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),P.setSize($,V,false)),P}var Ye=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function We(e,o){if(H==="ready")return true;if(H==="idle"&&P){H="compiling";let t=P;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===P&&(H="ready"),_();});}return false}function De(e){lt(),clearTimeout(we),j.add(e),_();}function _e(e){j.delete(e),j.size===0&&(clearTimeout(we),we=setTimeout(()=>{j.size||!P||(P.dispose(),P.forceContextLoss(),P=null,$=V=0,H="idle");},1500));}function _(){if(!re){for(let e of j)if(e.active){re=requestAnimationFrame(Ne);return}}}var dt=10,Ie=new WeakMap,Me=0;function Ne(e){re=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(Me+i)%s],d=Ie.get(r)??e-16;if(Ie.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-d)/1e3)),e),performance.now()-t>dt){i++;break}}Me=(Me+i)%s,re=requestAnimationFrame(Ne);}var Z={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ve(e){let o=new SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],d=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let h=t.getX(n),p=t.getY(n),b=t.getZ(n),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(b*1e4)}`,S=i.get(E);if(S===void 0){S=r.length/3,i.set(E,S);let k=Math.hypot(h,p,b)||1;r.push(h/k,p/k,b/k);}d[n]=S;}let a=[];for(let n=0;n<s.count;n+=3){let h=d[s.getX(n)],p=d[s.getX(n+1)],b=d[s.getX(n+2)];h!==p&&p!==b&&h!==b&&a.push(h,p,b);}o.dispose();let l=new BufferGeometry,u=new Float32Array(r);return l.setAttribute("position",new BufferAttribute(new Float32Array(u),3)),l.setIndex(a),{geometry:l,dirs:u}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function ke(e,o,t,s,i,r,d){let a=o,l=t,u=s;switch(e){case "pebble":{a=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,u=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);a=o*n,l=t*n,u=s*n;break}case "drop":{let n=Math.max(t,0),h=1-.8*Math.pow(n,1.6);a=o*h*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,u=s*h*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);a=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,u=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);a=o*h*.84,l=t*h*.82,u=s*h*.84;break}case "ghost":{if(t>=0)a=o,l=t,u=s;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,s)||1,b=o/p,E=s/p,S=Math.atan2(s,o),k=.085*Math.sin(5*S+2.2*i),C=1.04,A=.13,B=-1.05+k,q;if(h<.45){let m=h/.45;q=1+.04*m,l=-m*(1.05-A)+k*ft(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);q=C-A+A*Math.cos(m),l=B+A-A*Math.sin(m);}else {let m=(h-.75)/.25;q=(C-A)*(1-m),l=B+.22*m*m*(3-2*m);}a=b*q,u=E*q;}a*=.9,l=(l+.1)*.9,u*=.9;break}}r[d]=a,r[d+1]=l,r[d+2]=u;}var I=null,ae=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,ae=I.createGain(),ae.gain.value=.55,ae.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:d=.1,wobble:a=0}){let l=e.currentTime+s,u=e.createOscillator(),n=e.createGain();if(u.type=r,u.frequency.setValueAtTime(o,l),t&&u.frequency.exponentialRampToValueAtTime(t,l+i),a){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=a,h.connect(p).connect(u.frequency),h.start(l),h.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(d,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),u.connect(n).connect(ae),u.start(l),u.stop(l+i+.02);}var Se={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Xe=false;function gt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function he(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,Se[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](t),Y=null;});}}var Ee=-0.85,je=new Map;function Tt(e){let o=je.get(e);if(!o){let{geometry:t,dirs:s}=Ve(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),je.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),Te=e=>1-Math.pow(1-e,3),le=class{constructor(){this.scene=new Scene;this.camera=new PerspectiveCamera(30,1,.1,50);this.group=new Group;this.v=new Vector3;this.geometry=new BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=J();this.poseIdle=J();this.moodTime=0;this.poseA=J();this.poseB=J();this.face=ge(oe.idle);this.target=ge(oe.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=ee(this.body);this.faceCT=this.faceC;this.cheek=te(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=SRGBColorSpace,this.faceTex.minFilter=LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ee,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new HemisphereLight(16774120,7234140,1.7));let o=new DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
|
|
1
|
+
import {Scene,PerspectiveCamera,Group,Vector3,BufferGeometry,CanvasTexture,SRGBColorSpace,LinearMipmapLinearFilter,Mesh,HemisphereLight,DirectionalLight,MeshStandardMaterial,BufferAttribute,WebGLRenderer,SphereGeometry}from'three';var Ue=["ghost","pebble","blob","drop","bean","cube"],Je=["idle","thinking","loading","listening","speaking","happy","surprised","sad","error","sleeping","love"],M={shape:"ghost",mood:"idle",color:"#6c5ce7",size:160,speed:1,followCursor:true,interactive:true,shadow:true,sound:false,settle:0},Bt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},Ot={idle:{label:"Idle",description:"Breathing softly. Blinks now and then."},thinking:{label:"Thinking",description:"Eyes drift up. Little thoughts pop."},loading:{label:"Loading",description:"Half-lidded, wobbling patiently."},listening:{label:"Listening",description:"All eyes. Nods along to every word."},speaking:{label:"Speaking",description:"Chatty mouth, bouncing to the rhythm."},happy:{label:"Happy",description:"Bounces with joy. Cheeks glow."},surprised:{label:"Surprised",description:"Wide eyes, tiny o. Did not see that coming."},sad:{label:"Sad",description:"Slumps a little. Needs a minute."},error:{label:"Error",description:"Something broke. Shakes it off."},sleeping:{label:"Sleeping",description:"Deep, slow breaths. Do not disturb."},love:{label:"Love",description:"Heart eyes, floating on air."}},Lt={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},de=e=>Ue.includes(e),ue=e=>Je.includes(e);var O=null;function X(e,o){if(!e)return o;let t=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(e.trim());if(t)return Fe(t[1]);if(O||(O=document.createElement("canvas").getContext("2d")),!O)return o;O.fillStyle="#010203",O.fillStyle=e;let s=String(O.fillStyle);if(s==="#010203"&&e.trim()!=="#010203")return o;if(s.startsWith("#"))return Fe(s.slice(1));O.canvas.width=O.canvas.height=1,O.clearRect(0,0,1,1),O.fillRect(0,0,1,1);let i=O.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Fe(e){e.length===3&&(e=e[0]+e[0]+e[1]+e[1]+e[2]+e[2]);let o=parseInt(e,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var L=e=>`rgb(${Math.round(e[0]*255)},${Math.round(e[1]*255)},${Math.round(e[2]*255)})`,x=(e,o,t)=>[e[0]+(o[0]-e[0])*t,e[1]+(o[1]-e[1])*t,e[2]+(o[2]-e[2])*t],pe=e=>e<=.04045?e/12.92:Math.pow((e+.055)/1.055,2.4),me=e=>.2126*pe(e[0])+.7152*pe(e[1])+.0722*pe(e[2]),Ze=[.07,.06,.08],Qe=[209/255,190/255,173/255],et=[1,.42,.48],ee=e=>me(e)>.06?x(Ze,e,.06):Qe,te=e=>x(e,et,.78);var U=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],G={eyeOpen:1,eyeSize:1,lookX:0,lookY:0,lidCover:0,lidTilt:0,happyEye:0,closedEye:0,xEye:0,heartEye:0,mouthW:.1,mouthCurve:.35,mouthOpen:0,mouthRound:0,mouthSkew:0,mouthWave:0,blush:.45},oe={idle:{...G},thinking:{...G,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...G,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...G,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...G,mouthW:.09,mouthCurve:.25},happy:{...G,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...G,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...G,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...G,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...G,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...G,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Be={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},ge=e=>({...e}),tt=.31,ot=-0.04,st=.098,fe=.128,it=.2,be=.05;function Oe(e,o,t,s,i,r,d,a,l,u){let n=e.canvas.width;e.setTransform(1,0,0,1,0,0),e.clearRect(0,0,n,n),e.setTransform(n/2,0,0,n/2,n/2,n/2),e.lineCap="round",e.lineJoin="round";let h=o.lookX*.075,p=o.lookY*.06;if(o.blush>.01){let m=n*.018;e.save(),"filter"in e&&(e.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h,f=.16+p,c=e.createRadialGradient(v,f,0,v,f,.16);c.addColorStop(0,i),c.addColorStop(1,"rgba(0,0,0,0)"),e.globalAlpha=Math.min(1,o.blush)*.62,e.fillStyle=c,e.beginPath(),e.ellipse(v,f,.17,.12,0,0,Math.PI*2),e.fill();}e.restore();let g=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(g>.01){e.save(),e.globalAlpha=g*.55,e.strokeStyle=i,e.lineWidth=.016,e.lineCap="round";for(let y of [-1,1]){let v=y*.52+h,f=.16+p;for(let c=-1;c<=1;c++){let T=v+c*.045;e.beginPath(),e.moveTo(T+.018,f-.035),e.lineTo(T-.018,f+.035),e.stroke();}}e.restore();}}e.fillStyle=s,e.strokeStyle=s,e.lineWidth=be;let b=Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye),E=1-b;for(let m of [-1,1]){let g=m*tt+h,y=ot+p,v=st*o.eyeSize;if(E>.01){e.globalAlpha=E;let f=Math.max(.06,o.eyeOpen*t),c=fe*o.eyeSize*f;if(e.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-fe*o.eyeSize+2*fe*o.eyeSize*o.lidCover;e.translate(g,W),e.rotate(m*o.lidTilt*.6),e.beginPath(),e.rect(-1,0,2,2),e.clip(),e.rotate(-m*o.lidTilt*.6),e.translate(-g,-W);}r>.5&&ze(e,g,y,v*2.1,c*1.9,s,.35*E);let T=e.createRadialGradient(g,y-c*.2,0,g,y,Math.max(v,c));if(T.addColorStop(0,d),T.addColorStop(1,s),e.fillStyle=T,e.beginPath(),e.ellipse(g,y,v,c,0,0,Math.PI*2),e.fill(),e.fillStyle=s,f>.35){let R=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ce=a==="#fff"?.3:.34;e.globalAlpha=E*Math.min(1,(f-.35)*3),e.fillStyle=a,e.beginPath(),e.arc(g-v*.1+R*v*.5,y-c*.16+W*c*.42,v*ce,0,Math.PI*2),e.fill(),e.fillStyle=s;}e.restore();}if(r>.5&&b>.01&&ze(e,g,y,v*2,v*1.8,s,.3*b),e.fillStyle=s,o.happyEye>.01){let f=.6+.4*o.happyEye;e.globalAlpha=Math.min(1,o.happyEye),e.beginPath(),e.arc(g,y+.045,v*1.15*f,Math.PI*1.12,Math.PI*1.88),e.stroke();}if(o.closedEye>.01){let f=.6+.4*o.closedEye;e.globalAlpha=Math.min(1,o.closedEye),e.beginPath(),e.moveTo(g-v*1.15*f,y+.01),e.quadraticCurveTo(g,y+.07*f,g+v*1.15*f,y+.01),e.stroke();}if(o.xEye>.01){let f=v*.95*(.6+.4*o.xEye);e.globalAlpha=Math.min(1,o.xEye),e.beginPath(),e.moveTo(g-f,y-f),e.lineTo(g+f,y+f),e.moveTo(g+f,y-f),e.lineTo(g-f,y+f),e.stroke();}o.heartEye>.01&&(e.globalAlpha=Math.min(1,o.heartEye),ye(e,g,y,v*1.35*(.5+.5*o.heartEye)),e.fill());}let S=h+o.mouthSkew*.07,k=it+p,C=Math.min(1,Math.max(0,o.mouthRound)),A=o.mouthW,B=e.createLinearGradient(S-A-.02,k,S+A+.02,k);B.addColorStop(0,u),B.addColorStop(.5,d),B.addColorStop(1,u);let q=(m,g,y,v)=>{e.fillStyle=l,m(),e.fill(),g>.02&&(e.save(),m(),e.clip(),e.fillStyle=i,e.globalAlpha*=Math.min(1,(g-.02)*12),e.beginPath(),e.ellipse(S,y,v,g*.55,0,0,Math.PI*2),e.fill(),e.restore()),e.strokeStyle=B;let f=e.lineWidth;e.lineWidth=f*.6,m(),e.stroke(),e.lineWidth=f;};if(C<.99){e.globalAlpha=1-C;let m=18,g=c=>o.mouthCurve*.075*(1-c*c),y=(c,T)=>{let R=c/m*2-1,W=S+R*A,ce=k+g(R)+o.mouthSkew*R*.03+o.mouthWave*.018*Math.sin((R+1)*Math.PI*2)+T*(1-R*R);return [W,ce]},v=Math.max(0,o.mouthOpen),f=()=>{for(let c=0;c<=m;c++){let[T,R]=y(c,-v*.15);c?e.lineTo(T,R):e.moveTo(T,R);}};v>.003?q(()=>{e.beginPath(),f();for(let T=m;T>=0;T--){let[R,W]=y(T,v);e.lineTo(R,W);}e.closePath();},v,y(m/2,v)[1],A*.55):(e.strokeStyle=B,e.beginPath(),f(),e.stroke());}if(C>.01){e.globalAlpha=C;let m=Math.max(.02,o.mouthOpen*.55+.01),g=()=>{e.beginPath(),e.ellipse(S,k+.01,m*.85,m,0,0,Math.PI*2);};e.lineWidth=be*.8,q(g,o.mouthOpen,k+.01+m*.8,m*.6),e.lineWidth=be;}e.globalAlpha=1;}function ze(e,o,t,s,i,r,d){let a=e.createRadialGradient(o,t,0,o,t,1);a.addColorStop(0,r),a.addColorStop(1,"rgba(0,0,0,0)"),e.save(),e.globalAlpha=d,e.translate(o,t),e.scale(s,i),e.translate(-o,-t),e.fillStyle=a,e.beginPath(),e.arc(o,t,1,0,Math.PI*2),e.fill(),e.restore();}function ye(e,o,t,s){e.beginPath(),e.moveTo(o,t+s*.85),e.bezierCurveTo(o-s*1.25,t+s*.05,o-s*.75,t-s*.95,o,t-s*.35),e.bezierCurveTo(o+s*.75,t-s*.95,o+s*1.25,t+s*.05,o,t+s*.85),e.closePath();}var J=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:w,cos:nt,abs:rt,max:se,exp:at,PI:ie}=Math;function ne(e,o,t,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,e){case "idle":{let i=w(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*w(o*1.1),s.y=.02*w(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*w(i),s.rz=.12*nt(i),s.ry=.22*w(i),s.y=.02*w(i*2),s.lookX=.55*w(i);break}case "listening":{let i=se(0,w(o*3.2));s.rx=.09*i*i;let r=rt(w(o*7.1)*.5+w(o*11.3)*.3+w(o*3.7)*.2);s.st=.03*r;break}case "speaking":{let i=se(0,w(o*9)*.6+w(o*14.3)*.4)*(.65+.35*w(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*w(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*w(ie*i);let r=se(0,1-i/.14)+se(0,(i-.9)/.1);s.st=.08*w(ie*i)-.14*r,s.rz=.05*w(o*1.35*ie);break}case "surprised":{if(t<.45){let i=w(ie*t/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*w(o*38)*at(-t*2),s.rx=-0.06;break}case "sad":{let i=w(o*1.1);s.y=-0.06,s.rx=.2,s.st=-0.04+.015*i,s.rz=-0.04;break}case "error":{let i=t%2.8;if(i<.55){let r=1-i/.55;s.ry=.26*w(i*42)*r,s.rz=.05*w(i*42+1)*r;}s.st=-0.02;break}case "sleeping":{let i=w(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*w(o*1.6),s.rz=.05*w(o*.8),s.st=.015*w(o*1.6+1);break}}return s}function ve(e,o,t,s){for(let i of Object.keys(s))s[i]=e[i]+(o[i]-e[i])*t;return s}var Le={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var j=new Set,P=null,$=0,V=0,re=0,we,Ge=false,H="idle",D={x:0,y:0,lastMove:-1e9,known:false},z={reducedMotion:false,lowPower:false};function lt(){if(Ge)return;Ge=true,addEventListener("pointermove",o=>{D.x=o.clientX,D.y=o.clientY,D.lastMove=performance.now(),D.known=true;},{passive:true}),document.addEventListener("visibilitychange",_);let e=matchMedia("(prefers-reduced-motion: reduce)");z.reducedMotion=e.matches,e.addEventListener?.("change",o=>z.reducedMotion=o.matches);}function qe(e,o){return P||(P=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),P.setPixelRatio(1),P.setClearColor(0,0),z.lowPower=ct(P.getContext()),P.setScissorTest(true),P.domElement.addEventListener("webglcontextlost",t=>{t.preventDefault(),P?.dispose(),P=null,$=V=0,H="idle";})),(e>$||o>V)&&($=Math.max($,e),V=Math.max(V,o),P.setSize($,V,false)),P}var Ye=()=>V;function ct(e){try{let o=e.getExtension("WEBGL_debug_renderer_info"),t=String(e.getParameter(o?o.UNMASKED_RENDERER_WEBGL:e.RENDERER));return /swiftshader|llvmpipe|softpipe|software|basic render/i.test(t)}catch{return false}}function We(e,o){if(H==="ready")return true;if(H==="idle"&&P){H="compiling";let t=P;t.compileAsync(e,o).catch(()=>{}).then(()=>{t===P&&(H="ready"),_();});}return false}function De(e){lt(),clearTimeout(we),j.add(e),_();}function _e(e){j.delete(e),j.size===0&&(clearTimeout(we),we=setTimeout(()=>{j.size||!P||(P.dispose(),P.forceContextLoss(),P=null,$=V=0,H="idle");},1500));}function _(){if(!re){for(let e of j)if(e.active){re=requestAnimationFrame(Ne);return}}}var dt=10,Ie=new WeakMap,Me=0;function Ne(e){re=0;let o=[...j].filter(r=>r.active);if(!o.length)return;let t=performance.now(),s=o.length,i=0;for(;i<s;i++){let r=o[(Me+i)%s],d=Ie.get(r)??e-16;if(Ie.set(r,e),r.tick(Math.min(.05,Math.max(0,(e-d)/1e3)),e),performance.now()-t>dt){i++;break}}Me=(Me+i)%s,re=requestAnimationFrame(Ne);}var Z={pebble:{faceY:.02,faceScale:1.3,animated:false},blob:{faceY:.02,faceScale:1.28,animated:true},drop:{faceY:-0.24,faceScale:1.22,animated:false},bean:{faceY:.14,faceScale:.92,animated:false},cube:{faceY:0,faceScale:1.22,animated:false},ghost:{faceY:.22,faceScale:1.24,animated:true}};function Ve(e){let o=new SphereGeometry(1,e*2,Math.round(e*1.5)),t=o.getAttribute("position"),s=o.getIndex(),i=new Map,r=[],d=new Uint32Array(t.count);for(let n=0;n<t.count;n++){let h=t.getX(n),p=t.getY(n),b=t.getZ(n),E=`${Math.round(h*1e4)},${Math.round(p*1e4)},${Math.round(b*1e4)}`,S=i.get(E);if(S===void 0){S=r.length/3,i.set(E,S);let k=Math.hypot(h,p,b)||1;r.push(h/k,p/k,b/k);}d[n]=S;}let a=[];for(let n=0;n<s.count;n+=3){let h=d[s.getX(n)],p=d[s.getX(n+1)],b=d[s.getX(n+2)];h!==p&&p!==b&&h!==b&&a.push(h,p,b);}o.dispose();let l=new BufferGeometry,u=new Float32Array(r);return l.setAttribute("position",new BufferAttribute(new Float32Array(u),3)),l.setIndex(a),{geometry:l,dirs:u}}var ft=(e,o,t)=>{let s=Math.min(1,Math.max(0,(t-e)/(o-e)));return s*s*(3-2*s)};function ke(e,o,t,s,i,r,d){let a=o,l=t,u=s;switch(e){case "pebble":{a=o*1.1+.04*t,l=t*.92*(.95+.05*Math.tanh(3*t))+.03*o,u=s*.94;break}case "blob":{let n=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*t+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);a=o*n,l=t*n,u=s*n;break}case "drop":{let n=Math.max(t,0),h=1-.8*Math.pow(n,1.6);a=o*h*.98+.1*n*n*n,l=(t<0?t*.9:t*1.32)-.12,u=s*h*.98;break}case "bean":{let n=t*.78+.42*Math.tanh(t*3);a=(o*.8+.14*(n*n/1.44)-.07)*.9,l=n*.88,u=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(t),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);a=o*h*.84,l=t*h*.82,u=s*h*.84;break}case "ghost":{if(t>=0)a=o,l=t,u=s;else {let h=(Math.acos(Math.max(-1,t))-Math.PI/2)/(Math.PI/2),p=Math.hypot(o,s)||1,b=o/p,E=s/p,S=Math.atan2(s,o),k=.085*Math.sin(5*S+2.2*i),C=1.04,A=.13,B=-1.05+k,q;if(h<.45){let m=h/.45;q=1+.04*m,l=-m*(1.05-A)+k*ft(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);q=C-A+A*Math.cos(m),l=B+A-A*Math.sin(m);}else {let m=(h-.75)/.25;q=(C-A)*(1-m),l=B+.22*m*m*(3-2*m);}a=b*q,u=E*q;}a*=.9,l=(l+.1)*.9,u*=.9;break}}r[d]=a,r[d+1]=l,r[d+2]=u;}var I=null,ae=null;function bt(){if(typeof window>"u"||!("AudioContext"in window))return null;if(gt(),!I){I=new AudioContext;let e=I.createBiquadFilter();e.type="lowpass",e.frequency.value=3200,ae=I.createGain(),ae.gain.value=.55,ae.connect(e).connect(I.destination);}return I.state==="suspended"&&I.resume(),I}function F(e,{freq:o,to:t,at:s=0,dur:i=.12,type:r="sine",gain:d=.1,wobble:a=0}){let l=e.currentTime+s,u=e.createOscillator(),n=e.createGain();if(u.type=r,u.frequency.setValueAtTime(o,l),t&&u.frequency.exponentialRampToValueAtTime(t,l+i),a){let h=e.createOscillator(),p=e.createGain();h.frequency.value=7,p.gain.value=a,h.connect(p).connect(u.frequency),h.start(l),h.stop(l+i+.05);}n.gain.setValueAtTime(1e-4,l),n.gain.exponentialRampToValueAtTime(d,l+Math.min(.012,i/4)),n.gain.exponentialRampToValueAtTime(1e-4,l+i),u.connect(n).connect(ae),u.start(l),u.stop(l+i+.02);}var Se={idle:e=>F(e,{freq:520,to:780,dur:.09,gain:.08}),thinking:e=>[660,760,900].forEach((o,t)=>F(e,{freq:o,to:o*1.08,at:t*.1,dur:.07,type:"triangle",gain:.06})),loading:e=>[0,.14,.28].forEach(o=>F(e,{freq:940,at:o,dur:.04,gain:.05})),listening:e=>{F(e,{freq:523,dur:.12,type:"triangle",gain:.07}),F(e,{freq:784,at:.1,dur:.22,type:"triangle",gain:.07});},speaking:e=>[380,460,340,520,420].forEach((o,t)=>F(e,{freq:o,to:o*1.15,at:t*.075,dur:.06,type:"triangle",gain:.06})),happy:e=>[523,659,784,1047].forEach((o,t)=>F(e,{freq:o,at:t*.07,dur:.14,type:"triangle",gain:.07})),surprised:e=>F(e,{freq:280,to:980,dur:.18,gain:.09,wobble:30}),sad:e=>{F(e,{freq:560,to:300,dur:.5,gain:.07}),F(e,{freq:420,to:240,at:.12,dur:.5,gain:.04});},error:e=>[0,.14].forEach(o=>F(e,{freq:170,to:150,at:o,dur:.1,type:"sawtooth",gain:.045})),sleeping:e=>{F(e,{freq:150,to:115,dur:.6,gain:.07,wobble:6}),F(e,{freq:300,to:230,at:.05,dur:.5,gain:.02});},love:e=>[1319,1568,2093,1760].forEach((o,t)=>F(e,{freq:o,at:t*.08,dur:.22,gain:.04}))},Y=null,Xe=false;function gt(){if(Xe)return;Xe=true;let e=()=>{I&&I.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](I),Y=null;});};for(let o of ["pointerdown","touchend","keydown"])addEventListener(o,e,{passive:true});}function he(e,o=false){let t=bt();if(t){if(t.state==="running"){Y=null,Se[e](t);return}Y={mood:e,until:performance.now()+(o?6e3:1200)},t.resume().then(()=>{Y&&performance.now()<Y.until&&Se[Y.mood](t),Y=null;});}}var Ee=-0.85,je=new Map;function Tt(e){let o=je.get(e);if(!o){let{geometry:t,dirs:s}=Ve(e);o={dirs:s,index:new Uint32Array(t.getIndex().array)},t.dispose(),je.set(e,o);}return o}var K=(e,o)=>1-Math.exp(-e*o),Te=e=>1-Math.pow(1-e,3),le=class{constructor(){this.scene=new Scene;this.camera=new PerspectiveCamera(30,1,.1,50);this.group=new Group;this.v=new Vector3;this.geometry=new BufferGeometry;this.faceCanvas=document.createElement("canvas");this.uniforms={uFace:{value:null},uFaceY:{value:0},uFaceScale:{value:1.5}};this.state={...M};this.visible=false;this.connected=false;this.w=0;this.h=0;this.t=Math.random()*100;this.shapeFrom=M.shape;this.shapeTo=M.shape;this.morph=1;this.shapeDirty=true;this.shown=M.mood;this.prevShown=M.mood;this.tau=10;this.prevTau=10;this.blend=1;this.reaction=null;this.pose=J();this.poseIdle=J();this.moodTime=0;this.poseA=J();this.poseB=J();this.face=ge(oe.idle);this.target=ge(oe.idle);this.drawn=new Float32Array(U.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=X(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=ee(this.body);this.faceCT=this.faceC;this.cheek=te(this.body);this.cheekT=this.cheek;this.look={x:0,y:0};this.wander={x:0,y:0,next:0};this.squash=0;this.squashV=0;this.pop=.6;this.popV=0;this.hover=0;this.hovering=false;this.overlay={dots:0,zz:0,hearts:0,waves:0,bang:0};this.needsFrame=true;this.ready=false;this.css=[0,0];this.quality=1;this.lowPowerApplied=false;this.cost=0;this.costFrames=0;this.lastFrame=[];this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.faceCtx=this.faceCanvas.getContext("2d"),this.faceCanvas.width=this.faceCanvas.height=256,this.faceTex=new CanvasTexture(this.faceCanvas),this.faceTex.colorSpace=SRGBColorSpace,this.faceTex.minFilter=LinearMipmapLinearFilter,this.faceTex.anisotropy=4,this.uniforms.uFace.value=this.faceTex,this.setSegments(24),this.material=this.createMaterial(),this.mesh=new Mesh(this.geometry,this.material),this.mesh.frustumCulled=false,this.mesh.position.y=-Ee,this.group.add(this.mesh),this.scene.add(this.group),this.scene.add(new HemisphereLight(16774120,7234140,1.7));let o=new DirectionalLight(16777215,1.9);o.position.set(2.5,3.5,4);let t=new DirectionalLight(16770770,1.4);t.position.set(-3.5,2,-3);let s=new DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,t,s),this.camera.position.set(0,.3,6.6),this.camera.lookAt(0,.12,0);}get active(){return this.visible&&this.connected&&this.w>0}createMaterial(){let o=new MeshStandardMaterial({roughness:.62,metalness:0});return o.color.setRGB(...this.body,SRGBColorSpace),o.customProgramCacheKey=()=>"moodi-1",o.onBeforeCompile=t=>{Object.assign(t.uniforms,this.uniforms),t.vertexShader=t.vertexShader.replace("#include <common>",`#include <common>
|
|
2
2
|
varying vec3 vFaceP;
|
|
3
3
|
varying vec3 vFaceN;`).replace("#include <begin_vertex>",`#include <begin_vertex>
|
|
4
4
|
vFaceP = position;
|