Skip to content

Commit

Permalink
assign collider indices with springbone data information
Browse files Browse the repository at this point in the history
  • Loading branch information
memelotsqui committed Nov 19, 2023
1 parent 924d292 commit 246e102
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions src/library/VRMExporterv0.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++) {
Expand All @@ -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
}
Expand All @@ -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,
Expand Down

0 comments on commit 246e102

Please sign in to comment.