Merge branch 'feature/negative-model-import-reverse-winding' of https://github.com/GaryMcWhorter/FlaxEngine into GaryMcWhorter-feature/negative-model-import-reverse-winding

This commit is contained in:
Wojtek Figat
2024-12-16 23:00:24 +01:00
5 changed files with 38 additions and 0 deletions

View File

@@ -765,6 +765,8 @@ bool ModelTool::ImportDataAssimp(const String& path, ModelData& data, Options& o
flags |= aiProcess_FixInfacingNormals | aiProcess_GenSmoothNormals;
if (options.CalculateTangents)
flags |= aiProcess_CalcTangentSpace;
if (options.ReverseWindingOrder)
flags &= ~aiProcess_FlipWindingOrder;
if (options.OptimizeMeshes)
flags |= aiProcess_OptimizeMeshes | aiProcess_SplitLargeMeshes | aiProcess_ImproveCacheLocality;
if (options.MergeMeshes)

View File

@@ -385,6 +385,19 @@ bool ProcessMesh(ImporterData& data, FbxMesh* fbxMesh, MeshData& mesh, String& e
mesh.Indices[i] = fbxIndices[i];
}
if (data.Options.ReverseWindingOrder)
{
for (int32 i = 0; i < vertexCount; i += 3)
{
Swap(meshIndices[i + 1], meshIndices[i + 2]);
Swap(meshPositions[i + 1], meshPositions[i + 2]);
if (meshNormals)
Swap(meshNormals[i + 1], meshNormals[i + 2]);
if (meshTangents)
Swap(meshTangents[i + 1], meshTangents[i + 2]);
}
}
// Texture coordinates
FbxGeometryElementUV* texcoords = fbxMesh->GetElementUV(0);
if (texcoords)

View File

@@ -775,6 +775,24 @@ bool ProcessMesh(ModelData& result, OpenFbxImporterData& data, const ofbx::Mesh*
}
}
// Reverse winding order
if (data.Options.ReverseWindingOrder)
{
uint32* meshIndices = mesh.Indices.Get();
Float3* meshPositions = mesh.Positions.Get();
Float3* meshNormals = mesh.Normals.HasItems() ? mesh.Normals.Get() : nullptr;
Float3* meshTangents = mesh.Tangents.HasItems() ? mesh.Tangents.Get() : nullptr;
for (int i = 0; i < vertexCount; i += 3) {
Swap(meshIndices[i + 1], meshIndices[i + 2]);
Swap(meshPositions[i + 1], meshPositions[i + 2]);
if (meshNormals)
Swap(meshNormals[i + 1], meshNormals[i + 2]);
if (meshTangents)
Swap(meshTangents[i + 1], meshTangents[i + 2]);
}
}
// Lightmap UVs
if (data.Options.LightmapUVsSource == ModelLightmapUVsSource::Disable)
{

View File

@@ -667,6 +667,7 @@ void ModelTool::Options::Serialize(SerializeStream& stream, const void* otherObj
SERIALIZE(FlipNormals);
SERIALIZE(CalculateTangents);
SERIALIZE(SmoothingTangentsAngle);
SERIALIZE(ReverseWindingOrder);
SERIALIZE(OptimizeMeshes);
SERIALIZE(MergeMeshes);
SERIALIZE(ImportLODs);
@@ -717,6 +718,7 @@ void ModelTool::Options::Deserialize(DeserializeStream& stream, ISerializeModifi
DESERIALIZE(FlipNormals);
DESERIALIZE(CalculateTangents);
DESERIALIZE(SmoothingTangentsAngle);
DESERIALIZE(ReverseWindingOrder);
DESERIALIZE(OptimizeMeshes);
DESERIALIZE(MergeMeshes);
DESERIALIZE(ImportLODs);

View File

@@ -170,6 +170,9 @@ public:
// Specifies the maximum angle (in degrees) that may be between two vertex tangents before their tangents and bi-tangents are smoothed. The default value is 45.
API_FIELD(Attributes="EditorOrder(45), EditorDisplay(\"Geometry\"), VisibleIf(nameof(ShowSmoothingTangentsAngle)), Limit(0, 45, 0.1f)")
float SmoothingTangentsAngle = 45.0f;
// If checked, the winding order of the vertices will be reversed.
API_FIELD(Attributes="EditorOrder(47), EditorDisplay(\"Geometry\"), VisibleIf(nameof(ShowGeometry))")
bool ReverseWindingOrder = false;
// Enable/disable meshes geometry optimization.
API_FIELD(Attributes="EditorOrder(50), EditorDisplay(\"Geometry\"), VisibleIf(nameof(ShowGeometry))")
bool OptimizeMeshes = true;