@pixiv/three-vrm-springbone 3.0.0 → 3.1.0
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/lib/three-vrm-springbone.cjs +222 -91
- package/lib/three-vrm-springbone.min.cjs +1 -1
- package/lib/three-vrm-springbone.module.js +222 -91
- package/lib/three-vrm-springbone.module.min.js +1 -1
- package/package.json +5 -4
- package/types/VRMSpringBoneColliderShapeCapsule.d.ts +8 -3
- package/types/VRMSpringBoneColliderShapePlane.d.ts +18 -0
- package/types/VRMSpringBoneColliderShapeSphere.d.ts +6 -1
- package/types/VRMSpringBoneLoaderPlugin.d.ts +7 -0
- package/types/VRMSpringBoneLoaderPluginOptions.d.ts +6 -0
- package/types/helpers/utils/ColliderShapePlaneBufferGeometry.d.ts +15 -0
- package/types/index.d.ts +1 -0
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! (c) 2019-2024 pixiv Inc. - https://github.com/pixiv/three-vrm/blob/release/LICENSE */
|
|
2
|
-
var U=(l,e,t)=>new Promise((r,i)=>{var o=s=>{try{a(t.next(s))}catch(d){i(d)}},n=s=>{try{a(t.throw(s))}catch(d){i(d)}},a=s=>s.done?r(s.value):Promise.resolve(s.value).then(o,n);a((t=t.apply(l,e)).next())});import*as y from"three";import*as Y from"three";var O=class{};var nt=new Y.Vector3,V=new Y.Vector3,L=class extends O{get type(){return"capsule"}constructor(e){var t,r,i;super(),this.offset=(t=e==null?void 0:e.offset)!=null?t:new Y.Vector3(0,0,0),this.tail=(r=e==null?void 0:e.tail)!=null?r:new Y.Vector3(0,0,0),this.radius=(i=e==null?void 0:e.radius)!=null?i:0}calculateCollision(e,t,r,i){nt.copy(this.offset).applyMatrix4(e),V.copy(this.tail).applyMatrix4(e),V.sub(nt);let o=V.lengthSq();i.copy(t).sub(nt);let n=V.dot(i);n<=0||(o<=n||V.multiplyScalar(n/o),i.sub(V));let a=r+this.radius,s=i.length()-a;return i.normalize(),s}};import*as lt from"three";var D=class extends O{get type(){return"sphere"}constructor(e){var t,r;super(),this.offset=(t=e==null?void 0:e.offset)!=null?t:new lt.Vector3(0,0,0),this.radius=(r=e==null?void 0:e.radius)!=null?r:0}calculateCollision(e,t,r,i){i.copy(this.offset).applyMatrix4(e),i.negate().add(t);let o=r+this.radius,n=i.length()-o;return i.normalize(),n}};import*as b from"three";var T=new b.Vector3,K=class extends b.BufferGeometry{constructor(t){super();this.worldScale=1;this._currentRadius=0;this._currentOffset=new b.Vector3;this._currentTail=new b.Vector3;this._shape=t,this._attrPos=new b.BufferAttribute(new Float32Array(396),3),this.setAttribute("position",this._attrPos),this._attrIndex=new b.BufferAttribute(new Uint16Array(264),1),this.setIndex(this._attrIndex),this._buildIndex(),this.update()}update(){let t=!1,r=this._shape.radius/this.worldScale;this._currentRadius!==r&&(this._currentRadius=r,t=!0),this._currentOffset.equals(this._shape.offset)||(this._currentOffset.copy(this._shape.offset),t=!0);let i=T.copy(this._shape.tail).divideScalar(this.worldScale);this._currentTail.distanceToSquared(i)>1e-10&&(this._currentTail.copy(i),t=!0),t&&this._buildPosition()}_buildPosition(){T.copy(this._currentTail).sub(this._currentOffset);let t=T.length()/this._currentRadius;for(let o=0;o<=16;o++){let n=o/16*Math.PI;this._attrPos.setXYZ(o,-Math.sin(n),-Math.cos(n),0),this._attrPos.setXYZ(17+o,t+Math.sin(n),Math.cos(n),0),this._attrPos.setXYZ(34+o,-Math.sin(n),0,-Math.cos(n)),this._attrPos.setXYZ(51+o,t+Math.sin(n),0,Math.cos(n))}for(let o=0;o<32;o++){let n=o/16*Math.PI;this._attrPos.setXYZ(68+o,0,Math.sin(n),Math.cos(n)),this._attrPos.setXYZ(100+o,t,Math.sin(n),Math.cos(n))}let r=Math.atan2(T.y,Math.sqrt(T.x*T.x+T.z*T.z)),i=-Math.atan2(T.z,T.x);this.rotateZ(r),this.rotateY(i),this.scale(this._currentRadius,this._currentRadius,this._currentRadius),this.translate(this._currentOffset.x,this._currentOffset.y,this._currentOffset.z),this._attrPos.needsUpdate=!0}_buildIndex(){for(let t=0;t<34;t++){let r=(t+1)%34;this._attrIndex.setXY(t*2,t,r),this._attrIndex.setXY(68+t*2,34+t,34+r)}for(let t=0;t<32;t++){let r=(t+1)%32;this._attrIndex.setXY(136+t*2,68+t,68+r),this._attrIndex.setXY(200+t*2,100+t,100+r)}this._attrIndex.needsUpdate=!0}};import*as B from"three";var tt=class extends B.BufferGeometry{constructor(t){super();this.worldScale=1;this._currentRadius=0;this._currentOffset=new B.Vector3;this._shape=t,this._attrPos=new B.BufferAttribute(new Float32Array(32*3*3),3),this.setAttribute("position",this._attrPos),this._attrIndex=new B.BufferAttribute(new Uint16Array(64*3),1),this.setIndex(this._attrIndex),this._buildIndex(),this.update()}update(){let t=!1,r=this._shape.radius/this.worldScale;this._currentRadius!==r&&(this._currentRadius=r,t=!0),this._currentOffset.equals(this._shape.offset)||(this._currentOffset.copy(this._shape.offset),t=!0),t&&this._buildPosition()}_buildPosition(){for(let t=0;t<32;t++){let r=t/16*Math.PI;this._attrPos.setXYZ(t,Math.cos(r),Math.sin(r),0),this._attrPos.setXYZ(32+t,0,Math.cos(r),Math.sin(r)),this._attrPos.setXYZ(64+t,Math.sin(r),0,Math.cos(r))}this.scale(this._currentRadius,this._currentRadius,this._currentRadius),this.translate(this._currentOffset.x,this._currentOffset.y,this._currentOffset.z),this._attrPos.needsUpdate=!0}_buildIndex(){for(let t=0;t<32;t++){let r=(t+1)%32;this._attrIndex.setXY(t*2,t,r),this._attrIndex.setXY(64+t*2,32+t,32+r),this._attrIndex.setXY(128+t*2,64+t,64+r)}this._attrIndex.needsUpdate=!0}};var Rt=new y.Vector3,J=class extends y.Group{constructor(e){if(super(),this.matrixAutoUpdate=!1,this.collider=e,this.collider.shape instanceof D)this._geometry=new tt(this.collider.shape);else if(this.collider.shape instanceof L)this._geometry=new K(this.collider.shape);else throw new Error("VRMSpringBoneColliderHelper: Unknown collider shape type detected");let t=new y.LineBasicMaterial({color:16711935,depthTest:!1,depthWrite:!1});this._line=new y.LineSegments(this._geometry,t),this.add(this._line)}dispose(){this._geometry.dispose()}updateMatrixWorld(e){this.collider.updateWorldMatrix(!0,!1),this.matrix.copy(this.collider.matrixWorld);let t=this.matrix.elements;this._geometry.worldScale=Rt.set(t[0],t[1],t[2]).length(),this._geometry.update(),super.updateMatrixWorld(e)}};import*as v from"three";import*as w from"three";var et=class extends w.BufferGeometry{constructor(t){super();this.worldScale=1;this._currentRadius=0;this._currentTail=new w.Vector3;this._springBone=t,this._attrPos=new w.BufferAttribute(new Float32Array(294),3),this.setAttribute("position",this._attrPos),this._attrIndex=new w.BufferAttribute(new Uint16Array(194),1),this.setIndex(this._attrIndex),this._buildIndex(),this.update()}update(){let t=!1,r=this._springBone.settings.hitRadius/this.worldScale;this._currentRadius!==r&&(this._currentRadius=r,t=!0),this._currentTail.equals(this._springBone.initialLocalChildPosition)||(this._currentTail.copy(this._springBone.initialLocalChildPosition),t=!0),t&&this._buildPosition()}_buildPosition(){for(let t=0;t<32;t++){let r=t/16*Math.PI;this._attrPos.setXYZ(t,Math.cos(r),Math.sin(r),0),this._attrPos.setXYZ(32+t,0,Math.cos(r),Math.sin(r)),this._attrPos.setXYZ(64+t,Math.sin(r),0,Math.cos(r))}this.scale(this._currentRadius,this._currentRadius,this._currentRadius),this.translate(this._currentTail.x,this._currentTail.y,this._currentTail.z),this._attrPos.setXYZ(96,0,0,0),this._attrPos.setXYZ(97,this._currentTail.x,this._currentTail.y,this._currentTail.z),this._attrPos.needsUpdate=!0}_buildIndex(){for(let t=0;t<32;t++){let r=(t+1)%32;this._attrIndex.setXY(t*2,t,r),this._attrIndex.setXY(64+t*2,32+t,32+r),this._attrIndex.setXY(128+t*2,64+t,64+r)}this._attrIndex.setXY(192,96,97),this._attrIndex.needsUpdate=!0}};var _t=new v.Vector3,z=class extends v.Group{constructor(e){super(),this.matrixAutoUpdate=!1,this.springBone=e,this._geometry=new et(this.springBone);let t=new v.LineBasicMaterial({color:16776960,depthTest:!1,depthWrite:!1});this._line=new v.LineSegments(this._geometry,t),this.add(this._line)}dispose(){this._geometry.dispose()}updateMatrixWorld(e){this.springBone.bone.updateWorldMatrix(!0,!1),this.matrix.copy(this.springBone.bone.matrixWorld);let t=this.matrix.elements;this._geometry.worldScale=_t.set(t[0],t[1],t[2]).length(),this._geometry.update(),super.updateMatrixWorld(e)}};import*as pt from"three";var N=class extends pt.Object3D{constructor(e){super(),this.shape=e}};import*as c from"three";import*as ct from"three";var mt=new ct.Matrix4;function rt(l){return l.invert?l.invert():l.getInverse(mt.copy(l)),l}import*as dt from"three";var it=class{constructor(e){this._inverseCache=new dt.Matrix4;this._shouldUpdateInverse=!0;this.matrix=e;let t={set:(r,i,o)=>(this._shouldUpdateInverse=!0,r[i]=o,!0)};this._originalElements=e.elements,e.elements=new Proxy(e.elements,t)}get inverse(){return this._shouldUpdateInverse&&(rt(this._inverseCache.copy(this.matrix)),this._shouldUpdateInverse=!1),this._inverseCache}revert(){this.matrix.elements=this._originalElements}};var gt=new c.Matrix4,x=new c.Vector3,Z=new c.Vector3,St=new c.Vector3,A=new c.Vector3,ut=new c.Vector3,q=new c.Vector3,ht=new c.Quaternion,W=new c.Matrix4,Mt=new c.Matrix4,ot=class{constructor(e,t,r={},i=[]){this._currentTail=new c.Vector3;this._prevTail=new c.Vector3;this._boneAxis=new c.Vector3;this._worldSpaceBoneLength=0;this._center=null;this._initialLocalMatrix=new c.Matrix4;this._initialLocalRotation=new c.Quaternion;this._initialLocalChildPosition=new c.Vector3;var o,n,a,s,d,m;this.bone=e,this.bone.matrixAutoUpdate=!1,this.child=t,this.settings={hitRadius:(o=r.hitRadius)!=null?o:0,stiffness:(n=r.stiffness)!=null?n:1,gravityPower:(a=r.gravityPower)!=null?a:0,gravityDir:(d=(s=r.gravityDir)==null?void 0:s.clone())!=null?d:new c.Vector3(0,-1,0),dragForce:(m=r.dragForce)!=null?m:.4},this.colliderGroups=i}get center(){return this._center}set center(e){var t;(t=this._center)!=null&&t.userData.inverseCacheProxy&&(this._center.userData.inverseCacheProxy.revert(),delete this._center.userData.inverseCacheProxy),this._center=e,this._center&&(this._center.userData.inverseCacheProxy||(this._center.userData.inverseCacheProxy=new it(this._center.matrixWorld)))}get initialLocalChildPosition(){return this._initialLocalChildPosition}get _parentMatrixWorld(){return this.bone.parent?this.bone.parent.matrixWorld:gt}setInitState(){this._initialLocalMatrix.copy(this.bone.matrix),this._initialLocalRotation.copy(this.bone.quaternion),this.child?this._initialLocalChildPosition.copy(this.child.position):this._initialLocalChildPosition.copy(this.bone.position).normalize().multiplyScalar(.07);let e=this._getMatrixWorldToCenter(W);this.bone.localToWorld(this._currentTail.copy(this._initialLocalChildPosition)).applyMatrix4(e),this._prevTail.copy(this._currentTail),this._boneAxis.copy(this._initialLocalChildPosition).normalize()}reset(){this.bone.quaternion.copy(this._initialLocalRotation),this.bone.updateMatrix(),this.bone.matrixWorld.multiplyMatrices(this._parentMatrixWorld,this.bone.matrix);let e=this._getMatrixWorldToCenter(W);this.bone.localToWorld(this._currentTail.copy(this._initialLocalChildPosition)).applyMatrix4(e),this._prevTail.copy(this._currentTail)}update(e){if(e<=0)return;this._calcWorldSpaceBoneLength(),A.setFromMatrixPosition(this.bone.matrixWorld);let t=this._getMatrixWorldToCenter(W);ut.copy(A).applyMatrix4(t);let r=ht.setFromRotationMatrix(t),i=Mt.copy(t).multiply(this._parentMatrixWorld),o=Z.copy(this._boneAxis).applyMatrix4(this._initialLocalMatrix).applyMatrix4(i).sub(ut).normalize(),n=St.copy(this.settings.gravityDir).applyQuaternion(r).normalize(),a=this._getMatrixCenterToWorld(W);q.copy(this._currentTail).add(x.copy(this._currentTail).sub(this._prevTail).multiplyScalar(1-this.settings.dragForce)).add(x.copy(o).multiplyScalar(this.settings.stiffness*e)).add(x.copy(n).multiplyScalar(this.settings.gravityPower*e)).applyMatrix4(a),q.sub(A).normalize().multiplyScalar(this._worldSpaceBoneLength).add(A),this._collision(q),t=this._getMatrixWorldToCenter(W),this._prevTail.copy(this._currentTail),this._currentTail.copy(x.copy(q).applyMatrix4(t));let s=rt(W.copy(this._parentMatrixWorld).multiply(this._initialLocalMatrix)),d=ht.setFromUnitVectors(this._boneAxis,x.copy(q).applyMatrix4(s).normalize());this.bone.quaternion.copy(this._initialLocalRotation).multiply(d),this.bone.updateMatrix(),this.bone.matrixWorld.multiplyMatrices(this._parentMatrixWorld,this.bone.matrix)}_collision(e){this.colliderGroups.forEach(t=>{t.colliders.forEach(r=>{let i=r.shape.calculateCollision(r.matrixWorld,e,this.settings.hitRadius,x);i<0&&(e.add(x.multiplyScalar(-i)),e.sub(A).normalize().multiplyScalar(this._worldSpaceBoneLength).add(A))})})}_calcWorldSpaceBoneLength(){x.setFromMatrixPosition(this.bone.matrixWorld),this.child?Z.setFromMatrixPosition(this.child.matrixWorld):(Z.copy(this._initialLocalChildPosition),Z.applyMatrix4(this.bone.matrixWorld)),this._worldSpaceBoneLength=x.sub(Z).length()}_getMatrixCenterToWorld(e){return this._center?e.copy(this._center.matrixWorld):e.identity(),e}_getMatrixWorldToCenter(e){return this._center?e.copy(this._center.userData.inverseCacheProxy.inverse):e.identity(),e}};import*as C from"three";function ft(l,e){let t=[],r=l;for(;r!==null;)t.unshift(r),r=r.parent;t.forEach(i=>{e(i)})}function st(l,e){l.children.forEach(t=>{e(t)||st(t,e)})}var $=class{constructor(){this._joints=new Set;this._objectSpringBonesMap=new Map}get joints(){return this._joints}get springBones(){return console.warn("VRMSpringBoneManager: springBones is deprecated. use joints instead."),this._joints}get colliderGroups(){let e=new Set;return this._joints.forEach(t=>{t.colliderGroups.forEach(r=>{e.add(r)})}),Array.from(e)}get colliders(){let e=new Set;return this.colliderGroups.forEach(t=>{t.colliders.forEach(r=>{e.add(r)})}),Array.from(e)}addJoint(e){this._joints.add(e);let t=this._objectSpringBonesMap.get(e.bone);t==null&&(t=new Set,this._objectSpringBonesMap.set(e.bone,t)),t.add(e)}addSpringBone(e){console.warn("VRMSpringBoneManager: addSpringBone() is deprecated. use addJoint() instead."),this.addJoint(e)}deleteJoint(e){this._joints.delete(e),this._objectSpringBonesMap.get(e.bone).delete(e)}deleteSpringBone(e){console.warn("VRMSpringBoneManager: deleteSpringBone() is deprecated. use deleteJoint() instead."),this.deleteJoint(e)}setInitState(){let e=new Set,t=new Set,r=new Set;for(let i of this._joints)this._processSpringBone(i,e,t,r,o=>o.setInitState())}reset(){let e=new Set,t=new Set,r=new Set;for(let i of this._joints)this._processSpringBone(i,e,t,r,o=>o.reset())}update(e){let t=new Set,r=new Set,i=new Set;for(let o of this._joints)this._processSpringBone(o,t,r,i,n=>n.update(e)),st(o.bone,n=>{var a,s;return((s=(a=this._objectSpringBonesMap.get(n))==null?void 0:a.size)!=null?s:0)>0?!0:(n.updateWorldMatrix(!1,!1),!1)})}_processSpringBone(e,t,r,i,o){if(r.has(e))return;if(t.has(e))throw new Error("VRMSpringBoneManager: Circular dependency detected while updating springbones");t.add(e);let n=this._getDependencies(e);for(let a of n)ft(a,s=>{let d=this._objectSpringBonesMap.get(s);if(d)for(let m of d)this._processSpringBone(m,t,r,i,o);else i.has(s)||(s.updateWorldMatrix(!1,!1),i.add(s))});e.bone.updateMatrix(),e.bone.updateWorldMatrix(!1,!1),o(e),i.add(e.bone),r.add(e)}_getDependencies(e){let t=new Set,r=e.bone.parent;return r&&t.add(r),e.colliderGroups.forEach(i=>{i.colliders.forEach(o=>{t.add(o)})}),t}};var Tt=new Set(["1.0","1.0-beta"]),P=class P{get name(){return P.EXTENSION_NAME}constructor(e,t){this.parser=e,this.jointHelperRoot=t==null?void 0:t.jointHelperRoot,this.colliderHelperRoot=t==null?void 0:t.colliderHelperRoot}afterRoot(e){return U(this,null,function*(){e.userData.vrmSpringBoneManager=yield this._import(e)})}_import(e){return U(this,null,function*(){let t=yield this._v1Import(e);if(t!=null)return t;let r=yield this._v0Import(e);return r!=null?r:null})}_v1Import(e){return U(this,null,function*(){var m,G,F,u,I;let t=e.parser.json;if(!(((m=t.extensionsUsed)==null?void 0:m.indexOf(P.EXTENSION_NAME))!==-1))return null;let i=new $,o=yield e.parser.getDependencies("node"),n=(G=t.extensions)==null?void 0:G[P.EXTENSION_NAME];if(!n)return null;let a=n.specVersion;if(!Tt.has(a))return console.warn(`VRMSpringBoneLoaderPlugin: Unknown ${P.EXTENSION_NAME} specVersion "${a}"`),null;let s=(F=n.colliders)==null?void 0:F.map((f,g)=>{var R,p,S,_,M;let E=o[f.node],h=f.shape;if(h.sphere)return this._importSphereCollider(E,{offset:new C.Vector3().fromArray((R=h.sphere.offset)!=null?R:[0,0,0]),radius:(p=h.sphere.radius)!=null?p:0});if(h.capsule)return this._importCapsuleCollider(E,{offset:new C.Vector3().fromArray((S=h.capsule.offset)!=null?S:[0,0,0]),radius:(_=h.capsule.radius)!=null?_:0,tail:new C.Vector3().fromArray((M=h.capsule.tail)!=null?M:[0,0,0])});throw new Error(`VRMSpringBoneLoaderPlugin: The collider #${g} has no valid shape`)}),d=(u=n.colliderGroups)==null?void 0:u.map((f,g)=>{var h;return{colliders:((h=f.colliders)!=null?h:[]).map(R=>{let p=s==null?void 0:s[R];if(p==null)throw new Error(`VRMSpringBoneLoaderPlugin: The colliderGroup #${g} attempted to use a collider #${R} but not found`);return p}),name:f.name}});return(I=n.springs)==null||I.forEach((f,g)=>{var S;let E=f.joints,h=(S=f.colliderGroups)==null?void 0:S.map(_=>{let M=d==null?void 0:d[_];if(M==null)throw new Error(`VRMSpringBoneLoaderPlugin: The spring #${g} attempted to use a colliderGroup ${_} but not found`);return M}),R=f.center!=null?o[f.center]:void 0,p;E.forEach(_=>{if(p){let M=p.node,Q=o[M],k=_.node,H=o[k],j={hitRadius:p.hitRadius,dragForce:p.dragForce,gravityPower:p.gravityPower,stiffness:p.stiffness,gravityDir:p.gravityDir!=null?new C.Vector3().fromArray(p.gravityDir):void 0},X=this._importJoint(Q,H,j,h);R&&(X.center=R),i.addJoint(X)}p=_})}),i.setInitState(),i})}_v0Import(e){return U(this,null,function*(){var m,G,F;let t=e.parser.json;if(!(((m=t.extensionsUsed)==null?void 0:m.indexOf("VRM"))!==-1))return null;let i=(G=t.extensions)==null?void 0:G.VRM,o=i==null?void 0:i.secondaryAnimation;if(!o)return null;let n=o==null?void 0:o.boneGroups;if(!n)return null;let a=new $,s=yield e.parser.getDependencies("node"),d=(F=o.colliderGroups)==null?void 0:F.map(u=>{var g;let I=s[u.node];return{colliders:((g=u.colliders)!=null?g:[]).map((E,h)=>{var p,S,_;let R=new C.Vector3(0,0,0);return E.offset&&R.set((p=E.offset.x)!=null?p:0,(S=E.offset.y)!=null?S:0,E.offset.z?-E.offset.z:0),this._importSphereCollider(I,{offset:R,radius:(_=E.radius)!=null?_:0})})}});return n==null||n.forEach((u,I)=>{let f=u.bones;f&&f.forEach(g=>{var _,M,Q,k;let E=s[g],h=new C.Vector3;u.gravityDir?h.set((_=u.gravityDir.x)!=null?_:0,(M=u.gravityDir.y)!=null?M:0,(Q=u.gravityDir.z)!=null?Q:0):h.set(0,-1,0);let R=u.center!=null?s[u.center]:void 0,p={hitRadius:u.hitRadius,dragForce:u.dragForce,gravityPower:u.gravityPower,stiffness:u.stiffiness,gravityDir:h},S=(k=u.colliderGroups)==null?void 0:k.map(H=>{let j=d==null?void 0:d[H];if(j==null)throw new Error(`VRMSpringBoneLoaderPlugin: The spring #${I} attempted to use a colliderGroup ${H} but not found`);return j});E.traverse(H=>{var at;let j=(at=H.children[0])!=null?at:null,X=this._importJoint(H,j,p,S);R&&(X.center=R),a.addJoint(X)})})}),e.scene.updateMatrixWorld(),a.setInitState(),a})}_importJoint(e,t,r,i){let o=new ot(e,t,r,i);if(this.jointHelperRoot){let n=new z(o);this.jointHelperRoot.add(n),n.renderOrder=this.jointHelperRoot.renderOrder}return o}_importSphereCollider(e,t){let{offset:r,radius:i}=t,o=new D({offset:r,radius:i}),n=new N(o);if(e.add(n),this.colliderHelperRoot){let a=new J(n);this.colliderHelperRoot.add(a),a.renderOrder=this.colliderHelperRoot.renderOrder}return n}_importCapsuleCollider(e,t){let{offset:r,radius:i,tail:o}=t,n=new L({offset:r,radius:i,tail:o}),a=new N(n);if(e.add(a),this.colliderHelperRoot){let s=new J(a);this.colliderHelperRoot.add(s),s.renderOrder=this.colliderHelperRoot.renderOrder}return a}};P.EXTENSION_NAME="VRMC_springBone";var Et=P;export{N as VRMSpringBoneCollider,J as VRMSpringBoneColliderHelper,O as VRMSpringBoneColliderShape,L as VRMSpringBoneColliderShapeCapsule,D as VRMSpringBoneColliderShapeSphere,ot as VRMSpringBoneJoint,z as VRMSpringBoneJointHelper,Et as VRMSpringBoneLoaderPlugin,$ as VRMSpringBoneManager};
|
|
2
|
+
var Q=(a,t,e)=>new Promise((r,i)=>{var o=s=>{try{l(e.next(s))}catch(d){i(d)}},n=s=>{try{l(e.throw(s))}catch(d){i(d)}},l=s=>s.done?r(s.value):Promise.resolve(s.value).then(o,n);l((e=e.apply(a,t)).next())});import*as P from"three";import*as K from"three";var w=class{};var de=new K.Vector3,j=new K.Vector3,X=class extends w{get type(){return"capsule"}constructor(t){var e,r,i,o;super(),this.offset=(e=t==null?void 0:t.offset)!=null?e:new K.Vector3(0,0,0),this.tail=(r=t==null?void 0:t.tail)!=null?r:new K.Vector3(0,0,0),this.radius=(i=t==null?void 0:t.radius)!=null?i:0,this.inside=(o=t==null?void 0:t.inside)!=null?o:!1}calculateCollision(t,e,r,i){de.copy(this.offset).applyMatrix4(t),j.copy(this.tail).applyMatrix4(t),j.sub(de);let o=j.lengthSq();i.copy(e).sub(de);let n=j.dot(i);n<=0||(o<=n||j.multiplyScalar(n/o),i.sub(j));let l=this.inside?this.radius-r-i.length():i.length()-r-this.radius;return i.normalize(),this.inside&&i.negate(),l}};import*as F from"three";var he=new F.Vector3,ge=new F.Matrix3,Y=class extends w{get type(){return"plane"}constructor(t){var e,r;super(),this.offset=(e=t==null?void 0:t.offset)!=null?e:new F.Vector3(0,0,0),this.normal=(r=t==null?void 0:t.normal)!=null?r:new F.Vector3(0,0,1)}calculateCollision(t,e,r,i){i.copy(this.offset).applyMatrix4(t),i.negate().add(e),ge.getNormalMatrix(t),he.copy(this.normal).applyNormalMatrix(ge).normalize();let o=i.dot(he)-r;return i.copy(he),o}};import*as Te from"three";var U=class extends w{get type(){return"sphere"}constructor(t){var e,r,i;super(),this.offset=(e=t==null?void 0:t.offset)!=null?e:new Te.Vector3(0,0,0),this.radius=(r=t==null?void 0:t.radius)!=null?r:0,this.inside=(i=t==null?void 0:t.inside)!=null?i:!1}calculateCollision(t,e,r,i){i.copy(this.offset).applyMatrix4(t),i.negate().add(e);let o=this.inside?this.radius-r-i.length():i.length()-r-this.radius;return i.normalize(),this.inside&&i.negate(),o}};import*as V from"three";var y=new V.Vector3,oe=class extends V.BufferGeometry{constructor(e){super();this.worldScale=1;this._currentRadius=0;this._currentOffset=new V.Vector3;this._currentTail=new V.Vector3;this._shape=e,this._attrPos=new V.BufferAttribute(new Float32Array(396),3),this.setAttribute("position",this._attrPos),this._attrIndex=new V.BufferAttribute(new Uint16Array(264),1),this.setIndex(this._attrIndex),this._buildIndex(),this.update()}update(){let e=!1,r=this._shape.radius/this.worldScale;this._currentRadius!==r&&(this._currentRadius=r,e=!0),this._currentOffset.equals(this._shape.offset)||(this._currentOffset.copy(this._shape.offset),e=!0);let i=y.copy(this._shape.tail).divideScalar(this.worldScale);this._currentTail.distanceToSquared(i)>1e-10&&(this._currentTail.copy(i),e=!0),e&&this._buildPosition()}_buildPosition(){y.copy(this._currentTail).sub(this._currentOffset);let e=y.length()/this._currentRadius;for(let o=0;o<=16;o++){let n=o/16*Math.PI;this._attrPos.setXYZ(o,-Math.sin(n),-Math.cos(n),0),this._attrPos.setXYZ(17+o,e+Math.sin(n),Math.cos(n),0),this._attrPos.setXYZ(34+o,-Math.sin(n),0,-Math.cos(n)),this._attrPos.setXYZ(51+o,e+Math.sin(n),0,Math.cos(n))}for(let o=0;o<32;o++){let n=o/16*Math.PI;this._attrPos.setXYZ(68+o,0,Math.sin(n),Math.cos(n)),this._attrPos.setXYZ(100+o,e,Math.sin(n),Math.cos(n))}let r=Math.atan2(y.y,Math.sqrt(y.x*y.x+y.z*y.z)),i=-Math.atan2(y.z,y.x);this.rotateZ(r),this.rotateY(i),this.scale(this._currentRadius,this._currentRadius,this._currentRadius),this.translate(this._currentOffset.x,this._currentOffset.y,this._currentOffset.z),this._attrPos.needsUpdate=!0}_buildIndex(){for(let e=0;e<34;e++){let r=(e+1)%34;this._attrIndex.setXY(e*2,e,r),this._attrIndex.setXY(68+e*2,34+e,34+r)}for(let e=0;e<32;e++){let r=(e+1)%32;this._attrIndex.setXY(136+e*2,68+e,68+r),this._attrIndex.setXY(200+e*2,100+e,100+r)}this._attrIndex.needsUpdate=!0}};import*as C from"three";var ne=class extends C.BufferGeometry{constructor(e){super();this.worldScale=1;this._currentOffset=new C.Vector3;this._currentNormal=new C.Vector3;this._shape=e,this._attrPos=new C.BufferAttribute(new Float32Array(6*3),3),this.setAttribute("position",this._attrPos),this._attrIndex=new C.BufferAttribute(new Uint16Array(10),1),this.setIndex(this._attrIndex),this._buildIndex(),this.update()}update(){let e=!1;this._currentOffset.equals(this._shape.offset)||(this._currentOffset.copy(this._shape.offset),e=!0),this._currentNormal.equals(this._shape.normal)||(this._currentNormal.copy(this._shape.normal),e=!0),e&&this._buildPosition()}_buildPosition(){this._attrPos.setXYZ(0,-.5,-.5,0),this._attrPos.setXYZ(1,.5,-.5,0),this._attrPos.setXYZ(2,.5,.5,0),this._attrPos.setXYZ(3,-.5,.5,0),this._attrPos.setXYZ(4,0,0,0),this._attrPos.setXYZ(5,0,0,.25),this.translate(this._currentOffset.x,this._currentOffset.y,this._currentOffset.z),this.lookAt(this._currentNormal),this._attrPos.needsUpdate=!0}_buildIndex(){this._attrIndex.setXY(0,0,1),this._attrIndex.setXY(2,1,2),this._attrIndex.setXY(4,2,3),this._attrIndex.setXY(6,3,0),this._attrIndex.setXY(8,4,5),this._attrIndex.needsUpdate=!0}};import*as D from"three";var se=class extends D.BufferGeometry{constructor(e){super();this.worldScale=1;this._currentRadius=0;this._currentOffset=new D.Vector3;this._shape=e,this._attrPos=new D.BufferAttribute(new Float32Array(32*3*3),3),this.setAttribute("position",this._attrPos),this._attrIndex=new D.BufferAttribute(new Uint16Array(64*3),1),this.setIndex(this._attrIndex),this._buildIndex(),this.update()}update(){let e=!1,r=this._shape.radius/this.worldScale;this._currentRadius!==r&&(this._currentRadius=r,e=!0),this._currentOffset.equals(this._shape.offset)||(this._currentOffset.copy(this._shape.offset),e=!0),e&&this._buildPosition()}_buildPosition(){for(let e=0;e<32;e++){let r=e/16*Math.PI;this._attrPos.setXYZ(e,Math.cos(r),Math.sin(r),0),this._attrPos.setXYZ(32+e,0,Math.cos(r),Math.sin(r)),this._attrPos.setXYZ(64+e,Math.sin(r),0,Math.cos(r))}this.scale(this._currentRadius,this._currentRadius,this._currentRadius),this.translate(this._currentOffset.x,this._currentOffset.y,this._currentOffset.z),this._attrPos.needsUpdate=!0}_buildIndex(){for(let e=0;e<32;e++){let r=(e+1)%32;this._attrIndex.setXY(e*2,e,r),this._attrIndex.setXY(64+e*2,32+e,32+r),this._attrIndex.setXY(128+e*2,64+e,64+r)}this._attrIndex.needsUpdate=!0}};var we=new P.Vector3,J=class extends P.Group{constructor(t){if(super(),this.matrixAutoUpdate=!1,this.collider=t,this.collider.shape instanceof U)this._geometry=new se(this.collider.shape);else if(this.collider.shape instanceof X)this._geometry=new oe(this.collider.shape);else if(this.collider.shape instanceof Y)this._geometry=new ne(this.collider.shape);else throw new Error("VRMSpringBoneColliderHelper: Unknown collider shape type detected");let e=new P.LineBasicMaterial({color:16711935,depthTest:!1,depthWrite:!1});this._line=new P.LineSegments(this._geometry,e),this.add(this._line)}dispose(){this._geometry.dispose()}updateMatrixWorld(t){this.collider.updateWorldMatrix(!0,!1),this.matrix.copy(this.collider.matrixWorld);let e=this.matrix.elements;this._geometry.worldScale=we.set(e[0],e[1],e[2]).length(),this._geometry.update(),super.updateMatrixWorld(t)}};import*as I from"three";import*as G from"three";var ae=class extends G.BufferGeometry{constructor(e){super();this.worldScale=1;this._currentRadius=0;this._currentTail=new G.Vector3;this._springBone=e,this._attrPos=new G.BufferAttribute(new Float32Array(294),3),this.setAttribute("position",this._attrPos),this._attrIndex=new G.BufferAttribute(new Uint16Array(194),1),this.setIndex(this._attrIndex),this._buildIndex(),this.update()}update(){let e=!1,r=this._springBone.settings.hitRadius/this.worldScale;this._currentRadius!==r&&(this._currentRadius=r,e=!0),this._currentTail.equals(this._springBone.initialLocalChildPosition)||(this._currentTail.copy(this._springBone.initialLocalChildPosition),e=!0),e&&this._buildPosition()}_buildPosition(){for(let e=0;e<32;e++){let r=e/16*Math.PI;this._attrPos.setXYZ(e,Math.cos(r),Math.sin(r),0),this._attrPos.setXYZ(32+e,0,Math.cos(r),Math.sin(r)),this._attrPos.setXYZ(64+e,Math.sin(r),0,Math.cos(r))}this.scale(this._currentRadius,this._currentRadius,this._currentRadius),this.translate(this._currentTail.x,this._currentTail.y,this._currentTail.z),this._attrPos.setXYZ(96,0,0,0),this._attrPos.setXYZ(97,this._currentTail.x,this._currentTail.y,this._currentTail.z),this._attrPos.needsUpdate=!0}_buildIndex(){for(let e=0;e<32;e++){let r=(e+1)%32;this._attrIndex.setXY(e*2,e,r),this._attrIndex.setXY(64+e*2,32+e,32+r),this._attrIndex.setXY(128+e*2,64+e,64+r)}this._attrIndex.setXY(192,96,97),this._attrIndex.needsUpdate=!0}};var Ce=new I.Vector3,ee=class extends I.Group{constructor(t){super(),this.matrixAutoUpdate=!1,this.springBone=t,this._geometry=new ae(this.springBone);let e=new I.LineBasicMaterial({color:16776960,depthTest:!1,depthWrite:!1});this._line=new I.LineSegments(this._geometry,e),this.add(this._line)}dispose(){this._geometry.dispose()}updateMatrixWorld(t){this.springBone.bone.updateWorldMatrix(!0,!1),this.matrix.copy(this.springBone.bone.matrixWorld);let e=this.matrix.elements;this._geometry.worldScale=Ce.set(e[0],e[1],e[2]).length(),this._geometry.update(),super.updateMatrixWorld(t)}};import*as Me from"three";var N=class extends Me.Object3D{constructor(t){super(),this.shape=t}};import*as c from"three";import*as xe from"three";var Pe=new xe.Matrix4;function le(a){return a.invert?a.invert():a.getInverse(Pe.copy(a)),a}import*as be from"three";var pe=class{constructor(t){this._inverseCache=new be.Matrix4;this._shouldUpdateInverse=!0;this.matrix=t;let e={set:(r,i,o)=>(this._shouldUpdateInverse=!0,r[i]=o,!0)};this._originalElements=t.elements,t.elements=new Proxy(t.elements,e)}get inverse(){return this._shouldUpdateInverse&&(le(this._inverseCache.copy(this.matrix)),this._shouldUpdateInverse=!1),this._inverseCache}revert(){this.matrix.elements=this._originalElements}};var Ie=new c.Matrix4,v=new c.Vector3,te=new c.Vector3,Oe=new c.Vector3,z=new c.Vector3,ye=new c.Vector3,re=new c.Vector3,He=new c.Quaternion,Z=new c.Matrix4,Ae=new c.Matrix4,ce=class{constructor(t,e,r={},i=[]){this._currentTail=new c.Vector3;this._prevTail=new c.Vector3;this._boneAxis=new c.Vector3;this._worldSpaceBoneLength=0;this._center=null;this._initialLocalMatrix=new c.Matrix4;this._initialLocalRotation=new c.Quaternion;this._initialLocalChildPosition=new c.Vector3;var o,n,l,s,d,g;this.bone=t,this.bone.matrixAutoUpdate=!1,this.child=e,this.settings={hitRadius:(o=r.hitRadius)!=null?o:0,stiffness:(n=r.stiffness)!=null?n:1,gravityPower:(l=r.gravityPower)!=null?l:0,gravityDir:(d=(s=r.gravityDir)==null?void 0:s.clone())!=null?d:new c.Vector3(0,-1,0),dragForce:(g=r.dragForce)!=null?g:.4},this.colliderGroups=i}get center(){return this._center}set center(t){var e;(e=this._center)!=null&&e.userData.inverseCacheProxy&&(this._center.userData.inverseCacheProxy.revert(),delete this._center.userData.inverseCacheProxy),this._center=t,this._center&&(this._center.userData.inverseCacheProxy||(this._center.userData.inverseCacheProxy=new pe(this._center.matrixWorld)))}get initialLocalChildPosition(){return this._initialLocalChildPosition}get _parentMatrixWorld(){return this.bone.parent?this.bone.parent.matrixWorld:Ie}setInitState(){this._initialLocalMatrix.copy(this.bone.matrix),this._initialLocalRotation.copy(this.bone.quaternion),this.child?this._initialLocalChildPosition.copy(this.child.position):this._initialLocalChildPosition.copy(this.bone.position).normalize().multiplyScalar(.07);let t=this._getMatrixWorldToCenter(Z);this.bone.localToWorld(this._currentTail.copy(this._initialLocalChildPosition)).applyMatrix4(t),this._prevTail.copy(this._currentTail),this._boneAxis.copy(this._initialLocalChildPosition).normalize()}reset(){this.bone.quaternion.copy(this._initialLocalRotation),this.bone.updateMatrix(),this.bone.matrixWorld.multiplyMatrices(this._parentMatrixWorld,this.bone.matrix);let t=this._getMatrixWorldToCenter(Z);this.bone.localToWorld(this._currentTail.copy(this._initialLocalChildPosition)).applyMatrix4(t),this._prevTail.copy(this._currentTail)}update(t){if(t<=0)return;this._calcWorldSpaceBoneLength(),z.setFromMatrixPosition(this.bone.matrixWorld);let e=this._getMatrixWorldToCenter(Z);ye.copy(z).applyMatrix4(e);let r=He.setFromRotationMatrix(e),i=Ae.copy(e).multiply(this._parentMatrixWorld),o=te.copy(this._boneAxis).applyMatrix4(this._initialLocalMatrix).applyMatrix4(i).sub(ye).normalize(),n=Oe.copy(this.settings.gravityDir).applyQuaternion(r).normalize(),l=this._getMatrixCenterToWorld(Z);re.copy(this._currentTail).add(v.copy(this._currentTail).sub(this._prevTail).multiplyScalar(1-this.settings.dragForce)).add(v.copy(o).multiplyScalar(this.settings.stiffness*t)).add(v.copy(n).multiplyScalar(this.settings.gravityPower*t)).applyMatrix4(l),re.sub(z).normalize().multiplyScalar(this._worldSpaceBoneLength).add(z),this._collision(re),e=this._getMatrixWorldToCenter(Z),this._prevTail.copy(this._currentTail),this._currentTail.copy(v.copy(re).applyMatrix4(e));let s=le(Z.copy(this._parentMatrixWorld).multiply(this._initialLocalMatrix)),d=He.setFromUnitVectors(this._boneAxis,v.copy(re).applyMatrix4(s).normalize());this.bone.quaternion.copy(this._initialLocalRotation).multiply(d),this.bone.updateMatrix(),this.bone.matrixWorld.multiplyMatrices(this._parentMatrixWorld,this.bone.matrix)}_collision(t){this.colliderGroups.forEach(e=>{e.colliders.forEach(r=>{let i=r.shape.calculateCollision(r.matrixWorld,t,this.settings.hitRadius,v);i<0&&(t.add(v.multiplyScalar(-i)),t.sub(z).normalize().multiplyScalar(this._worldSpaceBoneLength).add(z))})})}_calcWorldSpaceBoneLength(){v.setFromMatrixPosition(this.bone.matrixWorld),this.child?te.setFromMatrixPosition(this.child.matrixWorld):(te.copy(this._initialLocalChildPosition),te.applyMatrix4(this.bone.matrixWorld)),this._worldSpaceBoneLength=v.sub(te).length()}_getMatrixCenterToWorld(t){return this._center?t.copy(this._center.matrixWorld):t.identity(),t}_getMatrixWorldToCenter(t){return this._center?t.copy(this._center.userData.inverseCacheProxy.inverse):t.identity(),t}};import*as S from"three";function Ve(a,t){let e=[],r=a;for(;r!==null;)e.unshift(r),r=r.parent;e.forEach(i=>{t(i)})}function ue(a,t){a.children.forEach(e=>{t(e)||ue(e,t)})}var ie=class{constructor(){this._joints=new Set;this._objectSpringBonesMap=new Map}get joints(){return this._joints}get springBones(){return console.warn("VRMSpringBoneManager: springBones is deprecated. use joints instead."),this._joints}get colliderGroups(){let t=new Set;return this._joints.forEach(e=>{e.colliderGroups.forEach(r=>{t.add(r)})}),Array.from(t)}get colliders(){let t=new Set;return this.colliderGroups.forEach(e=>{e.colliders.forEach(r=>{t.add(r)})}),Array.from(t)}addJoint(t){this._joints.add(t);let e=this._objectSpringBonesMap.get(t.bone);e==null&&(e=new Set,this._objectSpringBonesMap.set(t.bone,e)),e.add(t)}addSpringBone(t){console.warn("VRMSpringBoneManager: addSpringBone() is deprecated. use addJoint() instead."),this.addJoint(t)}deleteJoint(t){this._joints.delete(t),this._objectSpringBonesMap.get(t.bone).delete(t)}deleteSpringBone(t){console.warn("VRMSpringBoneManager: deleteSpringBone() is deprecated. use deleteJoint() instead."),this.deleteJoint(t)}setInitState(){let t=new Set,e=new Set,r=new Set;for(let i of this._joints)this._processSpringBone(i,t,e,r,o=>o.setInitState())}reset(){let t=new Set,e=new Set,r=new Set;for(let i of this._joints)this._processSpringBone(i,t,e,r,o=>o.reset())}update(t){let e=new Set,r=new Set,i=new Set;for(let o of this._joints)this._processSpringBone(o,e,r,i,n=>n.update(t)),ue(o.bone,n=>{var l,s;return((s=(l=this._objectSpringBonesMap.get(n))==null?void 0:l.size)!=null?s:0)>0?!0:(n.updateWorldMatrix(!1,!1),!1)})}_processSpringBone(t,e,r,i,o){if(r.has(t))return;if(e.has(t))throw new Error("VRMSpringBoneManager: Circular dependency detected while updating springbones");e.add(t);let n=this._getDependencies(t);for(let l of n)Ve(l,s=>{let d=this._objectSpringBonesMap.get(s);if(d)for(let g of d)this._processSpringBone(g,e,r,i,o);else i.has(s)||(s.updateWorldMatrix(!1,!1),i.add(s))});t.bone.updateMatrix(),t.bone.updateWorldMatrix(!1,!1),o(t),i.add(t.bone),r.add(t)}_getDependencies(t){let e=new Set,r=t.bone.parent;return r&&e.add(r),t.colliderGroups.forEach(i=>{i.colliders.forEach(o=>{e.add(o)})}),e}};var ve="VRMC_springBone_extended_collider",Le=new Set(["1.0","1.0-beta"]),je=new Set(["1.0"]),O=class O{get name(){return O.EXTENSION_NAME}constructor(t,e){var r;this.parser=t,this.jointHelperRoot=e==null?void 0:e.jointHelperRoot,this.colliderHelperRoot=e==null?void 0:e.colliderHelperRoot,this.useExtendedColliders=(r=e==null?void 0:e.useExtendedColliders)!=null?r:!0}afterRoot(t){return Q(this,null,function*(){t.userData.vrmSpringBoneManager=yield this._import(t)})}_import(t){return Q(this,null,function*(){let e=yield this._v1Import(t);if(e!=null)return e;let r=yield this._v0Import(t);return r!=null?r:null})}_v1Import(t){return Q(this,null,function*(){var g,q,$,h,W;let e=t.parser.json;if(!(((g=e.extensionsUsed)==null?void 0:g.indexOf(O.EXTENSION_NAME))!==-1))return null;let i=new ie,o=yield t.parser.getDependencies("node"),n=(q=e.extensions)==null?void 0:q[O.EXTENSION_NAME];if(!n)return null;let l=n.specVersion;if(!Le.has(l))return console.warn(`VRMSpringBoneLoaderPlugin: Unknown ${O.EXTENSION_NAME} specVersion "${l}"`),null;let s=($=n.colliders)==null?void 0:$.map((E,T)=>{var p,M,_,x,A,L,b,H,B,k,fe,Ee,Re,_e,me;let u=o[E.node],f=E.shape,R=(p=E.extensions)==null?void 0:p[ve];if(this.useExtendedColliders&&R!=null){let Se=R.specVersion;if(!je.has(Se))console.warn(`VRMSpringBoneLoaderPlugin: Unknown ${ve} specVersion "${Se}". Fallbacking to the ${O.EXTENSION_NAME} definition`);else{let m=R.shape;if(m.sphere)return this._importSphereCollider(u,{offset:new S.Vector3().fromArray((M=m.sphere.offset)!=null?M:[0,0,0]),radius:(_=m.sphere.radius)!=null?_:0,inside:(x=m.sphere.inside)!=null?x:!1});if(m.capsule)return this._importCapsuleCollider(u,{offset:new S.Vector3().fromArray((A=m.capsule.offset)!=null?A:[0,0,0]),radius:(L=m.capsule.radius)!=null?L:0,tail:new S.Vector3().fromArray((b=m.capsule.tail)!=null?b:[0,0,0]),inside:(H=m.capsule.inside)!=null?H:!1});if(m.plane)return this._importPlaneCollider(u,{offset:new S.Vector3().fromArray((B=m.plane.offset)!=null?B:[0,0,0]),normal:new S.Vector3().fromArray((k=m.plane.normal)!=null?k:[0,0,1])})}}if(f.sphere)return this._importSphereCollider(u,{offset:new S.Vector3().fromArray((fe=f.sphere.offset)!=null?fe:[0,0,0]),radius:(Ee=f.sphere.radius)!=null?Ee:0,inside:!1});if(f.capsule)return this._importCapsuleCollider(u,{offset:new S.Vector3().fromArray((Re=f.capsule.offset)!=null?Re:[0,0,0]),radius:(_e=f.capsule.radius)!=null?_e:0,tail:new S.Vector3().fromArray((me=f.capsule.tail)!=null?me:[0,0,0]),inside:!1});throw new Error(`VRMSpringBoneLoaderPlugin: The collider #${T} has no valid shape`)}),d=(h=n.colliderGroups)==null?void 0:h.map((E,T)=>{var f;return{colliders:((f=E.colliders)!=null?f:[]).map(R=>{let p=s==null?void 0:s[R];if(p==null)throw new Error(`VRMSpringBoneLoaderPlugin: The colliderGroup #${T} attempted to use a collider #${R} but not found`);return p}),name:E.name}});return(W=n.springs)==null||W.forEach((E,T)=>{var M;let u=E.joints,f=(M=E.colliderGroups)==null?void 0:M.map(_=>{let x=d==null?void 0:d[_];if(x==null)throw new Error(`VRMSpringBoneLoaderPlugin: The spring #${T} attempted to use a colliderGroup ${_} but not found`);return x}),R=E.center!=null?o[E.center]:void 0,p;u.forEach(_=>{if(p){let x=p.node,A=o[x],L=_.node,b=o[L],H={hitRadius:p.hitRadius,dragForce:p.dragForce,gravityPower:p.gravityPower,stiffness:p.stiffness,gravityDir:p.gravityDir!=null?new S.Vector3().fromArray(p.gravityDir):void 0},B=this._importJoint(A,b,H,f);R&&(B.center=R),i.addJoint(B)}p=_})}),i.setInitState(),i})}_v0Import(t){return Q(this,null,function*(){var g,q,$;let e=t.parser.json;if(!(((g=e.extensionsUsed)==null?void 0:g.indexOf("VRM"))!==-1))return null;let i=(q=e.extensions)==null?void 0:q.VRM,o=i==null?void 0:i.secondaryAnimation;if(!o)return null;let n=o==null?void 0:o.boneGroups;if(!n)return null;let l=new ie,s=yield t.parser.getDependencies("node"),d=($=o.colliderGroups)==null?void 0:$.map(h=>{var T;let W=s[h.node];return{colliders:((T=h.colliders)!=null?T:[]).map((u,f)=>{var p,M,_;let R=new S.Vector3(0,0,0);return u.offset&&R.set((p=u.offset.x)!=null?p:0,(M=u.offset.y)!=null?M:0,u.offset.z?-u.offset.z:0),this._importSphereCollider(W,{offset:R,radius:(_=u.radius)!=null?_:0,inside:!1})})}});return n==null||n.forEach((h,W)=>{let E=h.bones;E&&E.forEach(T=>{var _,x,A,L;let u=s[T],f=new S.Vector3;h.gravityDir?f.set((_=h.gravityDir.x)!=null?_:0,(x=h.gravityDir.y)!=null?x:0,(A=h.gravityDir.z)!=null?A:0):f.set(0,-1,0);let R=h.center!=null?s[h.center]:void 0,p={hitRadius:h.hitRadius,dragForce:h.dragForce,gravityPower:h.gravityPower,stiffness:h.stiffiness,gravityDir:f},M=(L=h.colliderGroups)==null?void 0:L.map(b=>{let H=d==null?void 0:d[b];if(H==null)throw new Error(`VRMSpringBoneLoaderPlugin: The spring #${W} attempted to use a colliderGroup ${b} but not found`);return H});u.traverse(b=>{var k;let H=(k=b.children[0])!=null?k:null,B=this._importJoint(b,H,p,M);R&&(B.center=R),l.addJoint(B)})})}),t.scene.updateMatrixWorld(),l.setInitState(),l})}_importJoint(t,e,r,i){let o=new ce(t,e,r,i);if(this.jointHelperRoot){let n=new ee(o);this.jointHelperRoot.add(n),n.renderOrder=this.jointHelperRoot.renderOrder}return o}_importSphereCollider(t,e){let r=new U(e),i=new N(r);if(t.add(i),this.colliderHelperRoot){let o=new J(i);this.colliderHelperRoot.add(o),o.renderOrder=this.colliderHelperRoot.renderOrder}return i}_importCapsuleCollider(t,e){let r=new X(e),i=new N(r);if(t.add(i),this.colliderHelperRoot){let o=new J(i);this.colliderHelperRoot.add(o),o.renderOrder=this.colliderHelperRoot.renderOrder}return i}_importPlaneCollider(t,e){let r=new Y(e),i=new N(r);if(t.add(i),this.colliderHelperRoot){let o=new J(i);this.colliderHelperRoot.add(o),o.renderOrder=this.colliderHelperRoot.renderOrder}return i}};O.EXTENSION_NAME="VRMC_springBone";var Be=O;export{N as VRMSpringBoneCollider,J as VRMSpringBoneColliderHelper,w as VRMSpringBoneColliderShape,X as VRMSpringBoneColliderShapeCapsule,Y as VRMSpringBoneColliderShapePlane,U as VRMSpringBoneColliderShapeSphere,ce as VRMSpringBoneJoint,ee as VRMSpringBoneJointHelper,Be as VRMSpringBoneLoaderPlugin,ie as VRMSpringBoneManager};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pixiv/three-vrm-springbone",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Spring bone module for @pixiv/three-vrm",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "pixiv",
|
|
@@ -51,8 +51,9 @@
|
|
|
51
51
|
]
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@pixiv/types-vrm-0.0": "3.
|
|
55
|
-
"@pixiv/types-vrmc-springbone-1.0": "3.
|
|
54
|
+
"@pixiv/types-vrm-0.0": "3.1.0",
|
|
55
|
+
"@pixiv/types-vrmc-springbone-1.0": "3.1.0",
|
|
56
|
+
"@pixiv/types-vrmc-springbone-extended-collider-1.0": "3.1.0"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
59
|
"@types/three": "^0.167.0",
|
|
@@ -61,5 +62,5 @@
|
|
|
61
62
|
"peerDependencies": {
|
|
62
63
|
"three": ">=0.137"
|
|
63
64
|
},
|
|
64
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "2e7ddda794c067440c420bada91f2b05f581c2f6"
|
|
65
66
|
}
|
|
@@ -3,21 +3,26 @@ import { VRMSpringBoneColliderShape } from './VRMSpringBoneColliderShape';
|
|
|
3
3
|
export declare class VRMSpringBoneColliderShapeCapsule extends VRMSpringBoneColliderShape {
|
|
4
4
|
get type(): 'capsule';
|
|
5
5
|
/**
|
|
6
|
-
* The offset of the head from the origin.
|
|
6
|
+
* The offset of the capsule head from the origin in local space.
|
|
7
7
|
*/
|
|
8
8
|
offset: THREE.Vector3;
|
|
9
9
|
/**
|
|
10
|
-
* The offset of the tail from the origin.
|
|
10
|
+
* The offset of the capsule tail from the origin in local space.
|
|
11
11
|
*/
|
|
12
12
|
tail: THREE.Vector3;
|
|
13
13
|
/**
|
|
14
|
-
* The radius.
|
|
14
|
+
* The radius of the capsule.
|
|
15
15
|
*/
|
|
16
16
|
radius: number;
|
|
17
|
+
/**
|
|
18
|
+
* If true, the collider prevents spring bones from going outside of the capsule instead.
|
|
19
|
+
*/
|
|
20
|
+
inside: boolean;
|
|
17
21
|
constructor(params?: {
|
|
18
22
|
radius?: number;
|
|
19
23
|
offset?: THREE.Vector3;
|
|
20
24
|
tail?: THREE.Vector3;
|
|
25
|
+
inside?: boolean;
|
|
21
26
|
});
|
|
22
27
|
calculateCollision(colliderMatrix: THREE.Matrix4, objectPosition: THREE.Vector3, objectRadius: number, target: THREE.Vector3): number;
|
|
23
28
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { VRMSpringBoneColliderShape } from './VRMSpringBoneColliderShape';
|
|
3
|
+
export declare class VRMSpringBoneColliderShapePlane extends VRMSpringBoneColliderShape {
|
|
4
|
+
get type(): 'plane';
|
|
5
|
+
/**
|
|
6
|
+
* The offset of the plane from the origin in local space.
|
|
7
|
+
*/
|
|
8
|
+
offset: THREE.Vector3;
|
|
9
|
+
/**
|
|
10
|
+
* The normal of the plane in local space. Must be normalized.
|
|
11
|
+
*/
|
|
12
|
+
normal: THREE.Vector3;
|
|
13
|
+
constructor(params?: {
|
|
14
|
+
offset?: THREE.Vector3;
|
|
15
|
+
normal?: THREE.Vector3;
|
|
16
|
+
});
|
|
17
|
+
calculateCollision(colliderMatrix: THREE.Matrix4, objectPosition: THREE.Vector3, objectRadius: number, target: THREE.Vector3): number;
|
|
18
|
+
}
|
|
@@ -3,16 +3,21 @@ import { VRMSpringBoneColliderShape } from './VRMSpringBoneColliderShape';
|
|
|
3
3
|
export declare class VRMSpringBoneColliderShapeSphere extends VRMSpringBoneColliderShape {
|
|
4
4
|
get type(): 'sphere';
|
|
5
5
|
/**
|
|
6
|
-
* The offset from the origin.
|
|
6
|
+
* The offset of the sphere from the origin in local space.
|
|
7
7
|
*/
|
|
8
8
|
offset: THREE.Vector3;
|
|
9
9
|
/**
|
|
10
10
|
* The radius.
|
|
11
11
|
*/
|
|
12
12
|
radius: number;
|
|
13
|
+
/**
|
|
14
|
+
* If true, the collider prevents spring bones from going outside of the sphere instead.
|
|
15
|
+
*/
|
|
16
|
+
inside: boolean;
|
|
13
17
|
constructor(params?: {
|
|
14
18
|
radius?: number;
|
|
15
19
|
offset?: THREE.Vector3;
|
|
20
|
+
inside?: boolean;
|
|
16
21
|
});
|
|
17
22
|
calculateCollision(colliderMatrix: THREE.Matrix4, objectPosition: THREE.Vector3, objectRadius: number, target: THREE.Vector3): number;
|
|
18
23
|
}
|
|
@@ -15,6 +15,12 @@ export declare class VRMSpringBoneLoaderPlugin implements GLTFLoaderPlugin {
|
|
|
15
15
|
* If `renderOrder` is set to the root, helpers will copy the same `renderOrder` .
|
|
16
16
|
*/
|
|
17
17
|
colliderHelperRoot?: THREE.Object3D;
|
|
18
|
+
/**
|
|
19
|
+
* If true, load colliders defined in `VRMC_springBone_extended_collider`.
|
|
20
|
+
* Set to `false` to disable loading extended colliders and use the fallback behavior.
|
|
21
|
+
* `true` by default.
|
|
22
|
+
*/
|
|
23
|
+
useExtendedColliders: boolean;
|
|
18
24
|
readonly parser: GLTFParser;
|
|
19
25
|
get name(): string;
|
|
20
26
|
constructor(parser: GLTFParser, options?: VRMSpringBoneLoaderPluginOptions);
|
|
@@ -31,4 +37,5 @@ export declare class VRMSpringBoneLoaderPlugin implements GLTFLoaderPlugin {
|
|
|
31
37
|
private _importJoint;
|
|
32
38
|
private _importSphereCollider;
|
|
33
39
|
private _importCapsuleCollider;
|
|
40
|
+
private _importPlaneCollider;
|
|
34
41
|
}
|
|
@@ -12,4 +12,10 @@ export interface VRMSpringBoneLoaderPluginOptions {
|
|
|
12
12
|
* If `renderOrder` is set to the root, helpers will copy the same `renderOrder` .
|
|
13
13
|
*/
|
|
14
14
|
colliderHelperRoot?: THREE.Object3D;
|
|
15
|
+
/**
|
|
16
|
+
* If true, load colliders defined in `VRMC_springBone_extended_collider`.
|
|
17
|
+
* Set to `false` to disable loading extended colliders and use the fallback behavior.
|
|
18
|
+
* `true` by default.
|
|
19
|
+
*/
|
|
20
|
+
useExtendedColliders?: boolean;
|
|
15
21
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { VRMSpringBoneColliderShapePlane } from '../../VRMSpringBoneColliderShapePlane';
|
|
3
|
+
import { ColliderShapeBufferGeometry } from './ColliderShapeBufferGeometry';
|
|
4
|
+
export declare class ColliderShapePlaneBufferGeometry extends THREE.BufferGeometry implements ColliderShapeBufferGeometry {
|
|
5
|
+
worldScale: number;
|
|
6
|
+
private readonly _attrPos;
|
|
7
|
+
private readonly _attrIndex;
|
|
8
|
+
private readonly _shape;
|
|
9
|
+
private readonly _currentOffset;
|
|
10
|
+
private readonly _currentNormal;
|
|
11
|
+
constructor(shape: VRMSpringBoneColliderShapePlane);
|
|
12
|
+
update(): void;
|
|
13
|
+
private _buildPosition;
|
|
14
|
+
private _buildIndex;
|
|
15
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './VRMSpringBoneCollider';
|
|
|
3
3
|
export * from './VRMSpringBoneColliderGroup';
|
|
4
4
|
export * from './VRMSpringBoneColliderShape';
|
|
5
5
|
export * from './VRMSpringBoneColliderShapeCapsule';
|
|
6
|
+
export * from './VRMSpringBoneColliderShapePlane';
|
|
6
7
|
export * from './VRMSpringBoneColliderShapeSphere';
|
|
7
8
|
export * from './VRMSpringBoneJoint';
|
|
8
9
|
export * from './VRMSpringBoneLoaderPlugin';
|