ColladaLoader.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /** Defines the collada loader class */
  2. /*
  3. Open Asset Import Library (assimp)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  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. #ifndef AI_COLLADALOADER_H_INC
  35. #define AI_COLLADALOADER_H_INC
  36. #include "ColladaParser.h"
  37. #include <assimp/BaseImporter.h>
  38. struct aiNode;
  39. struct aiCamera;
  40. struct aiLight;
  41. struct aiTexture;
  42. struct aiAnimation;
  43. namespace Assimp {
  44. struct ColladaMeshIndex {
  45. std::string mMeshID;
  46. size_t mSubMesh;
  47. std::string mMaterial;
  48. ColladaMeshIndex(const std::string &pMeshID, size_t pSubMesh, const std::string &pMaterial) :
  49. mMeshID(pMeshID), mSubMesh(pSubMesh), mMaterial(pMaterial) {
  50. ai_assert(!pMeshID.empty());
  51. }
  52. bool operator<(const ColladaMeshIndex &p) const {
  53. if (mMeshID == p.mMeshID) {
  54. if (mSubMesh == p.mSubMesh)
  55. return mMaterial < p.mMaterial;
  56. else
  57. return mSubMesh < p.mSubMesh;
  58. } else {
  59. return mMeshID < p.mMeshID;
  60. }
  61. }
  62. };
  63. /**
  64. * @brief Loader class to read Collada scenes.
  65. *
  66. * Collada is over-engineered to death, with every new iteration bringing more useless stuff,
  67. * so I limited the data to what I think is useful for games.
  68. */
  69. class ColladaLoader : public BaseImporter {
  70. public:
  71. /// The class constructor.
  72. ColladaLoader();
  73. /// The class destructor.
  74. ~ColladaLoader() override = default;
  75. /// Returns whether the class can handle the format of the given file.
  76. /// @see BaseImporter::CanRead() for more details.
  77. bool CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const override;
  78. protected:
  79. /// See #BaseImporter::GetInfo for the details
  80. const aiImporterDesc *GetInfo() const override;
  81. /// See #BaseImporter::SetupProperties for the details
  82. void SetupProperties(const Importer *pImp) override;
  83. /// See #BaseImporter::InternReadFile for the details
  84. void InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) override;
  85. /// Recursively constructs a scene node for the given parser node and returns it.
  86. aiNode *BuildHierarchy(const ColladaParser &pParser, const Collada::Node *pNode);
  87. /// Resolve node instances
  88. void ResolveNodeInstances(const ColladaParser &pParser, const Collada::Node *pNode,
  89. std::vector<const Collada::Node *> &resolved);
  90. /// Builds meshes for the given node and references them
  91. void BuildMeshesForNode(const ColladaParser &pParser, const Collada::Node *pNode,
  92. aiNode *pTarget);
  93. /// Lookup for meshes by their name
  94. aiMesh *findMesh(const std::string &meshid);
  95. /// Creates a mesh for the given ColladaMesh face subset and returns the newly created mesh
  96. aiMesh *CreateMesh(const ColladaParser &pParser, const Collada::Mesh *pSrcMesh, const Collada::SubMesh &pSubMesh,
  97. const Collada::Controller *pSrcController, size_t pStartVertex, size_t pStartFace);
  98. /// Builds cameras for the given node and references them
  99. void BuildCamerasForNode(const ColladaParser &pParser, const Collada::Node *pNode,
  100. aiNode *pTarget);
  101. /// Builds lights for the given node and references them
  102. void BuildLightsForNode(const ColladaParser &pParser, const Collada::Node *pNode,
  103. aiNode *pTarget);
  104. /// Stores all meshes in the given scene
  105. void StoreSceneMeshes(aiScene *pScene);
  106. /// Stores all materials in the given scene
  107. void StoreSceneMaterials(aiScene *pScene);
  108. /// Stores all lights in the given scene
  109. void StoreSceneLights(aiScene *pScene);
  110. /// Stores all cameras in the given scene
  111. void StoreSceneCameras(aiScene *pScene);
  112. /// Stores all textures in the given scene
  113. void StoreSceneTextures(aiScene *pScene);
  114. /// Stores all animations
  115. /// @param pScene Target scene to store the anims
  116. /// @param parser The collada parser
  117. void StoreAnimations(aiScene *pScene, const ColladaParser &parser);
  118. /** Stores all animations for the given source anim and its nested child animations
  119. * @param pScene target scene to store the anims
  120. * @param pSrcAnim the source animation to process
  121. * @param pPrefix Prefix to the name in case of nested animations
  122. */
  123. void StoreAnimations(aiScene *pScene, const ColladaParser &pParser, const Collada::Animation *pSrcAnim, const std::string &pPrefix);
  124. /** Constructs the animation for the given source anim */
  125. void CreateAnimation(aiScene *pScene, const ColladaParser &pParser, const Collada::Animation *pSrcAnim, const std::string &pName);
  126. /** Constructs materials from the collada material definitions */
  127. void BuildMaterials(ColladaParser &pParser, aiScene *pScene);
  128. /** Fill materials from the collada material definitions */
  129. void FillMaterials(const ColladaParser &pParser, aiScene *pScene);
  130. /** Resolve UV channel mappings*/
  131. void ApplyVertexToEffectSemanticMapping(Collada::Sampler &sampler,
  132. const Collada::SemanticMappingTable &table);
  133. /** Add a texture and all of its sampling properties to a material*/
  134. void AddTexture(aiMaterial &mat, const ColladaParser &pParser,
  135. const Collada::Effect &effect,
  136. const Collada::Sampler &sampler,
  137. aiTextureType type, unsigned int idx = 0);
  138. /** Resolves the texture name for the given effect texture entry */
  139. aiString FindFilenameForEffectTexture(const ColladaParser &pParser,
  140. const Collada::Effect &pEffect, const std::string &pName);
  141. /** Reads a float value from an accessor and its data array.
  142. * @param pAccessor The accessor to use for reading
  143. * @param pData The data array to read from
  144. * @param pIndex The index of the element to retrieve
  145. * @param pOffset Offset into the element, for multipart elements such as vectors or matrices
  146. * @return the specified value
  147. */
  148. ai_real ReadFloat(const Collada::Accessor &pAccessor, const Collada::Data &pData, size_t pIndex, size_t pOffset) const;
  149. /** Reads a string value from an accessor and its data array.
  150. * @param pAccessor The accessor to use for reading
  151. * @param pData The data array to read from
  152. * @param pIndex The index of the element to retrieve
  153. * @return the specified value
  154. */
  155. const std::string &ReadString(const Collada::Accessor &pAccessor, const Collada::Data &pData, size_t pIndex) const;
  156. /** Recursively collects all nodes into the given array */
  157. void CollectNodes(const aiNode *pNode, std::vector<const aiNode *> &poNodes) const;
  158. /** Finds a node in the collada scene by the given name */
  159. const Collada::Node *FindNode(const Collada::Node *pNode, const std::string &pName) const;
  160. /** Finds a node in the collada scene by the given SID */
  161. const Collada::Node *FindNodeBySID(const Collada::Node *pNode, const std::string &pSID) const;
  162. /** Finds a proper name for a node derived from the collada-node's properties */
  163. std::string FindNameForNode(const Collada::Node *pNode);
  164. protected:
  165. /** Filename, for a verbose error message */
  166. std::string mFileName;
  167. /** Which mesh-material compound was stored under which mesh ID */
  168. std::map<ColladaMeshIndex, size_t> mMeshIndexByID;
  169. /** Which material was stored under which index in the scene */
  170. std::map<std::string, size_t> mMaterialIndexByName;
  171. /** Accumulated meshes for the target scene */
  172. std::vector<aiMesh *> mMeshes;
  173. /** Accumulated morph target meshes */
  174. std::vector<aiMesh *> mTargetMeshes;
  175. /** Temporary material list */
  176. std::vector<std::pair<Collada::Effect *, aiMaterial *>> newMats;
  177. /** Temporary camera list */
  178. std::vector<aiCamera *> mCameras;
  179. /** Temporary light list */
  180. std::vector<aiLight *> mLights;
  181. /** Temporary texture list */
  182. std::vector<aiTexture *> mTextures;
  183. /** Accumulated animations for the target scene */
  184. std::vector<aiAnimation *> mAnims;
  185. bool noSkeletonMesh;
  186. bool removeEmptyBones;
  187. bool ignoreUpDirection;
  188. bool ignoreUnitSize;
  189. bool useColladaName;
  190. /** Used by FindNameForNode() to generate unique node names */
  191. unsigned int mNodeNameCounter;
  192. };
  193. } // end of namespace Assimp
  194. #endif // AI_COLLADALOADER_H_INC