import{a,j as o,R as F}from"./react-CYZQF_UV.js";import{d as T,b as B,V as O,i as P,a as Y,a9 as t,v as _,aa as I,ab as A}from"./three-Cs75aIhT.js";import"./BroadcasterLive-D6NMA-d_.js";import"./livekit-IBzVVcEu.js";import"./main-CEJ7cS7B.js";import"./index-Ec65i0Q1.js";import"./web3-D0IbNXhH.js";const U={neutral:t.Neutral,excited:t.Happy,serious:t.Angry,victory:t.Happy,danger:t.Surprised},D=[t.Happy,t.Angry,t.Sad,t.Relaxed,t.Surprised],N=[t.Aa,t.Ih,t.Ou,t.Ee,t.Oh];function W(u){const[m,g]=F.useState(null);return a.useEffect(()=>{let l=!1;const e=new _;return e.register(s=>new I(s)),e.load(u,s=>{if(l)return;const f=s.userData.vrm;if(!f){console.warn("[VrmAvatarHost] No vrm in gltf userData",u);return}A.removeUnnecessaryVertices(s.scene),A.combineSkeletons(s.scene),f.scene.traverse(v=>{v.frustumCulled=!1}),A.rotateVRM0(f),g(f)},void 0,s=>console.warn("[VrmAvatarHost] load error",u,s)),()=>{l=!0}},[u]),m}const z=({mouthOpenAmount:u,expression:m,avatarUrl:g})=>{const l=g??"/avatars/host.vrm",e=W(l),{scene:s,camera:f}=B(),v=a.useRef(0),R=a.useRef(0),y=a.useRef(0),V=a.useRef(0),S=a.useRef(0),w=a.useRef(0);return a.useEffect(()=>{var x,i,p;if(!e)return;s.add(e.scene);try{const n=e.humanoid;if(n){const r=(b,H,L,k)=>{const j=n.getNormalizedBoneNode(b);j&&j.rotation.set(H,L,k)};r("leftUpperArm",0,0,-1.25),r("rightUpperArm",0,0,1.25),r("leftLowerArm",0,.15,-.05),r("rightLowerArm",0,-.15,.05),r("leftHand",0,0,0),r("rightHand",0,0,0),r("leftShoulder",0,0,-.05),r("rightShoulder",0,0,.05)}}catch(n){console.warn("[VrmAvatarHost] pose error",n)}let E=null;try{const n=((x=e.humanoid)==null?void 0:x.getNormalizedBoneNode("head"))??((p=(i=e.humanoid)==null?void 0:i.getRawBoneNode)==null?void 0:p.call(i,"head"));if(n){const r=new O;n.getWorldPosition(r),E=r.y}}catch{}e.scene.updateMatrixWorld(!0);const c=new P().setFromObject(e.scene),d=Math.max(.1,c.max.y-c.min.y),M=E??c.min.y+d*.92,h=-c.min.y-d/2;return e.scene.position.set(0,h,0),w.current=h,f.lookAt(0,0,0),console.log("[VrmAvatarHost] loaded",l,{height:d.toFixed(2),headY:M.toFixed(2),offsetY:h.toFixed(2)}),()=>{s.remove(e.scene)}},[e,s,f,l]),Y((E,c)=>{var x;if(!e)return;v.current+=(u-v.current)*Math.min(1,c*18);const d=v.current;S.current+=c,S.current>.12&&d>.15&&(V.current=(V.current+1)%N.length,S.current=0),N.forEach((i,p)=>{var r;const n=p===V.current?d:0;try{(r=e.expressionManager)==null||r.setValue(i,n)}catch{}});const M=U[m];D.forEach(i=>{var n;const p=i===M?.85:0;try{(n=e.expressionManager)==null||n.setValue(i,p)}catch{}}),R.current-=c,R.current<=0&&y.current===0&&(y.current=1,setTimeout(()=>{y.current=0},130),R.current=2+Math.random()*3);try{(x=e.expressionManager)==null||x.setValue(t.Blink,y.current)}catch{}const h=performance.now()/1e3;e.scene.position.y=w.current+Math.sin(h*1.4)*.012,e.scene.rotation.y=Math.sin(h*.6)*.05,e.update(c)}),null},Q=({mouthOpenAmount:u,expression:m,avatarUrl:g})=>{const l=a.useMemo(()=>o.jsxs(o.Fragment,{children:[o.jsx("ambientLight",{intensity:.85,color:"#ffffff"}),o.jsx("directionalLight",{position:[1,2,1.5],intensity:1.1,color:"#ffffff"}),o.jsx("directionalLight",{position:[-1.5,1,1],intensity:.55,color:"#ffe6c8"}),o.jsx("directionalLight",{position:[0,-1,1],intensity:.25,color:"#ffffff"})]}),[]);return o.jsx("div",{className:`avatar-host avatar-host--${m}`,children:o.jsxs(T,{gl:{alpha:!0,antialias:!0,preserveDrawingBuffer:!1},camera:{position:[0,0,2.4],fov:36},style:{background:"transparent",width:"100%",height:"100%"},children:[l,o.jsx(a.Suspense,{fallback:null,children:o.jsx(z,{mouthOpenAmount:u,expression:m,avatarUrl:g})})]})})};export{Q as VrmAvatarHost,Q as default};
