Skip to content

Commit 1685b05

Browse files
qingyouzhaoiche033
authored andcommitted
Check normal presence before trying to read asMesh normals (#654)
* In AssimpLoader::Implementation::CreateSubMesh check normal presence before reading * RecalculateNormals if submesh has no normals --------- Signed-off-by: Qingyou Zhao <[email protected]> (cherry picked from commit a50d8d3)
1 parent a4feedc commit 1685b05

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

graphics/src/AssimpLoader.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -668,18 +668,20 @@ SubMesh AssimpLoader::Implementation::CreateSubMesh(
668668
{
669669
// Add the vertex
670670
math::Vector3d vertex;
671-
math::Vector3d normal;
672671
vertex.X(_assimpMesh->mVertices[vertexIdx].x);
673672
vertex.Y(_assimpMesh->mVertices[vertexIdx].y);
674673
vertex.Z(_assimpMesh->mVertices[vertexIdx].z);
675-
normal.X(_assimpMesh->mNormals[vertexIdx].x);
676-
normal.Y(_assimpMesh->mNormals[vertexIdx].y);
677-
normal.Z(_assimpMesh->mNormals[vertexIdx].z);
678674
vertex = _transform * vertex;
679-
normal = rot * normal;
680-
normal.Normalize();
681675
subMesh.AddVertex(vertex);
682-
subMesh.AddNormal(normal);
676+
if (_assimpMesh->HasNormals()) {
677+
math::Vector3d normal;
678+
normal.X(_assimpMesh->mNormals[vertexIdx].x);
679+
normal.Y(_assimpMesh->mNormals[vertexIdx].y);
680+
normal.Z(_assimpMesh->mNormals[vertexIdx].z);
681+
normal = rot * normal;
682+
normal.Normalize();
683+
subMesh.AddNormal(normal);
684+
}
683685
// Iterate over sets of texture coordinates
684686
for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i)
685687
{
@@ -699,6 +701,9 @@ SubMesh AssimpLoader::Implementation::CreateSubMesh(
699701
subMesh.AddIndex(face.mIndices[2]);
700702
}
701703
subMesh.SetMaterialIndex(_assimpMesh->mMaterialIndex);
704+
if (subMesh.NormalCount() == 0u){
705+
subMesh.RecalculateNormals();
706+
}
702707
return subMesh;
703708
}
704709

0 commit comments

Comments
 (0)