From 55fecfa54dbc646462e6702dac95f5ed6f08b8e8 Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Mon, 3 Jul 2023 14:23:11 +1000 Subject: [PATCH 1/2] Fix incorrect flat normals. --- Source/CesiumRuntime/Private/CesiumGltfComponent.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp index b656bfa6c..011ea0044 100644 --- a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp +++ b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp @@ -302,10 +302,18 @@ static void computeFlatNormals( FStaticMeshBuildVertex& v1 = vertices[i + 1]; FStaticMeshBuildVertex& v2 = vertices[i + 2]; + // The Y axis has previously been inverted, so undo that before + // computing the normal direction. Then invert the Y coordinate of the + // normal, too. + TMeshVector3 v01 = v1.Position - v0.Position; + v01.Y = -v01.Y; TMeshVector3 v02 = v2.Position - v0.Position; + v02.Y = -v02.Y; TMeshVector3 normal = TMeshVector3::CrossProduct(v01, v02); + normal.Y = -normal.Y; + v0.TangentX = v1.TangentX = v2.TangentX = TMeshVector3(0.0f); v0.TangentY = v1.TangentY = v2.TangentY = TMeshVector3(0.0f); v0.TangentZ = v1.TangentZ = v2.TangentZ = normal.GetSafeNormal(); From abe7b215d8d81ed8a7a7bee51db398ab9f105c4a Mon Sep 17 00:00:00 2001 From: Kevin Ring Date: Mon, 3 Jul 2023 14:36:29 +1000 Subject: [PATCH 2/2] Make sure tangents are right, too. --- .../Private/CesiumGltfComponent.cpp | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp index 011ea0044..a71aabee6 100644 --- a/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp +++ b/Source/CesiumRuntime/Private/CesiumGltfComponent.cpp @@ -221,7 +221,7 @@ static void mikkGetPosition( *reinterpret_cast*>(Context->m_pUserData); const TMeshVector3& position = vertices[FaceIdx * 3 + VertIdx].Position; Position[0] = position.X; - Position[1] = position.Y; + Position[1] = -position.Y; Position[2] = position.Z; } @@ -234,7 +234,7 @@ static void mikkGetNormal( *reinterpret_cast*>(Context->m_pUserData); const TMeshVector3& normal = vertices[FaceIdx * 3 + VertIdx].TangentZ; Normal[0] = normal.X; - Normal[1] = normal.Y; + Normal[1] = -normal.Y; Normal[2] = normal.Z; } @@ -259,10 +259,19 @@ static void mikkSetTSpaceBasic( TArray& vertices = *reinterpret_cast*>(Context->m_pUserData); FStaticMeshBuildVertex& vertex = vertices[FaceIdx * 3 + VertIdx]; - vertex.TangentX = TMeshVector3(Tangent[0], Tangent[1], Tangent[2]); - vertex.TangentY = - BitangentSign * - TMeshVector3::CrossProduct(vertex.TangentZ, vertex.TangentX); + + FVector3f TangentZ = vertex.TangentZ; + TangentZ.Y = -TangentZ.Y; + + FVector3f TangentX = TMeshVector3(Tangent[0], Tangent[1], Tangent[2]); + FVector3f TangentY = + BitangentSign * TMeshVector3::CrossProduct(TangentZ, TangentX); + + TangentX.Y = -TangentX.Y; + TangentY.Y = -TangentY.Y; + + vertex.TangentX = TangentX; + vertex.TangentY = TangentY; } static void computeTangentSpace(TArray& vertices) {