From 3084f7af5400ab38b86b32e9edf5e18af667d07e Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Sat, 18 Nov 2017 14:19:13 -0800 Subject: [PATCH] further WIP --- src/Fbx2Raw.cpp | 137 ++++++++++++-------------------------- src/Raw2Gltf.cpp | 5 +- src/RawModel.h | 10 +++ src/glTF/MaterialData.cpp | 10 ++- src/glTF/MaterialData.h | 5 +- 5 files changed, 65 insertions(+), 102 deletions(-) diff --git a/src/Fbx2Raw.cpp b/src/Fbx2Raw.cpp index 23a5bad..eaad0f9 100644 --- a/src/Fbx2Raw.cpp +++ b/src/Fbx2Raw.cpp @@ -91,17 +91,17 @@ private: class FbxRoughMetMaterialAccess { struct FbxMaterialProperties { - FbxFileTexture *texColor {}; - FbxVector4 colBase {}; - FbxFileTexture *texNormal {}; - FbxFileTexture *texMetallic {}; - FbxDouble metallic {}; - FbxFileTexture *texRoughness {}; - FbxDouble roughness {}; - FbxFileTexture *texEmissive {}; - FbxVector4 colEmissive {}; - FbxDouble emissiveIntensity; - FbxFileTexture *texAmbientOcclusion {}; + const FbxFileTexture *texColor {}; + FbxVector4 colBase { 1, 1, 1, 1 }; + const FbxFileTexture *texNormal {}; + const FbxFileTexture *texMetallic {}; + FbxDouble metallic {}; + const FbxFileTexture *texRoughness {}; + FbxDouble roughness {}; + const FbxFileTexture *texEmissive {}; + FbxVector4 colEmissive {}; + FbxDouble emissiveIntensity; + const FbxFileTexture *texAmbientOcclusion {}; }; private: @@ -124,7 +124,7 @@ public: {} struct FbxMaterialProperties extractTextures() { - struct FbxMaterialProperties res; + struct FbxMaterialProperties res { }; const FbxProperty mayaProp = fbxMaterial->FindProperty("Maya"); if (mayaProp.GetPropertyDataType() != FbxCompoundDT) { @@ -134,77 +134,29 @@ public: fmt::printf("Maya property type for material %s: %s\n", fbxMaterial->GetName(), foo.GetName()); - const FbxProperty normalMapProp = mayaProp.FindHierarchical("TEX_normal_map"); - res.texNormal = normalMapProp.GetSrcObject(); - fmt::printf("TEX_normal_map property type for material %s: %s\n", fbxMaterial->GetName(), normalMapProp.GetPropertyDataType().GetName()); + auto getTex = [&](std::string propName) { + const FbxFileTexture *ptr = nullptr; - const FbxProperty colorMapProp = mayaProp.FindHierarchical("TEX_color_map"); - res.texColor = colorMapProp.GetSrcObject(); - fmt::printf("TEX_color_map property type for material %s: %s\n", fbxMaterial->GetName(), colorMapProp.GetPropertyDataType().GetName()); + const FbxProperty useProp = mayaProp.FindHierarchical(("use_" + propName + "_map").c_str()); + if (useProp.IsValid() && useProp.Get()) { + const FbxProperty texProp = mayaProp.FindHierarchical(("TEX_" + propName + "_map").c_str()); + if (texProp.IsValid()) { + fmt::printf("%s property type for material %s: %s\n", propName, fbxMaterial->GetName(), texProp.GetPropertyDataType().GetName()); + ptr = texProp.GetSrcObject(); + } + } + return ptr; + }; + + res.texNormal = getTex("normal"); + res.texColor = getTex("color"); + res.texAmbientOcclusion = getTex("ao"); + res.texEmissive = getTex("emissive"); + res.texMetallic = getTex("metallic"); + res.texRoughness = getTex("roughness"); return res; } - - std::tuple getSurfaceScalar(const char *propName) const - { - const FbxProperty prop = fbxMaterial->FindProperty(propName); - - FbxDouble val(0); - FbxFileTexture *tex = prop.GetSrcObject(); - if (tex != nullptr && textureLocations.find(tex) == textureLocations.end()) { - tex = nullptr; - } - if (tex == nullptr && prop.IsValid()) { - val = prop.Get(); - } - return std::make_tuple(val, tex); - } - - std::tuple getSurfaceVector(const char *propName) const - { - const FbxProperty prop = fbxMaterial->FindProperty(propName); - - FbxDouble3 val(1, 1, 1); - FbxFileTexture *tex = prop.GetSrcObject(); - if (tex != nullptr && textureLocations.find(tex) == textureLocations.end()) { - tex = nullptr; - } - if (tex == nullptr && prop.IsValid()) { - val = prop.Get(); - } - return std::make_tuple(val, tex); - } - - std::tuple getSurfaceValues(const char *colName, const char *facName) const - { - const FbxProperty colProp = fbxMaterial->FindProperty(colName); - const FbxProperty facProp = fbxMaterial->FindProperty(facName); - - FbxDouble3 colorVal(1, 1, 1); - FbxDouble factorVal(1); - - FbxFileTexture *colTex = colProp.GetSrcObject(); - if (colTex != nullptr && textureLocations.find(colTex) == textureLocations.end()) { - colTex = nullptr; - } - if (colTex == nullptr && colProp.IsValid()) { - colorVal = colProp.Get(); - } - FbxFileTexture *facTex = facProp.GetSrcObject(); - if (facTex != nullptr && textureLocations.find(facTex) == textureLocations.end()) { - facTex = nullptr; - } - if (facTex == nullptr && facProp.IsValid()) { - factorVal = facProp.Get(); - } - - auto val = FbxVector4( - colorVal[0] * factorVal, - colorVal[1] * factorVal, - colorVal[2] * factorVal, - factorVal); - return std::make_tuple(val, colTex, facTex); - }; }; class FbxMaterialAccess @@ -393,14 +345,14 @@ public: } auto summary = summaries[materialNum]; if (summary == nullptr) { - summary = summaries[materialNum] = std::make_shared( + summary = summaries[materialNum] = std::make_shared( mesh->GetNode()->GetSrcObject(materialNum), textureLocations); } } } - const std::shared_ptr GetMaterial(const int polygonIndex) const + const std::shared_ptr GetMaterial(const int polygonIndex) const { if (mappingMode != FbxGeometryElement::eNone) { const int materialNum = indices->GetAt((mappingMode == FbxGeometryElement::eByPolygon) ? polygonIndex : 0); @@ -414,7 +366,7 @@ public: private: FbxGeometryElement::EMappingMode mappingMode; - std::vector> summaries {}; + std::vector> summaries {}; const FbxMesh *mesh; const FbxLayerElementArrayTemplate *indices; }; @@ -805,7 +757,7 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std: for (int polygonIndex = 0; polygonIndex < pMesh->GetPolygonCount(); polygonIndex++) { FBX_ASSERT(pMesh->GetPolygonSize(polygonIndex) == 3); - const std::shared_ptr fbxMaterial = materials.GetMaterial(polygonIndex); + const std::shared_ptr fbxMaterial = materials.GetMaterial(polygonIndex); int textures[RAW_TEXTURE_USAGE_MAX]; std::fill_n(textures, RAW_TEXTURE_USAGE_MAX, -1); @@ -824,7 +776,7 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std: const auto &matProps = fbxMaterial->props; - const auto maybeAddTexture = [&](FbxFileTexture *tex, RawTextureUsage usage) { + const auto maybeAddTexture = [&](const FbxFileTexture *tex, RawTextureUsage usage) { if (tex != nullptr) { // dig out the inferred filename from the textureLocations map FbxString inferredPath = textureLocations.find(tex)->second; @@ -832,19 +784,12 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std: } }; - ambient = matProps.colAmbient; - maybeAddTexture(matProps.texAmbient, RAW_TEXTURE_USAGE_AMBIENT); - specular = matProps.colSpecular; - maybeAddTexture(matProps.texSpecular, RAW_TEXTURE_USAGE_SPECULAR); - diffuse = matProps.colDiffuse; - maybeAddTexture(matProps.texDiffuse, RAW_TEXTURE_USAGE_DIFFUSE); - emissive = matProps.colEmissive; - maybeAddTexture(matProps.texEmissive, RAW_TEXTURE_USAGE_EMISSIVE); - + maybeAddTexture(matProps.texColor, RAW_TEXTURE_USAGE_ALBEDO); maybeAddTexture(matProps.texNormal, RAW_TEXTURE_USAGE_NORMAL); - - shininess = matProps.shininess; - maybeAddTexture(matProps.texShininess, RAW_TEXTURE_USAGE_SHININESS); + maybeAddTexture(matProps.texEmissive, RAW_TEXTURE_USAGE_EMISSIVE); + maybeAddTexture(matProps.texRoughness, RAW_TEXTURE_USAGE_ROUGHNESS); + maybeAddTexture(matProps.texMetallic, RAW_TEXTURE_USAGE_METALLIC); + maybeAddTexture(matProps.texAmbientOcclusion, RAW_TEXTURE_USAGE_OCCLUSION); } RawVertex rawVertices[3]; diff --git a/src/Raw2Gltf.cpp b/src/Raw2Gltf.cpp index 2c06466..8791ccb 100644 --- a/src/Raw2Gltf.cpp +++ b/src/Raw2Gltf.cpp @@ -425,7 +425,10 @@ ModelData *Raw2Gltf( std::shared_ptr pbrMetRough; if (options.usePBRMetRough) { - pbrMetRough.reset(new PBRMetallicRoughness(getTex(RAW_TEXTURE_USAGE_DIFFUSE), material.diffuseFactor)); + pbrMetRough.reset(new PBRMetallicRoughness( + getTex(RAW_TEXTURE_USAGE_ALBEDO), + getTex(RAW_TEXTURE_USAGE_ROUGHNESS), // TODO: do actual layer-baking! + material.diffuseFactor)); } std::shared_ptr pbrSpecGloss; if (options.usePBRSpecGloss) { diff --git a/src/RawModel.h b/src/RawModel.h index 1d0961f..4bfe015 100644 --- a/src/RawModel.h +++ b/src/RawModel.h @@ -104,6 +104,10 @@ enum RawTextureUsage RAW_TEXTURE_USAGE_SHININESS, RAW_TEXTURE_USAGE_EMISSIVE, RAW_TEXTURE_USAGE_REFLECTION, + RAW_TEXTURE_USAGE_ALBEDO, + RAW_TEXTURE_USAGE_OCCLUSION, + RAW_TEXTURE_USAGE_ROUGHNESS, + RAW_TEXTURE_USAGE_METALLIC, RAW_TEXTURE_USAGE_MAX }; @@ -127,6 +131,12 @@ inline std::string DescribeTextureUsage(int usage) return "emissive"; case RAW_TEXTURE_USAGE_REFLECTION: return "reflection"; + case RAW_TEXTURE_USAGE_OCCLUSION: + return "occlusion"; + case RAW_TEXTURE_USAGE_ROUGHNESS: + return "roughness"; + case RAW_TEXTURE_USAGE_METALLIC: + return "metallic"; case RAW_TEXTURE_USAGE_MAX: default: return "unknown"; diff --git a/src/glTF/MaterialData.cpp b/src/glTF/MaterialData.cpp index 60984cc..59f31de 100644 --- a/src/glTF/MaterialData.cpp +++ b/src/glTF/MaterialData.cpp @@ -126,10 +126,11 @@ void to_json(json &j, const PBRSpecularGlossiness &d) } PBRMetallicRoughness::PBRMetallicRoughness( - const TextureData *baseColorTexture, const Vec4f &baseolorFactor, - float metallic, float roughness) + const TextureData *baseColorTexture, const TextureData *metRoughTexture, + const Vec4f &baseColorFactor, float metallic, float roughness) : baseColorTexture(Tex::ref(baseColorTexture)), - baseColorFactor(baseolorFactor), + metRoughTexture(Tex::ref(metRoughTexture)), + baseColorFactor(baseColorFactor), metallic(metallic), roughness(roughness) { @@ -144,6 +145,9 @@ void to_json(json &j, const PBRMetallicRoughness &d) if (d.baseColorFactor.LengthSquared() > 0) { j["baseColorFactor"] = toStdVec(d.baseColorFactor); } + if (d.metRoughTexture != nullptr) { + j["metallicRoughnessTexture"] = *d.metRoughTexture; + } if (d.metallic != 1.0f) { j["metallicFactor"] = d.metallic; } diff --git a/src/glTF/MaterialData.h b/src/glTF/MaterialData.h index 3c45ed1..afcfcd7 100644 --- a/src/glTF/MaterialData.h +++ b/src/glTF/MaterialData.h @@ -70,10 +70,11 @@ struct PBRSpecularGlossiness struct PBRMetallicRoughness { PBRMetallicRoughness( - const TextureData *baseColorTexture, const Vec4f &baseolorFactor, - float metallic = 0.1f, float roughness = 0.4f); + const TextureData *baseColorTexture, const TextureData *metRoughTexture, + const Vec4f &baseColorFactor, float metallic = 0.1f, float roughness = 0.6f); std::unique_ptr baseColorTexture; + std::unique_ptr metRoughTexture; const Vec4f baseColorFactor; const float metallic; const float roughness;