Merge pull request #11393 from birarda/feat/auto-bake

don't export embedded textures with original, remove triangle warning
This commit is contained in:
Clément Brisset 2017-09-18 16:21:07 -07:00 committed by GitHub
commit 108de209bb
2 changed files with 32 additions and 39 deletions

View file

@ -244,7 +244,7 @@ QString texturePathRelativeToFBX(QUrl fbxURL, QUrl textureURL) {
QString FBXBaker::createBakedTextureFileName(const QFileInfo& textureFileInfo) { QString FBXBaker::createBakedTextureFileName(const QFileInfo& textureFileInfo) {
// first make sure we have a unique base name for this texture // first make sure we have a unique base name for this texture
// in case another texture referenced by this model has the same base name // in case another texture referenced by this model has the same base name
auto nameMatches = _textureNameMatchCount[textureFileInfo.baseName()]; auto& nameMatches = _textureNameMatchCount[textureFileInfo.baseName()];
QString bakedTextureFileName { textureFileInfo.completeBaseName() }; QString bakedTextureFileName { textureFileInfo.completeBaseName() };
@ -262,28 +262,32 @@ QString FBXBaker::createBakedTextureFileName(const QFileInfo& textureFileInfo) {
return bakedTextureFileName; return bakedTextureFileName;
} }
QUrl FBXBaker::getTextureURL(const QFileInfo& textureFileInfo, QString relativeFileName) { QUrl FBXBaker::getTextureURL(const QFileInfo& textureFileInfo, QString relativeFileName, bool isEmbedded) {
QUrl urlToTexture; QUrl urlToTexture;
if (textureFileInfo.exists() && textureFileInfo.isFile()) { auto apparentRelativePath = QFileInfo(relativeFileName.replace("\\", "/"));
// set the texture URL to the local texture that we have confirmed exists
urlToTexture = QUrl::fromLocalFile(textureFileInfo.absoluteFilePath());
} else {
// external texture that we'll need to download or find
// first check if it the RelativePath to the texture in the FBX was relative if (isEmbedded) {
auto apparentRelativePath = QFileInfo(relativeFileName.replace("\\", "/")); urlToTexture = _fbxURL.toString() + "/" + apparentRelativePath.filePath();
} else {
// this is a relative file path which will require different handling if (textureFileInfo.exists() && textureFileInfo.isFile()) {
// depending on the location of the original FBX // set the texture URL to the local texture that we have confirmed exists
if (_fbxURL.isLocalFile() && apparentRelativePath.exists() && apparentRelativePath.isFile()) { urlToTexture = QUrl::fromLocalFile(textureFileInfo.absoluteFilePath());
// the absolute path we ran into for the texture in the FBX exists on this machine
// so use that file
urlToTexture = QUrl::fromLocalFile(apparentRelativePath.absoluteFilePath());
} else { } else {
// we didn't find the texture on this machine at the absolute path // external texture that we'll need to download or find
// so assume that it is right beside the FBX to match the behaviour of interface
urlToTexture = _fbxURL.resolved(apparentRelativePath.fileName()); // this is a relative file path which will require different handling
// depending on the location of the original FBX
if (_fbxURL.isLocalFile() && apparentRelativePath.exists() && apparentRelativePath.isFile()) {
// the absolute path we ran into for the texture in the FBX exists on this machine
// so use that file
urlToTexture = QUrl::fromLocalFile(apparentRelativePath.absoluteFilePath());
} else {
// we didn't find the texture on this machine at the absolute path
// so assume that it is right beside the FBX to match the behaviour of interface
urlToTexture = _fbxURL.resolved(apparentRelativePath.fileName());
}
} }
} }
@ -335,7 +339,6 @@ void FBXBaker::rewriteAndBakeSceneModels() {
} }
if (numTriangles == 0) { if (numTriangles == 0) {
handleWarning("Skipping compression of mesh because no triangles were found");
continue; continue;
} }
@ -582,8 +585,12 @@ void FBXBaker::rewriteAndBakeSceneTextures() {
qCDebug(model_baking).noquote() << "Re-mapping" << fbxTextureFileName qCDebug(model_baking).noquote() << "Re-mapping" << fbxTextureFileName
<< "to" << bakedTextureFileName; << "to" << bakedTextureFileName;
// check if this was an embedded texture we have already have in-memory content for
auto textureContent = _textureContent.value(fbxTextureFileName.toLocal8Bit());
// figure out the URL to this texture, embedded or external // figure out the URL to this texture, embedded or external
auto urlToTexture = getTextureURL(textureFileInfo, fbxTextureFileName); auto urlToTexture = getTextureURL(textureFileInfo, fbxTextureFileName,
!textureContent.isNull());
// write the new filename into the FBX scene // write the new filename into the FBX scene
textureChild.properties[0] = bakedTextureFileName.toLocal8Bit(); textureChild.properties[0] = bakedTextureFileName.toLocal8Bit();
@ -595,9 +602,6 @@ void FBXBaker::rewriteAndBakeSceneTextures() {
QString textureID { object->properties[0].toByteArray() }; QString textureID { object->properties[0].toByteArray() };
auto textureType = textureTypes[textureID]; auto textureType = textureTypes[textureID];
// check if this was an embedded texture we have already have in-memory content for
auto textureContent = _textureContent.value(fbxTextureFileName.toLocal8Bit());
// bake this texture asynchronously // bake this texture asynchronously
bakeTexture(urlToTexture, textureType, _bakedOutputDir, textureContent); bakeTexture(urlToTexture, textureType, _bakedOutputDir, textureContent);
} }
@ -649,12 +653,10 @@ void FBXBaker::handleBakedTexture() {
// use the path to the texture being baked to determine if this was an embedded or a linked texture // use the path to the texture being baked to determine if this was an embedded or a linked texture
// it is embeddded if the texure being baked was inside the original output folder // it is embeddded if the texure being baked was inside a folder with the name of the FBX
// since that is where the FBX SDK places the .fbm folder it generates when importing the FBX // since that is the fake URL we provide when baking external textures
auto originalOutputFolder = QUrl::fromLocalFile(_originalOutputDir); if (!_fbxURL.isParentOf(bakedTexture->getTextureURL())) {
if (!originalOutputFolder.isParentOf(bakedTexture->getTextureURL())) {
// for linked textures we want to save a copy of original texture beside the original FBX // for linked textures we want to save a copy of original texture beside the original FBX
qCDebug(model_baking) << "Saving original texture for" << bakedTexture->getTextureURL(); qCDebug(model_baking) << "Saving original texture for" << bakedTexture->getTextureURL();
@ -734,20 +736,11 @@ void FBXBaker::exportScene() {
} }
void FBXBaker::removeEmbeddedMediaFolder() {
// now that the bake is complete, remove the embedded media folder produced by the FBX SDK when it imports an FBX
//auto embeddedMediaFolderName = _fbxURL.fileName().replace(".fbx", ".fbm");
//QDir(_bakedOutputDir + ORIGINAL_OUTPUT_SUBFOLDER + embeddedMediaFolderName).removeRecursively();
}
void FBXBaker::checkIfTexturesFinished() { void FBXBaker::checkIfTexturesFinished() {
// check if we're done everything we need to do for this FBX // check if we're done everything we need to do for this FBX
// and emit our finished signal if we're done // and emit our finished signal if we're done
if (_bakingTextures.isEmpty()) { if (_bakingTextures.isEmpty()) {
// remove the embedded media folder that the FBX SDK produces when reading the original
removeEmbeddedMediaFolder();
if (hasErrors()) { if (hasErrors()) {
// if we're checking for completion but we have errors // if we're checking for completion but we have errors
// that means one or more of our texture baking operations failed // that means one or more of our texture baking operations failed

View file

@ -66,7 +66,7 @@ private:
void checkIfTexturesFinished(); void checkIfTexturesFinished();
QString createBakedTextureFileName(const QFileInfo& textureFileInfo); QString createBakedTextureFileName(const QFileInfo& textureFileInfo);
QUrl getTextureURL(const QFileInfo& textureFileInfo, QString relativeFileName); QUrl getTextureURL(const QFileInfo& textureFileInfo, QString relativeFileName, bool isEmbedded = false);
void bakeTexture(const QUrl& textureURL, image::TextureUsage::Type textureType, const QDir& outputDir, void bakeTexture(const QUrl& textureURL, image::TextureUsage::Type textureType, const QDir& outputDir,
const QByteArray& textureContent = QByteArray()); const QByteArray& textureContent = QByteArray());