Exporter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2009-2017, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #ifndef ANKI_TOOLS_SCENE_EXPORTER_H
  6. #define ANKI_TOOLS_SCENE_EXPORTER_H
  7. #include <string>
  8. #include <array>
  9. #include <cstdint>
  10. #include <fstream>
  11. #include <vector>
  12. #include <assimp/Importer.hpp>
  13. #include <assimp/scene.h>
  14. #include <assimp/postprocess.h>
  15. #include "Common.h"
  16. const uint32_t INVALID_INDEX = 0xFFFFFFFF;
  17. /// Thin mesh wrapper
  18. struct Model
  19. {
  20. uint32_t m_meshIndex = INVALID_INDEX; ///< Mesh index in the scene
  21. uint32_t m_materialIndex = INVALID_INDEX;
  22. std::string m_lod1MeshName;
  23. };
  24. /// Scene node.
  25. struct Node
  26. {
  27. uint32_t m_modelIndex; ///< Index inside Exporter::m_models
  28. aiMatrix4x4 m_transform;
  29. std::string m_group;
  30. std::string m_collisionMesh;
  31. };
  32. const uint32_t MAX_BONES_PER_VERTEX = 4;
  33. /// Bone/weight info for a single vertex
  34. struct VertexWeight
  35. {
  36. std::array<uint32_t, MAX_BONES_PER_VERTEX> m_boneIndices;
  37. std::array<float, MAX_BONES_PER_VERTEX> m_weigths;
  38. uint32_t m_bonesCount;
  39. };
  40. class Portal
  41. {
  42. public:
  43. uint32_t m_meshIndex;
  44. aiMatrix4x4 m_transform;
  45. };
  46. using Sector = Portal;
  47. class ParticleEmitter
  48. {
  49. public:
  50. std::string m_filename;
  51. aiMatrix4x4 m_transform;
  52. };
  53. class StaticCollisionNode
  54. {
  55. public:
  56. uint32_t m_meshIndex;
  57. aiMatrix4x4 m_transform;
  58. };
  59. class ReflectionProbe
  60. {
  61. public:
  62. aiVector3D m_position;
  63. float m_radius;
  64. };
  65. class ReflectionProxy
  66. {
  67. public:
  68. aiMatrix4x4 m_transform;
  69. uint32_t m_meshIndex; ///< Points to the scene that is not triangulated.
  70. };
  71. class OccluderNode
  72. {
  73. public:
  74. aiMatrix4x4 m_transform;
  75. uint32_t m_meshIndex; ///< Points to the scene that is not triangulated.
  76. };
  77. class DecalNode
  78. {
  79. public:
  80. aiMatrix4x4 m_transform;
  81. std::string m_diffuseTextureAtlasFilename;
  82. std::string m_diffuseSubTextureName;
  83. std::string m_normalRoughnessAtlasFilename;
  84. std::string m_normalRoughnessSubTextureName;
  85. aiVector3D m_size;
  86. std::array<float, 2> m_factors = {{1.0, 1.0}};
  87. };
  88. /// AnKi exporter.
  89. class Exporter
  90. {
  91. public:
  92. std::string m_inputFilename;
  93. std::string m_outputDirectory;
  94. std::string m_rpath;
  95. std::string m_texrpath;
  96. bool m_flipyz = false;
  97. const aiScene* m_scene = nullptr;
  98. const aiScene* m_sceneNoTriangles = nullptr;
  99. Assimp::Importer m_importer;
  100. Assimp::Importer m_importerNoTriangles;
  101. std::vector<Model> m_models;
  102. std::vector<Node> m_nodes;
  103. std::ofstream m_sceneFile;
  104. std::vector<StaticCollisionNode> m_staticCollisionNodes;
  105. std::vector<Portal> m_portals;
  106. std::vector<Sector> m_sectors;
  107. std::vector<ParticleEmitter> m_particleEmitters;
  108. std::vector<ReflectionProbe> m_reflectionProbes;
  109. std::vector<ReflectionProxy> m_reflectionProxies;
  110. std::vector<OccluderNode> m_occluders;
  111. std::vector<DecalNode> m_decals;
  112. /// Load the scene.
  113. void load();
  114. /// Export.
  115. void exportAll();
  116. private:
  117. /// @name Helpers
  118. /// @{
  119. /// Convert one 4x4 matrix to AnKi friendly matrix.
  120. aiMatrix4x4 toAnkiMatrix(const aiMatrix4x4& in) const;
  121. /// Convert one 3x3 matrix to AnKi friendly matrix.
  122. aiMatrix3x3 toAnkiMatrix(const aiMatrix3x3& in) const;
  123. void writeTransform(const aiMatrix4x4& mat);
  124. /// Write transformation of a node
  125. void writeNodeTransform(const std::string& node, const aiMatrix4x4& mat);
  126. const aiMesh& getMeshAt(unsigned index) const;
  127. const aiMaterial& getMaterialAt(unsigned index) const;
  128. std::string getModelName(const Model& model) const;
  129. /// Visits the node hierarchy and gathers models and nodes.
  130. void visitNode(const aiNode* ainode);
  131. /// @}
  132. /// Export a mesh.
  133. /// @param transform If not nullptr then transform the vertices using that.
  134. void exportMesh(const aiMesh& mesh, const aiMatrix4x4* transform, unsigned vertCountPerFace) const;
  135. /// Export a skeleton.
  136. void exportSkeleton(const aiMesh& mesh) const;
  137. /// Export a material.
  138. void exportMaterial(const aiMaterial& mtl) const;
  139. /// Export a model.
  140. void exportModel(const Model& model) const;
  141. /// Export a light.
  142. void exportLight(const aiLight& light);
  143. /// Export a camera.
  144. void exportCamera(const aiCamera& cam);
  145. /// Export an animation.
  146. void exportAnimation(const aiAnimation& anim, unsigned index);
  147. /// Export a static collision mesh.
  148. void exportCollisionMesh(uint32_t meshIdx);
  149. /// Helper.
  150. static std::string getMaterialName(const aiMaterial& mtl);
  151. };
  152. #endif