move material mapping to hfm prep step

This commit is contained in:
SamGondelman 2019-02-07 14:10:09 -08:00
parent 3addcb630c
commit e601f6c59f
6 changed files with 93 additions and 30 deletions

View file

@ -1326,7 +1326,7 @@ HFMModel* FBXSerializer::extractHFMModel(const QVariantHash& mapping, const QStr
hfmModel.meshExtents.reset();
// Create the Material Library
consolidateHFMMaterials(mapping);
consolidateHFMMaterials();
// We can't allow the scaling of a given image to different sizes, because the hash used for the KTX cache is based on the original image
// Allowing scaling of the same image to different sizes would cause different KTX files to target the same cache key

View file

@ -153,7 +153,7 @@ public:
QHash<QString, HFMMaterial> _hfmMaterials;
QHash<QString, MaterialParam> _materialParams;
void consolidateHFMMaterials(const QVariantHash& mapping);
void consolidateHFMMaterials();
bool _loadLightmaps { true };
float _lightmapOffset { 0.0f };

View file

@ -75,15 +75,7 @@ HFMTexture FBXSerializer::getTexture(const QString& textureID, const QString& ma
return texture;
}
void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) {
QJsonObject materialMap;
if (mapping.contains("materialMap")) {
QByteArray materialMapValue = mapping.value("materialMap").toByteArray();
materialMap = QJsonDocument::fromJson(materialMapValue).object();
if (materialMap.isEmpty()) {
qCDebug(modelformat) << "fbx Material Map found but did not produce valid JSON:" << materialMapValue;
}
}
void FBXSerializer::consolidateHFMMaterials() {
for (QHash<QString, HFMMaterial>::iterator it = _hfmMaterials.begin(); it != _hfmMaterials.end(); it++) {
HFMMaterial& material = (*it);
@ -266,23 +258,6 @@ void FBXSerializer::consolidateHFMMaterials(const QVariantHash& mapping) {
}
qCDebug(modelformat) << " fbx material Name:" << material.name;
if (materialMap.contains(material.name)) {
QJsonObject materialOptions = materialMap.value(material.name).toObject();
qCDebug(modelformat) << "Mapping fbx material:" << material.name << " with HifiMaterial: " << materialOptions;
if (materialOptions.contains("scattering")) {
float scattering = (float) materialOptions.value("scattering").toDouble();
material._material->setScattering(scattering);
}
if (materialOptions.contains("scatteringMap")) {
QByteArray scatteringMap = materialOptions.value("scatteringMap").toVariant().toByteArray();
material.scatteringTexture = HFMTexture();
material.scatteringTexture.name = material.name + ".scatteringMap";
material.scatteringTexture.filename = scatteringMap;
}
}
if (material.opacity <= 0.0f) {
material._material->setOpacity(1.0f);
} else {

View file

@ -0,0 +1,55 @@
//
// Created by Sam Gondelman on 2/7/2019
// Copyright 2019 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "ApplyMaterialMappingTask.h"
#include "ModelBakerLogging.h"
void ApplyMaterialMappingTask::run(const baker::BakeContextPointer& context, const Input& input, Output& output) {
const auto& materialsIn = input.get0();
const auto& mapping = input.get1();
auto materialsOut = materialsIn;
auto mappingIter = mapping.find("materialMap");
if (mappingIter != mapping.end()) {
QByteArray materialMapValue = mappingIter.value().toByteArray();
QJsonObject materialMap = QJsonDocument::fromJson(materialMapValue).object();
if (materialMap.isEmpty()) {
qCDebug(model_baker) << "Material Map found but did not produce valid JSON:" << materialMapValue;
} else {
for (auto& material : materialsOut) {
auto materialMapIter = materialMap.find(material.name);
if (materialMapIter != materialMap.end()) {
QJsonObject materialOptions = materialMapIter.value().toObject();
qCDebug(model_baker) << "Mapping material:" << material.name << " with HFMaterial: " << materialOptions;
{
auto scatteringIter = materialOptions.find("scattering");
if (scatteringIter != materialOptions.end()) {
float scattering = (float)scatteringIter.value().toDouble();
material._material->setScattering(scattering);
}
}
{
auto scatteringMapIter = materialOptions.find("scatteringMap");
if (scatteringMapIter != materialOptions.end()) {
QByteArray scatteringMap = scatteringMapIter.value().toVariant().toByteArray();
material.scatteringTexture = HFMTexture();
material.scatteringTexture.name = material.name + ".scatteringMap";
material.scatteringTexture.filename = scatteringMap;
}
}
}
}
}
}
output = materialsOut;
}

View file

@ -0,0 +1,27 @@
//
// Created by Sam Gondelman on 2/7/2019
// Copyright 2019 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_ApplyMaterialMappingTask_h
#define hifi_ApplyMaterialMappingTask_h
#include <QHash>
#include <hfm/HFM.h>
#include "Engine.h"
class ApplyMaterialMappingTask {
public:
using Input = baker::VaryingSet2<QHash<QString, hfm::Material>, QVariantHash>;
using Output = QHash<QString, hfm::Material>;
using JobModel = baker::Job::ModelIO<ApplyMaterialMappingTask, Input, Output>;
void run(const baker::BakeContextPointer& context, const Input& input, Output& output);
};
#endif // hifi_ApplyMaterialMappingTask_h

View file

@ -20,6 +20,7 @@
#include "CalculateBlendshapeNormalsTask.h"
#include "CalculateBlendshapeTangentsTask.h"
#include "PrepareJointsTask.h"
#include "ApplyMaterialMappingTask.h"
namespace baker {
@ -101,7 +102,7 @@ namespace baker {
class BuildModelTask {
public:
using Input = VaryingSet5<hfm::Model::Pointer, std::vector<hfm::Mesh>, std::vector<hfm::Joint>, QMap<int, glm::quat> /*jointRotationOffsets*/, QHash<QString, int> /*jointIndices*/>;
using Input = VaryingSet6<hfm::Model::Pointer, std::vector<hfm::Mesh>, std::vector<hfm::Joint>, QMap<int, glm::quat>, QHash<QString, int>, QHash<QString, hfm::Material>>;
using Output = hfm::Model::Pointer;
using JobModel = Job::ModelIO<BuildModelTask, Input, Output>;
@ -111,6 +112,7 @@ namespace baker {
hfmModelOut->joints = QVector<hfm::Joint>::fromStdVector(input.get2());
hfmModelOut->jointRotationOffsets = input.get3();
hfmModelOut->jointIndices = input.get4();
hfmModelOut->materials = input.get5();
output = hfmModelOut;
}
};
@ -154,12 +156,16 @@ namespace baker {
const auto jointRotationOffsets = jointInfoOut.getN<PrepareJointsTask::Output>(1);
const auto jointIndices = jointInfoOut.getN<PrepareJointsTask::Output>(2);
// Apply material mapping
const auto materialMappingInputs = ApplyMaterialMappingTask::Input(materials, mapping).asVarying();
const auto materialsOut = model.addJob<ApplyMaterialMappingTask>("ApplyMaterialMapping", materialMappingInputs);
// Combine the outputs into a new hfm::Model
const auto buildBlendshapesInputs = BuildBlendshapesTask::Input(blendshapesPerMeshIn, normalsPerBlendshapePerMesh, tangentsPerBlendshapePerMesh).asVarying();
const auto blendshapesPerMeshOut = model.addJob<BuildBlendshapesTask>("BuildBlendshapes", buildBlendshapesInputs);
const auto buildMeshesInputs = BuildMeshesTask::Input(meshesIn, graphicsMeshes, normalsPerMesh, tangentsPerMesh, blendshapesPerMeshOut).asVarying();
const auto meshesOut = model.addJob<BuildMeshesTask>("BuildMeshes", buildMeshesInputs);
const auto buildModelInputs = BuildModelTask::Input(hfmModelIn, meshesOut, jointsOut, jointRotationOffsets, jointIndices).asVarying();
const auto buildModelInputs = BuildModelTask::Input(hfmModelIn, meshesOut, jointsOut, jointRotationOffsets, jointIndices, materialsOut).asVarying();
hfmModelOut = model.addJob<BuildModelTask>("BuildModel", buildModelInputs);
}
};