diff --git a/src/library/VRMExporterv0.js b/src/library/VRMExporterv0.js index c7156877..3c704b1c 100644 --- a/src/library/VRMExporterv0.js +++ b/src/library/VRMExporterv0.js @@ -516,26 +516,22 @@ export default class VRMExporterv0 { } } }) + console.log(rootSpringBones); // should be fetched from rootSpringBonesIndexes instead const colliderGroups = []; - const colliderGroupsIndexes = []; const skeleton = meshes.find(mesh => mesh.isSkinnedMesh)?.skeleton || null; - console.log(skeleton.bones); - let count = 0; for (let i =0; i < skeleton.bones.length;i++){ const bn = skeleton.bones[i]; if (bn.userData.VRMcolliders){ - colliderGroupsIndexes.push(count); - count++; - console.log(bn.userData.VRMcolliders); // get the node value here const colliderGroup = { node:nodeNames.indexOf(bn.name), - colliders:[] + colliders:[], + name:bn.name } bn.userData.VRMcolliders.forEach(collider => { const sphere = collider.sphere @@ -547,20 +543,7 @@ export default class VRMExporterv0 { colliderGroups.push(colliderGroup) } } - console.log(colliderGroups); - console.log(colliderGroupsIndexes); - // old way, we were hard coding the collider bone, we should fetch it instead - // colliderBones.forEach((colliderBone, i) => { - // const nodeIndex = nodes.indexOf(colliderBone); - // const colliderGroup = { - // "colliders": [ - // { "offset": { "x": 0, "y": 0.05, "z": 0 }, "radius": 0.075 } - // ], - // "node": nodeIndex - // } - // colliderGroups.push(colliderGroup); - // colliderGroupsIndexes.push(i); - // }) + console.log("COLLIDER GROUPS", colliderGroups); const findBoneIndex = (boneName) =>{ for (let i = 0; i < nodes.length; i++) { @@ -572,11 +555,44 @@ export default class VRMExporterv0 { return -1; } + // returns the bone index of the bones name and its childrens + const findBoneIndices = (boneName) =>{ + const bnIndex = findBoneIndex(boneName); + if (bnIndex == -1){ + return [-1] + } + else{ + const result = []; + const rootBone = nodes[bnIndex] + rootBone.traverse((child)=>{ + if (child.isBone){ + result.push(findBoneIndex(child.name)); + } + }) + return result; + } + } + const boneGroups = []; rootSpringBones.forEach(springBone => { - let boneIndex = findBoneIndex(springBone.name); + const boneIndices = findBoneIndices(springBone.name); + + // get the collider group indices + const colliderIndices = []; + springBone.colliderGroups.forEach(colliderGroup => { + const springCollider = colliderGroup.colliders[0]; + const springParent = springCollider.parent; + + const ind = colliderGroups.findIndex(group => group.name === springParent.name); + if (ind != -1){ + colliderIndices.push(ind); + } + else{ + console.warn("no collider group for bone name: ", springParent.name + " was found"); + } + }); - if (boneIndex === -1) { + if (boneIndices === [-1]) { console.warn("No bone found for spring bone " + springBone.name); return; // Skip to the next iteration } @@ -587,9 +603,9 @@ export default class VRMExporterv0 { boneGroups.push( { - bones: [boneIndex], + bones: boneIndices, center:centerIndex, - colliderGroups: colliderGroupsIndexes, // XXX validate, currently placing all indices + colliderGroups: colliderIndices, dragForce: settings.dragForce, gravityDir: { x: settings.gravityDir.x, y: settings.gravityDir.y, z: settings.gravityDir.z }, gravityPower: settings.gravityPower,