/*! (c) 2019-2026 pixiv Inc. - https://github.com/pixiv/three-vrm/blob/release/LICENSE */ var K=(o,e,t)=>new Promise((n,r)=>{var i=l=>{try{a(t.next(l))}catch(u){r(u)}},s=l=>{try{a(t.throw(l))}catch(u){r(u)}},a=l=>l.done?n(l.value):Promise.resolve(l.value).then(i,s);a((t=t.apply(o,e)).next())});import*as X from"three";import*as ye from"three";import*as U from"three";import*as ne from"three";import*as we from"three";import*as b from"three";import*as O from"three";import*as $ from"three";import*as F from"three";import*as T from"three";import*as He from"three";import*as oe from"three";import*as M from"three";import*as ie from"three";import*as R from"three";import*as te from"three";import*as rt from"three";var V=(o,e,t)=>new Promise((n,r)=>{var i=l=>{try{a(t.next(l))}catch(u){r(u)}},s=l=>{try{a(t.throw(l))}catch(u){r(u)}},a=l=>l.done?n(l.value):Promise.resolve(l.value).then(i,s);a((t=t.apply(o,e)).next())}),he=class extends ye.Object3D{constructor(o){super(),this.weight=0,this.isBinary=!1,this.overrideBlink="none",this.overrideLookAt="none",this.overrideMouth="none",this._binds=[],this.name=`VRMExpression_${o}`,this.expressionName=o,this.type="VRMExpression",this.visible=!1}get binds(){return this._binds}get overrideBlinkAmount(){return this.overrideBlink==="block"?0.5?1:0:this.weight}addBind(o){this._binds.push(o)}deleteBind(o){let e=this._binds.indexOf(o);e>=0&&this._binds.splice(e,1)}applyWeight(o){var e;let t=this.outputWeight;t*=(e=o==null?void 0:o.multiplier)!=null?e:1,this.isBinary&&t<1&&(t=0),this._binds.forEach(n=>n.applyWeight(t))}clearAppliedWeight(){this._binds.forEach(o=>o.clearAppliedWeight())}};function Ce(o,e,t){var n,r;let i=o.parser.json,s=(n=i.nodes)==null?void 0:n[e];if(s==null)return console.warn(`extractPrimitivesInternal: Attempt to use nodes[${e}] of glTF but the node doesn't exist`),null;let a=s.mesh;if(a==null)return null;let l=(r=i.meshes)==null?void 0:r[a];if(l==null)return console.warn(`extractPrimitivesInternal: Attempt to use meshes[${a}] of glTF but the mesh doesn't exist`),null;let u=l.primitives.length,d=[];return t.traverse(f=>{d.length{t.has(n)&&(e[n]=r)}),e}get customExpressionMap(){let e={},t=new Set(Object.values(z));return Object.entries(this._expressionMap).forEach(([n,r])=>{t.has(n)||(e[n]=r)}),e}copy(e){return this._expressions.concat().forEach(n=>{this.unregisterExpression(n)}),e._expressions.forEach(n=>{this.registerExpression(n)}),this.blinkExpressionNames=e.blinkExpressionNames.concat(),this.lookAtExpressionNames=e.lookAtExpressionNames.concat(),this.mouthExpressionNames=e.mouthExpressionNames.concat(),this}clone(){return new ve().copy(this)}getExpression(e){var t;return(t=this._expressionMap[e])!=null?t:null}registerExpression(e){this._expressions.push(e),this._expressionMap[e.expressionName]=e}unregisterExpression(e){let t=this._expressions.indexOf(e);t===-1&&console.warn("VRMExpressionManager: The specified expressions is not registered"),this._expressions.splice(t,1),delete this._expressionMap[e.expressionName]}getValue(e){var t;let n=this.getExpression(e);return(t=n==null?void 0:n.weight)!=null?t:null}setValue(e,t){let n=this.getExpression(e);n&&(n.weight=Fe(t))}resetValues(){this._expressions.forEach(e=>{e.weight=0})}getExpressionTrackName(e){let t=this.getExpression(e);return t?`${t.name}.weight`:null}update(){let e=this._calculateWeightMultipliers();this._expressions.forEach(t=>{t.clearAppliedWeight()}),this._expressions.forEach(t=>{let n=1,r=t.expressionName;this.blinkExpressionNames.indexOf(r)!==-1&&(n*=e.blink),this.lookAtExpressionNames.indexOf(r)!==-1&&(n*=e.lookAt),this.mouthExpressionNames.indexOf(r)!==-1&&(n*=e.mouth),t.applyWeight({multiplier:n})})}_calculateWeightMultipliers(){let e=1,t=1,n=1;return this._expressions.forEach(r=>{e-=r.overrideBlinkAmount,t-=r.overrideLookAtAmount,n-=r.overrideMouthAmount}),e=Math.max(0,e),t=Math.max(0,t),n=Math.max(0,n),{blink:e,lookAt:t,mouth:n}}},Q={Color:"color",EmissionColor:"emissionColor",ShadeColor:"shadeColor",MatcapColor:"matcapColor",RimColor:"rimColor",OutlineColor:"outlineColor"},We={_Color:Q.Color,_EmissionColor:Q.EmissionColor,_ShadeColor:Q.ShadeColor,_RimColor:Q.RimColor,_OutlineColor:Q.OutlineColor},Qe=new ne.Color,xe=class Te{constructor({material:e,type:t,targetValue:n,targetAlpha:r}){this.material=e,this.type=t,this.targetValue=n,this.targetAlpha=r!=null?r:1;let i=this._initColorBindState(),s=this._initAlphaBindState();this._state={color:i,alpha:s}}applyWeight(e){let{color:t,alpha:n}=this._state;if(t!=null){let{propertyName:r,deltaValue:i}=t,s=this.material[r];s!=null&&s.add(Qe.copy(i).multiplyScalar(e))}if(n!=null){let{propertyName:r,deltaValue:i}=n;this.material[r]!=null&&(this.material[r]+=i*e)}}clearAppliedWeight(){let{color:e,alpha:t}=this._state;if(e!=null){let{propertyName:n,initialValue:r}=e,i=this.material[n];i!=null&&i.copy(r)}if(t!=null){let{propertyName:n,initialValue:r}=t;this.material[n]!=null&&(this.material[n]=r)}}_initColorBindState(){var e,t,n;let{material:r,type:i,targetValue:s}=this,a=this._getPropertyNameMap(),l=(t=(e=a==null?void 0:a[i])==null?void 0:e[0])!=null?t:null;if(l==null)return console.warn(`Tried to add a material color bind to the material ${(n=r.name)!=null?n:"(no name)"}, the type ${i} but the material or the type is not supported.`),null;let d=r[l].clone(),f=new ne.Color(s.r-d.r,s.g-d.g,s.b-d.b);return{propertyName:l,initialValue:d,deltaValue:f}}_initAlphaBindState(){var e,t,n;let{material:r,type:i,targetAlpha:s}=this,a=this._getPropertyNameMap(),l=(t=(e=a==null?void 0:a[i])==null?void 0:e[1])!=null?t:null;if(l==null&&s!==1)return console.warn(`Tried to add a material alpha bind to the material ${(n=r.name)!=null?n:"(no name)"}, the type ${i} but the material or the type does not support alpha.`),null;if(l==null)return null;let u=r[l],d=s-u;return{propertyName:l,initialValue:u,deltaValue:d}}_getPropertyNameMap(){var e,t;return(t=(e=Object.entries(Te._propertyNameMapMap).find(([n])=>this.material[n]===!0))==null?void 0:e[1])!=null?t:null}};xe._propertyNameMapMap={isMeshStandardMaterial:{color:["color","opacity"],emissionColor:["emissive",null]},isMeshBasicMaterial:{color:["color","opacity"]},isMToonMaterial:{color:["color","opacity"],emissionColor:["emissive",null],outlineColor:["outlineColorFactor",null],matcapColor:["matcapFactor",null],rimColor:["parametricRimColorFactor",null],shadeColor:["shadeColorFactor",null]}};var pe=xe,me=class{constructor({primitives:o,index:e,weight:t}){this.primitives=o,this.index=e,this.weight=t}applyWeight(o){this.primitives.forEach(e=>{var t;((t=e.morphTargetInfluences)==null?void 0:t[this.index])!=null&&(e.morphTargetInfluences[this.index]+=this.weight*o)})}clearAppliedWeight(){this.primitives.forEach(o=>{var e;((e=o.morphTargetInfluences)==null?void 0:e[this.index])!=null&&(o.morphTargetInfluences[this.index]=0)})}},fe=new we.Vector2,Pe=class Ae{constructor({material:e,scale:t,offset:n}){var r,i;this.material=e,this.scale=t,this.offset=n;let s=(r=Object.entries(Ae._propertyNamesMap).find(([a])=>e[a]===!0))==null?void 0:r[1];s==null?(console.warn(`Tried to add a texture transform bind to the material ${(i=e.name)!=null?i:"(no name)"} but the material is not supported.`),this._properties=[]):(this._properties=[],s.forEach(a=>{var l;let u=(l=e[a])==null?void 0:l.clone();if(!u)return null;e[a]=u;let d=u.offset.clone(),f=u.repeat.clone(),m=n.clone().sub(d),p=t.clone().sub(f);this._properties.push({name:a,initialOffset:d,deltaOffset:m,initialScale:f,deltaScale:p})}))}applyWeight(e){this._properties.forEach(t=>{let n=this.material[t.name];n!==void 0&&(n.offset.add(fe.copy(t.deltaOffset).multiplyScalar(e)),n.repeat.add(fe.copy(t.deltaScale).multiplyScalar(e)))})}clearAppliedWeight(){this._properties.forEach(e=>{let t=this.material[e.name];t!==void 0&&(t.offset.copy(e.initialOffset),t.repeat.copy(e.initialScale))})}};Pe._propertyNamesMap={isMeshStandardMaterial:["map","emissiveMap","bumpMap","normalMap","displacementMap","roughnessMap","metalnessMap","alphaMap"],isMeshBasicMaterial:["map","specularMap","alphaMap"],isMToonMaterial:["map","normalMap","emissiveMap","shadeMultiplyTexture","rimMultiplyTexture","outlineWidthMultiplyTexture","uvAnimationMaskTexture"]};var ge=Pe,ze=new Set(["1.0","1.0-beta"]),je=class Le{get name(){return"VRMExpressionLoaderPlugin"}constructor(e){this.parser=e}afterRoot(e){return V(this,null,function*(){e.userData.vrmExpressionManager=yield this._import(e)})}_import(e){return V(this,null,function*(){let t=yield this._v1Import(e);if(t)return t;let n=yield this._v0Import(e);return n||null})}_v1Import(e){return V(this,null,function*(){var t,n;let r=this.parser.json;if(!(((t=r.extensionsUsed)==null?void 0:t.indexOf("VRMC_vrm"))!==-1))return null;let s=(n=r.extensions)==null?void 0:n.VRMC_vrm;if(!s)return null;let a=s.specVersion;if(!ze.has(a))return console.warn(`VRMExpressionLoaderPlugin: Unknown VRMC_vrm specVersion "${a}"`),null;let l=s.expressions;if(!l)return null;let u=new Set(Object.values(z)),d=new Map;l.preset!=null&&Object.entries(l.preset).forEach(([m,p])=>{if(p!=null){if(!u.has(m)){console.warn(`VRMExpressionLoaderPlugin: Unknown preset name "${m}" detected. Ignoring the expression`);return}d.set(m,p)}}),l.custom!=null&&Object.entries(l.custom).forEach(([m,p])=>{if(u.has(m)){console.warn(`VRMExpressionLoaderPlugin: Custom expression cannot have preset name "${m}". Ignoring the expression`);return}d.set(m,p)});let f=new ce;return yield Promise.all(Array.from(d.entries()).map(m=>V(this,[m],function*([p,c]){var h,g,_,E,y,v,w;let x=new he(p);if(e.scene.add(x),x.isBinary=(h=c.isBinary)!=null?h:!1,x.overrideBlink=(g=c.overrideBlink)!=null?g:"none",x.overrideLookAt=(_=c.overrideLookAt)!=null?_:"none",x.overrideMouth=(E=c.overrideMouth)!=null?E:"none",(y=c.morphTargetBinds)==null||y.forEach(P=>V(this,null,function*(){var A;if(P.node===void 0||P.index===void 0)return;let N=yield de(e,P.node),H=P.index;if(!N.every(k=>Array.isArray(k.morphTargetInfluences)&&H{let N=A.material;N&&(Array.isArray(N)?P.push(...N):P.push(N))}),(v=c.materialColorBinds)==null||v.forEach(A=>V(this,null,function*(){P.filter(H=>{var k;let D=(k=this.parser.associations.get(H))==null?void 0:k.materials;return A.material===D}).forEach(H=>{x.addBind(new pe({material:H,type:A.type,targetValue:new U.Color().fromArray(A.targetValue),targetAlpha:A.targetValue[3]}))})})),(w=c.textureTransformBinds)==null||w.forEach(A=>V(this,null,function*(){P.filter(H=>{var k;let D=(k=this.parser.associations.get(H))==null?void 0:k.materials;return A.material===D}).forEach(H=>{var k,D;x.addBind(new ge({material:H,offset:new U.Vector2().fromArray((k=A.offset)!=null?k:[0,0]),scale:new U.Vector2().fromArray((D=A.scale)!=null?D:[1,1])}))})}))}f.registerExpression(x)}))),f})}_v0Import(e){return V(this,null,function*(){var t;let n=this.parser.json,r=(t=n.extensions)==null?void 0:t.VRM;if(!r)return null;let i=r.blendShapeMaster;if(!i)return null;let s=new ce,a=i.blendShapeGroups;if(!a)return s;let l=new Set;return yield Promise.all(a.map(u=>V(this,null,function*(){var d;let f=u.presetName,m=f!=null&&Le.v0v1PresetNameMap[f]||null,p=m!=null?m:u.name;if(p==null){console.warn("VRMExpressionLoaderPlugin: One of custom expressions has no name. Ignoring the expression");return}if(l.has(p)){console.warn(`VRMExpressionLoaderPlugin: An expression preset ${f} has duplicated entries. Ignoring the expression`);return}l.add(p);let c=new he(p);e.scene.add(c),c.isBinary=(d=u.isBinary)!=null?d:!1,u.binds&&u.binds.forEach(g=>V(this,null,function*(){var _;if(g.mesh===void 0||g.index===void 0)return;let E=[];(_=n.nodes)==null||_.forEach((v,w)=>{v.mesh===g.mesh&&E.push(w)});let y=g.index;yield Promise.all(E.map(v=>V(this,null,function*(){var w;let x=yield de(e,v);if(!x.every(P=>Array.isArray(P.morphTargetInfluences)&&y{if(g.materialName===void 0||g.propertyName===void 0||g.targetValue===void 0)return;let _=[];e.scene.traverse(y=>{if(y.material){let v=y.material;Array.isArray(v)?_.push(...v.filter(w=>(w.name===g.materialName||w.name===g.materialName+" (Outline)")&&_.indexOf(w)===-1)):v.name===g.materialName&&_.indexOf(v)===-1&&_.push(v)}});let E=g.propertyName;_.forEach(y=>{if(E==="_MainTex_ST"){let w=new U.Vector2(g.targetValue[0],g.targetValue[1]),x=new U.Vector2(g.targetValue[2],g.targetValue[3]);x.y=1-x.y-w.y,c.addBind(new ge({material:y,scale:w,offset:x}));return}let v=We[E];if(v){c.addBind(new pe({material:y,type:v,targetValue:new U.Color().fromArray(g.targetValue),targetAlpha:g.targetValue[3]}));return}console.warn(E+" is not supported")})}),s.registerExpression(c)}))),s})}};je.v0v1PresetNameMap={a:"aa",e:"ee",i:"ih",o:"oh",u:"ou",blink:"blink",joy:"happy",angry:"angry",sorrow:"sad",fun:"relaxed",lookup:"lookUp",lookdown:"lookDown",lookleft:"lookLeft",lookright:"lookRight",blink_l:"blinkLeft",blink_r:"blinkRight",neutral:"neutral"};var Ve=class C{constructor(e,t){this._firstPersonOnlyLayer=C.DEFAULT_FIRSTPERSON_ONLY_LAYER,this._thirdPersonOnlyLayer=C.DEFAULT_THIRDPERSON_ONLY_LAYER,this._initializedLayers=!1,this.humanoid=e,this.meshAnnotations=t}copy(e){if(this.humanoid!==e.humanoid)throw new Error("VRMFirstPerson: humanoid must be same in order to copy");return this.meshAnnotations=e.meshAnnotations.map(t=>({meshes:t.meshes.concat(),type:t.type})),this}clone(){return new C(this.humanoid,this.meshAnnotations).copy(this)}get firstPersonOnlyLayer(){return this._firstPersonOnlyLayer}get thirdPersonOnlyLayer(){return this._thirdPersonOnlyLayer}setup({firstPersonOnlyLayer:e=C.DEFAULT_FIRSTPERSON_ONLY_LAYER,thirdPersonOnlyLayer:t=C.DEFAULT_THIRDPERSON_ONLY_LAYER}={}){this._initializedLayers||(this._firstPersonOnlyLayer=e,this._thirdPersonOnlyLayer=t,this.meshAnnotations.forEach(n=>{n.meshes.forEach(r=>{n.type==="firstPersonOnly"?(r.layers.set(this._firstPersonOnlyLayer),r.traverse(i=>i.layers.set(this._firstPersonOnlyLayer))):n.type==="thirdPersonOnly"?(r.layers.set(this._thirdPersonOnlyLayer),r.traverse(i=>i.layers.set(this._thirdPersonOnlyLayer))):n.type==="auto"&&this._createHeadlessModel(r)})}),this._initializedLayers=!0)}_excludeTriangles(e,t,n,r){let i=0;if(t!=null&&t.length>0)for(let s=0;s0&&r.includes(f[0])||d[1]>0&&r.includes(f[1])||d[2]>0&&r.includes(f[2])||d[3]>0&&r.includes(f[3]))continue;let m=t[l],p=n[l];if(m[0]>0&&r.includes(p[0])||m[1]>0&&r.includes(p[1])||m[2]>0&&r.includes(p[2])||m[3]>0&&r.includes(p[3]))continue;let c=t[u],h=n[u];c[0]>0&&r.includes(h[0])||c[1]>0&&r.includes(h[1])||c[2]>0&&r.includes(h[2])||c[3]>0&&r.includes(h[3])||(e[i++]=a,e[i++]=l,e[i++]=u)}return i}_createErasedMesh(e,t){let n=new b.SkinnedMesh(e.geometry.clone(),e.material);n.name=`${e.name}(erase)`,n.frustumCulled=e.frustumCulled,n.layers.set(this._firstPersonOnlyLayer);let r=n.geometry,i=r.getAttribute("skinIndex"),s=i instanceof b.GLBufferAttribute?[]:i.array,a=[];for(let h=0;h{this._isEraseTarget(i)&&n.push(s)}),!n.length){t.layers.enable(this._thirdPersonOnlyLayer),t.layers.enable(this._firstPersonOnlyLayer);return}t.layers.set(this._thirdPersonOnlyLayer);let r=this._createErasedMesh(t,n);e.add(r)}_createHeadlessModel(e){if(e.type==="Group")if(e.layers.set(this._thirdPersonOnlyLayer),this._isEraseTarget(e))e.traverse(t=>t.layers.set(this._thirdPersonOnlyLayer));else{let t=new b.Group;t.name=`_headless_${e.name}`,t.layers.set(this._firstPersonOnlyLayer),e.parent.add(t),e.children.filter(n=>n.type==="SkinnedMesh").forEach(n=>{let r=n;this._createHeadlessModelForSkinnedMesh(t,r)})}else if(e.type==="SkinnedMesh"){let t=e;this._createHeadlessModelForSkinnedMesh(e.parent,t)}else this._isEraseTarget(e)&&(e.layers.set(this._thirdPersonOnlyLayer),e.traverse(t=>t.layers.set(this._thirdPersonOnlyLayer)))}_isEraseTarget(e){return e===this.humanoid.getRawBoneNode("head")?!0:e.parent?this._isEraseTarget(e.parent):!1}};Ve.DEFAULT_FIRSTPERSON_ONLY_LAYER=9;Ve.DEFAULT_THIRDPERSON_ONLY_LAYER=10;var ht=new O.Vector3,dt=new O.Vector3,ct=new O.Quaternion;var re={hips:null,spine:"hips",chest:"spine",upperChest:"chest",neck:"upperChest",head:"neck",leftEye:"head",rightEye:"head",jaw:"head",leftUpperLeg:"hips",leftLowerLeg:"leftUpperLeg",leftFoot:"leftLowerLeg",leftToes:"leftFoot",rightUpperLeg:"hips",rightLowerLeg:"rightUpperLeg",rightFoot:"rightLowerLeg",rightToes:"rightFoot",leftShoulder:"upperChest",leftUpperArm:"leftShoulder",leftLowerArm:"leftUpperArm",leftHand:"leftLowerArm",rightShoulder:"upperChest",rightUpperArm:"rightShoulder",rightLowerArm:"rightUpperArm",rightHand:"rightLowerArm",leftThumbMetacarpal:"leftHand",leftThumbProximal:"leftThumbMetacarpal",leftThumbDistal:"leftThumbProximal",leftIndexProximal:"leftHand",leftIndexIntermediate:"leftIndexProximal",leftIndexDistal:"leftIndexIntermediate",leftMiddleProximal:"leftHand",leftMiddleIntermediate:"leftMiddleProximal",leftMiddleDistal:"leftMiddleIntermediate",leftRingProximal:"leftHand",leftRingIntermediate:"leftRingProximal",leftRingDistal:"leftRingIntermediate",leftLittleProximal:"leftHand",leftLittleIntermediate:"leftLittleProximal",leftLittleDistal:"leftLittleIntermediate",rightThumbMetacarpal:"rightHand",rightThumbProximal:"rightThumbMetacarpal",rightThumbDistal:"rightThumbProximal",rightIndexProximal:"rightHand",rightIndexIntermediate:"rightIndexProximal",rightIndexDistal:"rightIndexIntermediate",rightMiddleProximal:"rightHand",rightMiddleIntermediate:"rightMiddleProximal",rightMiddleDistal:"rightMiddleIntermediate",rightRingProximal:"rightHand",rightRingIntermediate:"rightRingProximal",rightRingDistal:"rightRingIntermediate",rightLittleProximal:"rightHand",rightLittleIntermediate:"rightLittleProximal",rightLittleDistal:"rightLittleIntermediate"};function qe(o){return o.invert?o.invert():o.inverse(),o}var pt=new $.Vector3,mt=new $.Quaternion;var ft=new F.Vector3,gt=new F.Quaternion,Rt=new F.Vector3;var Et=new T.Quaternion,Mt=new T.Quaternion,_t=new T.Vector3,yt=new T.Vector3,Re=Math.sqrt(2)/2,vt=new T.Quaternion(0,0,-Re,Re),xt=new T.Vector3(0,1,0);var Ye=new ie.Vector3,$e=new ie.Vector3;function ee(o,e){return o.matrixWorld.decompose(Ye,e,$e),e}function Y(o){return[Math.atan2(-o.z,o.x),Math.atan2(o.y,Math.sqrt(o.x*o.x+o.z*o.z))]}function Ee(o){let e=Math.round(o/2/Math.PI);return o-2*Math.PI*e}var Me=new M.Vector3(0,0,1),Ge=new M.Vector3,Xe=new M.Vector3,Ke=new M.Vector3,Ze=new M.Quaternion,Z=new M.Quaternion,_e=new M.Quaternion,Je=new M.Quaternion,J=new M.Euler,ke=class be{constructor(e,t){this.offsetFromHeadBone=new M.Vector3,this.autoUpdate=!0,this.faceFront=new M.Vector3(0,0,1),this.humanoid=e,this.applier=t,this._yaw=0,this._pitch=0,this._needsUpdate=!0,this._restHeadWorldQuaternion=this.getLookAtWorldQuaternion(new M.Quaternion)}get yaw(){return this._yaw}set yaw(e){this._yaw=e,this._needsUpdate=!0}get pitch(){return this._pitch}set pitch(e){this._pitch=e,this._needsUpdate=!0}get euler(){return console.warn("VRMLookAt: euler is deprecated. use getEuler() instead."),this.getEuler(new M.Euler)}getEuler(e){return e.set(M.MathUtils.DEG2RAD*this._pitch,M.MathUtils.DEG2RAD*this._yaw,0,"YXZ")}copy(e){if(this.humanoid!==e.humanoid)throw new Error("VRMLookAt: humanoid must be same in order to copy");return this.offsetFromHeadBone.copy(e.offsetFromHeadBone),this.applier=e.applier,this.autoUpdate=e.autoUpdate,this.target=e.target,this.faceFront.copy(e.faceFront),this}clone(){return new be(this.humanoid,this.applier).copy(this)}reset(){this._yaw=0,this._pitch=0,this._needsUpdate=!0}getLookAtWorldPosition(e){let t=this.humanoid.getRawBoneNode("head");return e.copy(this.offsetFromHeadBone).applyMatrix4(t.matrixWorld)}getLookAtWorldQuaternion(e){let t=this.humanoid.getRawBoneNode("head");return ee(t,e)}getFaceFrontQuaternion(e){if(this.faceFront.distanceToSquared(Me)<.01)return e.copy(this._restHeadWorldQuaternion).invert();let[t,n]=Y(this.faceFront);return J.set(0,.5*Math.PI+t,n,"YZX"),e.setFromEuler(J).premultiply(Je.copy(this._restHeadWorldQuaternion).invert())}getLookAtWorldDirection(e){return this.getLookAtWorldQuaternion(Z),this.getFaceFrontQuaternion(_e),e.copy(Me).applyQuaternion(Z).applyQuaternion(_e).applyEuler(this.getEuler(J))}lookAt(e){let t=Ze.copy(this._restHeadWorldQuaternion).multiply(qe(this.getLookAtWorldQuaternion(Z))),n=this.getLookAtWorldPosition(Xe),r=Ke.copy(e).sub(n).applyQuaternion(t).normalize(),[i,s]=Y(this.faceFront),[a,l]=Y(r),u=Ee(a-i),d=Ee(s-l);this._yaw=M.MathUtils.RAD2DEG*u,this._pitch=M.MathUtils.RAD2DEG*d,this._needsUpdate=!0}update(e){this.target!=null&&this.autoUpdate&&this.lookAt(this.target.getWorldPosition(Ge)),this._needsUpdate&&(this._needsUpdate=!1,this.applier.applyYawPitch(this._yaw,this._pitch))}};ke.EULER_ORDER="YXZ";var Be=ke,et=new R.Vector3(0,0,1),I=new R.Quaternion,S=new R.Quaternion,L=new R.Euler(0,0,0,"YXZ"),tt=class{constructor(o,e,t,n,r){this.humanoid=o,this.rangeMapHorizontalInner=e,this.rangeMapHorizontalOuter=t,this.rangeMapVerticalDown=n,this.rangeMapVerticalUp=r,this.faceFront=new R.Vector3(0,0,1),this._restQuatLeftEye=new R.Quaternion,this._restQuatRightEye=new R.Quaternion,this._restLeftEyeParentWorldQuat=new R.Quaternion,this._restRightEyeParentWorldQuat=new R.Quaternion;let i=this.humanoid.getRawBoneNode("leftEye"),s=this.humanoid.getRawBoneNode("rightEye");i&&(this._restQuatLeftEye.copy(i.quaternion),ee(i.parent,this._restLeftEyeParentWorldQuat)),s&&(this._restQuatRightEye.copy(s.quaternion),ee(s.parent,this._restRightEyeParentWorldQuat))}applyYawPitch(o,e){let t=this.humanoid.getRawBoneNode("leftEye"),n=this.humanoid.getRawBoneNode("rightEye"),r=this.humanoid.getNormalizedBoneNode("leftEye"),i=this.humanoid.getNormalizedBoneNode("rightEye");t&&(e<0?L.x=-R.MathUtils.DEG2RAD*this.rangeMapVerticalDown.map(-e):L.x=R.MathUtils.DEG2RAD*this.rangeMapVerticalUp.map(e),o<0?L.y=-R.MathUtils.DEG2RAD*this.rangeMapHorizontalInner.map(-o):L.y=R.MathUtils.DEG2RAD*this.rangeMapHorizontalOuter.map(o),I.setFromEuler(L),this._getWorldFaceFrontQuat(S),r.quaternion.copy(S).multiply(I).multiply(S.invert()),I.copy(this._restLeftEyeParentWorldQuat),t.quaternion.copy(r.quaternion).multiply(I).premultiply(I.invert()).multiply(this._restQuatLeftEye)),n&&(e<0?L.x=-R.MathUtils.DEG2RAD*this.rangeMapVerticalDown.map(-e):L.x=R.MathUtils.DEG2RAD*this.rangeMapVerticalUp.map(e),o<0?L.y=-R.MathUtils.DEG2RAD*this.rangeMapHorizontalOuter.map(-o):L.y=R.MathUtils.DEG2RAD*this.rangeMapHorizontalInner.map(o),I.setFromEuler(L),this._getWorldFaceFrontQuat(S),i.quaternion.copy(S).multiply(I).multiply(S.invert()),I.copy(this._restRightEyeParentWorldQuat),n.quaternion.copy(i.quaternion).multiply(I).premultiply(I.invert()).multiply(this._restQuatRightEye))}lookAt(o){console.warn("VRMLookAtBoneApplier: lookAt() is deprecated. use apply() instead.");let e=R.MathUtils.RAD2DEG*o.y,t=R.MathUtils.RAD2DEG*o.x;this.applyYawPitch(e,t)}_getWorldFaceFrontQuat(o){if(this.faceFront.distanceToSquared(et)<.01)return o.identity();let[e,t]=Y(this.faceFront);return L.set(0,.5*Math.PI+e,t,"YZX"),o.setFromEuler(L)}};tt.type="bone";var nt=class{constructor(o,e,t,n,r){this.expressions=o,this.rangeMapHorizontalInner=e,this.rangeMapHorizontalOuter=t,this.rangeMapVerticalDown=n,this.rangeMapVerticalUp=r}applyYawPitch(o,e){e<0?(this.expressions.setValue("lookDown",0),this.expressions.setValue("lookUp",this.rangeMapVerticalUp.map(-e))):(this.expressions.setValue("lookUp",0),this.expressions.setValue("lookDown",this.rangeMapVerticalDown.map(e))),o<0?(this.expressions.setValue("lookLeft",0),this.expressions.setValue("lookRight",this.rangeMapHorizontalOuter.map(-o))):(this.expressions.setValue("lookRight",0),this.expressions.setValue("lookLeft",this.rangeMapHorizontalOuter.map(o)))}lookAt(o){console.warn("VRMLookAtBoneApplier: lookAt() is deprecated. use apply() instead.");let e=te.MathUtils.RAD2DEG*o.y,t=te.MathUtils.RAD2DEG*o.x;this.applyYawPitch(e,t)}};nt.type="expression";import*as G from"three";var Ie=180/Math.PI,se=new G.Euler,W=class extends G.Object3D{constructor(e){super(),this.vrmLookAt=e,this.type="VRMLookAtQuaternionProxy";let t=this.rotation._onChangeCallback;this.rotation._onChange(()=>{t(),this._applyToLookAt()});let n=this.quaternion._onChangeCallback;this.quaternion._onChange(()=>{n(),this._applyToLookAt()})}_applyToLookAt(){se.setFromQuaternion(this.quaternion,Be.EULER_ORDER),this.vrmLookAt.yaw=Ie*se.y,this.vrmLookAt.pitch=Ie*se.x}};function Ne(o,e,t){var i,s;let n=new Map,r=new Map;for(let[a,l]of o.humanoidTracks.rotation.entries()){let u=(i=e.getNormalizedBoneNode(a))==null?void 0:i.name;if(u!=null){let d=new X.QuaternionKeyframeTrack(`${u}.quaternion`,l.times,l.values.map((f,m)=>t==="0"&&m%2===0?-f:f));r.set(a,d)}}for(let[a,l]of o.humanoidTracks.translation.entries()){let u=(s=e.getNormalizedBoneNode(a))==null?void 0:s.name;if(u!=null){let d=o.restHipsPosition.y,m=e.normalizedRestPose.hips.position[1]/d,p=l.clone();p.values=p.values.map((c,h)=>(t==="0"&&h%3!==1?-c:c)*m),p.name=`${u}.position`,n.set(a,p)}}return{translation:n,rotation:r}}function Ue(o,e){let t=new Map,n=new Map;for(let[r,i]of o.expressionTracks.preset.entries()){let s=e.getExpressionTrackName(r);if(s!=null){let a=i.clone();a.name=s,t.set(r,a)}}for(let[r,i]of o.expressionTracks.custom.entries()){let s=e.getExpressionTrackName(r);if(s!=null){let a=i.clone();a.name=s,n.set(r,a)}}return{preset:t,custom:n}}function Oe(o,e){if(o.lookAtTrack==null)return null;let t=o.lookAtTrack.clone();return t.name=e,t}function ot(o,e){let t=[],n=Ne(o,e.humanoid,e.meta.metaVersion);if(t.push(...n.translation.values()),t.push(...n.rotation.values()),e.expressionManager!=null){let r=Ue(o,e.expressionManager);t.push(...r.preset.values()),t.push(...r.custom.values())}if(e.lookAt!=null){let r=e.scene.children.find(s=>s instanceof W);r==null?(console.warn("createVRMAnimationClip: VRMLookAtQuaternionProxy is not found. Creating a new one automatically. To suppress this warning, create a VRMLookAtQuaternionProxy manually"),r=new W(e.lookAt),r.name="VRMLookAtQuaternionProxy",e.scene.add(r)):r.name===""&&(console.warn("createVRMAnimationClip: VRMLookAtQuaternionProxy is found but its name is not set. Setting the name automatically. To suppress this warning, set the name manually"),r.name="VRMLookAtQuaternionProxy");let i=Oe(o,`${r.name}.quaternion`);i!=null&&t.push(i)}return new X.AnimationClip("Clip",o.duration,t)}import*as De from"three";var j=class{constructor(){this.duration=0,this.restHipsPosition=new De.Vector3,this.humanoidTracks={translation:new Map,rotation:new Map},this.expressionTracks={preset:new Map,custom:new Map},this.lookAtTrack=null}};import*as B from"three";function ae(o,e){let t=o.length,n=[],r=[],i=0;for(let s=0;s{let E=t.animations[_],y=this._parseAnimation(g,E,s,a);return y.restHipsPosition=d,y});e.userData.vrmAnimations=m})}_createNodeMap(e){var l,u,d,f,m;let t=new Map,n=new Map,r=(l=e.humanoid)==null?void 0:l.humanBones;r&&Object.entries(r).forEach(([p,c])=>{let h=c==null?void 0:c.node;h!=null&&t.set(h,p)});let i=(u=e.expressions)==null?void 0:u.preset;i&&Object.entries(i).forEach(([p,c])=>{let h=c==null?void 0:c.node;h!=null&&n.set(h,p)});let s=(d=e.expressions)==null?void 0:d.custom;s&&Object.entries(s).forEach(([p,c])=>{let{node:h}=c;n.set(h,p)});let a=(m=(f=e.lookAt)==null?void 0:f.node)!=null?m:null;return{humanoidIndexToName:t,expressionsIndexToName:n,lookAtIndex:a}}_createBoneWorldMatrixMap(e,t){return K(this,null,function*(){var i,s;e.scene.updateWorldMatrix(!1,!0);let n=yield e.parser.getDependencies("node"),r=new Map;if(t.humanoid==null)return r;for(let[a,l]of Object.entries(t.humanoid.humanBones)){let u=l==null?void 0:l.node;if(u!=null){let d=n[u];r.set(a,d.matrixWorld),a==="hips"&&r.set("hipsParent",(s=(i=d.parent)==null?void 0:i.matrixWorld)!=null?s:it)}}return r})}_parseAnimation(e,t,n,r){let i=e.tracks,s=t.channels,a=new j;return a.duration=e.duration,s.forEach((l,u)=>{let{node:d,path:f}=l.target,m=i[u];if(d==null)return;let p=n.humanoidIndexToName.get(d);if(p!=null){let h=re[p];for(;h!=null&&r.get(h)==null;)h=re[h];if(h==null&&(h="hipsParent"),f==="translation")if(p!=="hips")console.warn(`The loading animation contains a translation track for ${p}, which is not permitted in the VRMC_vrm_animation spec. ignoring the track`);else{let g=r.get("hipsParent"),_=ae(m.values,3).flatMap(y=>q.fromArray(y).applyMatrix4(g).toArray()),E=m.clone();E.values=new Float32Array(_),a.humanoidTracks.translation.set(p,E)}else if(f==="rotation"){let g=r.get(p),_=r.get(h);g.decompose(q,le,q),le.invert(),_.decompose(q,Se,q);let E=ae(m.values,4).flatMap(v=>st.fromArray(v).premultiply(Se).multiply(le).toArray()),y=m.clone();y.values=new Float32Array(E),a.humanoidTracks.rotation.set(p,y)}else throw new Error(`Invalid path "${f}"`);return}let c=n.expressionsIndexToName.get(d);if(c!=null){if(f==="translation"){let h=m.times,g=new Float32Array(m.values.length/3);for(let E=0;E