@@ -5815,15 +5815,17 @@ static Model LoadGLTF(const char *fileName)
5815
5815
5816
5816
for (unsigned int p = 0 ; p < mesh -> primitives_count ; p ++ )
5817
5817
{
5818
+ bool hasJoints = false;
5819
+
5818
5820
// NOTE: We only support primitives defined by triangles
5819
5821
if (mesh -> primitives [p ].type != cgltf_primitive_type_triangles ) continue ;
5820
5822
5821
5823
for (unsigned int j = 0 ; j < mesh -> primitives [p ].attributes_count ; j ++ )
5822
5824
{
5823
5825
// NOTE: JOINTS_1 + WEIGHT_1 will be used for +4 joints influencing a vertex -> Not supported by raylib
5824
-
5825
5826
if (mesh -> primitives [p ].attributes [j ].type == cgltf_attribute_type_joints ) // JOINTS_n (vec4: 4 bones max per vertex / u8, u16)
5826
5827
{
5828
+ hasJoints = true;
5827
5829
cgltf_accessor * attribute = mesh -> primitives [p ].attributes [j ].data ;
5828
5830
5829
5831
// NOTE: JOINTS_n can only be vec4 and u8/u16
@@ -5924,6 +5926,34 @@ static Model LoadGLTF(const char *fileName)
5924
5926
}
5925
5927
}
5926
5928
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
+
5927
5957
// Animated vertex data
5928
5958
model .meshes [meshIndex ].animVertices = RL_CALLOC (model .meshes [meshIndex ].vertexCount * 3 , sizeof (float ));
5929
5959
memcpy (model .meshes [meshIndex ].animVertices , model .meshes [meshIndex ].vertices , model .meshes [meshIndex ].vertexCount * 3 * sizeof (float ));
0 commit comments