Skip to content

Commit

Permalink
Merge pull request #141 from M3-org/fix-various-issues-2
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
madjin authored Mar 16, 2024
2 parents f27be5d + 8355a4d commit 366d9a5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
31 changes: 9 additions & 22 deletions src/library/VRMExporterv0.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,28 +554,11 @@ 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 => {
const boneIndices = findBoneIndices(springBone.name);
if (boneIndices[0] === -1) {
//const boneIndices = findBoneIndices(springBone.name);
const boneIndex = findBoneIndex(springBone.name)
if (boneIndex === -1) {
console.warn("Spring bone " + springBone.name + " was removed during cleanup process. Skipping.");
return; // Skip to the next iteration
}
Expand All @@ -588,7 +571,8 @@ export default class VRMExporterv0 {
const springParent = springCollider.parent;
const ind = colliderGroups.findIndex(group => group.name === springParent.name);
if (ind != -1){
colliderIndices.push(ind);
if (!colliderIndices.includes(ind))
colliderIndices.push(ind);
}
else{
console.warn("No collider group for bone name: ", springParent.name + " was found");
Expand All @@ -604,10 +588,12 @@ export default class VRMExporterv0 {
if (centerIndex == -1) console.warn("no center bone for spring bone " + springBone.name);
// springBone: bone:boneObject, center:boneObject, string:name, array:colliderGroup, settings:object,
const settings = springBone.settings;

// FIX!!

boneGroups.push(
{
bones: boneIndices,
bones: [boneIndex],
center:centerIndex,
colliderGroups: colliderIndices,
dragForce: settings.dragForce,
Expand All @@ -625,6 +611,7 @@ export default class VRMExporterv0 {
}
console.log(outputSecondaryAnimation);


outputVrmMeta.texture = icon ? outputImages.length - 1 : undefined;
const bufferViews = [];
bufferViews.push(...images.map((image) => ({
Expand Down
5 changes: 4 additions & 1 deletion src/library/load-utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VRMLoaderPlugin, VRMUtils } from '@pixiv/three-vrm';
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader"
import { getAsArray, renameVRMBones } from "../library/utils"
import { getAsArray, renameVRMBones,getUniqueId } from "../library/utils"
import { findChildByName } from '../library/utils';
import { PropertyBinding } from 'three';

Expand Down Expand Up @@ -72,6 +72,8 @@ const saveVRM0Colliders = (gltf) => {
offset:[collider.offset.x,collider.offset.y, collider.offset.z]
}}));
}
// add a unique id so we dont duplicat them later when merging different colliders
nodeObject.userData.VRMcollidersID = getUniqueId();
});
}
}
Expand Down Expand Up @@ -108,6 +110,7 @@ const saveVRM1Colliders = (gltf) => {
shape.offset[0] = -shape.offset[0];
}
}
currentNode.userData.VRMcollidersID = getUniqueId();
currentNode.userData.VRMcolliders.push(colliderShape)
}
else {
Expand Down
12 changes: 8 additions & 4 deletions src/library/merge-geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ function createMergedSkeleton(meshes, scale){
clone.bone.userData.VRMcolliders = bone.userData.VRMcolliders;
}
else{
clone.bone.userData.VRMcolliders = [
...clone.bone.userData.VRMcolliders,
...bone.userData.VRMcolliders
];
// compare before merge if its not already added:
// this case happens when a single trait model includes more than on skinned mesh
if (bone.userData.VRMcollidersID != clone.bone.userData.VRMcollidersID){
clone.bone.userData.VRMcolliders = [
...clone.bone.userData.VRMcolliders,
...bone.userData.VRMcolliders
];
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/library/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,17 @@ export const createBoneDirection = (skinMesh) => {
}
geometry.userData.boneDirections = boneDirections;
};

export const getUniqueId = () => {
const timestamp = new Date().getTime();

const random = Math.random().toString(36).substr(2, 9); // Using base36 encoding

const uniqueId = timestamp + '-' + random;

return uniqueId;
}

export const renameVRMBones = (vrm) => {
const bones = vrm.humanoid.humanBones;

Expand Down

0 comments on commit 366d9a5

Please sign in to comment.