From 471d2acc92651df88a51ea00ea8b8566608f676e Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Thu, 3 Dec 2020 11:17:45 +0000 Subject: [PATCH] Improvements & style --- code/AssetLib/FBX/FBXConverter.cpp | 12 +++++------ code/AssetLib/FBX/FBXProperties.cpp | 33 ++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/code/AssetLib/FBX/FBXConverter.cpp b/code/AssetLib/FBX/FBXConverter.cpp index 7070042f8..ba7089f29 100644 --- a/code/AssetLib/FBX/FBXConverter.cpp +++ b/code/AssetLib/FBX/FBXConverter.cpp @@ -2031,19 +2031,19 @@ void FBXConverter::SetTextureProperties(aiMaterial *out_mat, const TextureMap &_ // Glossiness vs roughness in 3ds Max Pbr Materials int useGlossiness; - if (out_mat->Get("$raw.3dsMax|main|useGlossiness", aiTextureType_NONE, 0, useGlossiness) == aiReturn_SUCCESS) - { + if (out_mat->Get("$raw.3dsMax|main|useGlossiness", aiTextureType_NONE, 0, useGlossiness) == aiReturn_SUCCESS) { // These textures swap meaning if ((useGlossiness == 1) != (material type is Specular/Gloss)) - if (useGlossiness == 1) - { + if (useGlossiness == 1) { TrySetTextureProperties(out_mat, _textures, "3dsMax|main|roughness_map", aiTextureType_SHININESS, mesh); TrySetTextureProperties(out_mat, _textures, "3dsMax|main|glossiness_map", aiTextureType_SHININESS, mesh); } - else // useGlossiness == 2 - { + else if (useGlossiness == 2) { TrySetTextureProperties(out_mat, _textures, "3dsMax|main|roughness_map", aiTextureType_DIFFUSE_ROUGHNESS, mesh); TrySetTextureProperties(out_mat, _textures, "3dsMax|main|glossiness_map", aiTextureType_DIFFUSE_ROUGHNESS, mesh); } + else { + FBXImporter::LogWarn("A 3dsMax Pbr Material must have a useGlossiness value to correctly interpret roughness and glossiness textures."); + } } } diff --git a/code/AssetLib/FBX/FBXProperties.cpp b/code/AssetLib/FBX/FBXProperties.cpp index ccccd3012..84098cf10 100644 --- a/code/AssetLib/FBX/FBXProperties.cpp +++ b/code/AssetLib/FBX/FBXProperties.cpp @@ -69,6 +69,20 @@ Property::~Property() namespace { +void checkTokenCount(const TokenList& tok, unsigned int expectedCount) +{ + ai_assert(expectedCount >= 2); + if (tok.size() < expectedCount) { + const std::string& s = ParseTokenAsString(*tok[1]); + if (tok[1]->IsBinary()) { + throw DeadlyImportError("Not enough tokens for property of type ", s, " at offset ", tok[1]->Offset()); + } + else { + throw DeadlyImportError("Not enough tokens for property of type ", s, " at line ", tok[1]->Line()); + } + } +} + // ------------------------------------------------------------------------------------------------ // read a typed property out of a FBX element. The return value is nullptr if the property cannot be read. Property* ReadTypedProperty(const Element& element) @@ -83,23 +97,23 @@ Property* ReadTypedProperty(const Element& element) const std::string& s = ParseTokenAsString(*tok[1]); const char* const cs = s.c_str(); if (!strcmp(cs,"KString")) { - ai_assert(tok.size() >= 5); + checkTokenCount(tok, 5); return new TypedProperty(ParseTokenAsString(*tok[4])); } else if (!strcmp(cs,"bool") || !strcmp(cs,"Bool")) { - ai_assert(tok.size() >= 5); + checkTokenCount(tok, 5); return new TypedProperty(ParseTokenAsInt(*tok[4]) != 0); } else if (!strcmp(cs, "int") || !strcmp(cs, "Int") || !strcmp(cs, "enum") || !strcmp(cs, "Enum") || !strcmp(cs, "Integer")) { - ai_assert(tok.size() >= 5); + checkTokenCount(tok, 5); return new TypedProperty(ParseTokenAsInt(*tok[4])); } else if (!strcmp(cs, "ULongLong")) { - ai_assert(tok.size() >= 5); + checkTokenCount(tok, 5); return new TypedProperty(ParseTokenAsID(*tok[4])); } else if (!strcmp(cs, "KTime")) { - ai_assert(tok.size() >= 5); + checkTokenCount(tok, 5); return new TypedProperty(ParseTokenAsInt64(*tok[4])); } else if (!strcmp(cs,"Vector3D") || @@ -110,7 +124,7 @@ Property* ReadTypedProperty(const Element& element) !strcmp(cs,"Lcl Rotation") || !strcmp(cs,"Lcl Scaling") ) { - ai_assert(tok.size() >= 7); + checkTokenCount(tok, 7); return new TypedProperty(aiVector3D( ParseTokenAsFloat(*tok[4]), ParseTokenAsFloat(*tok[5]), @@ -118,12 +132,11 @@ Property* ReadTypedProperty(const Element& element) ); } else if (!strcmp(cs,"double") || !strcmp(cs,"Number") || !strcmp(cs,"Float") || !strcmp(cs,"FieldOfView") || !strcmp( cs, "UnitScaleFactor" ) ) { - ai_assert(tok.size() >= 5); + checkTokenCount(tok, 5); return new TypedProperty(ParseTokenAsFloat(*tok[4])); } - else if (!strcmp(cs, "ColorAndAlpha")) - { - ai_assert(tok.size() >= 8); + else if (!strcmp(cs, "ColorAndAlpha")) { + checkTokenCount(tok, 8); return new TypedProperty(aiColor4D( ParseTokenAsFloat(*tok[4]), ParseTokenAsFloat(*tok[5]),