From e5037855d90591ddef81382b5025e873f4f421da Mon Sep 17 00:00:00 2001 From: Qingyou Zhao Date: Tue, 10 Dec 2024 13:05:57 -0800 Subject: [PATCH] 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 (cherry picked from commit a50d8d3177ed60036c40197cb6b98cf86281b599) --- graphics/src/AssimpLoader.cc | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/graphics/src/AssimpLoader.cc b/graphics/src/AssimpLoader.cc index 742cfe37..da270aaa 100644 --- a/graphics/src/AssimpLoader.cc +++ b/graphics/src/AssimpLoader.cc @@ -668,18 +668,20 @@ SubMesh AssimpLoader::Implementation::CreateSubMesh( { // Add the vertex math::Vector3d vertex; - math::Vector3d normal; vertex.X(_assimpMesh->mVertices[vertexIdx].x); vertex.Y(_assimpMesh->mVertices[vertexIdx].y); vertex.Z(_assimpMesh->mVertices[vertexIdx].z); - normal.X(_assimpMesh->mNormals[vertexIdx].x); - normal.Y(_assimpMesh->mNormals[vertexIdx].y); - normal.Z(_assimpMesh->mNormals[vertexIdx].z); vertex = _transform * vertex; - normal = rot * normal; - normal.Normalize(); subMesh.AddVertex(vertex); - subMesh.AddNormal(normal); + if (_assimpMesh->HasNormals()) { + math::Vector3d normal; + normal.X(_assimpMesh->mNormals[vertexIdx].x); + normal.Y(_assimpMesh->mNormals[vertexIdx].y); + normal.Z(_assimpMesh->mNormals[vertexIdx].z); + normal = rot * normal; + normal.Normalize(); + subMesh.AddNormal(normal); + } // Iterate over sets of texture coordinates for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { @@ -699,6 +701,9 @@ SubMesh AssimpLoader::Implementation::CreateSubMesh( subMesh.AddIndex(face.mIndices[2]); } subMesh.SetMaterialIndex(_assimpMesh->mMaterialIndex); + if (subMesh.NormalCount() == 0u){ + subMesh.RecalculateNormals(); + } return subMesh; }