Add separate textures flag.

Don't convert tga to png/jpg if the texture is already converted
This commit is contained in:
K. S. Ernest (iFire) Lee 2021-11-28 11:48:12 -08:00
parent 762a183b6b
commit a3dfcda66e
5 changed files with 30 additions and 12 deletions

View File

@ -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 {

View File

@ -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;

View File

@ -18,6 +18,10 @@
#include <gltf/properties/ImageData.hpp>
#include <gltf/properties/TextureData.hpp>
#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<TextureData> 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<TextureData> 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) {

View File

@ -19,6 +19,10 @@
#include "FBX2glTF.h"
#include "String_Utils.hpp"
#ifdef CopyFile
#undef CopyFile
#endif
namespace FileUtils {
std::vector<std::string> ListFolderFiles(

View File

@ -15,6 +15,10 @@
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#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() {