FBXImporter.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. r
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ----------------------------------------------------------------------
  33. */
  34. /** @file FBXImporter.cpp
  35. * @brief Implementation of the FBX importer.
  36. */
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include "FBXImporter.h"
  39. #include "FBXConverter.h"
  40. #include "FBXDocument.h"
  41. #include "FBXParser.h"
  42. #include "FBXTokenizer.h"
  43. #include "FBXUtil.h"
  44. #include <assimp/MemoryIOWrapper.h>
  45. #include <assimp/StreamReader.h>
  46. #include <assimp/importerdesc.h>
  47. #include <assimp/Importer.hpp>
  48. namespace Assimp {
  49. template <>
  50. const char *LogFunctions<FBXImporter>::Prefix() {
  51. static auto prefix = "FBX: ";
  52. return prefix;
  53. }
  54. } // namespace Assimp
  55. using namespace Assimp;
  56. using namespace Assimp::Formatter;
  57. using namespace Assimp::FBX;
  58. namespace {
  59. static const aiImporterDesc desc = {
  60. "Autodesk FBX Importer",
  61. "",
  62. "",
  63. "",
  64. aiImporterFlags_SupportTextFlavour,
  65. 0,
  66. 0,
  67. 0,
  68. 0,
  69. "fbx"
  70. };
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Constructor to be privately used by #Importer
  74. FBXImporter::FBXImporter() {
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Destructor, private as well
  78. FBXImporter::~FBXImporter() {
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Returns whether the class can handle the format of the given file.
  82. bool FBXImporter::CanRead(const std::string & pFile, IOSystem * pIOHandler, bool /*checkSig*/) const {
  83. // at least ASCII-FBX files usually have a 'FBX' somewhere in their head
  84. static const char *tokens[] = { "fbx" };
  85. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
  86. }
  87. // ------------------------------------------------------------------------------------------------
  88. // List all extensions handled by this loader
  89. const aiImporterDesc *FBXImporter::GetInfo() const {
  90. return &desc;
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. // Setup configuration properties for the loader
  94. void FBXImporter::SetupProperties(const Importer *pImp) {
  95. settings.readAllLayers = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_GEOMETRY_LAYERS, true);
  96. settings.readAllMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ALL_MATERIALS, false);
  97. settings.readMaterials = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_MATERIALS, true);
  98. settings.readTextures = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_TEXTURES, true);
  99. settings.readCameras = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_CAMERAS, true);
  100. settings.readLights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_LIGHTS, true);
  101. settings.readAnimations = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_ANIMATIONS, true);
  102. settings.readWeights = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_READ_WEIGHTS, true);
  103. settings.strictMode = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_STRICT_MODE, false);
  104. settings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true);
  105. settings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true);
  106. settings.useLegacyEmbeddedTextureNaming = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING, false);
  107. settings.removeEmptyBones = pImp->GetPropertyBool(AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES, true);
  108. settings.convertToMeters = pImp->GetPropertyBool(AI_CONFIG_FBX_CONVERT_TO_M, false);
  109. }
  110. // ------------------------------------------------------------------------------------------------
  111. // Imports the given file into the given scene structure.
  112. void FBXImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
  113. auto streamCloser = [&](IOStream *pStream) {
  114. pIOHandler->Close(pStream);
  115. };
  116. std::unique_ptr<IOStream, decltype(streamCloser)> stream(pIOHandler->Open(pFile, "rb"), streamCloser);
  117. if (!stream) {
  118. ThrowException("Could not open file for reading");
  119. }
  120. ASSIMP_LOG_DEBUG("Reading FBX file");
  121. // read entire file into memory - no streaming for this, fbx
  122. // files can grow large, but the assimp output data structure
  123. // then becomes very large, too. Assimp doesn't support
  124. // streaming for its output data structures so the net win with
  125. // streaming input data would be very low.
  126. std::vector<char> contents;
  127. contents.resize(stream->FileSize() + 1);
  128. stream->Read(&*contents.begin(), 1, contents.size() - 1);
  129. contents[contents.size() - 1] = 0;
  130. const char *const begin = &*contents.begin();
  131. // broadphase tokenizing pass in which we identify the core
  132. // syntax elements of FBX (brackets, commas, key:value mappings)
  133. TokenList tokens;
  134. try {
  135. bool is_binary = false;
  136. if (!strncmp(begin, "Kaydara FBX Binary", 18)) {
  137. is_binary = true;
  138. TokenizeBinary(tokens, begin, contents.size());
  139. } else {
  140. Tokenize(tokens, begin);
  141. }
  142. // use this information to construct a very rudimentary
  143. // parse-tree representing the FBX scope structure
  144. Parser parser(tokens, is_binary);
  145. // take the raw parse-tree and convert it to a FBX DOM
  146. Document doc(parser, settings);
  147. // convert the FBX DOM to aiScene
  148. ConvertToAssimpScene(pScene, doc, settings.removeEmptyBones);
  149. // size relative to cm
  150. float size_relative_to_cm = doc.GlobalSettings().UnitScaleFactor();
  151. if (size_relative_to_cm == 0.0)
  152. {
  153. // BaseImporter later asserts that fileScale is non-zero.
  154. ThrowException("The UnitScaleFactor must be non-zero");
  155. }
  156. // Set FBX file scale is relative to CM must be converted to M for
  157. // assimp universal format (M)
  158. SetFileScale(size_relative_to_cm * 0.01f);
  159. std::for_each(tokens.begin(), tokens.end(), Util::delete_fun<Token>());
  160. } catch (std::exception &) {
  161. std::for_each(tokens.begin(), tokens.end(), Util::delete_fun<Token>());
  162. throw;
  163. }
  164. }
  165. #endif // !ASSIMP_BUILD_NO_FBX_IMPORTER