Skip to content

Commit 3d292a6

Browse files
authored
Merge pull request #4923 from JeffM2501/gltf_bone_fix
[rmodels] Fix GLTF bone weight assignments for meshes that are parented to armature bones.
2 parents 82c87d1 + e53a43b commit 3d292a6

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/rmodels.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5815,15 +5815,17 @@ static Model LoadGLTF(const char *fileName)
58155815

58165816
for (unsigned int p = 0; p < mesh->primitives_count; p++)
58175817
{
5818+
bool hasJoints = false;
5819+
58185820
// NOTE: We only support primitives defined by triangles
58195821
if (mesh->primitives[p].type != cgltf_primitive_type_triangles) continue;
58205822

58215823
for (unsigned int j = 0; j < mesh->primitives[p].attributes_count; j++)
58225824
{
58235825
// NOTE: JOINTS_1 + WEIGHT_1 will be used for +4 joints influencing a vertex -> Not supported by raylib
5824-
58255826
if (mesh->primitives[p].attributes[j].type == cgltf_attribute_type_joints) // JOINTS_n (vec4: 4 bones max per vertex / u8, u16)
58265827
{
5828+
hasJoints = true;
58275829
cgltf_accessor *attribute = mesh->primitives[p].attributes[j].data;
58285830

58295831
// NOTE: JOINTS_n can only be vec4 and u8/u16
@@ -5924,6 +5926,34 @@ static Model LoadGLTF(const char *fileName)
59245926
}
59255927
}
59265928

5929+
// check if we are animated, and the mesh was not given any bone assignments, but is the child of a bone node
5930+
// in this case we need to fully attach all the verts to the parent bone so it will animate with the bone.
5931+
if (data->skins_count > 0 && !hasJoints && node->parent != NULL && node->parent->mesh == NULL)
5932+
{
5933+
int parentBoneId = -1;
5934+
for (int joint = 0; joint < model.boneCount; joint++)
5935+
{
5936+
if (data->skins[0].joints[joint] == node->parent)
5937+
{
5938+
parentBoneId = joint;
5939+
break;
5940+
}
5941+
}
5942+
5943+
if (parentBoneId >= 0)
5944+
{
5945+
model.meshes[meshIndex].boneIds = RL_CALLOC(model.meshes[meshIndex].vertexCount * 4, sizeof(unsigned char));
5946+
model.meshes[meshIndex].boneWeights = RL_CALLOC(model.meshes[meshIndex].vertexCount * 4, sizeof(float));
5947+
5948+
for (int vertexIndex = 0; vertexIndex < model.meshes[meshIndex].vertexCount * 4; vertexIndex += 4)
5949+
{
5950+
model.meshes[meshIndex].boneIds[vertexIndex] = (unsigned char)parentBoneId;
5951+
model.meshes[meshIndex].boneWeights[vertexIndex] = 1.0f;
5952+
}
5953+
}
5954+
5955+
}
5956+
59275957
// Animated vertex data
59285958
model.meshes[meshIndex].animVertices = RL_CALLOC(model.meshes[meshIndex].vertexCount*3, sizeof(float));
59295959
memcpy(model.meshes[meshIndex].animVertices, model.meshes[meshIndex].vertices, model.meshes[meshIndex].vertexCount*3*sizeof(float));

0 commit comments

Comments
 (0)