3
0
Fork 0
mirror of https://github.com/lubosz/overte.git synced 2025-04-27 09:55:45 +02:00

embedded textures and relative material paths

This commit is contained in:
SamGondelman 2019-03-29 10:08:31 -07:00
parent b98f47d1f3
commit 585a022dca
3 changed files with 42 additions and 4 deletions

View file

@ -149,8 +149,18 @@ void MaterialBaker::processMaterial() {
if (!_textureBakers.contains(textureKey)) {
auto baseTextureFileName = _textureFileNamer.createBaseTextureFileName(textureURL.fileName(), it->second);
QByteArray content;
{
auto textureContentMapIter = _textureContentMap.find(networkMaterial.second->getName());
if (textureContentMapIter != _textureContentMap.end()) {
auto textureUsageIter = textureContentMapIter->second.find(it->second);
if (textureUsageIter != textureContentMapIter->second.end()) {
content = textureUsageIter->second;
}
}
}
QSharedPointer<TextureBaker> textureBaker {
new TextureBaker(textureURL, it->second, _textureOutputDir, "", baseTextureFileName),
new TextureBaker(textureURL, it->second, _textureOutputDir, "", baseTextureFileName, content),
&TextureBaker::deleteLater
};
textureBaker->setMapChannel(mapChannel);
@ -159,6 +169,7 @@ void MaterialBaker::processMaterial() {
textureBaker->moveToThread(_getNextOvenWorkerThreadOperator ? _getNextOvenWorkerThreadOperator() : thread());
QMetaObject::invokeMethod(textureBaker.data(), "bake");
}
// FIXME: we need to detect when our material has opacity and output the opacityMap
_materialsNeedingRewrite.insert(textureKey, networkMaterial.second);
} else {
qCDebug(material_baking) << "Texture extension not supported: " << extension;
@ -254,10 +265,31 @@ void MaterialBaker::outputMaterial() {
emit finished();
}
void MaterialBaker::addTexture(const QString& materialName, image::TextureUsage::Type textureUsage, const hfm::Texture& texture) {
std::string name = materialName.toStdString();
auto& textureUsageMap = _textureContentMap[materialName.toStdString()];
if (textureUsageMap.find(textureUsage) == textureUsageMap.end()) {
// Content may be empty, unless the data is inlined
textureUsageMap[textureUsage] = texture.content;
}
};
void MaterialBaker::setMaterials(const QHash<QString, hfm::Material>& materials, const QString& baseURL) {
_materialResource = NetworkMaterialResourcePointer(new NetworkMaterialResource(), [](NetworkMaterialResource* ptr) { ptr->deleteLater(); });
for (auto& material : materials) {
_materialResource->parsedMaterials.names.push_back(material.name.toStdString());
_materialResource->parsedMaterials.networkMaterials[material.name.toStdString()] = std::make_shared<NetworkMaterial>(material, baseURL);
// Store any embedded texture content
addTexture(material.name, image::TextureUsage::NORMAL_TEXTURE, material.normalTexture);
addTexture(material.name, image::TextureUsage::ALBEDO_TEXTURE, material.albedoTexture);
addTexture(material.name, image::TextureUsage::GLOSS_TEXTURE, material.glossTexture);
addTexture(material.name, image::TextureUsage::ROUGHNESS_TEXTURE, material.roughnessTexture);
addTexture(material.name, image::TextureUsage::SPECULAR_TEXTURE, material.specularTexture);
addTexture(material.name, image::TextureUsage::METALLIC_TEXTURE, material.metallicTexture);
addTexture(material.name, image::TextureUsage::EMISSIVE_TEXTURE, material.emissiveTexture);
addTexture(material.name, image::TextureUsage::OCCLUSION_TEXTURE, material.occlusionTexture);
addTexture(material.name, image::TextureUsage::SCATTERING_TEXTURE, material.scatteringTexture);
addTexture(material.name, image::TextureUsage::LIGHTMAP_TEXTURE, material.lightmapTexture);
}
}

View file

@ -65,6 +65,9 @@ private:
QScriptEngine _scriptEngine;
static std::function<QThread*()> _getNextOvenWorkerThreadOperator;
TextureFileNamer _textureFileNamer;
void addTexture(const QString& materialName, image::TextureUsage::Type textureUsage, const hfm::Texture& texture);
std::unordered_map<std::string, std::unordered_map<image::TextureUsage::Type, QByteArray>> _textureContentMap;
};
#endif // !hifi_MaterialBaker_h

View file

@ -275,15 +275,18 @@ void ModelBaker::handleFinishedMaterialBaker() {
if (baker) {
if (!baker->hasErrors()) {
auto bakedMaterialURL = baker->getBakedMaterialData();
// this MaterialBaker is done and everything went according to plan
qCDebug(model_baking) << "Adding baked material to FST mapping " << bakedMaterialURL;
qCDebug(model_baking) << "Adding baked material to FST mapping " << baker->getBakedMaterialData();
QString relativeBakedMaterialURL = "/" + _modelURL.fileName();
auto baseName = relativeBakedMaterialURL.left(relativeBakedMaterialURL.lastIndexOf('.'));
relativeBakedMaterialURL = baseName + BAKED_MATERIAL_EXTENSION;
// First we add the materials in the model
QJsonArray materialMapping;
for (auto material : _hfmModel->materials) {
QJsonObject json;
json["mat::" + material.name] = bakedMaterialURL + "?" + material.name;
json["mat::" + material.name] = relativeBakedMaterialURL + "?" + material.name;
materialMapping.push_back(json);
}