From 3a810152fb8e93a73411bdf7f97ed1f739c4f995 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 7 Mar 2016 18:30:22 -0800 Subject: [PATCH 1/4] Trying to consolidate the FBX reader for Blender /maya --- libraries/fbx/src/FBXReader.cpp | 58 +++++++++++++----------- libraries/fbx/src/FBXReader.h | 4 ++ libraries/fbx/src/FBXReader_Material.cpp | 21 ++++++--- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 500f856450..2be04c30bd 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -865,11 +865,10 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } } else if (object.name == "Material") { FBXMaterial material; - if (object.properties.at(1).toByteArray().contains("StingrayPBS")) { - material.isPBSMaterial = true; - } + material.name = (object.properties.at(1).toString()); foreach (const FBXNode& subobject, object.children) { bool properties = false; + QByteArray propertyName; int index; if (subobject.name == "Properties60") { @@ -882,25 +881,33 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS propertyName = "P"; index = 4; } - if (!material.isPBSMaterial && properties) { - foreach (const FBXNode& property, subobject.children) { + if (properties) { + std::vector unknowns; + foreach(const FBXNode& property, subobject.children) { if (property.name == propertyName) { if (property.properties.at(0) == "DiffuseColor") { material.diffuseColor = getVec3(property.properties, index); - } else if (property.properties.at(0) == "Diffuse") { - material.diffuseColor = getVec3(property.properties, index); } else if (property.properties.at(0) == "DiffuseFactor") { material.diffuseFactor = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Diffuse") { + // material.diffuseColor = getVec3(property.properties, index); + // material.diffuseFactor = 1.0; } else if (property.properties.at(0) == "SpecularColor") { material.specularColor = getVec3(property.properties, index); - } else if (property.properties.at(0) == "Specular") { - material.specularColor = getVec3(property.properties, index); } else if (property.properties.at(0) == "SpecularFactor") { material.specularFactor = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Specular") { + // material.specularColor = getVec3(property.properties, index); + // material.specularFactor = 1.0; - } else if (property.properties.at(0) == "Emissive") { + } else if (property.properties.at(0) == "EmissiveColor") { material.emissiveColor = getVec3(property.properties, index); + } else if (property.properties.at(0) == "EmissiveFactor") { + material.emissiveFactor = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Emissive") { + // material.emissiveColor = getVec3(property.properties, index); + // material.emissiveFactor = 1.0; } else if (property.properties.at(0) == "Shininess") { material.shininess = property.properties.at(index).value(); @@ -908,42 +915,39 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (property.properties.at(0) == "Opacity") { material.opacity = property.properties.at(index).value(); } -#if defined(DEBUG_FBXREADER) - else { - const QString propname = property.properties.at(0).toString(); - if (propname == "EmissiveFactor") { - } - } -#endif - } - } - } else if (material.isPBSMaterial && properties) { - std::vector unknowns; - foreach(const FBXNode& property, subobject.children) { - if (property.name == propertyName) { - if (property.properties.at(0) == "Maya|use_normal_map") { + // Sting Ray Material Properties!!!! + else if (property.properties.at(0) == "Maya|use_normal_map") { + material.isPBSMaterial = true; material.useNormalMap = (bool)property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|base_color") { + material.isPBSMaterial = true; material.diffuseColor = getVec3(property.properties, index); } else if (property.properties.at(0) == "Maya|use_color_map") { + material.isPBSMaterial = true; material.useAlbedoMap = (bool) property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|roughness") { + material.isPBSMaterial = true; material.roughness = property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|use_roughness_map") { + material.isPBSMaterial = true; material.useRoughnessMap = (bool)property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|metallic") { + material.isPBSMaterial = true; material.metallic = property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|use_metallic_map") { + material.isPBSMaterial = true; material.useMetallicMap = (bool)property.properties.at(index).value(); - } else if (property.properties.at(0) == "Maya|emissive") { + material.isPBSMaterial = true; material.emissiveColor = getVec3(property.properties, index); } else if (property.properties.at(0) == "Maya|emissive_intensity") { + material.isPBSMaterial = true; material.emissiveIntensity = property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|use_emissive_map") { + material.isPBSMaterial = true; material.useEmissiveMap = (bool)property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|use_ao_map") { @@ -1071,7 +1075,9 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS if (connection.properties.at(0) == "OP") { int counter = 0; QByteArray type = connection.properties.at(3).toByteArray().toLower(); - if ((type.contains("diffuse") && !type.contains("tex_global_diffuse"))) { + if (type.contains("DiffuseFactor")) { + diffuseFactorTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); + } else if ((type.contains("diffuse") && !type.contains("tex_global_diffuse"))) { diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); } else if (type.contains("tex_color_map")) { diffuseTextures.insert(getID(connection.properties, 2), getID(connection.properties, 1)); diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index 3fefd837f3..53918bd149 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -143,6 +143,8 @@ public: float specularFactor = 1.0f; glm::vec3 emissiveColor{ 0.0f }; + float emissiveFactor = 0.0f; + float shininess = 23.0f; float opacity = 1.0f; @@ -151,6 +153,7 @@ public: float emissiveIntensity{ 1.0f }; QString materialID; + QString name; model::MaterialPointer _material; FBXTexture normalTexture; @@ -417,6 +420,7 @@ public: QHash diffuseTextures; + QHash diffuseFactorTextures; QHash transparentTextures; QHash bumpTextures; QHash normalTextures; diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index d87eac405f..f3b728ef46 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -68,12 +68,22 @@ void FBXReader::consolidateFBXMaterials() { // the pure material associated with this part bool detectDifferentUVs = false; FBXTexture diffuseTexture; + FBXTexture diffuseFactorTexture; QString diffuseTextureID = diffuseTextures.value(material.materialID); - if (!diffuseTextureID.isNull()) { + QString diffuseFactorTextureID = diffuseFactorTextures.value(material.materialID); + + if (!diffuseFactorTextureID.isNull() || !diffuseTextureID.isNull()) { + // If both factor and color are specified, color wins + if (!diffuseFactorTextureID.isNull() && diffuseTextureID.isNull()) { + diffuseTextureID = diffuseFactorTextureID; + // Avoid the DiffuseFactor effect in this case because here Maya would put 0.5... + material.diffuseFactor = 1.0; + } + diffuseTexture = getTexture(diffuseTextureID); - + // FBX files generated by 3DSMax have an intermediate texture parent, apparently - foreach (const QString& childTextureID, _connectionChildMap.values(diffuseTextureID)) { + foreach(const QString& childTextureID, _connectionChildMap.values(diffuseTextureID)) { if (_textureFilenames.contains(childTextureID)) { diffuseTexture = getTexture(diffuseTextureID); } @@ -173,11 +183,10 @@ void FBXReader::consolidateFBXMaterials() { // Finally create the true material representation material._material = std::make_shared(); - material._material->setEmissive(material.emissiveColor); + material._material->setEmissive(material.emissiveColor * material.emissiveFactor); + // Do not use the Diffuse Factor from FBX at all, this simpliies the export path auto diffuse = material.diffuseColor; - // FIXME: Do not use the Diffuse Factor yet as some FBX models have it set to 0 - // diffuse *= material.diffuseFactor; material._material->setAlbedo(diffuse); if (material.isPBSMaterial) { From 5299b06e832da5ea3ffb1f5da5bdf0ac03ee1920 Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 8 Mar 2016 11:26:16 -0800 Subject: [PATCH 2/4] Reintroducing the use of the DiffuseFactor so both Bender and MAya looks closer to Hifi --- libraries/fbx/src/FBXReader_Material.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index f3b728ef46..bd70bafe65 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -183,10 +183,13 @@ void FBXReader::consolidateFBXMaterials() { // Finally create the true material representation material._material = std::make_shared(); - material._material->setEmissive(material.emissiveColor * material.emissiveFactor); - // Do not use the Diffuse Factor from FBX at all, this simpliies the export path - auto diffuse = material.diffuseColor; + // Emissive color is the mix of emissiveColor with emissiveFactor + auto emissive = material.emissiveColor * material.emissiveFactor; + material._material->setEmissive(emissive); + + // Final diffuse color is the mix of diffuseColor with diffuseFactor + auto diffuse = material.diffuseColor * material.diffuseFactor; material._material->setAlbedo(diffuse); if (material.isPBSMaterial) { From bb9957b7f17bc2eb59c718b8f807ce219df41eae Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 8 Mar 2016 18:45:20 -0800 Subject: [PATCH 3/4] using comments from review --- libraries/fbx/src/FBXReader.cpp | 23 +++++++++++++++++------ libraries/fbx/src/FBXReader.h | 10 +++++----- libraries/fbx/src/FBXReader_Material.cpp | 6 ++++-- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libraries/fbx/src/FBXReader.cpp b/libraries/fbx/src/FBXReader.cpp index 17b4cc7c9a..a63cf78393 100644 --- a/libraries/fbx/src/FBXReader.cpp +++ b/libraries/fbx/src/FBXReader.cpp @@ -892,24 +892,27 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (property.properties.at(0) == "DiffuseFactor") { material.diffuseFactor = property.properties.at(index).value(); } else if (property.properties.at(0) == "Diffuse") { - // material.diffuseColor = getVec3(property.properties, index); - // material.diffuseFactor = 1.0; + // NOTE: this is uneeded but keep it for now for debug + // material.diffuseColor = getVec3(property.properties, index); + // material.diffuseFactor = 1.0; } else if (property.properties.at(0) == "SpecularColor") { material.specularColor = getVec3(property.properties, index); } else if (property.properties.at(0) == "SpecularFactor") { material.specularFactor = property.properties.at(index).value(); } else if (property.properties.at(0) == "Specular") { - // material.specularColor = getVec3(property.properties, index); - // material.specularFactor = 1.0; + // NOTE: this is uneeded but keep it for now for debug + // material.specularColor = getVec3(property.properties, index); + // material.specularFactor = 1.0; } else if (property.properties.at(0) == "EmissiveColor") { material.emissiveColor = getVec3(property.properties, index); } else if (property.properties.at(0) == "EmissiveFactor") { material.emissiveFactor = property.properties.at(index).value(); } else if (property.properties.at(0) == "Emissive") { - // material.emissiveColor = getVec3(property.properties, index); - // material.emissiveFactor = 1.0; + // NOTE: this is uneeded but keep it for now for debug + // material.emissiveColor = getVec3(property.properties, index); + // material.emissiveFactor = 1.0; } else if (property.properties.at(0) == "Shininess") { material.shininess = property.properties.at(index).value(); @@ -917,6 +920,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (property.properties.at(0) == "Opacity") { material.opacity = property.properties.at(index).value(); } + // Sting Ray Material Properties!!!! else if (property.properties.at(0) == "Maya|use_normal_map") { material.isPBSMaterial = true; @@ -925,6 +929,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (property.properties.at(0) == "Maya|base_color") { material.isPBSMaterial = true; material.diffuseColor = getVec3(property.properties, index); + } else if (property.properties.at(0) == "Maya|use_color_map") { material.isPBSMaterial = true; material.useAlbedoMap = (bool) property.properties.at(index).value(); @@ -932,6 +937,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (property.properties.at(0) == "Maya|roughness") { material.isPBSMaterial = true; material.roughness = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|use_roughness_map") { material.isPBSMaterial = true; material.useRoughnessMap = (bool)property.properties.at(index).value(); @@ -939,20 +945,25 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS } else if (property.properties.at(0) == "Maya|metallic") { material.isPBSMaterial = true; material.metallic = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|use_metallic_map") { material.isPBSMaterial = true; material.useMetallicMap = (bool)property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|emissive") { material.isPBSMaterial = true; material.emissiveColor = getVec3(property.properties, index); + } else if (property.properties.at(0) == "Maya|emissive_intensity") { material.isPBSMaterial = true; material.emissiveIntensity = property.properties.at(index).value(); + } else if (property.properties.at(0) == "Maya|use_emissive_map") { material.isPBSMaterial = true; material.useEmissiveMap = (bool)property.properties.at(index).value(); } else if (property.properties.at(0) == "Maya|use_ao_map") { + material.isPBSMaterial = true; material.useOcclusionMap = (bool)property.properties.at(index).value(); } else { diff --git a/libraries/fbx/src/FBXReader.h b/libraries/fbx/src/FBXReader.h index c2b777b0db..a1fc30d1f4 100644 --- a/libraries/fbx/src/FBXReader.h +++ b/libraries/fbx/src/FBXReader.h @@ -138,15 +138,15 @@ public: opacity(opacity) {} glm::vec3 diffuseColor{ 1.0f }; - float diffuseFactor = 1.0f; + float diffuseFactor{ 1.0f }; glm::vec3 specularColor{ 0.02f }; - float specularFactor = 1.0f; + float specularFactor{ 1.0f }; glm::vec3 emissiveColor{ 0.0f }; - float emissiveFactor = 0.0f; + float emissiveFactor{ 0.0f }; - float shininess = 23.0f; - float opacity = 1.0f; + float shininess{ 23.0f }; + float opacity{ 1.0f }; float metallic{ 0.0f }; float roughness{ 1.0f }; diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index b8bca16ef0..60c1e747c0 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -80,10 +80,12 @@ void FBXReader::consolidateFBXMaterials() { QString diffuseFactorTextureID = diffuseFactorTextures.value(material.materialID); if (!diffuseFactorTextureID.isNull() || !diffuseTextureID.isNull()) { - // If both factor and color are specified, color wins + // If both factor and color are specified, the texture bound to DiffuseColor wins if (!diffuseFactorTextureID.isNull() && diffuseTextureID.isNull()) { diffuseTextureID = diffuseFactorTextureID; - // Avoid the DiffuseFactor effect in this case because here Maya would put 0.5... + // If the diffuseTextureID comes from the Texture bound to DiffuseFactor, we know it s exported from maya + // And the DiffuseFactor is forced to 0.5 by Maya which is bad + // So we need to force it to 1.0 material.diffuseFactor = 1.0; } From f82bd441a955e4cfc38378483d70d8de92f01e1d Mon Sep 17 00:00:00 2001 From: samcake Date: Wed, 9 Mar 2016 10:34:49 -0800 Subject: [PATCH 4/4] IMproving th ecomment --- libraries/fbx/src/FBXReader_Material.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/fbx/src/FBXReader_Material.cpp b/libraries/fbx/src/FBXReader_Material.cpp index 60c1e747c0..fb272a1af9 100644 --- a/libraries/fbx/src/FBXReader_Material.cpp +++ b/libraries/fbx/src/FBXReader_Material.cpp @@ -79,8 +79,8 @@ void FBXReader::consolidateFBXMaterials() { QString diffuseTextureID = diffuseTextures.value(material.materialID); QString diffuseFactorTextureID = diffuseFactorTextures.value(material.materialID); + // If both factor and color textures are specified, the texture bound to DiffuseColor wins if (!diffuseFactorTextureID.isNull() || !diffuseTextureID.isNull()) { - // If both factor and color are specified, the texture bound to DiffuseColor wins if (!diffuseFactorTextureID.isNull() && diffuseTextureID.isNull()) { diffuseTextureID = diffuseFactorTextureID; // If the diffuseTextureID comes from the Texture bound to DiffuseFactor, we know it s exported from maya