fixing embedded textures and materialMapping priority

This commit is contained in:
SamGondelman 2019-04-02 19:04:23 -07:00
parent 14bd5ed687
commit 770b863604
4 changed files with 31 additions and 22 deletions

View file

@ -107,31 +107,35 @@ void MaterialBaker::processMaterial() {
auto mapChannel = texturePair.first;
auto textureMap = texturePair.second;
if (textureMap.texture && textureMap.texture->_textureSource) {
auto textureSource = textureMap.texture->_textureSource;
auto type = textureMap.texture->getTextureType();
QUrl url = textureSource->getUrl();
QString cleanURL = url.adjusted(QUrl::RemoveQuery | QUrl::RemoveFragment).toDisplayString();
QByteArray content;
QUrl textureURL;
{
bool foundEmbeddedTexture = false;
auto textureContentMapIter = _textureContentMap.find(networkMaterial.second->getName());
if (textureContentMapIter != _textureContentMap.end()) {
auto textureUsageIter = textureContentMapIter->second.find(type);
if (textureUsageIter != textureContentMapIter->second.end()) {
content = textureUsageIter->second.first;
textureURL = textureUsageIter->second.second;
foundEmbeddedTexture = true;
}
}
if (!foundEmbeddedTexture && textureMap.texture->_textureSource) {
textureURL = textureMap.texture->_textureSource->getUrl().adjusted(QUrl::RemoveQuery | QUrl::RemoveFragment);
}
}
QString cleanURL = textureURL.toDisplayString();
auto idx = cleanURL.lastIndexOf('.');
auto extension = idx >= 0 ? url.toDisplayString().mid(idx + 1).toLower() : "";
QString extension = idx >= 0 ? cleanURL.mid(idx + 1).toLower() : "";
if (QImageReader::supportedImageFormats().contains(extension.toLatin1())) {
QUrl textureURL = url.adjusted(QUrl::RemoveQuery | QUrl::RemoveFragment);
QPair<QUrl, image::TextureUsage::Type> textureKey(textureURL, type);
if (!_textureBakers.contains(textureKey)) {
auto baseTextureFileName = _textureFileNamer.createBaseTextureFileName(textureURL.fileName(), type);
QByteArray content;
{
auto textureContentMapIter = _textureContentMap.find(networkMaterial.second->getName());
if (textureContentMapIter != _textureContentMap.end()) {
auto textureUsageIter = textureContentMapIter->second.find(type);
if (textureUsageIter != textureContentMapIter->second.end()) {
content = textureUsageIter->second;
}
}
}
QSharedPointer<TextureBaker> textureBaker {
new TextureBaker(textureURL, type, _textureOutputDir, "", baseTextureFileName, content),
&TextureBaker::deleteLater
@ -238,11 +242,10 @@ void MaterialBaker::outputMaterial() {
}
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;
textureUsageMap[textureUsage] = { texture.content, texture.filename };
}
};

View file

@ -66,7 +66,7 @@ private:
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;
std::unordered_map<std::string, std::unordered_map<image::TextureUsage::Type, std::pair<QByteArray, QString>>> _textureContentMap;
};
#endif // !hifi_MaterialBaker_h

View file

@ -277,7 +277,7 @@ void ModelBaker::handleFinishedMaterialBaker() {
// this MaterialBaker is done and everything went according to plan
qCDebug(model_baking) << "Adding baked material to FST mapping " << baker->getBakedMaterialData();
QString relativeBakedMaterialURL = "/" + _modelURL.fileName();
QString relativeBakedMaterialURL = _modelURL.fileName();
auto baseName = relativeBakedMaterialURL.left(relativeBakedMaterialURL.lastIndexOf('.'));
relativeBakedMaterialURL = baseName + BAKED_MATERIAL_EXTENSION;

View file

@ -1550,7 +1550,13 @@ void Model::applyMaterialMapping() {
continue;
}
auto materialLoaded = [this, networkMaterialResource, shapeIDs, renderItemsKey, primitiveMode, useDualQuaternionSkinning]() {
// This needs to be precomputed before the lambda, since the lambdas could be called out of order
std::unordered_map<unsigned int, quint16> priorityMapPerResource;
for (auto shapeID : shapeIDs) {
priorityMapPerResource[shapeID] = ++_priorityMap[shapeID];
}
auto materialLoaded = [this, networkMaterialResource, shapeIDs, priorityMapPerResource, renderItemsKey, primitiveMode, useDualQuaternionSkinning]() {
if (networkMaterialResource->isFailed() || networkMaterialResource->parsedMaterials.names.size() == 0) {
return;
}
@ -1577,7 +1583,7 @@ void Model::applyMaterialMapping() {
auto itemID = _modelMeshRenderItemIDs[shapeID];
auto meshIndex = _modelMeshRenderItemShapes[shapeID].meshIndex;
bool invalidatePayloadShapeKey = shouldInvalidatePayloadShapeKey(meshIndex);
graphics::MaterialLayer material = graphics::MaterialLayer(networkMaterial, ++_priorityMap[shapeID]);
graphics::MaterialLayer material = graphics::MaterialLayer(networkMaterial, priorityMapPerResource.at(shapeID));
_materialMapping[shapeID].push_back(material);
transaction.updateItem<ModelMeshPartPayload>(itemID, [material, renderItemsKey,
invalidatePayloadShapeKey, primitiveMode, useDualQuaternionSkinning](ModelMeshPartPayload& data) {