Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 72 additions & 7 deletions SharedProjects/Babylon2GLTF/GLTFExporter.Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,41 @@ private GLTFMesh ExportMesh(BabylonMesh babylonMesh, GLTF gltf, BabylonScene bab
{
// In babylon, the 4 bones indices are stored in a single int
// Each bone index is 8-bit offset from the next
ushort[] unpackBabylonBonesToArray (uint babylonBoneIndices)
{
uint bone3 = babylonBoneIndices >> 24;
uint bone2 = (babylonBoneIndices << 8) >> 24;
uint bone1 = (babylonBoneIndices << 16) >> 24;
uint bone0 = (babylonBoneIndices << 24) >> 24;
babylonBoneIndices -= bone0 << 0;
return new ushort[] { (ushort)bone0, (ushort)bone1, (ushort)bone2, (ushort)bone3 };
}

uint bonesIndicesMerged = (uint)meshData.matricesIndices[indexVertex];
uint bone3 = bonesIndicesMerged >> 24;
uint bone2 = (bonesIndicesMerged << 8 ) >> 24;
uint bone1 = (bonesIndicesMerged << 16) >> 24;
uint bone0 = (bonesIndicesMerged << 24) >> 24;
bonesIndicesMerged -= bone0 << 0;
var bonesIndicesArray = new ushort[] { (ushort)bone0, (ushort)bone1, (ushort)bone2, (ushort)bone3 };
globalVertex.BonesIndices = bonesIndicesArray;
globalVertex.BonesIndices = unpackBabylonBonesToArray(bonesIndicesMerged);
globalVertex.BonesWeights = ArrayExtension.SubArrayFromEntity(meshData.matricesWeights, indexVertex, 4);
void clearBoneUnusedIndices(ushort[] indices, float[] weights)
{
for (int i = 0; i < indices.Length; ++i)
{
// Zero out indices of unused joint weights to avoid ACCESSOR_JOINTS_USED_ZERO_WEIGHT.
if (MathUtilities.IsAlmostEqualTo(weights[i], 0, float.Epsilon))
{
indices[i] = 0;
}
}
}

clearBoneUnusedIndices(globalVertex.BonesIndices, globalVertex.BonesWeights);

if (hasBonesExtra)
{
uint bonesIndicesExtraMerged = (uint)meshData.matricesIndicesExtra[indexVertex];
globalVertex.BonesIndicesExtra = unpackBabylonBonesToArray(bonesIndicesExtraMerged);
globalVertex.BonesWeightsExtra = ArrayExtension.SubArrayFromEntity(meshData.matricesWeightsExtra, indexVertex, 4);

clearBoneUnusedIndices(globalVertex.BonesIndicesExtra, globalVertex.BonesWeightsExtra);
}
}

globalVertices.Add(globalVertex);
Expand Down Expand Up @@ -367,6 +393,11 @@ private GLTFMesh ExportMesh(BabylonMesh babylonMesh, GLTF gltf, BabylonScene bab

meshPrimitive.attributes.Add(GLTFMeshPrimitive.Attribute.JOINTS_0.ToString(), tmpGltfMeshPrimitive.attributes[GLTFMeshPrimitive.Attribute.JOINTS_0.ToString()]);
meshPrimitive.attributes.Add(GLTFMeshPrimitive.Attribute.WEIGHTS_0.ToString(), tmpGltfMeshPrimitive.attributes[GLTFMeshPrimitive.Attribute.WEIGHTS_0.ToString()]);
if (hasBonesExtra)
{
meshPrimitive.attributes.Add(GLTFMeshPrimitive.Attribute.JOINTS_1.ToString(), tmpGltfMeshPrimitive.attributes[GLTFMeshPrimitive.Attribute.JOINTS_1.ToString()]);
meshPrimitive.attributes.Add(GLTFMeshPrimitive.Attribute.WEIGHTS_1.ToString(), tmpGltfMeshPrimitive.attributes[GLTFMeshPrimitive.Attribute.WEIGHTS_1.ToString()]);
}
sharedSkinnedMeshesByOriginal[tmpGltfMesh].Add(gltfMesh);
}
else
Expand All @@ -387,6 +418,23 @@ private GLTFMesh ExportMesh(BabylonMesh babylonMesh, GLTF gltf, BabylonScene bab
joints.ForEach(n => accessorJoints.bytesList.AddRange(BitConverter.GetBytes(n)));
accessorJoints.count = globalVerticesSubMesh.Count;

if (hasBonesExtra)
{
// --- Joints Extra ---
var accessorJointsExtra = GLTFBufferService.Instance.CreateAccessor(
gltf,
GLTFBufferService.Instance.GetBufferViewUnsignedShortVec4(gltf, buffer),
"accessorJointsExtra",
GLTFAccessor.ComponentType.UNSIGNED_SHORT,
GLTFAccessor.TypeEnum.VEC4
);
meshPrimitive.attributes.Add(GLTFMeshPrimitive.Attribute.JOINTS_1.ToString(), accessorJointsExtra.index);
// Populate accessor
List<ushort> jointsExtra = globalVerticesSubMesh.SelectMany(v => new[] { v.BonesIndicesExtra[0], v.BonesIndicesExtra[1], v.BonesIndicesExtra[2], v.BonesIndicesExtra[3] }).ToList();
jointsExtra.ForEach(n => accessorJointsExtra.bytesList.AddRange(BitConverter.GetBytes(n)));
accessorJointsExtra.count = globalVerticesSubMesh.Count;
}

// --- Weights ---
var accessorWeights = GLTFBufferService.Instance.CreateAccessor(
gltf,
Expand All @@ -400,6 +448,23 @@ private GLTFMesh ExportMesh(BabylonMesh babylonMesh, GLTF gltf, BabylonScene bab
List<float> weightBones = globalVerticesSubMesh.SelectMany(v => new[] { v.BonesWeights[0], v.BonesWeights[1], v.BonesWeights[2], v.BonesWeights[3] }).ToList();
weightBones.ForEach(n => accessorWeights.bytesList.AddRange(BitConverter.GetBytes(n)));
accessorWeights.count = globalVerticesSubMesh.Count;

if (hasBonesExtra)
{
// --- Weights Extra ---
var accessorWeightsExtra = GLTFBufferService.Instance.CreateAccessor(
gltf,
GLTFBufferService.Instance.GetBufferViewFloatVec4(gltf, buffer),
"accessorWeightsExtra",
GLTFAccessor.ComponentType.FLOAT,
GLTFAccessor.TypeEnum.VEC4
);
meshPrimitive.attributes.Add(GLTFMeshPrimitive.Attribute.WEIGHTS_1.ToString(), accessorWeightsExtra.index);
// Populate accessor
List<float> weightBonesExtra = globalVerticesSubMesh.SelectMany(v => new[] { v.BonesWeightsExtra[0], v.BonesWeightsExtra[1], v.BonesWeightsExtra[2], v.BonesWeightsExtra[3] }).ToList();
weightBonesExtra.ForEach(n => accessorWeightsExtra.bytesList.AddRange(BitConverter.GetBytes(n)));
accessorWeightsExtra.count = globalVerticesSubMesh.Count;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions SharedProjects/Babylon2GLTF/GLTFGlobalVertex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class GLTFGlobalVertex
public BabylonVector2 UV2 { get; set; }
public float[] Color { get; set; }
public ushort[] BonesIndices { get; set; }
public ushort[] BonesIndicesExtra { get; set; }
public float[] BonesWeights { get; set; }
public float[] BonesWeightsExtra { get; set; }
}
}
2 changes: 2 additions & 0 deletions SharedProjects/BabylonExport.Entities/BabylonMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public interface IBabylonMeshData
float[] uvs6 { get; set; }
float[] colors { get; set; }
int[] matricesIndices { get; set; }
int[] matricesIndicesExtra { get; set; }
float[] matricesWeights { get; set; }
float[] matricesWeightsExtra { get; set; }
int[] indices { get; set; }
}

Expand Down
10 changes: 9 additions & 1 deletion SharedProjects/BabylonExport.Entities/BabylonVertexData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ public class BabylonVertexData : IBabylonMeshData
[DataMember]
public int[] matricesIndices { get; set; }

[DataMember]
public int[] matricesIndicesExtra { get; set; }

[DataMember]
public float[] matricesWeights { get; set; }


[DataMember]
public float[] matricesWeightsExtra { get; set; }

[DataMember]
public int[] indices { get; set; }
#endregion
Expand All @@ -76,7 +82,9 @@ public BabylonVertexData(IBabylonMeshData data)
uvs6 = data.uvs6;
colors = data.colors;
matricesIndices = data.matricesIndices;
matricesIndicesExtra = data.matricesIndicesExtra;
matricesWeights = data.matricesWeights;
matricesWeightsExtra = data.matricesWeightsExtra;
indices = data.indices;
}
}
Expand Down
4 changes: 3 additions & 1 deletion SharedProjects/GltfExport.Entities/GLTFMeshPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public enum Attribute
TEXCOORD_1,
COLOR_0,
JOINTS_0,
WEIGHTS_0
JOINTS_1,
WEIGHTS_0,
WEIGHTS_1
}

[DataMember(IsRequired = true)]
Expand Down