From 7eb9edf0b9b3e761ee815cfc50b9b9efcab6d2f7 Mon Sep 17 00:00:00 2001 From: Florian Born Date: Wed, 21 Sep 2022 11:45:54 +0200 Subject: [PATCH] Make FBX parser resilient to missing data streams --- code/AssetLib/FBX/FBXMeshGeometry.cpp | 31 +++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/code/AssetLib/FBX/FBXMeshGeometry.cpp b/code/AssetLib/FBX/FBXMeshGeometry.cpp index 58bacfad4..ace4ad749 100644 --- a/code/AssetLib/FBX/FBXMeshGeometry.cpp +++ b/code/AssetLib/FBX/FBXMeshGeometry.cpp @@ -415,9 +415,11 @@ void ResolveVertexDataArray(std::vector& data_out, const Scope& source, { bool isDirect = ReferenceInformationType == "Direct"; bool isIndexToDirect = ReferenceInformationType == "IndexToDirect"; + const bool hasDataElement = HasElement(source, dataElementName); + const bool hasIndexDataElement = HasElement(source, indexDataElementName); // fall-back to direct data if there is no index data element - if ( isIndexToDirect && !HasElement( source, indexDataElementName ) ) { + if (isIndexToDirect && !hasIndexDataElement) { isDirect = true; isIndexToDirect = false; } @@ -426,7 +428,8 @@ void ResolveVertexDataArray(std::vector& data_out, const Scope& source, // deal with this more elegantly and with less redundancy, but right // now it seems unavoidable. if (MappingInformationType == "ByVertice" && isDirect) { - if (!HasElement(source, dataElementName)) { + if (!hasDataElement) { + FBXImporter::LogWarn("missing data element: ", dataElementName); return; } std::vector tempData; @@ -448,7 +451,15 @@ void ResolveVertexDataArray(std::vector& data_out, const Scope& source, } else if (MappingInformationType == "ByVertice" && isIndexToDirect) { std::vector tempData; - ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName)); + if (!hasDataElement || !hasIndexDataElement) { + if (!hasDataElement) + FBXImporter::LogWarn("missing data element: ", dataElementName); + if (!hasIndexDataElement) + FBXImporter::LogWarn("missing index data element: ", indexDataElementName); + return; + } + + ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName)); std::vector uvIndices; ParseVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName)); @@ -473,6 +484,11 @@ void ResolveVertexDataArray(std::vector& data_out, const Scope& source, } } else if (MappingInformationType == "ByPolygonVertex" && isDirect) { + if (!hasDataElement) { + FBXImporter::LogWarn("missing data element: ", dataElementName); + return; + } + std::vector tempData; ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName)); @@ -487,7 +503,14 @@ void ResolveVertexDataArray(std::vector& data_out, const Scope& source, } else if (MappingInformationType == "ByPolygonVertex" && isIndexToDirect) { std::vector tempData; - ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName)); + if (!hasDataElement || !hasIndexDataElement) { + if (!hasDataElement) + FBXImporter::LogWarn("missing data element: ", dataElementName); + if (!hasIndexDataElement) + FBXImporter::LogWarn("missing index data element: ", indexDataElementName); + return; + } + ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName)); std::vector uvIndices; ParseVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));