Skip to content

Commit

Permalink
fix(fbx import): Prevent zero-length tangents/normals. (#2066)
Browse files Browse the repository at this point in the history
  • Loading branch information
froce authored Dec 2, 2023
1 parent 09b657a commit f99c965
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions sources/tools/Stride.Importer.FBX/MeshConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ public ref class MeshConverter
auto src_normal = normalElement->GetDirectArray().GetAt(normalIndex);
auto normalPointer = ((Vector3*)(vbPointer + normalOffset));
normal = sceneMapping->ConvertNormalFromFbx(src_normal);
if (isnan(normal.X) || isnan(normal.Y) || isnan(normal.Z))
if (isnan(normal.X) || isnan(normal.Y) || isnan(normal.Z) || normal.Length() < FLT_EPSILON)
normal = Vector3(1, 0, 0);
normal = Vector3::Normalize(normal);
*normalPointer = normal;
Expand All @@ -568,7 +568,7 @@ public ref class MeshConverter
auto src_tangent = tangentElement->GetDirectArray().GetAt(tangentIndex);
auto tangentPointer = ((Vector4*)(vbPointer + tangentOffset));
Vector3 tangent = sceneMapping->ConvertNormalFromFbx(src_tangent);
if (isnan(tangent.X) || isnan(tangent.Y) || isnan(tangent.Z))
if (isnan(tangent.X) || isnan(tangent.Y) || isnan(tangent.Z) || tangent.Length() < FLT_EPSILON)
{
*tangentPointer = Vector4(1, 0, 0, 1);
}
Expand All @@ -578,9 +578,16 @@ public ref class MeshConverter

int binormalIndex = GetGroupIndexForLayerElementTemplate(binormalElement, controlPointIndex, vertexIndex, edgeIndex, i, meshName, layerIndexFirstTimeError);
auto src_binormal = binormalElement->GetDirectArray().GetAt(binormalIndex);
Vector3 binormal = sceneMapping->ConvertNormalFromFbx(src_tangent);
// See GenerateTangentBinormal()
*tangentPointer = Vector4(tangent.X, tangent.Y, tangent.Z, Vector3::Dot(Vector3::Cross(normal, tangent), binormal) < 0.0f ? -1.0f : 1.0f);
Vector3 binormal = sceneMapping->ConvertNormalFromFbx(src_binormal);
if (isnan(binormal.X) || isnan(binormal.Y) || isnan(binormal.Z) || binormal.Length() < FLT_EPSILON)
{
*tangentPointer = Vector4(tangent.X, tangent.Y, tangent.Z, 1.0f);
}
else
{
// See GenerateTangentBinormal()
*tangentPointer = Vector4(tangent.X, tangent.Y, tangent.Z, Vector3::Dot(Vector3::Cross(normal, tangent), binormal) < 0.0f ? -1.0f : 1.0f);
}
}
}

Expand Down

0 comments on commit f99c965

Please sign in to comment.