From 00a9e9e92f768828ebdbafe3f23fa266c3d0334f Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Fri, 11 Jan 2019 17:14:10 -0800 Subject: [PATCH] Add support for Maya material uv scale/translation --- libraries/fbx/src/FBXSerializer.cpp | 37 +++++++++++++++++++- libraries/fbx/src/FBXSerializer.h | 19 +++++++++- libraries/fbx/src/FBXSerializer_Material.cpp | 31 +++++++++------- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/libraries/fbx/src/FBXSerializer.cpp b/libraries/fbx/src/FBXSerializer.cpp index e51e6a3615..adf722bfb2 100644 --- a/libraries/fbx/src/FBXSerializer.cpp +++ b/libraries/fbx/src/FBXSerializer.cpp @@ -930,6 +930,7 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr } } else if (object.name == "Material") { HFMMaterial material; + MaterialParam materialParam; material.name = (object.properties.at(1).toString()); foreach (const FBXNode& subobject, object.children) { bool properties = false; @@ -974,6 +975,8 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr static const QVariant MAYA_EMISSIVE_INTENSITY = QByteArray("Maya|emissive_intensity"); static const QVariant MAYA_USE_EMISSIVE_MAP = QByteArray("Maya|use_emissive_map"); static const QVariant MAYA_USE_AO_MAP = QByteArray("Maya|use_ao_map"); + static const QVariant MAYA_UV_SCALE = QByteArray("Maya|uv_scale"); + static const QVariant MAYA_UV_OFFSET = QByteArray("Maya|uv_offset"); @@ -1062,6 +1065,35 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr material.isPBSMaterial = true; material.useOcclusionMap = (bool)property.properties.at(index).value(); + } else if (property.properties.at(0) == MAYA_UV_SCALE) { + if (property.properties.size() == 6) { + glm::vec3 scale; + if (property.properties.at(2).value() == "Vector2") { + scale = glm::vec3(property.properties.at(4).value(), property.properties.at(5).value(), 1.0); + } else { // Vector (3d) + scale = glm::vec3(property.properties.at(3).value(), property.properties.at(4).value(), property.properties.at(5).value()); + } + if (scale.x == 0.0) { + scale.x = 1.0; + } + if (scale.y == 0.0) { + scale.y = 1.0; + } + if (scale.z == 0.0) { + scale.z = 1.0; + } + materialParam.scaling *= scale; + } + } else if (property.properties.at(0) == MAYA_UV_OFFSET) { + if (property.properties.size() == 6) { + glm::vec3 translation; + if (property.properties.at(2).value() == "Vector2") { + translation = glm::vec3(property.properties.at(4).value(), property.properties.at(5).value(), 1.0); + } else { // Vector (3d) + translation = glm::vec3(property.properties.at(3).value(), property.properties.at(4).value(), property.properties.at(5).value()); + } + materialParam.translation += translation; + } } else { const QString propname = property.properties.at(0).toString(); unknowns.push_back(propname.toStdString()); @@ -1083,6 +1115,7 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr } material.materialID = getID(object.properties); _hfmMaterials.insert(material.materialID, material); + _materialParams.insert(material.materialID, materialParam); } else if (object.name == "NodeAttribute") { @@ -1540,7 +1573,9 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr materialIndex++; } else if (_textureFilenames.contains(childID)) { - HFMTexture texture = getTexture(childID); + // NOTE (Sabrina 2019/01/11): getTextures now takes in the materialID as a second parameter, because FBX material nodes can sometimes have uv transform information (ex: "Maya|uv_scale") + // I'm leaving the second parameter blank right now as this code may never be used. + HFMTexture texture = getTexture(childID, ""); for (int j = 0; j < extracted.partMaterialTextures.size(); j++) { int partTexture = extracted.partMaterialTextures.at(j).second; if (partTexture == textureIndex && !(partTexture == 0 && materialsHaveTextures)) { diff --git a/libraries/fbx/src/FBXSerializer.h b/libraries/fbx/src/FBXSerializer.h index 0a07bacc54..568793789a 100644 --- a/libraries/fbx/src/FBXSerializer.h +++ b/libraries/fbx/src/FBXSerializer.h @@ -86,6 +86,22 @@ public: }; +class MaterialParam { +public: + glm::vec3 translation; + glm::vec3 scaling; + + MaterialParam() : + translation(0.0), + scaling(1.0) + {} + + MaterialParam(const MaterialParam& src) : + translation(src.translation), + scaling(src.scaling) + {} +}; + class ExtractedMesh; class FBXSerializer : public HFMSerializer { @@ -106,7 +122,7 @@ public: static glm::vec3 normalizeDirForPacking(const glm::vec3& dir); - HFMTexture getTexture(const QString& textureID); + HFMTexture getTexture(const QString& textureID, const QString& materialID); QHash _textureNames; // Hashes the original RelativeFilename of textures @@ -133,6 +149,7 @@ public: QHash occlusionTextures; QHash _hfmMaterials; + QHash _materialParams; void consolidateHFMMaterials(const QVariantHash& mapping); diff --git a/libraries/fbx/src/FBXSerializer_Material.cpp b/libraries/fbx/src/FBXSerializer_Material.cpp index 0ba7972752..3db7861180 100644 --- a/libraries/fbx/src/FBXSerializer_Material.cpp +++ b/libraries/fbx/src/FBXSerializer_Material.cpp @@ -27,7 +27,7 @@ #include -HFMTexture FBXSerializer::getTexture(const QString& textureID) { +HFMTexture FBXSerializer::getTexture(const QString& textureID, const QString& materialID) { HFMTexture texture; const QByteArray& filepath = _textureFilepaths.value(textureID); texture.content = _textureContent.value(filepath); @@ -66,6 +66,11 @@ HFMTexture FBXSerializer::getTexture(const QString& textureID) { } texture.texcoordSetName = p.UVSet; } + if (_materialParams.contains(materialID)) { + auto materialParam = _materialParams[materialID]; + texture.transform.postTranslate(materialParam.translation); + texture.transform.postScale(materialParam.scaling); + } return texture; } @@ -102,12 +107,12 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { material.diffuseFactor = 1.0; } - diffuseTexture = getTexture(diffuseTextureID); + diffuseTexture = getTexture(diffuseTextureID, material.materialID); // FBX files generated by 3DSMax have an intermediate texture parent, apparently foreach(const QString& childTextureID, _connectionChildMap.values(diffuseTextureID)) { if (_textureFilenames.contains(childTextureID)) { - diffuseTexture = getTexture(diffuseTextureID); + diffuseTexture = getTexture(diffuseTextureID, material.materialID); } } @@ -122,7 +127,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { transparentTextureID = diffuseTextureID; } if (!transparentTextureID.isNull()) { - transparentTexture = getTexture(transparentTextureID); + transparentTexture = getTexture(transparentTextureID, material.materialID); material.opacityTexture = transparentTexture; detectDifferentUVs |= (transparentTexture.texcoordSet != 0) || (!transparentTexture.transform.isIdentity()); } @@ -131,13 +136,13 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { QString bumpTextureID = bumpTextures.value(material.materialID); QString normalTextureID = normalTextures.value(material.materialID); if (!normalTextureID.isNull()) { - normalTexture = getTexture(normalTextureID); + normalTexture = getTexture(normalTextureID, material.materialID); normalTexture.isBumpmap = false; material.normalTexture = normalTexture; detectDifferentUVs |= (normalTexture.texcoordSet != 0) || (!normalTexture.transform.isIdentity()); } else if (!bumpTextureID.isNull()) { - normalTexture = getTexture(bumpTextureID); + normalTexture = getTexture(bumpTextureID, material.materialID); normalTexture.isBumpmap = true; material.normalTexture = normalTexture; @@ -147,7 +152,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { HFMTexture specularTexture; QString specularTextureID = specularTextures.value(material.materialID); if (!specularTextureID.isNull()) { - specularTexture = getTexture(specularTextureID); + specularTexture = getTexture(specularTextureID, material.materialID); detectDifferentUVs |= (specularTexture.texcoordSet != 0) || (!specularTexture.transform.isIdentity()); material.specularTexture = specularTexture; } @@ -155,7 +160,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { HFMTexture metallicTexture; QString metallicTextureID = metallicTextures.value(material.materialID); if (!metallicTextureID.isNull()) { - metallicTexture = getTexture(metallicTextureID); + metallicTexture = getTexture(metallicTextureID, material.materialID); detectDifferentUVs |= (metallicTexture.texcoordSet != 0) || (!metallicTexture.transform.isIdentity()); material.metallicTexture = metallicTexture; } @@ -163,7 +168,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { HFMTexture roughnessTexture; QString roughnessTextureID = roughnessTextures.value(material.materialID); if (!roughnessTextureID.isNull()) { - roughnessTexture = getTexture(roughnessTextureID); + roughnessTexture = getTexture(roughnessTextureID, material.materialID); material.roughnessTexture = roughnessTexture; detectDifferentUVs |= (roughnessTexture.texcoordSet != 0) || (!roughnessTexture.transform.isIdentity()); } @@ -171,7 +176,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { HFMTexture shininessTexture; QString shininessTextureID = shininessTextures.value(material.materialID); if (!shininessTextureID.isNull()) { - shininessTexture = getTexture(shininessTextureID); + shininessTexture = getTexture(shininessTextureID, material.materialID); material.glossTexture = shininessTexture; detectDifferentUVs |= (shininessTexture.texcoordSet != 0) || (!shininessTexture.transform.isIdentity()); } @@ -179,7 +184,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { HFMTexture emissiveTexture; QString emissiveTextureID = emissiveTextures.value(material.materialID); if (!emissiveTextureID.isNull()) { - emissiveTexture = getTexture(emissiveTextureID); + emissiveTexture = getTexture(emissiveTextureID, material.materialID); detectDifferentUVs |= (emissiveTexture.texcoordSet != 0) || (!emissiveTexture.transform.isIdentity()); material.emissiveTexture = emissiveTexture; @@ -202,7 +207,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { } if (!occlusionTextureID.isNull()) { - occlusionTexture = getTexture(occlusionTextureID); + occlusionTexture = getTexture(occlusionTextureID, material.materialID); detectDifferentUVs |= (occlusionTexture.texcoordSet != 0) || (!emissiveTexture.transform.isIdentity()); material.occlusionTexture = occlusionTexture; } @@ -222,7 +227,7 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) { } if (_loadLightmaps && !ambientTextureID.isNull()) { - ambientTexture = getTexture(ambientTextureID); + ambientTexture = getTexture(ambientTextureID, material.materialID); detectDifferentUVs |= (ambientTexture.texcoordSet != 0) || (!ambientTexture.transform.isIdentity()); material.lightmapTexture = ambientTexture; material.lightmapParams = lightmapParams;