| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Importer/GltfImporter.h>
- using namespace anki;
- static const char* USAGE = R"(Usage: %s in_file out_dir [options]
- Options:
- -rpath <string> : Replace all absolute paths of assets with that path
- -texrpath <string> : Same as rpath but for textures
- -optimize-meshes <0|1> : Optimize meshes. Default is 1
- -optimize-animations <0|1> : Optimize animations. Default is 1
- -j <thread_count> : Number of threads. Defaults to system's max
- -lod-count <1|2|3> : The number of geometry LODs to generate. Default is 1
- -lod-factor <float> : The decimate factor for each LOD. Default 0.25
- -light-scale <float> : Multiply the light intensity with this number. Default is 1.0
- -import-testures <0|1> : Import textures. Default is 0
- -v : Enable verbose log
- )";
- class CmdLineArgs
- {
- public:
- HeapMemoryPool m_pool = {allocAligned, nullptr};
- StringRaii m_inputFname = {&m_pool};
- StringRaii m_outDir = {&m_pool};
- StringRaii m_rpath = {&m_pool};
- StringRaii m_texRpath = {&m_pool};
- Bool m_optimizeMeshes = true;
- Bool m_optimizeAnimations = true;
- Bool m_importTextures = false;
- U32 m_threadCount = kMaxU32;
- U32 m_lodCount = 1;
- F32 m_lodFactor = 0.25f;
- F32 m_lightIntensityScale = 1.0f;
- };
- static Error parseCommandLineArgs(int argc, char** argv, CmdLineArgs& info)
- {
- Bool rpathFound = false;
- Bool texrpathFound = false;
- // Parse config
- if(argc < 3)
- {
- return Error::kUserData;
- }
- info.m_inputFname.create(argv[1]);
- info.m_outDir.sprintf("%s/", argv[2]);
- for(I i = 3; i < argc; i++)
- {
- if(strcmp(argv[i], "-texrpath") == 0)
- {
- texrpathFound = true;
- ++i;
- if(i < argc)
- {
- if(std::strlen(argv[i]) > 0)
- {
- info.m_texRpath.sprintf("%s/", argv[i]);
- }
- else
- {
- info.m_texRpath.create("");
- }
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-v") == 0)
- {
- Logger::getSingleton().enableVerbosity(true);
- }
- else if(strcmp(argv[i], "-rpath") == 0)
- {
- rpathFound = true;
- ++i;
- if(i < argc)
- {
- if(std::strlen(argv[i]) > 0)
- {
- info.m_rpath.sprintf("%s/", argv[i]);
- }
- else
- {
- info.m_rpath.create("");
- }
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-optimize-meshes") == 0)
- {
- ++i;
- if(i < argc)
- {
- I optimize = 1;
- ANKI_CHECK(CString(argv[i]).toNumber(optimize));
- info.m_optimizeMeshes = optimize != 0;
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-j") == 0)
- {
- ++i;
- if(i < argc)
- {
- U32 count = 0;
- ANKI_CHECK(CString(argv[i]).toNumber(count));
- info.m_threadCount = count;
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-lod-count") == 0)
- {
- ++i;
- if(i < argc)
- {
- ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodCount));
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-lod-factor") == 0)
- {
- ++i;
- if(i < argc)
- {
- ANKI_CHECK(CString(argv[i]).toNumber(info.m_lodFactor));
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-light-scale") == 0)
- {
- ++i;
- if(i < argc)
- {
- ANKI_CHECK(CString(argv[i]).toNumber(info.m_lightIntensityScale));
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-optimize-animations") == 0)
- {
- ++i;
- if(i < argc)
- {
- I optimize = 1;
- ANKI_CHECK(CString(argv[i]).toNumber(optimize));
- info.m_optimizeAnimations = optimize != 0;
- }
- else
- {
- return Error::kUserData;
- }
- }
- else if(strcmp(argv[i], "-import-textures") == 0)
- {
- ++i;
- if(i < argc)
- {
- I val = 1;
- ANKI_CHECK(CString(argv[i]).toNumber(val));
- info.m_importTextures = val != 0;
- }
- else
- {
- return Error::kUserData;
- }
- }
- else
- {
- return Error::kUserData;
- }
- }
- if(!rpathFound)
- {
- info.m_rpath = info.m_outDir;
- }
- if(!texrpathFound)
- {
- info.m_texRpath = info.m_rpath;
- }
- return Error::kNone;
- }
- ANKI_MAIN_FUNCTION(myMain)
- int myMain(int argc, char** argv)
- {
- CmdLineArgs cmdArgs;
- if(parseCommandLineArgs(argc, argv, cmdArgs))
- {
- ANKI_IMPORTER_LOGE(USAGE, argv[0]);
- return 1;
- }
- HeapMemoryPool pool(allocAligned, nullptr);
- StringRaii comment(&pool);
- for(I32 i = 0; i < argc; ++i)
- {
- if(i != 0)
- {
- comment.append(" ");
- }
- if(CString(argv[i]).getLength())
- {
- comment.append(argv[i]);
- }
- else
- {
- comment.append("\"\"");
- }
- }
- GltfImporterInitInfo initInfo;
- initInfo.m_inputFilename = cmdArgs.m_inputFname;
- initInfo.m_outDirectory = cmdArgs.m_outDir;
- initInfo.m_rpath = cmdArgs.m_rpath;
- initInfo.m_texrpath = cmdArgs.m_texRpath;
- initInfo.m_optimizeMeshes = cmdArgs.m_optimizeMeshes;
- initInfo.m_optimizeAnimations = cmdArgs.m_optimizeAnimations;
- initInfo.m_lodFactor = cmdArgs.m_lodFactor;
- initInfo.m_lodCount = cmdArgs.m_lodCount;
- initInfo.m_lightIntensityScale = cmdArgs.m_lightIntensityScale;
- initInfo.m_threadCount = cmdArgs.m_threadCount;
- initInfo.m_comment = comment;
- initInfo.m_importTextures = cmdArgs.m_importTextures;
- GltfImporter importer(&pool);
- if(importer.init(initInfo))
- {
- return 1;
- }
- if(importer.writeAll())
- {
- return 1;
- }
- return 0;
- }
|