From a3dfcda66e2240d379b11f0943e698d8d23583dc Mon Sep 17 00:00:00 2001 From: "K. S. Ernest (iFire) Lee" Date: Sun, 28 Nov 2021 11:48:12 -0800 Subject: [PATCH] Add separate textures flag. Don't convert tga to png/jpg if the texture is already converted --- src/FBX2glTF.cpp | 5 +++++ src/FBX2glTF.h | 2 ++ src/gltf/TextureBuilder.cpp | 25 ++++++++++++++----------- src/utils/File_Utils.cpp | 4 ++++ src/utils/File_Utils.hpp | 6 +++++- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/FBX2glTF.cpp b/src/FBX2glTF.cpp index 0b85ff1..5bb8841 100644 --- a/src/FBX2glTF.cpp +++ b/src/FBX2glTF.cpp @@ -51,6 +51,10 @@ int main(int argc, char* argv[]) { "-e,--embed", gltfOptions.embedResources, "Inline buffers as data:// URIs within generated non-binary glTF."); + + app.add_flag( + "-t,--separate-textures", gltfOptions.separateTextures, "Write texture files out separately"); + app.add_flag("-b,--binary", gltfOptions.outputBinary, "Output a single binary format .glb file."); app.add_option( @@ -321,6 +325,7 @@ int main(int argc, char* argv[]) { if (gltfOptions.outputBinary) { // add .glb to output path, unless it already ends in exactly that + outputFolder = FileUtils::getFolder(outputPath) + "/"; if (suffix.has_value() && suffix.value() == "glb") { modelPath = outputPath; } else { diff --git a/src/FBX2glTF.h b/src/FBX2glTF.h index abdcab3..e9ee876 100644 --- a/src/FBX2glTF.h +++ b/src/FBX2glTF.h @@ -90,6 +90,8 @@ struct GltfOptions { /** If non-binary, whether to inline all resources, for a single (large) .glTF file. */ bool embedResources{false}; + bool separateTextures{true}; + /** Whether and how to use KHR_draco_mesh_compression to minimize static geometry size. */ struct { bool enabled = false; diff --git a/src/gltf/TextureBuilder.cpp b/src/gltf/TextureBuilder.cpp index b2325e5..a59997d 100644 --- a/src/gltf/TextureBuilder.cpp +++ b/src/gltf/TextureBuilder.cpp @@ -18,6 +18,10 @@ #include #include +#ifdef CopyFile +#undef CopyFile +#endif + // keep track of some texture data as we load them struct TexInfo { explicit TexInfo(int rawTexIx) : rawTexIx(rawTexIx) {} @@ -138,7 +142,7 @@ std::shared_ptr TextureBuilder::combine( } ImageData* image; - if (options.outputBinary) { + if (options.outputBinary && !options.separateTextures) { const auto bufferView = gltf.AddRawBufferView(*gltf.defaultBuffer, imgBuffer.data(), to_uint32(imgBuffer.size())); image = new ImageData(mergedName, *bufferView, png ? "image/png" : "image/jpeg"); @@ -200,18 +204,17 @@ std::shared_ptr TextureBuilder::simple(int rawTexIndex, const std:: } else if (!relativeFilename.empty()) { image = new ImageData(relativeFilename, relativeFilename); - std::string outputPath = outputFolder + "/" + relativeFilename; auto srcAbs = FileUtils::GetAbsolutePath(rawTexture.fileLocation); - auto dstAbs = FileUtils::GetAbsolutePath(outputPath); - if (srcAbs != dstAbs) - if (FileUtils::CopyFile(rawTexture.fileLocation, outputPath, true)) { - if (verboseOutput) { - fmt::printf("Copied texture '%s' to output folder: %s\n", textureName, outputPath); + if (!FileUtils::FileExists(outputPath) && srcAbs != dstAbs) { + if (FileUtils::CopyFile(rawTexture.fileLocation, outputPath, true)) { + if (verboseOutput) { + fmt::printf("Copied texture '%s' to output folder: %s\n", textureName, outputPath); + } + } else { + // no point commenting further on read/write error; CopyFile() does enough of that, and we + // certainly want to to add an image struct to the glTF JSON, with the correct relative + // path reference, even if the copy failed. } - } else { - // no point commenting further on read/write error; CopyFile() does enough of that, and we - // certainly want to to add an image struct to the glTF JSON, with the correct relative path - // reference, even if the copy failed. } } if (!image) { diff --git a/src/utils/File_Utils.cpp b/src/utils/File_Utils.cpp index 1de773a..1f2b148 100644 --- a/src/utils/File_Utils.cpp +++ b/src/utils/File_Utils.cpp @@ -19,6 +19,10 @@ #include "FBX2glTF.h" #include "String_Utils.hpp" +#ifdef CopyFile +#undef CopyFile +#endif + namespace FileUtils { std::vector ListFolderFiles( diff --git a/src/utils/File_Utils.hpp b/src/utils/File_Utils.hpp index 10f31ca..5529003 100644 --- a/src/utils/File_Utils.hpp +++ b/src/utils/File_Utils.hpp @@ -15,6 +15,10 @@ #include #include +#ifdef CopyFile +#undef CopyFile +#endif + namespace FileUtils { std::string GetCurrentFolder(); @@ -34,7 +38,7 @@ bool CopyFile( bool createPath = false); inline std::string GetAbsolutePath(const std::string& filePath) { - return boost::filesystem::canonical(boost::filesystem::absolute(filePath)).string(); + return boost::filesystem::absolute(filePath).string(); } inline std::string GetCurrentFolder() {