Skip to content

Commit 546d859

Browse files
authored
Add root join when single bone skeleton (#1030)
1 parent b893631 commit 546d859

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

3ds Max/Max2Babylon/Exporter/BabylonExporter.Mesh.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,9 @@ int CreateGlobalVertex(IIGameMesh mesh, BabylonAbstractMesh babylonAbstractMesh,
11831183
float[] weight = new float[4] { 0, 0, 0, 0 };
11841184
int[] bone = new int[4] { 0, 0, 0, 0 };
11851185
var nbBones = skin.GetNumberOfBones(vertexIndex);
1186+
// Babylon, nor GLTF do not support single bone skeleton, we may add a root node with no transform.
1187+
// this is echoing the process into BabylonExporter.Skeleton ExportBones(Skin)
1188+
var offset = skin.TotalBoneCount == 1 ? 1 : 0;
11861189

11871190
int currentVtxBone = 0;
11881191
int currentSkinBone = 0;
@@ -1194,7 +1197,7 @@ int CreateGlobalVertex(IIGameMesh mesh, BabylonAbstractMesh babylonAbstractMesh,
11941197
if (boneWeight <= 0)
11951198
continue;
11961199

1197-
bone[currentVtxBone] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID);
1200+
bone[currentVtxBone] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID) + offset; // add optional offset
11981201
weight[currentVtxBone] = skin.GetWeight(vertexIndex, currentSkinBone);
11991202
++currentVtxBone;
12001203
}
@@ -1227,7 +1230,7 @@ int CreateGlobalVertex(IIGameMesh mesh, BabylonAbstractMesh babylonAbstractMesh,
12271230
break;
12281231
}
12291232

1230-
bone[currentVtxBone - 4] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID);
1233+
bone[currentVtxBone - 4] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID) + offset; // add optional offset
12311234
weight[currentVtxBone - 4] = skin.GetWeight(vertexIndex, currentSkinBone);
12321235
++currentVtxBone;
12331236
}

3ds Max/Max2Babylon/Exporter/BabylonExporter.Skeleton.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,28 @@ private BabylonBone[] ExportBones(IIGameSkin skin)
423423
bones.Add(bone);
424424
}
425425

426+
// Babylon, nor GLTF do not support single bone skeleton, we may add a root node with no transform.
427+
if( bones.Count == 1)
428+
{
429+
var root = new BabylonBone()
430+
{
431+
id = Guid.NewGuid().ToString(),
432+
parentNodeId = null,
433+
name = "root-bone-added",
434+
index = 0,
435+
parentBoneIndex = -1,
436+
matrix = BabylonMatrix.Identity().m
437+
};
438+
439+
var bone = bones[0];
440+
bone.parentNodeId = root.id;
441+
bone.index = 1;
442+
bone.parentBoneIndex = 0;
443+
444+
bones.Insert(0, root);
445+
}
446+
447+
// finally return the bones.
426448
return bones.ToArray();
427449
}
428450
}

0 commit comments

Comments
 (0)