Skip to content

Commit

Permalink
Add Root Node (#1028)
Browse files Browse the repository at this point in the history
Add an additional root node when only one joint into skeleton
  • Loading branch information
pandaGaume committed Feb 10, 2022
1 parent 1bf7a27 commit 80cfea5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion 3ds Max/Max2Babylon/Exporter/BabylonExporter.Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,9 @@ int CreateGlobalVertex(IIGameMesh mesh, BabylonAbstractMesh babylonAbstractMesh,
float[] weight = new float[4] { 0, 0, 0, 0 };
int[] bone = new int[4] { 0, 0, 0, 0 };
var nbBones = skin.GetNumberOfBones(vertexIndex);
// Babylon, nor GLTF do not support single bone skeleton, we may add a root node with no transform.
// this is echoing the process into BabylonExporter.Skeleton ExportBones(Skin)
var offset = nbBones == 1 ? 1 : 0;

int currentVtxBone = 0;
int currentSkinBone = 0;
Expand All @@ -1192,7 +1195,7 @@ int CreateGlobalVertex(IIGameMesh mesh, BabylonAbstractMesh babylonAbstractMesh,
if (boneWeight <= 0)
continue;

bone[currentVtxBone] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID);
bone[currentVtxBone] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID) + offset; // add optional offset
weight[currentVtxBone] = skin.GetWeight(vertexIndex, currentSkinBone);
++currentVtxBone;
}
Expand Down
22 changes: 22 additions & 0 deletions 3ds Max/Max2Babylon/Exporter/BabylonExporter.Skeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,28 @@ private BabylonBone[] ExportBones(IIGameSkin skin)
bones.Add(bone);
}

// Babylon, nor GLTF do not support single bone skeleton, we may add a root node with no transform.
if( bones.Count == 1)
{
var root = new BabylonBone()
{
id = Guid.NewGuid().ToString(),
parentNodeId = null,
name = "root-bone-added",
index = 0,
parentBoneIndex = -1,
matrix = BabylonMatrix.Identity().m
};

var bone = bones[0];
bone.parentNodeId = root.id;
bone.index = 1;
bone.parentBoneIndex = 0;

bones.Insert(0, root);
}

// finally return the bones.
return bones.ToArray();
}
}
Expand Down

0 comments on commit 80cfea5

Please sign in to comment.