further WIP

This commit is contained in:
Par Winzell 2017-11-18 14:19:13 -08:00
parent 2b35ef428f
commit 3084f7af54
5 changed files with 65 additions and 102 deletions

View File

@ -91,17 +91,17 @@ private:
class FbxRoughMetMaterialAccess class FbxRoughMetMaterialAccess
{ {
struct FbxMaterialProperties { struct FbxMaterialProperties {
FbxFileTexture *texColor {}; const FbxFileTexture *texColor {};
FbxVector4 colBase {}; FbxVector4 colBase { 1, 1, 1, 1 };
FbxFileTexture *texNormal {}; const FbxFileTexture *texNormal {};
FbxFileTexture *texMetallic {}; const FbxFileTexture *texMetallic {};
FbxDouble metallic {}; FbxDouble metallic {};
FbxFileTexture *texRoughness {}; const FbxFileTexture *texRoughness {};
FbxDouble roughness {}; FbxDouble roughness {};
FbxFileTexture *texEmissive {}; const FbxFileTexture *texEmissive {};
FbxVector4 colEmissive {}; FbxVector4 colEmissive {};
FbxDouble emissiveIntensity; FbxDouble emissiveIntensity;
FbxFileTexture *texAmbientOcclusion {}; const FbxFileTexture *texAmbientOcclusion {};
}; };
private: private:
@ -124,7 +124,7 @@ public:
{} {}
struct FbxMaterialProperties extractTextures() { struct FbxMaterialProperties extractTextures() {
struct FbxMaterialProperties res; struct FbxMaterialProperties res { };
const FbxProperty mayaProp = fbxMaterial->FindProperty("Maya"); const FbxProperty mayaProp = fbxMaterial->FindProperty("Maya");
if (mayaProp.GetPropertyDataType() != FbxCompoundDT) { if (mayaProp.GetPropertyDataType() != FbxCompoundDT) {
@ -134,77 +134,29 @@ public:
fmt::printf("Maya property type for material %s: %s\n", fbxMaterial->GetName(), foo.GetName()); fmt::printf("Maya property type for material %s: %s\n", fbxMaterial->GetName(), foo.GetName());
const FbxProperty normalMapProp = mayaProp.FindHierarchical("TEX_normal_map"); auto getTex = [&](std::string propName) {
res.texNormal = normalMapProp.GetSrcObject<FbxFileTexture>(); const FbxFileTexture *ptr = nullptr;
fmt::printf("TEX_normal_map property type for material %s: %s\n", fbxMaterial->GetName(), normalMapProp.GetPropertyDataType().GetName());
const FbxProperty colorMapProp = mayaProp.FindHierarchical("TEX_color_map"); const FbxProperty useProp = mayaProp.FindHierarchical(("use_" + propName + "_map").c_str());
res.texColor = colorMapProp.GetSrcObject<FbxFileTexture>(); if (useProp.IsValid() && useProp.Get<bool>()) {
fmt::printf("TEX_color_map property type for material %s: %s\n", fbxMaterial->GetName(), colorMapProp.GetPropertyDataType().GetName()); 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<FbxFileTexture>();
}
}
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; return res;
} }
std::tuple<FbxDouble, FbxFileTexture *> getSurfaceScalar(const char *propName) const
{
const FbxProperty prop = fbxMaterial->FindProperty(propName);
FbxDouble val(0);
FbxFileTexture *tex = prop.GetSrcObject<FbxFileTexture>();
if (tex != nullptr && textureLocations.find(tex) == textureLocations.end()) {
tex = nullptr;
}
if (tex == nullptr && prop.IsValid()) {
val = prop.Get<FbxDouble>();
}
return std::make_tuple(val, tex);
}
std::tuple<FbxDouble3, FbxFileTexture *> getSurfaceVector(const char *propName) const
{
const FbxProperty prop = fbxMaterial->FindProperty(propName);
FbxDouble3 val(1, 1, 1);
FbxFileTexture *tex = prop.GetSrcObject<FbxFileTexture>();
if (tex != nullptr && textureLocations.find(tex) == textureLocations.end()) {
tex = nullptr;
}
if (tex == nullptr && prop.IsValid()) {
val = prop.Get<FbxDouble3>();
}
return std::make_tuple(val, tex);
}
std::tuple<FbxVector4, FbxFileTexture *, FbxFileTexture *> 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<FbxFileTexture>();
if (colTex != nullptr && textureLocations.find(colTex) == textureLocations.end()) {
colTex = nullptr;
}
if (colTex == nullptr && colProp.IsValid()) {
colorVal = colProp.Get<FbxDouble3>();
}
FbxFileTexture *facTex = facProp.GetSrcObject<FbxFileTexture>();
if (facTex != nullptr && textureLocations.find(facTex) == textureLocations.end()) {
facTex = nullptr;
}
if (facTex == nullptr && facProp.IsValid()) {
factorVal = facProp.Get<FbxDouble>();
}
auto val = FbxVector4(
colorVal[0] * factorVal,
colorVal[1] * factorVal,
colorVal[2] * factorVal,
factorVal);
return std::make_tuple(val, colTex, facTex);
};
}; };
class FbxMaterialAccess class FbxMaterialAccess
@ -393,14 +345,14 @@ public:
} }
auto summary = summaries[materialNum]; auto summary = summaries[materialNum];
if (summary == nullptr) { if (summary == nullptr) {
summary = summaries[materialNum] = std::make_shared<FbxMaterialAccess>( summary = summaries[materialNum] = std::make_shared<FbxRoughMetMaterialAccess>(
mesh->GetNode()->GetSrcObject<FbxSurfaceMaterial>(materialNum), mesh->GetNode()->GetSrcObject<FbxSurfaceMaterial>(materialNum),
textureLocations); textureLocations);
} }
} }
} }
const std::shared_ptr<FbxMaterialAccess> GetMaterial(const int polygonIndex) const const std::shared_ptr<FbxRoughMetMaterialAccess> GetMaterial(const int polygonIndex) const
{ {
if (mappingMode != FbxGeometryElement::eNone) { if (mappingMode != FbxGeometryElement::eNone) {
const int materialNum = indices->GetAt((mappingMode == FbxGeometryElement::eByPolygon) ? polygonIndex : 0); const int materialNum = indices->GetAt((mappingMode == FbxGeometryElement::eByPolygon) ? polygonIndex : 0);
@ -414,7 +366,7 @@ public:
private: private:
FbxGeometryElement::EMappingMode mappingMode; FbxGeometryElement::EMappingMode mappingMode;
std::vector<std::shared_ptr<FbxMaterialAccess>> summaries {}; std::vector<std::shared_ptr<FbxRoughMetMaterialAccess>> summaries {};
const FbxMesh *mesh; const FbxMesh *mesh;
const FbxLayerElementArrayTemplate<int> *indices; const FbxLayerElementArrayTemplate<int> *indices;
}; };
@ -805,7 +757,7 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std:
for (int polygonIndex = 0; polygonIndex < pMesh->GetPolygonCount(); polygonIndex++) { for (int polygonIndex = 0; polygonIndex < pMesh->GetPolygonCount(); polygonIndex++) {
FBX_ASSERT(pMesh->GetPolygonSize(polygonIndex) == 3); FBX_ASSERT(pMesh->GetPolygonSize(polygonIndex) == 3);
const std::shared_ptr<FbxMaterialAccess> fbxMaterial = materials.GetMaterial(polygonIndex); const std::shared_ptr<FbxRoughMetMaterialAccess> fbxMaterial = materials.GetMaterial(polygonIndex);
int textures[RAW_TEXTURE_USAGE_MAX]; int textures[RAW_TEXTURE_USAGE_MAX];
std::fill_n(textures, RAW_TEXTURE_USAGE_MAX, -1); 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 &matProps = fbxMaterial->props;
const auto maybeAddTexture = [&](FbxFileTexture *tex, RawTextureUsage usage) { const auto maybeAddTexture = [&](const FbxFileTexture *tex, RawTextureUsage usage) {
if (tex != nullptr) { if (tex != nullptr) {
// dig out the inferred filename from the textureLocations map // dig out the inferred filename from the textureLocations map
FbxString inferredPath = textureLocations.find(tex)->second; 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.texColor, RAW_TEXTURE_USAGE_ALBEDO);
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.texNormal, RAW_TEXTURE_USAGE_NORMAL); maybeAddTexture(matProps.texNormal, RAW_TEXTURE_USAGE_NORMAL);
maybeAddTexture(matProps.texEmissive, RAW_TEXTURE_USAGE_EMISSIVE);
shininess = matProps.shininess; maybeAddTexture(matProps.texRoughness, RAW_TEXTURE_USAGE_ROUGHNESS);
maybeAddTexture(matProps.texShininess, RAW_TEXTURE_USAGE_SHININESS); maybeAddTexture(matProps.texMetallic, RAW_TEXTURE_USAGE_METALLIC);
maybeAddTexture(matProps.texAmbientOcclusion, RAW_TEXTURE_USAGE_OCCLUSION);
} }
RawVertex rawVertices[3]; RawVertex rawVertices[3];

View File

@ -425,7 +425,10 @@ ModelData *Raw2Gltf(
std::shared_ptr<PBRMetallicRoughness> pbrMetRough; std::shared_ptr<PBRMetallicRoughness> pbrMetRough;
if (options.usePBRMetRough) { 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<PBRSpecularGlossiness> pbrSpecGloss; std::shared_ptr<PBRSpecularGlossiness> pbrSpecGloss;
if (options.usePBRSpecGloss) { if (options.usePBRSpecGloss) {

View File

@ -104,6 +104,10 @@ enum RawTextureUsage
RAW_TEXTURE_USAGE_SHININESS, RAW_TEXTURE_USAGE_SHININESS,
RAW_TEXTURE_USAGE_EMISSIVE, RAW_TEXTURE_USAGE_EMISSIVE,
RAW_TEXTURE_USAGE_REFLECTION, RAW_TEXTURE_USAGE_REFLECTION,
RAW_TEXTURE_USAGE_ALBEDO,
RAW_TEXTURE_USAGE_OCCLUSION,
RAW_TEXTURE_USAGE_ROUGHNESS,
RAW_TEXTURE_USAGE_METALLIC,
RAW_TEXTURE_USAGE_MAX RAW_TEXTURE_USAGE_MAX
}; };
@ -127,6 +131,12 @@ inline std::string DescribeTextureUsage(int usage)
return "emissive"; return "emissive";
case RAW_TEXTURE_USAGE_REFLECTION: case RAW_TEXTURE_USAGE_REFLECTION:
return "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: case RAW_TEXTURE_USAGE_MAX:
default: default:
return "unknown"; return "unknown";

View File

@ -126,10 +126,11 @@ void to_json(json &j, const PBRSpecularGlossiness &d)
} }
PBRMetallicRoughness::PBRMetallicRoughness( PBRMetallicRoughness::PBRMetallicRoughness(
const TextureData *baseColorTexture, const Vec4f &baseolorFactor, const TextureData *baseColorTexture, const TextureData *metRoughTexture,
float metallic, float roughness) const Vec4f &baseColorFactor, float metallic, float roughness)
: baseColorTexture(Tex::ref(baseColorTexture)), : baseColorTexture(Tex::ref(baseColorTexture)),
baseColorFactor(baseolorFactor), metRoughTexture(Tex::ref(metRoughTexture)),
baseColorFactor(baseColorFactor),
metallic(metallic), metallic(metallic),
roughness(roughness) roughness(roughness)
{ {
@ -144,6 +145,9 @@ void to_json(json &j, const PBRMetallicRoughness &d)
if (d.baseColorFactor.LengthSquared() > 0) { if (d.baseColorFactor.LengthSquared() > 0) {
j["baseColorFactor"] = toStdVec(d.baseColorFactor); j["baseColorFactor"] = toStdVec(d.baseColorFactor);
} }
if (d.metRoughTexture != nullptr) {
j["metallicRoughnessTexture"] = *d.metRoughTexture;
}
if (d.metallic != 1.0f) { if (d.metallic != 1.0f) {
j["metallicFactor"] = d.metallic; j["metallicFactor"] = d.metallic;
} }

View File

@ -70,10 +70,11 @@ struct PBRSpecularGlossiness
struct PBRMetallicRoughness struct PBRMetallicRoughness
{ {
PBRMetallicRoughness( PBRMetallicRoughness(
const TextureData *baseColorTexture, const Vec4f &baseolorFactor, const TextureData *baseColorTexture, const TextureData *metRoughTexture,
float metallic = 0.1f, float roughness = 0.4f); const Vec4f &baseColorFactor, float metallic = 0.1f, float roughness = 0.6f);
std::unique_ptr<Tex> baseColorTexture; std::unique_ptr<Tex> baseColorTexture;
std::unique_ptr<Tex> metRoughTexture;
const Vec4f baseColorFactor; const Vec4f baseColorFactor;
const float metallic; const float metallic;
const float roughness; const float roughness;