moodi 0.1.0 → 0.1.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 Ne=["ghost","pebble","blob","drop","bean","cube"],Xe=["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},wt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},St={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."}},Ct={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},re=t=>Ne.includes(t),ae=t=>Xe.includes(t);var z=null;function _(t,o){if(!t)return o;let e=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(t.trim());if(e)return Se(e[1]);if(z||(z=document.createElement("canvas").getContext("2d")),!z)return o;z.fillStyle="#010203",z.fillStyle=t;let s=String(z.fillStyle);if(s==="#010203"&&t.trim()!=="#010203")return o;if(s.startsWith("#"))return Se(s.slice(1));z.canvas.width=z.canvas.height=1,z.clearRect(0,0,1,1),z.fillRect(0,0,1,1);let i=z.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Se(t){t.length===3&&(t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]);let o=parseInt(t,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var O=t=>`rgb(${Math.round(t[0]*255)},${Math.round(t[1]*255)},${Math.round(t[2]*255)})`,P=(t,o,e)=>[t[0]+(o[0]-t[0])*e,t[1]+(o[1]-t[1])*e,t[2]+(o[2]-t[2])*e],ne=t=>t<=.04045?t/12.92:Math.pow((t+.055)/1.055,2.4),he=t=>.2126*ne(t[0])+.7152*ne(t[1])+.0722*ne(t[2]),Ve=[.16,.11,.1],He=[1,.97,.93],$e=[1,.42,.48],U=t=>he(t)>.18?P(Ve,t,.12):He,J=t=>P(t,$e,.78);var $=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],L={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},Z={idle:{...L},thinking:{...L,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...L,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...L,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...L,mouthW:.09,mouthCurve:.25},happy:{...L,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...L,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...L,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...L,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...L,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...L,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Ce={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},de=t=>({...t}),je=.31,Ke=-0.04,Ue=.098,le=.128,Je=.2,ce=.05;function Te(t,o,e,s,i,a,l,d,u,p){let r=t.canvas.width;t.setTransform(1,0,0,1,0,0),t.clearRect(0,0,r,r),t.setTransform(r/2,0,0,r/2,r/2,r/2),t.lineCap="round",t.lineJoin="round";let h=o.lookX*.075,c=o.lookY*.06;if(o.blush>.01){let m=r*.018;t.save(),"filter"in t&&(t.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4,f=t.createRadialGradient(v,n,0,v,n,.16);f.addColorStop(0,i),f.addColorStop(1,"rgba(0,0,0,0)"),t.globalAlpha=Math.min(1,o.blush)*.62,t.fillStyle=f,t.beginPath(),t.ellipse(v,n,.17,.12,0,0,Math.PI*2),t.fill();}t.restore();let b=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(b>.01){t.save(),t.globalAlpha=b*.55,t.strokeStyle=i,t.lineWidth=.016,t.lineCap="round";for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4;for(let f=-1;f<=1;f++){let F=v+f*.045;t.beginPath(),t.moveTo(F+.018,n-.035),t.lineTo(F-.018,n+.035),t.stroke();}}t.restore();}}t.fillStyle=s,t.strokeStyle=s,t.lineWidth=ce;let A=1-Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye);for(let m of [-1,1]){let b=m*je+h,y=Ke+c,v=Ue*o.eyeSize;if(A>.01){t.globalAlpha=A;let n=Math.max(.06,o.eyeOpen*e),f=le*o.eyeSize*n;if(t.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-le*o.eyeSize+2*le*o.eyeSize*o.lidCover;t.translate(b,W),t.rotate(m*o.lidTilt*.6),t.beginPath(),t.rect(-1,0,2,2),t.clip(),t.rotate(-m*o.lidTilt*.6),t.translate(-b,-W);}t.shadowColor=s,t.shadowBlur=r*.035*a;let F=t.createRadialGradient(b,y-f*.2,0,b,y,Math.max(v,f));if(F.addColorStop(0,l),F.addColorStop(1,s),t.fillStyle=F,t.beginPath(),t.ellipse(b,y,v,f,0,0,Math.PI*2),t.fill(),t.shadowBlur=0,t.fillStyle=s,n>.35){let x=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ie=d==="#fff"?.3:.34;t.globalAlpha=A*Math.min(1,(n-.35)*3),t.fillStyle=d,t.beginPath(),t.arc(b-v*.1+x*v*.5,y-f*.16+W*f*.42,v*ie,0,Math.PI*2),t.fill(),t.fillStyle=s;}t.restore();}if(t.shadowColor=s,t.shadowBlur=r*.03*a,o.happyEye>.01){let n=.6+.4*o.happyEye;t.globalAlpha=Math.min(1,o.happyEye),t.beginPath(),t.arc(b,y+.045,v*1.15*n,Math.PI*1.12,Math.PI*1.88),t.stroke();}if(o.closedEye>.01){let n=.6+.4*o.closedEye;t.globalAlpha=Math.min(1,o.closedEye),t.beginPath(),t.moveTo(b-v*1.15*n,y+.01),t.quadraticCurveTo(b,y+.07*n,b+v*1.15*n,y+.01),t.stroke();}if(o.xEye>.01){let n=v*.95*(.6+.4*o.xEye);t.globalAlpha=Math.min(1,o.xEye),t.beginPath(),t.moveTo(b-n,y-n),t.lineTo(b+n,y+n),t.moveTo(b+n,y-n),t.lineTo(b-n,y+n),t.stroke();}o.heartEye>.01&&(t.globalAlpha=Math.min(1,o.heartEye),ue(t,b,y,v*1.35*(.5+.5*o.heartEye)),t.fill());}t.shadowBlur=0;let k=h*.6+o.mouthSkew*.07,w=Je+c*.6,T=Math.min(1,Math.max(0,o.mouthRound)),S=o.mouthW,R=t.createLinearGradient(k-S-.02,w,k+S+.02,w);R.addColorStop(0,p),R.addColorStop(.5,l),R.addColorStop(1,p),t.shadowColor=s,t.shadowBlur=r*.02*a;let B=(m,b,y,v)=>{t.fillStyle=u,t.fill(m),t.shadowBlur=0,b>.02&&(t.save(),t.clip(m),t.fillStyle=i,t.globalAlpha*=Math.min(1,(b-.02)*12),t.beginPath(),t.ellipse(k,y,v,b*.55,0,0,Math.PI*2),t.fill(),t.restore()),t.strokeStyle=R;let n=t.lineWidth;t.lineWidth=n*.6,t.stroke(m),t.lineWidth=n,t.shadowBlur=r*.02*a;};if(T<.99){t.globalAlpha=1-T;let m=18,b=f=>o.mouthCurve*.075*(1-f*f),y=(f,F)=>{let x=f/m*2-1,W=k+x*S,ie=w+b(x)+o.mouthSkew*x*.03+o.mouthWave*.018*Math.sin((x+1)*Math.PI*2)+F*(1-x*x);return [W,ie]},v=Math.max(0,o.mouthOpen),n=new Path2D;for(let f=0;f<=m;f++){let[F,x]=y(f,-v*.15);f?n.lineTo(F,x):n.moveTo(F,x);}if(v>.003){for(let f=m;f>=0;f--){let[F,x]=y(f,v);n.lineTo(F,x);}n.closePath(),B(n,v,y(m/2,v)[1],S*.55);}else t.strokeStyle=R,t.stroke(n);}if(T>.01){t.globalAlpha=T;let m=Math.max(.02,o.mouthOpen*.55+.01),b=new Path2D;b.ellipse(k,w+.01,m*.85,m,0,0,Math.PI*2),t.lineWidth=ce*.8,B(b,o.mouthOpen,w+.01+m*.8,m*.6),t.lineWidth=ce;}t.shadowBlur=0,t.globalAlpha=1;}function ue(t,o,e,s){t.beginPath(),t.moveTo(o,e+s*.85),t.bezierCurveTo(o-s*1.25,e+s*.05,o-s*.75,e-s*.95,o,e-s*.35),t.bezierCurveTo(o+s*.75,e-s*.95,o+s*1.25,e+s*.05,o,e+s*.85),t.closePath();}var te=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:g,cos:Ze,abs:Qe,max:Q,exp:et,PI:ee}=Math;function pe(t,o,e,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,t){case "idle":{let i=g(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*g(o*1.1),s.y=.02*g(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*g(i),s.rz=.12*Ze(i),s.ry=.22*g(i),s.y=.02*g(i*2),s.lookX=.55*g(i);break}case "listening":{let i=Q(0,g(o*3.2));s.rx=.09*i*i;let a=Qe(g(o*7.1)*.5+g(o*11.3)*.3+g(o*3.7)*.2);s.st=.03*a;break}case "speaking":{let i=Q(0,g(o*9)*.6+g(o*14.3)*.4)*(.65+.35*g(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*g(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*g(ee*i);let a=Q(0,1-i/.14)+Q(0,(i-.9)/.1);s.st=.08*g(ee*i)-.14*a,s.rz=.05*g(o*1.35*ee);break}case "surprised":{if(e<.45){let i=g(ee*e/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*g(o*38)*et(-e*2),s.rx=-0.06;break}case "sad":{let i=g(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=e%2.8;if(i<.55){let a=1-i/.55;s.ry=.26*g(i*42)*a,s.rz=.05*g(i*42+1)*a;}s.st=-0.02;break}case "sleeping":{let i=g(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*g(o*1.6),s.rz=.05*g(o*.8),s.st=.015*g(o*1.6+1);break}}return s}function Ae(t,o,e,s){for(let i of Object.keys(s))s[i]=t[i]+(o[i]-t[i])*e;return s}var Ee={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var X=new Set,E=null,q=0,Y=0,oe=0,be,Pe=false,N="idle",I={x:0,y:0,lastMove:-1e9,known:false},G={reducedMotion:false};function ot(){if(Pe)return;Pe=true,addEventListener("pointermove",o=>{I.x=o.clientX,I.y=o.clientY,I.lastMove=performance.now(),I.known=true;},{passive:true}),document.addEventListener("visibilitychange",D);let t=matchMedia("(prefers-reduced-motion: reduce)");G.reducedMotion=t.matches,t.addEventListener?.("change",o=>G.reducedMotion=o.matches);}function Fe(t,o){return E||(E=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),E.setPixelRatio(1),E.setClearColor(0,0),E.setScissorTest(true),E.domElement.addEventListener("webglcontextlost",e=>{e.preventDefault(),E?.dispose(),E=null,q=Y=0,N="idle";})),(t>q||o>Y)&&(q=Math.max(q,t),Y=Math.max(Y,o),E.setSize(q,Y,false)),E}var Re=()=>Y;function Be(t,o){if(N==="ready")return true;if(N==="idle"&&E){N="compiling";let e=E;e.compileAsync(t,o).catch(()=>{}).then(()=>{e===E&&(N="ready"),D();});}return false}function ze(t){ot(),clearTimeout(be),X.add(t),D();}function Oe(t){X.delete(t),X.size===0&&(clearTimeout(be),be=setTimeout(()=>{X.size||!E||(E.dispose(),E.forceContextLoss(),E=null,q=Y=0,N="idle");},1500));}function D(){if(!oe){for(let t of X)if(t.active){oe=requestAnimationFrame(Le);return}}}var st=10,xe=new WeakMap,me=0;function Le(t){oe=0;let o=[...X].filter(a=>a.active);if(!o.length)return;let e=performance.now(),s=o.length,i=0;for(;i<s;i++){let a=o[(me+i)%s],l=xe.get(a)??t-16;if(xe.set(a,t),a.tick(Math.min(.05,Math.max(0,(t-l)/1e3)),t),performance.now()-e>st){i++;break}}me=(me+i)%s,oe=requestAnimationFrame(Le);}var j={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 Ie(t){let o=new three.SphereGeometry(1,t*2,Math.round(t*1.5)),e=o.getAttribute("position"),s=o.getIndex(),i=new Map,a=[],l=new Uint32Array(e.count);for(let r=0;r<e.count;r++){let h=e.getX(r),c=e.getY(r),C=e.getZ(r),A=`${Math.round(h*1e4)},${Math.round(c*1e4)},${Math.round(C*1e4)}`,k=i.get(A);if(k===void 0){k=a.length/3,i.set(A,k);let w=Math.hypot(h,c,C)||1;a.push(h/w,c/w,C/w);}l[r]=k;}let d=[];for(let r=0;r<s.count;r+=3){let h=l[s.getX(r)],c=l[s.getX(r+1)],C=l[s.getX(r+2)];h!==c&&c!==C&&h!==C&&d.push(h,c,C);}o.dispose();let u=new three.BufferGeometry,p=new Float32Array(a);return u.setAttribute("position",new three.BufferAttribute(new Float32Array(p),3)),u.setIndex(d),{geometry:u,dirs:p}}var nt=(t,o,e)=>{let s=Math.min(1,Math.max(0,(e-t)/(o-t)));return s*s*(3-2*s)};function fe(t,o,e,s,i,a,l){let d=o,u=e,p=s;switch(t){case "pebble":{d=o*1.1+.04*e,u=e*.92*(.95+.05*Math.tanh(3*e))+.03*o,p=s*.94;break}case "blob":{let r=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*e+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);d=o*r,u=e*r,p=s*r;break}case "drop":{let r=Math.max(e,0),h=1-.8*Math.pow(r,1.6);d=o*h*.98+.1*r*r*r,u=(e<0?e*.9:e*1.32)-.12,p=s*h*.98;break}case "bean":{let r=e*.78+.42*Math.tanh(e*3);d=(o*.8+.14*(r*r/1.44)-.07)*.9,u=r*.88,p=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(e),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);d=o*h*.84,u=e*h*.82,p=s*h*.84;break}case "ghost":{if(e>=0)d=o,u=e,p=s;else {let h=(Math.acos(Math.max(-1,e))-Math.PI/2)/(Math.PI/2),c=Math.hypot(o,s)||1,C=o/c,A=s/c,k=Math.atan2(s,o),w=.085*Math.sin(5*k+2.2*i),T=1.04,S=.13,R=-1.05+w,B;if(h<.45){let m=h/.45;B=1+.04*m,u=-m*(1.05-S)+w*nt(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);B=T-S+S*Math.cos(m),u=R+S-S*Math.sin(m);}else {let m=(h-.75)/.25;B=(T-S)*(1-m),u=R+.22*m*m*(3-2*m);}d=C*B,p=A*B;}d*=.9,u=(u+.1)*.9,p*=.9;break}}a[l]=d,a[l+1]=u,a[l+2]=p;}var ve=-0.85,De=new Map;function gt(t){let o=De.get(t);if(!o){let{geometry:e,dirs:s}=Ie(t);o={dirs:s,index:new Uint32Array(e.getIndex().array)},e.dispose(),De.set(t,o);}return o}var V=(t,o)=>1-Math.exp(-t*o),We=t=>1-Math.pow(1-t,3),se=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=te();this.poseA=te();this.poseB=te();this.face=de(Z.idle);this.target=de(Z.idle);this.drawn=new Float32Array($.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=_(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=U(this.body);this.faceCT=this.faceC;this.cheek=J(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.css=[0,0];this.quality=1;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=-ve,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 e=new three.DirectionalLight(16770770,1.4);e.position.set(-3.5,2,-3);let s=new three.DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,e,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=e=>{Object.assign(e.uniforms,this.uniforms),e.vertexShader=e.vertexShader.replace("#include <common>",`#include <common>
|
|
1
|
+
'use strict';var three=require('three');var Ne=["ghost","pebble","blob","drop","bean","cube"],Xe=["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},wt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},St={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."}},Ct={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},re=t=>Ne.includes(t),ae=t=>Xe.includes(t);var z=null;function _(t,o){if(!t)return o;let e=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(t.trim());if(e)return Se(e[1]);if(z||(z=document.createElement("canvas").getContext("2d")),!z)return o;z.fillStyle="#010203",z.fillStyle=t;let s=String(z.fillStyle);if(s==="#010203"&&t.trim()!=="#010203")return o;if(s.startsWith("#"))return Se(s.slice(1));z.canvas.width=z.canvas.height=1,z.clearRect(0,0,1,1),z.fillRect(0,0,1,1);let i=z.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Se(t){t.length===3&&(t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]);let o=parseInt(t,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var O=t=>`rgb(${Math.round(t[0]*255)},${Math.round(t[1]*255)},${Math.round(t[2]*255)})`,P=(t,o,e)=>[t[0]+(o[0]-t[0])*e,t[1]+(o[1]-t[1])*e,t[2]+(o[2]-t[2])*e],ne=t=>t<=.04045?t/12.92:Math.pow((t+.055)/1.055,2.4),he=t=>.2126*ne(t[0])+.7152*ne(t[1])+.0722*ne(t[2]),Ve=[.07,.06,.08],He=[1,.97,.93],$e=[1,.42,.48],U=t=>he(t)>.06?P(Ve,t,.06):He,J=t=>P(t,$e,.78);var $=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],L={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},Z={idle:{...L},thinking:{...L,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...L,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...L,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...L,mouthW:.09,mouthCurve:.25},happy:{...L,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...L,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...L,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...L,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...L,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...L,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Ce={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},de=t=>({...t}),je=.31,Ke=-0.04,Ue=.098,le=.128,Je=.2,ce=.05;function Te(t,o,e,s,i,a,l,d,u,p){let r=t.canvas.width;t.setTransform(1,0,0,1,0,0),t.clearRect(0,0,r,r),t.setTransform(r/2,0,0,r/2,r/2,r/2),t.lineCap="round",t.lineJoin="round";let h=o.lookX*.075,c=o.lookY*.06;if(o.blush>.01){let m=r*.018;t.save(),"filter"in t&&(t.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4,f=t.createRadialGradient(v,n,0,v,n,.16);f.addColorStop(0,i),f.addColorStop(1,"rgba(0,0,0,0)"),t.globalAlpha=Math.min(1,o.blush)*.62,t.fillStyle=f,t.beginPath(),t.ellipse(v,n,.17,.12,0,0,Math.PI*2),t.fill();}t.restore();let b=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(b>.01){t.save(),t.globalAlpha=b*.55,t.strokeStyle=i,t.lineWidth=.016,t.lineCap="round";for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4;for(let f=-1;f<=1;f++){let F=v+f*.045;t.beginPath(),t.moveTo(F+.018,n-.035),t.lineTo(F-.018,n+.035),t.stroke();}}t.restore();}}t.fillStyle=s,t.strokeStyle=s,t.lineWidth=ce;let A=1-Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye);for(let m of [-1,1]){let b=m*je+h,y=Ke+c,v=Ue*o.eyeSize;if(A>.01){t.globalAlpha=A;let n=Math.max(.06,o.eyeOpen*e),f=le*o.eyeSize*n;if(t.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-le*o.eyeSize+2*le*o.eyeSize*o.lidCover;t.translate(b,W),t.rotate(m*o.lidTilt*.6),t.beginPath(),t.rect(-1,0,2,2),t.clip(),t.rotate(-m*o.lidTilt*.6),t.translate(-b,-W);}t.shadowColor=s,t.shadowBlur=r*.035*a;let F=t.createRadialGradient(b,y-f*.2,0,b,y,Math.max(v,f));if(F.addColorStop(0,l),F.addColorStop(1,s),t.fillStyle=F,t.beginPath(),t.ellipse(b,y,v,f,0,0,Math.PI*2),t.fill(),t.shadowBlur=0,t.fillStyle=s,n>.35){let x=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ie=d==="#fff"?.3:.34;t.globalAlpha=A*Math.min(1,(n-.35)*3),t.fillStyle=d,t.beginPath(),t.arc(b-v*.1+x*v*.5,y-f*.16+W*f*.42,v*ie,0,Math.PI*2),t.fill(),t.fillStyle=s;}t.restore();}if(t.shadowColor=s,t.shadowBlur=r*.03*a,o.happyEye>.01){let n=.6+.4*o.happyEye;t.globalAlpha=Math.min(1,o.happyEye),t.beginPath(),t.arc(b,y+.045,v*1.15*n,Math.PI*1.12,Math.PI*1.88),t.stroke();}if(o.closedEye>.01){let n=.6+.4*o.closedEye;t.globalAlpha=Math.min(1,o.closedEye),t.beginPath(),t.moveTo(b-v*1.15*n,y+.01),t.quadraticCurveTo(b,y+.07*n,b+v*1.15*n,y+.01),t.stroke();}if(o.xEye>.01){let n=v*.95*(.6+.4*o.xEye);t.globalAlpha=Math.min(1,o.xEye),t.beginPath(),t.moveTo(b-n,y-n),t.lineTo(b+n,y+n),t.moveTo(b+n,y-n),t.lineTo(b-n,y+n),t.stroke();}o.heartEye>.01&&(t.globalAlpha=Math.min(1,o.heartEye),ue(t,b,y,v*1.35*(.5+.5*o.heartEye)),t.fill());}t.shadowBlur=0;let k=h*.6+o.mouthSkew*.07,w=Je+c*.6,T=Math.min(1,Math.max(0,o.mouthRound)),S=o.mouthW,R=t.createLinearGradient(k-S-.02,w,k+S+.02,w);R.addColorStop(0,p),R.addColorStop(.5,l),R.addColorStop(1,p),t.shadowColor=s,t.shadowBlur=r*.02*a;let B=(m,b,y,v)=>{t.fillStyle=u,t.fill(m),t.shadowBlur=0,b>.02&&(t.save(),t.clip(m),t.fillStyle=i,t.globalAlpha*=Math.min(1,(b-.02)*12),t.beginPath(),t.ellipse(k,y,v,b*.55,0,0,Math.PI*2),t.fill(),t.restore()),t.strokeStyle=R;let n=t.lineWidth;t.lineWidth=n*.6,t.stroke(m),t.lineWidth=n,t.shadowBlur=r*.02*a;};if(T<.99){t.globalAlpha=1-T;let m=18,b=f=>o.mouthCurve*.075*(1-f*f),y=(f,F)=>{let x=f/m*2-1,W=k+x*S,ie=w+b(x)+o.mouthSkew*x*.03+o.mouthWave*.018*Math.sin((x+1)*Math.PI*2)+F*(1-x*x);return [W,ie]},v=Math.max(0,o.mouthOpen),n=new Path2D;for(let f=0;f<=m;f++){let[F,x]=y(f,-v*.15);f?n.lineTo(F,x):n.moveTo(F,x);}if(v>.003){for(let f=m;f>=0;f--){let[F,x]=y(f,v);n.lineTo(F,x);}n.closePath(),B(n,v,y(m/2,v)[1],S*.55);}else t.strokeStyle=R,t.stroke(n);}if(T>.01){t.globalAlpha=T;let m=Math.max(.02,o.mouthOpen*.55+.01),b=new Path2D;b.ellipse(k,w+.01,m*.85,m,0,0,Math.PI*2),t.lineWidth=ce*.8,B(b,o.mouthOpen,w+.01+m*.8,m*.6),t.lineWidth=ce;}t.shadowBlur=0,t.globalAlpha=1;}function ue(t,o,e,s){t.beginPath(),t.moveTo(o,e+s*.85),t.bezierCurveTo(o-s*1.25,e+s*.05,o-s*.75,e-s*.95,o,e-s*.35),t.bezierCurveTo(o+s*.75,e-s*.95,o+s*1.25,e+s*.05,o,e+s*.85),t.closePath();}var te=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:g,cos:Ze,abs:Qe,max:Q,exp:et,PI:ee}=Math;function pe(t,o,e,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,t){case "idle":{let i=g(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*g(o*1.1),s.y=.02*g(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*g(i),s.rz=.12*Ze(i),s.ry=.22*g(i),s.y=.02*g(i*2),s.lookX=.55*g(i);break}case "listening":{let i=Q(0,g(o*3.2));s.rx=.09*i*i;let a=Qe(g(o*7.1)*.5+g(o*11.3)*.3+g(o*3.7)*.2);s.st=.03*a;break}case "speaking":{let i=Q(0,g(o*9)*.6+g(o*14.3)*.4)*(.65+.35*g(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*g(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*g(ee*i);let a=Q(0,1-i/.14)+Q(0,(i-.9)/.1);s.st=.08*g(ee*i)-.14*a,s.rz=.05*g(o*1.35*ee);break}case "surprised":{if(e<.45){let i=g(ee*e/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*g(o*38)*et(-e*2),s.rx=-0.06;break}case "sad":{let i=g(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=e%2.8;if(i<.55){let a=1-i/.55;s.ry=.26*g(i*42)*a,s.rz=.05*g(i*42+1)*a;}s.st=-0.02;break}case "sleeping":{let i=g(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*g(o*1.6),s.rz=.05*g(o*.8),s.st=.015*g(o*1.6+1);break}}return s}function Ae(t,o,e,s){for(let i of Object.keys(s))s[i]=t[i]+(o[i]-t[i])*e;return s}var Ee={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var X=new Set,E=null,q=0,Y=0,oe=0,be,Pe=false,N="idle",I={x:0,y:0,lastMove:-1e9,known:false},G={reducedMotion:false};function ot(){if(Pe)return;Pe=true,addEventListener("pointermove",o=>{I.x=o.clientX,I.y=o.clientY,I.lastMove=performance.now(),I.known=true;},{passive:true}),document.addEventListener("visibilitychange",D);let t=matchMedia("(prefers-reduced-motion: reduce)");G.reducedMotion=t.matches,t.addEventListener?.("change",o=>G.reducedMotion=o.matches);}function Fe(t,o){return E||(E=new three.WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),E.setPixelRatio(1),E.setClearColor(0,0),E.setScissorTest(true),E.domElement.addEventListener("webglcontextlost",e=>{e.preventDefault(),E?.dispose(),E=null,q=Y=0,N="idle";})),(t>q||o>Y)&&(q=Math.max(q,t),Y=Math.max(Y,o),E.setSize(q,Y,false)),E}var Re=()=>Y;function Be(t,o){if(N==="ready")return true;if(N==="idle"&&E){N="compiling";let e=E;e.compileAsync(t,o).catch(()=>{}).then(()=>{e===E&&(N="ready"),D();});}return false}function ze(t){ot(),clearTimeout(be),X.add(t),D();}function Oe(t){X.delete(t),X.size===0&&(clearTimeout(be),be=setTimeout(()=>{X.size||!E||(E.dispose(),E.forceContextLoss(),E=null,q=Y=0,N="idle");},1500));}function D(){if(!oe){for(let t of X)if(t.active){oe=requestAnimationFrame(Le);return}}}var st=10,xe=new WeakMap,me=0;function Le(t){oe=0;let o=[...X].filter(a=>a.active);if(!o.length)return;let e=performance.now(),s=o.length,i=0;for(;i<s;i++){let a=o[(me+i)%s],l=xe.get(a)??t-16;if(xe.set(a,t),a.tick(Math.min(.05,Math.max(0,(t-l)/1e3)),t),performance.now()-e>st){i++;break}}me=(me+i)%s,oe=requestAnimationFrame(Le);}var j={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 Ie(t){let o=new three.SphereGeometry(1,t*2,Math.round(t*1.5)),e=o.getAttribute("position"),s=o.getIndex(),i=new Map,a=[],l=new Uint32Array(e.count);for(let r=0;r<e.count;r++){let h=e.getX(r),c=e.getY(r),C=e.getZ(r),A=`${Math.round(h*1e4)},${Math.round(c*1e4)},${Math.round(C*1e4)}`,k=i.get(A);if(k===void 0){k=a.length/3,i.set(A,k);let w=Math.hypot(h,c,C)||1;a.push(h/w,c/w,C/w);}l[r]=k;}let d=[];for(let r=0;r<s.count;r+=3){let h=l[s.getX(r)],c=l[s.getX(r+1)],C=l[s.getX(r+2)];h!==c&&c!==C&&h!==C&&d.push(h,c,C);}o.dispose();let u=new three.BufferGeometry,p=new Float32Array(a);return u.setAttribute("position",new three.BufferAttribute(new Float32Array(p),3)),u.setIndex(d),{geometry:u,dirs:p}}var nt=(t,o,e)=>{let s=Math.min(1,Math.max(0,(e-t)/(o-t)));return s*s*(3-2*s)};function fe(t,o,e,s,i,a,l){let d=o,u=e,p=s;switch(t){case "pebble":{d=o*1.1+.04*e,u=e*.92*(.95+.05*Math.tanh(3*e))+.03*o,p=s*.94;break}case "blob":{let r=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*e+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);d=o*r,u=e*r,p=s*r;break}case "drop":{let r=Math.max(e,0),h=1-.8*Math.pow(r,1.6);d=o*h*.98+.1*r*r*r,u=(e<0?e*.9:e*1.32)-.12,p=s*h*.98;break}case "bean":{let r=e*.78+.42*Math.tanh(e*3);d=(o*.8+.14*(r*r/1.44)-.07)*.9,u=r*.88,p=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(e),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);d=o*h*.84,u=e*h*.82,p=s*h*.84;break}case "ghost":{if(e>=0)d=o,u=e,p=s;else {let h=(Math.acos(Math.max(-1,e))-Math.PI/2)/(Math.PI/2),c=Math.hypot(o,s)||1,C=o/c,A=s/c,k=Math.atan2(s,o),w=.085*Math.sin(5*k+2.2*i),T=1.04,S=.13,R=-1.05+w,B;if(h<.45){let m=h/.45;B=1+.04*m,u=-m*(1.05-S)+w*nt(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);B=T-S+S*Math.cos(m),u=R+S-S*Math.sin(m);}else {let m=(h-.75)/.25;B=(T-S)*(1-m),u=R+.22*m*m*(3-2*m);}d=C*B,p=A*B;}d*=.9,u=(u+.1)*.9,p*=.9;break}}a[l]=d,a[l+1]=u,a[l+2]=p;}var ve=-0.85,De=new Map;function gt(t){let o=De.get(t);if(!o){let{geometry:e,dirs:s}=Ie(t);o={dirs:s,index:new Uint32Array(e.getIndex().array)},e.dispose(),De.set(t,o);}return o}var V=(t,o)=>1-Math.exp(-t*o),We=t=>1-Math.pow(1-t,3),se=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=te();this.poseA=te();this.poseB=te();this.face=de(Z.idle);this.target=de(Z.idle);this.drawn=new Float32Array($.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=_(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=U(this.body);this.faceCT=this.faceC;this.cheek=J(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.css=[0,0];this.quality=1;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=-ve,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 e=new three.DirectionalLight(16770770,1.4);e.position.set(-3.5,2,-3);let s=new three.DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,e,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=e=>{Object.assign(e.uniforms,this.uniforms),e.vertexShader=e.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 Ne=["ghost","pebble","blob","drop","bean","cube"],Xe=["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},wt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},St={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."}},Ct={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},re=t=>Ne.includes(t),ae=t=>Xe.includes(t);var z=null;function _(t,o){if(!t)return o;let e=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(t.trim());if(e)return Se(e[1]);if(z||(z=document.createElement("canvas").getContext("2d")),!z)return o;z.fillStyle="#010203",z.fillStyle=t;let s=String(z.fillStyle);if(s==="#010203"&&t.trim()!=="#010203")return o;if(s.startsWith("#"))return Se(s.slice(1));z.canvas.width=z.canvas.height=1,z.clearRect(0,0,1,1),z.fillRect(0,0,1,1);let i=z.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Se(t){t.length===3&&(t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]);let o=parseInt(t,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var O=t=>`rgb(${Math.round(t[0]*255)},${Math.round(t[1]*255)},${Math.round(t[2]*255)})`,P=(t,o,e)=>[t[0]+(o[0]-t[0])*e,t[1]+(o[1]-t[1])*e,t[2]+(o[2]-t[2])*e],ne=t=>t<=.04045?t/12.92:Math.pow((t+.055)/1.055,2.4),he=t=>.2126*ne(t[0])+.7152*ne(t[1])+.0722*ne(t[2]),Ve=[.16,.11,.1],He=[1,.97,.93],$e=[1,.42,.48],U=t=>he(t)>.18?P(Ve,t,.12):He,J=t=>P(t,$e,.78);var $=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],L={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},Z={idle:{...L},thinking:{...L,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...L,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...L,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...L,mouthW:.09,mouthCurve:.25},happy:{...L,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...L,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...L,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...L,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...L,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...L,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Ce={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},de=t=>({...t}),je=.31,Ke=-0.04,Ue=.098,le=.128,Je=.2,ce=.05;function Te(t,o,e,s,i,a,l,d,u,p){let r=t.canvas.width;t.setTransform(1,0,0,1,0,0),t.clearRect(0,0,r,r),t.setTransform(r/2,0,0,r/2,r/2,r/2),t.lineCap="round",t.lineJoin="round";let h=o.lookX*.075,c=o.lookY*.06;if(o.blush>.01){let m=r*.018;t.save(),"filter"in t&&(t.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4,f=t.createRadialGradient(v,n,0,v,n,.16);f.addColorStop(0,i),f.addColorStop(1,"rgba(0,0,0,0)"),t.globalAlpha=Math.min(1,o.blush)*.62,t.fillStyle=f,t.beginPath(),t.ellipse(v,n,.17,.12,0,0,Math.PI*2),t.fill();}t.restore();let b=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(b>.01){t.save(),t.globalAlpha=b*.55,t.strokeStyle=i,t.lineWidth=.016,t.lineCap="round";for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4;for(let f=-1;f<=1;f++){let F=v+f*.045;t.beginPath(),t.moveTo(F+.018,n-.035),t.lineTo(F-.018,n+.035),t.stroke();}}t.restore();}}t.fillStyle=s,t.strokeStyle=s,t.lineWidth=ce;let A=1-Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye);for(let m of [-1,1]){let b=m*je+h,y=Ke+c,v=Ue*o.eyeSize;if(A>.01){t.globalAlpha=A;let n=Math.max(.06,o.eyeOpen*e),f=le*o.eyeSize*n;if(t.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-le*o.eyeSize+2*le*o.eyeSize*o.lidCover;t.translate(b,W),t.rotate(m*o.lidTilt*.6),t.beginPath(),t.rect(-1,0,2,2),t.clip(),t.rotate(-m*o.lidTilt*.6),t.translate(-b,-W);}t.shadowColor=s,t.shadowBlur=r*.035*a;let F=t.createRadialGradient(b,y-f*.2,0,b,y,Math.max(v,f));if(F.addColorStop(0,l),F.addColorStop(1,s),t.fillStyle=F,t.beginPath(),t.ellipse(b,y,v,f,0,0,Math.PI*2),t.fill(),t.shadowBlur=0,t.fillStyle=s,n>.35){let x=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ie=d==="#fff"?.3:.34;t.globalAlpha=A*Math.min(1,(n-.35)*3),t.fillStyle=d,t.beginPath(),t.arc(b-v*.1+x*v*.5,y-f*.16+W*f*.42,v*ie,0,Math.PI*2),t.fill(),t.fillStyle=s;}t.restore();}if(t.shadowColor=s,t.shadowBlur=r*.03*a,o.happyEye>.01){let n=.6+.4*o.happyEye;t.globalAlpha=Math.min(1,o.happyEye),t.beginPath(),t.arc(b,y+.045,v*1.15*n,Math.PI*1.12,Math.PI*1.88),t.stroke();}if(o.closedEye>.01){let n=.6+.4*o.closedEye;t.globalAlpha=Math.min(1,o.closedEye),t.beginPath(),t.moveTo(b-v*1.15*n,y+.01),t.quadraticCurveTo(b,y+.07*n,b+v*1.15*n,y+.01),t.stroke();}if(o.xEye>.01){let n=v*.95*(.6+.4*o.xEye);t.globalAlpha=Math.min(1,o.xEye),t.beginPath(),t.moveTo(b-n,y-n),t.lineTo(b+n,y+n),t.moveTo(b+n,y-n),t.lineTo(b-n,y+n),t.stroke();}o.heartEye>.01&&(t.globalAlpha=Math.min(1,o.heartEye),ue(t,b,y,v*1.35*(.5+.5*o.heartEye)),t.fill());}t.shadowBlur=0;let k=h*.6+o.mouthSkew*.07,w=Je+c*.6,T=Math.min(1,Math.max(0,o.mouthRound)),S=o.mouthW,R=t.createLinearGradient(k-S-.02,w,k+S+.02,w);R.addColorStop(0,p),R.addColorStop(.5,l),R.addColorStop(1,p),t.shadowColor=s,t.shadowBlur=r*.02*a;let B=(m,b,y,v)=>{t.fillStyle=u,t.fill(m),t.shadowBlur=0,b>.02&&(t.save(),t.clip(m),t.fillStyle=i,t.globalAlpha*=Math.min(1,(b-.02)*12),t.beginPath(),t.ellipse(k,y,v,b*.55,0,0,Math.PI*2),t.fill(),t.restore()),t.strokeStyle=R;let n=t.lineWidth;t.lineWidth=n*.6,t.stroke(m),t.lineWidth=n,t.shadowBlur=r*.02*a;};if(T<.99){t.globalAlpha=1-T;let m=18,b=f=>o.mouthCurve*.075*(1-f*f),y=(f,F)=>{let x=f/m*2-1,W=k+x*S,ie=w+b(x)+o.mouthSkew*x*.03+o.mouthWave*.018*Math.sin((x+1)*Math.PI*2)+F*(1-x*x);return [W,ie]},v=Math.max(0,o.mouthOpen),n=new Path2D;for(let f=0;f<=m;f++){let[F,x]=y(f,-v*.15);f?n.lineTo(F,x):n.moveTo(F,x);}if(v>.003){for(let f=m;f>=0;f--){let[F,x]=y(f,v);n.lineTo(F,x);}n.closePath(),B(n,v,y(m/2,v)[1],S*.55);}else t.strokeStyle=R,t.stroke(n);}if(T>.01){t.globalAlpha=T;let m=Math.max(.02,o.mouthOpen*.55+.01),b=new Path2D;b.ellipse(k,w+.01,m*.85,m,0,0,Math.PI*2),t.lineWidth=ce*.8,B(b,o.mouthOpen,w+.01+m*.8,m*.6),t.lineWidth=ce;}t.shadowBlur=0,t.globalAlpha=1;}function ue(t,o,e,s){t.beginPath(),t.moveTo(o,e+s*.85),t.bezierCurveTo(o-s*1.25,e+s*.05,o-s*.75,e-s*.95,o,e-s*.35),t.bezierCurveTo(o+s*.75,e-s*.95,o+s*1.25,e+s*.05,o,e+s*.85),t.closePath();}var te=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:g,cos:Ze,abs:Qe,max:Q,exp:et,PI:ee}=Math;function pe(t,o,e,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,t){case "idle":{let i=g(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*g(o*1.1),s.y=.02*g(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*g(i),s.rz=.12*Ze(i),s.ry=.22*g(i),s.y=.02*g(i*2),s.lookX=.55*g(i);break}case "listening":{let i=Q(0,g(o*3.2));s.rx=.09*i*i;let a=Qe(g(o*7.1)*.5+g(o*11.3)*.3+g(o*3.7)*.2);s.st=.03*a;break}case "speaking":{let i=Q(0,g(o*9)*.6+g(o*14.3)*.4)*(.65+.35*g(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*g(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*g(ee*i);let a=Q(0,1-i/.14)+Q(0,(i-.9)/.1);s.st=.08*g(ee*i)-.14*a,s.rz=.05*g(o*1.35*ee);break}case "surprised":{if(e<.45){let i=g(ee*e/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*g(o*38)*et(-e*2),s.rx=-0.06;break}case "sad":{let i=g(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=e%2.8;if(i<.55){let a=1-i/.55;s.ry=.26*g(i*42)*a,s.rz=.05*g(i*42+1)*a;}s.st=-0.02;break}case "sleeping":{let i=g(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*g(o*1.6),s.rz=.05*g(o*.8),s.st=.015*g(o*1.6+1);break}}return s}function Ae(t,o,e,s){for(let i of Object.keys(s))s[i]=t[i]+(o[i]-t[i])*e;return s}var Ee={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var X=new Set,E=null,q=0,Y=0,oe=0,be,Pe=false,N="idle",I={x:0,y:0,lastMove:-1e9,known:false},G={reducedMotion:false};function ot(){if(Pe)return;Pe=true,addEventListener("pointermove",o=>{I.x=o.clientX,I.y=o.clientY,I.lastMove=performance.now(),I.known=true;},{passive:true}),document.addEventListener("visibilitychange",D);let t=matchMedia("(prefers-reduced-motion: reduce)");G.reducedMotion=t.matches,t.addEventListener?.("change",o=>G.reducedMotion=o.matches);}function Fe(t,o){return E||(E=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),E.setPixelRatio(1),E.setClearColor(0,0),E.setScissorTest(true),E.domElement.addEventListener("webglcontextlost",e=>{e.preventDefault(),E?.dispose(),E=null,q=Y=0,N="idle";})),(t>q||o>Y)&&(q=Math.max(q,t),Y=Math.max(Y,o),E.setSize(q,Y,false)),E}var Re=()=>Y;function Be(t,o){if(N==="ready")return true;if(N==="idle"&&E){N="compiling";let e=E;e.compileAsync(t,o).catch(()=>{}).then(()=>{e===E&&(N="ready"),D();});}return false}function ze(t){ot(),clearTimeout(be),X.add(t),D();}function Oe(t){X.delete(t),X.size===0&&(clearTimeout(be),be=setTimeout(()=>{X.size||!E||(E.dispose(),E.forceContextLoss(),E=null,q=Y=0,N="idle");},1500));}function D(){if(!oe){for(let t of X)if(t.active){oe=requestAnimationFrame(Le);return}}}var st=10,xe=new WeakMap,me=0;function Le(t){oe=0;let o=[...X].filter(a=>a.active);if(!o.length)return;let e=performance.now(),s=o.length,i=0;for(;i<s;i++){let a=o[(me+i)%s],l=xe.get(a)??t-16;if(xe.set(a,t),a.tick(Math.min(.05,Math.max(0,(t-l)/1e3)),t),performance.now()-e>st){i++;break}}me=(me+i)%s,oe=requestAnimationFrame(Le);}var j={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 Ie(t){let o=new SphereGeometry(1,t*2,Math.round(t*1.5)),e=o.getAttribute("position"),s=o.getIndex(),i=new Map,a=[],l=new Uint32Array(e.count);for(let r=0;r<e.count;r++){let h=e.getX(r),c=e.getY(r),C=e.getZ(r),A=`${Math.round(h*1e4)},${Math.round(c*1e4)},${Math.round(C*1e4)}`,k=i.get(A);if(k===void 0){k=a.length/3,i.set(A,k);let w=Math.hypot(h,c,C)||1;a.push(h/w,c/w,C/w);}l[r]=k;}let d=[];for(let r=0;r<s.count;r+=3){let h=l[s.getX(r)],c=l[s.getX(r+1)],C=l[s.getX(r+2)];h!==c&&c!==C&&h!==C&&d.push(h,c,C);}o.dispose();let u=new BufferGeometry,p=new Float32Array(a);return u.setAttribute("position",new BufferAttribute(new Float32Array(p),3)),u.setIndex(d),{geometry:u,dirs:p}}var nt=(t,o,e)=>{let s=Math.min(1,Math.max(0,(e-t)/(o-t)));return s*s*(3-2*s)};function fe(t,o,e,s,i,a,l){let d=o,u=e,p=s;switch(t){case "pebble":{d=o*1.1+.04*e,u=e*.92*(.95+.05*Math.tanh(3*e))+.03*o,p=s*.94;break}case "blob":{let r=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*e+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);d=o*r,u=e*r,p=s*r;break}case "drop":{let r=Math.max(e,0),h=1-.8*Math.pow(r,1.6);d=o*h*.98+.1*r*r*r,u=(e<0?e*.9:e*1.32)-.12,p=s*h*.98;break}case "bean":{let r=e*.78+.42*Math.tanh(e*3);d=(o*.8+.14*(r*r/1.44)-.07)*.9,u=r*.88,p=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(e),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);d=o*h*.84,u=e*h*.82,p=s*h*.84;break}case "ghost":{if(e>=0)d=o,u=e,p=s;else {let h=(Math.acos(Math.max(-1,e))-Math.PI/2)/(Math.PI/2),c=Math.hypot(o,s)||1,C=o/c,A=s/c,k=Math.atan2(s,o),w=.085*Math.sin(5*k+2.2*i),T=1.04,S=.13,R=-1.05+w,B;if(h<.45){let m=h/.45;B=1+.04*m,u=-m*(1.05-S)+w*nt(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);B=T-S+S*Math.cos(m),u=R+S-S*Math.sin(m);}else {let m=(h-.75)/.25;B=(T-S)*(1-m),u=R+.22*m*m*(3-2*m);}d=C*B,p=A*B;}d*=.9,u=(u+.1)*.9,p*=.9;break}}a[l]=d,a[l+1]=u,a[l+2]=p;}var ve=-0.85,De=new Map;function gt(t){let o=De.get(t);if(!o){let{geometry:e,dirs:s}=Ie(t);o={dirs:s,index:new Uint32Array(e.getIndex().array)},e.dispose(),De.set(t,o);}return o}var V=(t,o)=>1-Math.exp(-t*o),We=t=>1-Math.pow(1-t,3),se=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=te();this.poseA=te();this.poseB=te();this.face=de(Z.idle);this.target=de(Z.idle);this.drawn=new Float32Array($.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=_(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=U(this.body);this.faceCT=this.faceC;this.cheek=J(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.css=[0,0];this.quality=1;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=-ve,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 e=new DirectionalLight(16770770,1.4);e.position.set(-3.5,2,-3);let s=new DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,e,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=e=>{Object.assign(e.uniforms,this.uniforms),e.vertexShader=e.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 Ne=["ghost","pebble","blob","drop","bean","cube"],Xe=["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},wt={Iris:"#6c5ce7",Peach:"#f2a07b",Sage:"#8fb996",Sky:"#8ec5e8",Lilac:"#b9a3e3",Butter:"#f3d36b",Coral:"#f07167",Ink:"#2b2d42",Cloud:"#ecebe8"},St={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."}},Ct={pebble:"Pebble",blob:"Blob",drop:"Drop",bean:"Bean",cube:"Cube",ghost:"Ghost"},re=t=>Ne.includes(t),ae=t=>Xe.includes(t);var z=null;function _(t,o){if(!t)return o;let e=/^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(t.trim());if(e)return Se(e[1]);if(z||(z=document.createElement("canvas").getContext("2d")),!z)return o;z.fillStyle="#010203",z.fillStyle=t;let s=String(z.fillStyle);if(s==="#010203"&&t.trim()!=="#010203")return o;if(s.startsWith("#"))return Se(s.slice(1));z.canvas.width=z.canvas.height=1,z.clearRect(0,0,1,1),z.fillRect(0,0,1,1);let i=z.getImageData(0,0,1,1).data;return [i[0]/255,i[1]/255,i[2]/255]}function Se(t){t.length===3&&(t=t[0]+t[0]+t[1]+t[1]+t[2]+t[2]);let o=parseInt(t,16);return [(o>>16&255)/255,(o>>8&255)/255,(o&255)/255]}var O=t=>`rgb(${Math.round(t[0]*255)},${Math.round(t[1]*255)},${Math.round(t[2]*255)})`,P=(t,o,e)=>[t[0]+(o[0]-t[0])*e,t[1]+(o[1]-t[1])*e,t[2]+(o[2]-t[2])*e],ne=t=>t<=.04045?t/12.92:Math.pow((t+.055)/1.055,2.4),he=t=>.2126*ne(t[0])+.7152*ne(t[1])+.0722*ne(t[2]),Ve=[.07,.06,.08],He=[1,.97,.93],$e=[1,.42,.48],U=t=>he(t)>.06?P(Ve,t,.06):He,J=t=>P(t,$e,.78);var $=["eyeOpen","eyeSize","lookX","lookY","lidCover","lidTilt","happyEye","closedEye","xEye","heartEye","mouthW","mouthCurve","mouthOpen","mouthRound","mouthSkew","mouthWave","blush"],L={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},Z={idle:{...L},thinking:{...L,eyeOpen:.92,lookX:.7,lookY:-0.8,lidCover:.12,mouthW:.065,mouthCurve:0,mouthSkew:.8,blush:.3},loading:{...L,lidCover:.48,mouthW:.06,mouthCurve:.12,blush:.3},listening:{...L,eyeOpen:1.12,eyeSize:1.16,mouthW:.06,mouthCurve:.2,mouthOpen:.02},speaking:{...L,mouthW:.09,mouthCurve:.25},happy:{...L,happyEye:1,mouthW:.13,mouthCurve:1,mouthOpen:.08,blush:1},surprised:{...L,eyeOpen:1.25,eyeSize:1.22,mouthW:.06,mouthCurve:0,mouthOpen:.12,mouthRound:1,blush:.35},sad:{...L,eyeOpen:.95,lidCover:.32,lidTilt:.5,lookY:.45,mouthW:.08,mouthCurve:-0.75,blush:.2},error:{...L,xEye:1,mouthW:.1,mouthCurve:-0.2,mouthWave:1,blush:.15},sleeping:{...L,closedEye:1,mouthW:.04,mouthCurve:0,mouthOpen:.035,mouthRound:.7,blush:.55},love:{...L,heartEye:1,mouthW:.11,mouthCurve:.9,mouthOpen:.03,blush:1}},Ce={idle:1,thinking:.15,loading:0,listening:1,speaking:.9,happy:.6,surprised:.8,sad:.3,error:.3,sleeping:0,love:.7},de=t=>({...t}),je=.31,Ke=-0.04,Ue=.098,le=.128,Je=.2,ce=.05;function Te(t,o,e,s,i,a,l,d,u,p){let r=t.canvas.width;t.setTransform(1,0,0,1,0,0),t.clearRect(0,0,r,r),t.setTransform(r/2,0,0,r/2,r/2,r/2),t.lineCap="round",t.lineJoin="round";let h=o.lookX*.075,c=o.lookY*.06;if(o.blush>.01){let m=r*.018;t.save(),"filter"in t&&(t.filter=`blur(${m}px)`);for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4,f=t.createRadialGradient(v,n,0,v,n,.16);f.addColorStop(0,i),f.addColorStop(1,"rgba(0,0,0,0)"),t.globalAlpha=Math.min(1,o.blush)*.62,t.fillStyle=f,t.beginPath(),t.ellipse(v,n,.17,.12,0,0,Math.PI*2),t.fill();}t.restore();let b=Math.min(1,Math.max(0,(o.blush-.6)/.4));if(b>.01){t.save(),t.globalAlpha=b*.55,t.strokeStyle=i,t.lineWidth=.016,t.lineCap="round";for(let y of [-1,1]){let v=y*.52+h*.4,n=.16+c*.4;for(let f=-1;f<=1;f++){let F=v+f*.045;t.beginPath(),t.moveTo(F+.018,n-.035),t.lineTo(F-.018,n+.035),t.stroke();}}t.restore();}}t.fillStyle=s,t.strokeStyle=s,t.lineWidth=ce;let A=1-Math.min(1,o.happyEye+o.closedEye+o.xEye+o.heartEye);for(let m of [-1,1]){let b=m*je+h,y=Ke+c,v=Ue*o.eyeSize;if(A>.01){t.globalAlpha=A;let n=Math.max(.06,o.eyeOpen*e),f=le*o.eyeSize*n;if(t.save(),o.lidCover>.01||o.lidTilt>.01){let W=y-le*o.eyeSize+2*le*o.eyeSize*o.lidCover;t.translate(b,W),t.rotate(m*o.lidTilt*.6),t.beginPath(),t.rect(-1,0,2,2),t.clip(),t.rotate(-m*o.lidTilt*.6),t.translate(-b,-W);}t.shadowColor=s,t.shadowBlur=r*.035*a;let F=t.createRadialGradient(b,y-f*.2,0,b,y,Math.max(v,f));if(F.addColorStop(0,l),F.addColorStop(1,s),t.fillStyle=F,t.beginPath(),t.ellipse(b,y,v,f,0,0,Math.PI*2),t.fill(),t.shadowBlur=0,t.fillStyle=s,n>.35){let x=Math.max(-1,Math.min(1,o.lookX)),W=Math.max(-1,Math.min(1,o.lookY)),ie=d==="#fff"?.3:.34;t.globalAlpha=A*Math.min(1,(n-.35)*3),t.fillStyle=d,t.beginPath(),t.arc(b-v*.1+x*v*.5,y-f*.16+W*f*.42,v*ie,0,Math.PI*2),t.fill(),t.fillStyle=s;}t.restore();}if(t.shadowColor=s,t.shadowBlur=r*.03*a,o.happyEye>.01){let n=.6+.4*o.happyEye;t.globalAlpha=Math.min(1,o.happyEye),t.beginPath(),t.arc(b,y+.045,v*1.15*n,Math.PI*1.12,Math.PI*1.88),t.stroke();}if(o.closedEye>.01){let n=.6+.4*o.closedEye;t.globalAlpha=Math.min(1,o.closedEye),t.beginPath(),t.moveTo(b-v*1.15*n,y+.01),t.quadraticCurveTo(b,y+.07*n,b+v*1.15*n,y+.01),t.stroke();}if(o.xEye>.01){let n=v*.95*(.6+.4*o.xEye);t.globalAlpha=Math.min(1,o.xEye),t.beginPath(),t.moveTo(b-n,y-n),t.lineTo(b+n,y+n),t.moveTo(b+n,y-n),t.lineTo(b-n,y+n),t.stroke();}o.heartEye>.01&&(t.globalAlpha=Math.min(1,o.heartEye),ue(t,b,y,v*1.35*(.5+.5*o.heartEye)),t.fill());}t.shadowBlur=0;let k=h*.6+o.mouthSkew*.07,w=Je+c*.6,T=Math.min(1,Math.max(0,o.mouthRound)),S=o.mouthW,R=t.createLinearGradient(k-S-.02,w,k+S+.02,w);R.addColorStop(0,p),R.addColorStop(.5,l),R.addColorStop(1,p),t.shadowColor=s,t.shadowBlur=r*.02*a;let B=(m,b,y,v)=>{t.fillStyle=u,t.fill(m),t.shadowBlur=0,b>.02&&(t.save(),t.clip(m),t.fillStyle=i,t.globalAlpha*=Math.min(1,(b-.02)*12),t.beginPath(),t.ellipse(k,y,v,b*.55,0,0,Math.PI*2),t.fill(),t.restore()),t.strokeStyle=R;let n=t.lineWidth;t.lineWidth=n*.6,t.stroke(m),t.lineWidth=n,t.shadowBlur=r*.02*a;};if(T<.99){t.globalAlpha=1-T;let m=18,b=f=>o.mouthCurve*.075*(1-f*f),y=(f,F)=>{let x=f/m*2-1,W=k+x*S,ie=w+b(x)+o.mouthSkew*x*.03+o.mouthWave*.018*Math.sin((x+1)*Math.PI*2)+F*(1-x*x);return [W,ie]},v=Math.max(0,o.mouthOpen),n=new Path2D;for(let f=0;f<=m;f++){let[F,x]=y(f,-v*.15);f?n.lineTo(F,x):n.moveTo(F,x);}if(v>.003){for(let f=m;f>=0;f--){let[F,x]=y(f,v);n.lineTo(F,x);}n.closePath(),B(n,v,y(m/2,v)[1],S*.55);}else t.strokeStyle=R,t.stroke(n);}if(T>.01){t.globalAlpha=T;let m=Math.max(.02,o.mouthOpen*.55+.01),b=new Path2D;b.ellipse(k,w+.01,m*.85,m,0,0,Math.PI*2),t.lineWidth=ce*.8,B(b,o.mouthOpen,w+.01+m*.8,m*.6),t.lineWidth=ce;}t.shadowBlur=0,t.globalAlpha=1;}function ue(t,o,e,s){t.beginPath(),t.moveTo(o,e+s*.85),t.bezierCurveTo(o-s*1.25,e+s*.05,o-s*.75,e-s*.95,o,e-s*.35),t.bezierCurveTo(o+s*.75,e-s*.95,o+s*1.25,e+s*.05,o,e+s*.85),t.closePath();}var te=()=>({y:0,rx:0,ry:0,rz:0,st:0,mouth:0,lookX:0,lookY:0}),{sin:g,cos:Ze,abs:Qe,max:Q,exp:et,PI:ee}=Math;function pe(t,o,e,s){switch(s.y=s.rx=s.ry=s.rz=s.st=s.mouth=s.lookX=s.lookY=0,t){case "idle":{let i=g(o*1.6);s.y=.015*i,s.st=.018*i;break}case "thinking":{s.rz=.07*g(o*1.1),s.y=.02*g(o*2.2),s.ry=.15,s.rx=-0.06;break}case "loading":{let i=o*2.4;s.rx=.12*g(i),s.rz=.12*Ze(i),s.ry=.22*g(i),s.y=.02*g(i*2),s.lookX=.55*g(i);break}case "listening":{let i=Q(0,g(o*3.2));s.rx=.09*i*i;let a=Qe(g(o*7.1)*.5+g(o*11.3)*.3+g(o*3.7)*.2);s.st=.03*a;break}case "speaking":{let i=Q(0,g(o*9)*.6+g(o*14.3)*.4)*(.65+.35*g(o*1.7));s.mouth=.11*i,s.st=.04*i,s.rz=.03*g(o*2.1);break}case "happy":{let i=o*1.35%1;s.y=.2*g(ee*i);let a=Q(0,1-i/.14)+Q(0,(i-.9)/.1);s.st=.08*g(ee*i)-.14*a,s.rz=.05*g(o*1.35*ee);break}case "surprised":{if(e<.45){let i=g(ee*e/.45);s.y=.16*i,s.st=.12*i;}s.rz=.01*g(o*38)*et(-e*2),s.rx=-0.06;break}case "sad":{let i=g(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=e%2.8;if(i<.55){let a=1-i/.55;s.ry=.26*g(i*42)*a,s.rz=.05*g(i*42+1)*a;}s.st=-0.02;break}case "sleeping":{let i=g(o*1);s.st=.04*i,s.y=-0.03,s.rz=.1,s.rx=.1;break}case "love":{s.y=.07+.06*g(o*1.6),s.rz=.05*g(o*.8),s.st=.015*g(o*1.6+1);break}}return s}function Ae(t,o,e,s){for(let i of Object.keys(s))s[i]=t[i]+(o[i]-t[i])*e;return s}var Ee={thinking:"dots",sleeping:"zz",love:"hearts",listening:"waves",surprised:"bang"};var X=new Set,E=null,q=0,Y=0,oe=0,be,Pe=false,N="idle",I={x:0,y:0,lastMove:-1e9,known:false},G={reducedMotion:false};function ot(){if(Pe)return;Pe=true,addEventListener("pointermove",o=>{I.x=o.clientX,I.y=o.clientY,I.lastMove=performance.now(),I.known=true;},{passive:true}),document.addEventListener("visibilitychange",D);let t=matchMedia("(prefers-reduced-motion: reduce)");G.reducedMotion=t.matches,t.addEventListener?.("change",o=>G.reducedMotion=o.matches);}function Fe(t,o){return E||(E=new WebGLRenderer({antialias:true,alpha:true,powerPreference:"low-power",preserveDrawingBuffer:true}),E.setPixelRatio(1),E.setClearColor(0,0),E.setScissorTest(true),E.domElement.addEventListener("webglcontextlost",e=>{e.preventDefault(),E?.dispose(),E=null,q=Y=0,N="idle";})),(t>q||o>Y)&&(q=Math.max(q,t),Y=Math.max(Y,o),E.setSize(q,Y,false)),E}var Re=()=>Y;function Be(t,o){if(N==="ready")return true;if(N==="idle"&&E){N="compiling";let e=E;e.compileAsync(t,o).catch(()=>{}).then(()=>{e===E&&(N="ready"),D();});}return false}function ze(t){ot(),clearTimeout(be),X.add(t),D();}function Oe(t){X.delete(t),X.size===0&&(clearTimeout(be),be=setTimeout(()=>{X.size||!E||(E.dispose(),E.forceContextLoss(),E=null,q=Y=0,N="idle");},1500));}function D(){if(!oe){for(let t of X)if(t.active){oe=requestAnimationFrame(Le);return}}}var st=10,xe=new WeakMap,me=0;function Le(t){oe=0;let o=[...X].filter(a=>a.active);if(!o.length)return;let e=performance.now(),s=o.length,i=0;for(;i<s;i++){let a=o[(me+i)%s],l=xe.get(a)??t-16;if(xe.set(a,t),a.tick(Math.min(.05,Math.max(0,(t-l)/1e3)),t),performance.now()-e>st){i++;break}}me=(me+i)%s,oe=requestAnimationFrame(Le);}var j={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 Ie(t){let o=new SphereGeometry(1,t*2,Math.round(t*1.5)),e=o.getAttribute("position"),s=o.getIndex(),i=new Map,a=[],l=new Uint32Array(e.count);for(let r=0;r<e.count;r++){let h=e.getX(r),c=e.getY(r),C=e.getZ(r),A=`${Math.round(h*1e4)},${Math.round(c*1e4)},${Math.round(C*1e4)}`,k=i.get(A);if(k===void 0){k=a.length/3,i.set(A,k);let w=Math.hypot(h,c,C)||1;a.push(h/w,c/w,C/w);}l[r]=k;}let d=[];for(let r=0;r<s.count;r+=3){let h=l[s.getX(r)],c=l[s.getX(r+1)],C=l[s.getX(r+2)];h!==c&&c!==C&&h!==C&&d.push(h,c,C);}o.dispose();let u=new BufferGeometry,p=new Float32Array(a);return u.setAttribute("position",new BufferAttribute(new Float32Array(p),3)),u.setIndex(d),{geometry:u,dirs:p}}var nt=(t,o,e)=>{let s=Math.min(1,Math.max(0,(e-t)/(o-t)));return s*s*(3-2*s)};function fe(t,o,e,s,i,a,l){let d=o,u=e,p=s;switch(t){case "pebble":{d=o*1.1+.04*e,u=e*.92*(.95+.05*Math.tanh(3*e))+.03*o,p=s*.94;break}case "blob":{let r=.97+.045*Math.sin(2.1*o+1.3*i)*Math.sin(2.4*e+1.7*i+1)+.035*Math.sin(2.7*s+2.3*o+1.1*i);d=o*r,u=e*r,p=s*r;break}case "drop":{let r=Math.max(e,0),h=1-.8*Math.pow(r,1.6);d=o*h*.98+.1*r*r*r,u=(e<0?e*.9:e*1.32)-.12,p=s*h*.98;break}case "bean":{let r=e*.78+.42*Math.tanh(e*3);d=(o*.8+.14*(r*r/1.44)-.07)*.9,u=r*.88,p=s*.8*.9;break}case "cube":{let h=Math.pow(Math.pow(Math.abs(o),4.5)+Math.pow(Math.abs(e),4.5)+Math.pow(Math.abs(s),4.5),-0.2222222222222222);d=o*h*.84,u=e*h*.82,p=s*h*.84;break}case "ghost":{if(e>=0)d=o,u=e,p=s;else {let h=(Math.acos(Math.max(-1,e))-Math.PI/2)/(Math.PI/2),c=Math.hypot(o,s)||1,C=o/c,A=s/c,k=Math.atan2(s,o),w=.085*Math.sin(5*k+2.2*i),T=1.04,S=.13,R=-1.05+w,B;if(h<.45){let m=h/.45;B=1+.04*m,u=-m*(1.05-S)+w*nt(.45,1,m);}else if(h<.75){let m=(h-.45)/.3*(Math.PI/2);B=T-S+S*Math.cos(m),u=R+S-S*Math.sin(m);}else {let m=(h-.75)/.25;B=(T-S)*(1-m),u=R+.22*m*m*(3-2*m);}d=C*B,p=A*B;}d*=.9,u=(u+.1)*.9,p*=.9;break}}a[l]=d,a[l+1]=u,a[l+2]=p;}var ve=-0.85,De=new Map;function gt(t){let o=De.get(t);if(!o){let{geometry:e,dirs:s}=Ie(t);o={dirs:s,index:new Uint32Array(e.getIndex().array)},e.dispose(),De.set(t,o);}return o}var V=(t,o)=>1-Math.exp(-t*o),We=t=>1-Math.pow(1-t,3),se=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=te();this.poseA=te();this.poseB=te();this.face=de(Z.idle);this.target=de(Z.idle);this.drawn=new Float32Array($.length+7).fill(NaN);this.blink=1;this.blinkT=-1;this.nextBlink=2+Math.random()*3;this.body=_(M.color,[.95,.63,.48]);this.bodyT=this.body;this.faceC=U(this.body);this.faceCT=this.faceC;this.cheek=J(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.css=[0,0];this.quality=1;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=-ve,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 e=new DirectionalLight(16770770,1.4);e.position.set(-3.5,2,-3);let s=new DirectionalLight(16773350,.45);s.position.set(-2,-1,3),this.scene.add(o,e,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=e=>{Object.assign(e.uniforms,this.uniforms),e.vertexShader=e.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;
|