Skip to content

Commit e3a562a

Browse files
authored
UpdateModelAnimation does matrixtranspose(matrixinvert) only once per bone instead of per vertex (#5244)
1 parent aaf4c1d commit e3a562a

1 file changed

Lines changed: 46 additions & 41 deletions

File tree

src/rmodels.c

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,54 +2352,59 @@ void UpdateModelAnimation(Model model, ModelAnimation anim, int frame)
23522352
Mesh mesh = model.meshes[m];
23532353
Vector3 animVertex = { 0 };
23542354
Vector3 animNormal = { 0 };
2355+
Matrix boneMatrix = { 0 };
2356+
Matrix InverseBoneMatrix = { 0 };
23552357
int boneId = 0;
23562358
int boneCounter = 0;
23572359
float boneWeight = 0.0;
23582360
bool updated = false; // Flag to check when anim vertex information is updated
23592361
const int vValues = mesh.vertexCount*3;
23602362

23612363
// Skip if missing bone data, causes segfault without on some models
2362-
if ((mesh.boneWeights == NULL) || (mesh.boneIds == NULL)) continue;
2363-
2364-
for (int vCounter = 0; vCounter < vValues; vCounter += 3)
2365-
{
2366-
mesh.animVertices[vCounter] = 0;
2367-
mesh.animVertices[vCounter + 1] = 0;
2368-
mesh.animVertices[vCounter + 2] = 0;
2369-
if (mesh.animNormals != NULL)
2370-
{
2371-
mesh.animNormals[vCounter] = 0;
2372-
mesh.animNormals[vCounter + 1] = 0;
2373-
mesh.animNormals[vCounter + 2] = 0;
2374-
}
2375-
2376-
// Iterates over 4 bones per vertex
2377-
for (int j = 0; j < 4; j++, boneCounter++)
2378-
{
2379-
boneWeight = mesh.boneWeights[boneCounter];
2380-
boneId = mesh.boneIds[boneCounter];
2381-
2382-
// Early stop when no transformation will be applied
2383-
if (boneWeight == 0.0f) continue;
2384-
animVertex = (Vector3){ mesh.vertices[vCounter], mesh.vertices[vCounter + 1], mesh.vertices[vCounter + 2] };
2385-
animVertex = Vector3Transform(animVertex,model.meshes[m].boneMatrices[boneId]);
2386-
mesh.animVertices[vCounter] += animVertex.x*boneWeight;
2387-
mesh.animVertices[vCounter+1] += animVertex.y*boneWeight;
2388-
mesh.animVertices[vCounter+2] += animVertex.z*boneWeight;
2389-
updated = true;
2390-
2391-
// Normals processing
2392-
// NOTE: We use meshes.baseNormals (default normal) to calculate meshes.normals (animated normals)
2393-
if ((mesh.normals != NULL) && (mesh.animNormals != NULL ))
2394-
{
2395-
animNormal = (Vector3){ mesh.normals[vCounter], mesh.normals[vCounter + 1], mesh.normals[vCounter + 2] };
2396-
animNormal = Vector3Transform(animNormal, MatrixTranspose(MatrixInvert(model.meshes[m].boneMatrices[boneId])));
2397-
mesh.animNormals[vCounter] += animNormal.x*boneWeight;
2398-
mesh.animNormals[vCounter + 1] += animNormal.y*boneWeight;
2399-
mesh.animNormals[vCounter + 2] += animNormal.z*boneWeight;
2400-
}
2401-
}
2402-
}
2364+
if ((mesh.boneWeights == NULL) || (mesh.boneIds == NULL)) continue;
2365+
2366+
// Iterates over 4 bones per vertex
2367+
for (int j = 0; j < 4; j++, boneCounter++)
2368+
{
2369+
boneWeight = mesh.boneWeights[boneCounter];
2370+
boneId = mesh.boneIds[boneCounter];
2371+
2372+
// Early stop when no transformation will be applied
2373+
if (boneWeight == 0.0f) continue;
2374+
2375+
boneMatrix = model.meshes[m].boneMatrices[boneId];
2376+
InverseBoneMatrix = MatrixTranspose(MatrixInvert(boneMatrix));
2377+
2378+
for (int vCounter = 0; vCounter < vValues; vCounter += 3)
2379+
{
2380+
mesh.animVertices[vCounter] = 0;
2381+
mesh.animVertices[vCounter + 1] = 0;
2382+
mesh.animVertices[vCounter + 2] = 0;
2383+
if (mesh.animNormals != NULL)
2384+
{
2385+
mesh.animNormals[vCounter] = 0;
2386+
mesh.animNormals[vCounter + 1] = 0;
2387+
mesh.animNormals[vCounter + 2] = 0;
2388+
}
2389+
animVertex = (Vector3){ mesh.vertices[vCounter], mesh.vertices[vCounter + 1], mesh.vertices[vCounter + 2] };
2390+
animVertex = Vector3Transform(animVertex, boneMatrix);
2391+
mesh.animVertices[vCounter] += animVertex.x*boneWeight;
2392+
mesh.animVertices[vCounter+1] += animVertex.y*boneWeight;
2393+
mesh.animVertices[vCounter+2] += animVertex.z*boneWeight;
2394+
updated = true;
2395+
2396+
// Normals processing
2397+
// NOTE: We use meshes.baseNormals (default normal) to calculate meshes.normals (animated normals)
2398+
if ((mesh.normals != NULL) && (mesh.animNormals != NULL))
2399+
{
2400+
animNormal = (Vector3){ mesh.normals[vCounter], mesh.normals[vCounter + 1], mesh.normals[vCounter + 2] };
2401+
animNormal = Vector3Transform(animNormal, InverseBoneMatrix);
2402+
mesh.animNormals[vCounter] += animNormal.x*boneWeight;
2403+
mesh.animNormals[vCounter + 1] += animNormal.y*boneWeight;
2404+
mesh.animNormals[vCounter + 2] += animNormal.z*boneWeight;
2405+
}
2406+
}
2407+
}
24032408

24042409
if (updated)
24052410
{

0 commit comments

Comments
 (0)