Add support for Maya material uv scale/translation

This commit is contained in:
sabrina-shanman 2019-01-11 17:14:10 -08:00
parent 5b207775e7
commit 00a9e9e92f
3 changed files with 72 additions and 15 deletions

View file

@ -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<double>();
} else if (property.properties.at(0) == MAYA_UV_SCALE) {
if (property.properties.size() == 6) {
glm::vec3 scale;
if (property.properties.at(2).value<QString>() == "Vector2") {
scale = glm::vec3(property.properties.at(4).value<double>(), property.properties.at(5).value<double>(), 1.0);
} else { // Vector (3d)
scale = glm::vec3(property.properties.at(3).value<double>(), property.properties.at(4).value<double>(), property.properties.at(5).value<double>());
}
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<QString>() == "Vector2") {
translation = glm::vec3(property.properties.at(4).value<double>(), property.properties.at(5).value<double>(), 1.0);
} else { // Vector (3d)
translation = glm::vec3(property.properties.at(3).value<double>(), property.properties.at(4).value<double>(), property.properties.at(5).value<double>());
}
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)) {

View file

@ -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<QString, QString> _textureNames;
// Hashes the original RelativeFilename of textures
@ -133,6 +149,7 @@ public:
QHash<QString, QString> occlusionTextures;
QHash<QString, HFMMaterial> _hfmMaterials;
QHash<QString, MaterialParam> _materialParams;
void consolidateHFMMaterials(const QVariantHash& mapping);

View file

@ -27,7 +27,7 @@
#include <hfm/ModelFormatLogging.h>
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;