FbxExporter.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // FbxExporter.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "..\BabylonFbxNative\FbxSceneLoader.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <Windows.h>
  8. #include <string>
  9. #include <sstream>
  10. #include "..\BabylonFbxNative\BabylonScene.h"
  11. #include "..\BabylonFbxNative\GlobalSettings.h"
  12. std::string wstringToUtf8(const std::wstring& src){
  13. auto size = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), static_cast<int>( src.size()), nullptr, 0, nullptr, nullptr);
  14. std::string result;
  15. result.resize(size, ' ');
  16. WideCharToMultiByte(CP_UTF8, 0, src.c_str(), static_cast<int>(src.size()), &result[0], size,nullptr, nullptr);
  17. return result;
  18. }
  19. void exportTexture(const std::shared_ptr<BabylonTexture>& tex, const std::wstring& wOutputPath){
  20. if (!tex){
  21. return;
  22. }
  23. auto fullPath = tex->fullPath;
  24. for (;;){
  25. auto indexOfSlash = fullPath.find(L'/');
  26. if (indexOfSlash == fullPath.npos){
  27. break;
  28. }
  29. fullPath[indexOfSlash] = L'\\';
  30. }
  31. auto outputPath = tex->name;
  32. for (;;){
  33. auto indexOfSlash = outputPath.find(L'/');
  34. if (indexOfSlash == outputPath.npos){
  35. break;
  36. }
  37. outputPath[indexOfSlash] = L'\\';
  38. }
  39. size_t start = 0;
  40. for (;;){
  41. auto indexOfSlash = outputPath.find(L'\\', start);
  42. if (indexOfSlash == outputPath.npos){
  43. break;
  44. }
  45. auto pathToCreate = wOutputPath;
  46. if (pathToCreate[pathToCreate.size() - 1] != L'\\'){
  47. pathToCreate.push_back(L'\\');
  48. }
  49. pathToCreate.append(outputPath.begin(), outputPath.begin() + indexOfSlash);
  50. CreateDirectory(pathToCreate.c_str(), nullptr);
  51. start = indexOfSlash + 1;
  52. }
  53. auto fullOutputPath = wOutputPath;
  54. if (fullOutputPath[fullOutputPath.size() - 1] != L'\\'){
  55. fullOutputPath.push_back(L'\\');
  56. }
  57. fullOutputPath.append(outputPath);
  58. CopyFile(fullPath.c_str(), fullOutputPath.c_str(), false);
  59. }
  60. int _tmain(int argc, _TCHAR* argv[])
  61. {
  62. std::wcout << L"Usage : FbxExporter <path to fbx file> <outdir> [/fps:60|30|24] [/skipemptynodes]" << std::endl;
  63. if (argc < 3) {
  64. std::wcerr << L"Invalid argument count" << std::endl;
  65. return -1;
  66. }
  67. std::wstring wInputPath(argv[1]);
  68. std::wstring wInputDir(argv[1]);
  69. std::wstring wInputFileName(argv[1]);
  70. auto lastDirSeparator = wInputDir.find_last_of(L'\\');
  71. if (lastDirSeparator == wInputDir.npos) {
  72. wInputDir = L".";
  73. }
  74. else {
  75. wInputDir.erase(lastDirSeparator);
  76. wInputFileName.erase(0, lastDirSeparator + 1);
  77. }
  78. std::wstring wOutputPath(argv[2]);
  79. CreateDirectory(wOutputPath.c_str(), nullptr);
  80. bool skipEmptyNodes = false;
  81. for (int i = 3; i < argc; ++i){
  82. std::wstring warg = argv[i];
  83. if (warg == L"/skipemptynodes") {
  84. skipEmptyNodes = true;
  85. }
  86. else if (warg.find(L"/fps:") == 0){
  87. if (warg == L"/fps:60"){
  88. GlobalSettings::Current().AnimationsTimeMode = FbxTime::EMode::eFrames60;
  89. }
  90. else if (warg == L"/fps:30"){
  91. GlobalSettings::Current().AnimationsTimeMode = FbxTime::EMode::eFrames30;
  92. }
  93. else if (warg == L"/fps:24"){
  94. GlobalSettings::Current().AnimationsTimeMode = FbxTime::EMode::eFrames24;
  95. }
  96. else{
  97. std::wcerr << L"Unrecognized fps parameter" << std::endl;
  98. return -2;
  99. }
  100. }
  101. }
  102. FbxSceneLoader sceneLoader(wstringToUtf8(wInputPath));
  103. auto root = sceneLoader.rootNode();
  104. BabylonScene babScene(*root, skipEmptyNodes);
  105. for (auto& mat : babScene.materials()){
  106. exportTexture(mat.ambientTexture, wOutputPath);
  107. exportTexture(mat.diffuseTexture, wOutputPath);
  108. exportTexture(mat.specularTexture, wOutputPath);
  109. exportTexture(mat.emissiveTexture, wOutputPath);
  110. exportTexture(mat.reflectionTexture, wOutputPath);
  111. exportTexture(mat.bumpTexture, wOutputPath);
  112. }
  113. auto json = babScene.toJson();
  114. if (L'\\' != *wOutputPath.crbegin()) {
  115. wOutputPath.append(L"\\");
  116. }
  117. wOutputPath.append(wInputFileName);
  118. auto lastDot = wOutputPath.find_last_of(L'.');
  119. wOutputPath.erase(lastDot);
  120. wOutputPath.append(L".babylon");
  121. DeleteFile(wOutputPath.c_str());
  122. std::ofstream stream(wOutputPath);
  123. json.serialize(stream);
  124. stream.flush();
  125. return 0;
  126. }