From c8209aa976860eae67f74cf08b7004f5a3229b02 Mon Sep 17 00:00:00 2001 From: sabrina-shanman Date: Thu, 14 Mar 2019 14:31:14 -0700 Subject: [PATCH] Do not have multiple copies of the original texture file in the baked output --- libraries/baking/src/TextureBaker.cpp | 17 +++++++++-------- libraries/baking/src/TextureBaker.h | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libraries/baking/src/TextureBaker.cpp b/libraries/baking/src/TextureBaker.cpp index 8591cbd0aa..dfc684ddee 100644 --- a/libraries/baking/src/TextureBaker.cpp +++ b/libraries/baking/src/TextureBaker.cpp @@ -47,17 +47,19 @@ TextureBaker::TextureBaker(const QUrl& textureURL, image::TextureUsage::Type tex auto originalFilename = textureURL.fileName(); _baseFilename = originalFilename.left(originalFilename.lastIndexOf('.')); } + + _originalCopyFilePath = _outputDirectory.absoluteFilePath(_textureURL.fileName()); } void TextureBaker::bake() { // once our texture is loaded, kick off a the processing connect(this, &TextureBaker::originalTextureLoaded, this, &TextureBaker::processTexture); - if (_originalTexture.isEmpty()) { + if (_originalTexture.isEmpty() && !QFile(_originalCopyFilePath.toString()).exists()) { // first load the texture (either locally or remotely) loadTexture(); } else { - // we already have a texture passed to us, use that + // we already have a texture passed to us, or the texture is already saved, so use that emit originalTextureLoaded(); } } @@ -128,23 +130,22 @@ void TextureBaker::processTexture() { TextureMeta meta; - QString newFilename = _textureURL.fileName(); - QString addMapChannel = QString::fromStdString("_" + std::to_string(_textureType)); - newFilename.replace(QString("."), addMapChannel + "."); - QString originalCopyFilePath = _outputDirectory.absoluteFilePath(newFilename); + QString originalCopyFilePath = _originalCopyFilePath.toString(); + // Copy the original file into the baked output directory if it doesn't exist yet { QFile file { originalCopyFilePath }; - if (!file.open(QIODevice::WriteOnly) || file.write(_originalTexture) == -1) { + if (!file.exists() && (!file.open(QIODevice::WriteOnly) || file.write(_originalTexture) == -1)) { handleError("Could not write original texture for " + _textureURL.toString()); return; } // IMPORTANT: _originalTexture is empty past this point _originalTexture.clear(); _outputFiles.push_back(originalCopyFilePath); - meta.original = _metaTexturePathPrefix + newFilename; + meta.original = _metaTexturePathPrefix + _originalCopyFilePath.fileName(); } + // Load the copy of the original file from the baked output directory. New images will be created using the original as the source data. auto buffer = std::static_pointer_cast(std::make_shared(originalCopyFilePath)); if (!buffer->open(QIODevice::ReadOnly)) { handleError("Could not open original file at " + originalCopyFilePath); diff --git a/libraries/baking/src/TextureBaker.h b/libraries/baking/src/TextureBaker.h index 84e7c57aa1..9b86d875e9 100644 --- a/libraries/baking/src/TextureBaker.h +++ b/libraries/baking/src/TextureBaker.h @@ -73,6 +73,7 @@ private: QDir _outputDirectory; QString _metaTextureFileName; QString _metaTexturePathPrefix; + QUrl _originalCopyFilePath; std::atomic _abortProcessing { false };