Skip to content

Commit 80cfea5

Browse files
authored
Add Root Node (#1028)
Add an additional root node when only one joint into skeleton
1 parent 1bf7a27 commit 80cfea5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

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

11851188
int currentVtxBone = 0;
11861189
int currentSkinBone = 0;
@@ -1192,7 +1195,7 @@ int CreateGlobalVertex(IIGameMesh mesh, BabylonAbstractMesh babylonAbstractMesh,
11921195
if (boneWeight <= 0)
11931196
continue;
11941197

1195-
bone[currentVtxBone] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID);
1198+
bone[currentVtxBone] = boneIds.IndexOf(skin.GetIGameBone(vertexIndex, currentSkinBone).NodeID) + offset; // add optional offset
11961199
weight[currentVtxBone] = skin.GetWeight(vertexIndex, currentSkinBone);
11971200
++currentVtxBone;
11981201
}

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

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

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

0 commit comments

Comments
 (0)