mesh_splitter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Assimp2Json
  3. Copyright (c) 2011, Alexander C. Gessler
  4. Licensed under a 3-clause BSD license. See the LICENSE file for more information.
  5. */
  6. #include "mesh_splitter.h"
  7. #include <assimp/scene.h>
  8. // ----------------------------------------------------------------------------
  9. // Note: this is largely based on assimp's SplitLargeMeshes_Vertex process.
  10. // it is refactored and the coding style is slightly improved, though.
  11. // ----------------------------------------------------------------------------
  12. // ------------------------------------------------------------------------------------------------
  13. // Executes the post processing step on the given imported data.
  14. void MeshSplitter::Execute( aiScene* pScene) {
  15. std::vector<std::pair<aiMesh*, unsigned int> > source_mesh_map;
  16. for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  17. SplitMesh(a, pScene->mMeshes[a],source_mesh_map);
  18. }
  19. const unsigned int size = static_cast<unsigned int>(source_mesh_map.size());
  20. if (size != pScene->mNumMeshes) {
  21. // it seems something has been split. rebuild the mesh list
  22. delete[] pScene->mMeshes;
  23. pScene->mNumMeshes = size;
  24. pScene->mMeshes = new aiMesh*[size]();
  25. for (unsigned int i = 0; i < size;++i) {
  26. pScene->mMeshes[i] = source_mesh_map[i].first;
  27. }
  28. // now we need to update all nodes
  29. UpdateNode(pScene->mRootNode,source_mesh_map);
  30. }
  31. }
  32. // ------------------------------------------------------------------------------------------------
  33. void MeshSplitter::UpdateNode(aiNode* pcNode, const std::vector<std::pair<aiMesh*, unsigned int> >& source_mesh_map) {
  34. // TODO: should better use std::(multi)set for source_mesh_map.
  35. // for every index in out list build a new entry
  36. std::vector<unsigned int> aiEntries;
  37. aiEntries.reserve(pcNode->mNumMeshes + 1);
  38. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i) {
  39. for (unsigned int a = 0, end = static_cast<unsigned int>(source_mesh_map.size()); a < end;++a) {
  40. if (source_mesh_map[a].second == pcNode->mMeshes[i]) {
  41. aiEntries.push_back(a);
  42. }
  43. }
  44. }
  45. // now build the new list
  46. delete pcNode->mMeshes;
  47. pcNode->mNumMeshes = static_cast<unsigned int>(aiEntries.size());
  48. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  49. for (unsigned int b = 0; b < pcNode->mNumMeshes;++b) {
  50. pcNode->mMeshes[b] = aiEntries[b];
  51. }
  52. // recursively update children
  53. for (unsigned int i = 0, end = pcNode->mNumChildren; i < end;++i) {
  54. UpdateNode ( pcNode->mChildren[i], source_mesh_map );
  55. }
  56. }
  57. static const unsigned int WAS_NOT_COPIED = 0xffffffff;
  58. using PerVertexWeight = std::pair <unsigned int,float>;
  59. using VertexWeightTable = std::vector <PerVertexWeight>;
  60. // ------------------------------------------------------------------------------------------------
  61. VertexWeightTable* ComputeVertexBoneWeightTable(const aiMesh* pMesh) {
  62. if (!pMesh || !pMesh->mNumVertices || !pMesh->mNumBones) {
  63. return nullptr;
  64. }
  65. VertexWeightTable* const avPerVertexWeights = new VertexWeightTable[pMesh->mNumVertices];
  66. for (unsigned int i = 0; i < pMesh->mNumBones;++i) {
  67. aiBone* bone = pMesh->mBones[i];
  68. for (unsigned int a = 0; a < bone->mNumWeights;++a) {
  69. const aiVertexWeight& weight = bone->mWeights[a];
  70. avPerVertexWeights[weight.mVertexId].emplace_back(i,weight.mWeight);
  71. }
  72. }
  73. return avPerVertexWeights;
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. void MeshSplitter :: SplitMesh(unsigned int a, aiMesh* in_mesh, std::vector<std::pair<aiMesh*, unsigned int> >& source_mesh_map) {
  77. // TODO: should better use std::(multi)set for source_mesh_map.
  78. if (in_mesh->mNumVertices <= LIMIT) {
  79. source_mesh_map.emplace_back(in_mesh,a);
  80. return;
  81. }
  82. // build a per-vertex weight list if necessary
  83. VertexWeightTable* avPerVertexWeights = ComputeVertexBoneWeightTable(in_mesh);
  84. // we need to split this mesh into sub meshes. Estimate submesh size
  85. const unsigned int sub_meshes = (in_mesh->mNumVertices / LIMIT) + 1;
  86. // create a std::vector<unsigned int> to remember which vertices have already
  87. // been copied and to which position (i.e. output index)
  88. std::vector<unsigned int> was_copied_to;
  89. was_copied_to.resize(in_mesh->mNumVertices,WAS_NOT_COPIED);
  90. // Try to find a good estimate for the number of output faces
  91. // per mesh. Add 12.5% as buffer
  92. unsigned int size_estimated = in_mesh->mNumFaces / sub_meshes;
  93. size_estimated += size_estimated / 8;
  94. // now generate all submeshes
  95. unsigned int base = 0;
  96. while (true) {
  97. const unsigned int out_vertex_index = LIMIT;
  98. aiMesh* out_mesh = new aiMesh();
  99. out_mesh->mNumVertices = 0;
  100. out_mesh->mMaterialIndex = in_mesh->mMaterialIndex;
  101. // the name carries the adjacency information between the meshes
  102. out_mesh->mName = in_mesh->mName;
  103. typedef std::vector<aiVertexWeight> BoneWeightList;
  104. if (in_mesh->HasBones()) {
  105. out_mesh->mBones = new aiBone*[in_mesh->mNumBones]();
  106. }
  107. // clear the temporary helper array
  108. if (base) {
  109. std::fill(was_copied_to.begin(), was_copied_to.end(), WAS_NOT_COPIED);
  110. }
  111. std::vector<aiFace> vFaces;
  112. // reserve enough storage for most cases
  113. if (in_mesh->HasPositions()) {
  114. out_mesh->mVertices = new aiVector3D[out_vertex_index];
  115. }
  116. if (in_mesh->HasNormals()) {
  117. out_mesh->mNormals = new aiVector3D[out_vertex_index];
  118. }
  119. if (in_mesh->HasTangentsAndBitangents()) {
  120. out_mesh->mTangents = new aiVector3D[out_vertex_index];
  121. out_mesh->mBitangents = new aiVector3D[out_vertex_index];
  122. }
  123. for (unsigned int c = 0; in_mesh->HasVertexColors(c);++c) {
  124. out_mesh->mColors[c] = new aiColor4D[out_vertex_index];
  125. }
  126. for (unsigned int c = 0; in_mesh->HasTextureCoords(c);++c) {
  127. out_mesh->mNumUVComponents[c] = in_mesh->mNumUVComponents[c];
  128. out_mesh->mTextureCoords[c] = new aiVector3D[out_vertex_index];
  129. }
  130. vFaces.reserve(size_estimated);
  131. // (we will also need to copy the array of indices)
  132. while (base < in_mesh->mNumFaces) {
  133. const unsigned int iNumIndices = in_mesh->mFaces[base].mNumIndices;
  134. // doesn't catch degenerates but is quite fast
  135. unsigned int iNeed = 0;
  136. for (unsigned int v = 0; v < iNumIndices;++v) {
  137. unsigned int index = in_mesh->mFaces[base].mIndices[v];
  138. // check whether we do already have this vertex
  139. if (WAS_NOT_COPIED == was_copied_to[index]) {
  140. iNeed++;
  141. }
  142. }
  143. if (out_mesh->mNumVertices + iNeed > out_vertex_index) {
  144. // don't use this face
  145. break;
  146. }
  147. vFaces.emplace_back();
  148. aiFace& rFace = vFaces.back();
  149. // setup face type and number of indices
  150. rFace.mNumIndices = iNumIndices;
  151. rFace.mIndices = new unsigned int[iNumIndices];
  152. // need to update the output primitive types
  153. switch (rFace.mNumIndices)
  154. {
  155. case 1:
  156. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  157. break;
  158. case 2:
  159. out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  160. break;
  161. case 3:
  162. out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  163. break;
  164. default:
  165. out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  166. }
  167. // and copy the contents of the old array, offset them by current base
  168. for (unsigned int v = 0; v < iNumIndices;++v) {
  169. const unsigned int index = in_mesh->mFaces[base].mIndices[v];
  170. // check whether we do already have this vertex
  171. if (WAS_NOT_COPIED != was_copied_to[index]) {
  172. rFace.mIndices[v] = was_copied_to[index];
  173. continue;
  174. }
  175. // copy positions
  176. out_mesh->mVertices[out_mesh->mNumVertices] = (in_mesh->mVertices[index]);
  177. // copy normals
  178. if (in_mesh->HasNormals()) {
  179. out_mesh->mNormals[out_mesh->mNumVertices] = (in_mesh->mNormals[index]);
  180. }
  181. // copy tangents/bi-tangents
  182. if (in_mesh->HasTangentsAndBitangents()) {
  183. out_mesh->mTangents[out_mesh->mNumVertices] = (in_mesh->mTangents[index]);
  184. out_mesh->mBitangents[out_mesh->mNumVertices] = (in_mesh->mBitangents[index]);
  185. }
  186. // texture coordinates
  187. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
  188. if (in_mesh->HasTextureCoords( c)) {
  189. out_mesh->mTextureCoords[c][out_mesh->mNumVertices] = in_mesh->mTextureCoords[c][index];
  190. }
  191. }
  192. // vertex colors
  193. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
  194. if (in_mesh->HasVertexColors( c)) {
  195. out_mesh->mColors[c][out_mesh->mNumVertices] = in_mesh->mColors[c][index];
  196. }
  197. }
  198. // check whether we have bone weights assigned to this vertex
  199. rFace.mIndices[v] = out_mesh->mNumVertices;
  200. if (avPerVertexWeights) {
  201. VertexWeightTable& table = avPerVertexWeights[ out_mesh->mNumVertices ];
  202. for (VertexWeightTable::const_iterator iter = table.begin(), end = table.end(); iter != end;++iter) {
  203. // allocate the bone weight array if necessary and store it in the mBones field (HACK!)
  204. BoneWeightList* weight_list = reinterpret_cast<BoneWeightList*>(out_mesh->mBones[(*iter).first]);
  205. if (!weight_list) {
  206. weight_list = new BoneWeightList();
  207. out_mesh->mBones[(*iter).first] = reinterpret_cast<aiBone*>(weight_list);
  208. }
  209. weight_list->push_back(aiVertexWeight(out_mesh->mNumVertices,(*iter).second));
  210. }
  211. }
  212. was_copied_to[index] = out_mesh->mNumVertices;
  213. out_mesh->mNumVertices++;
  214. }
  215. base++;
  216. if(out_mesh->mNumVertices == out_vertex_index) {
  217. // break here. The face is only added if it was complete
  218. break;
  219. }
  220. }
  221. // check which bones we'll need to create for this submesh
  222. if (in_mesh->HasBones()) {
  223. aiBone** ppCurrent = out_mesh->mBones;
  224. for (unsigned int k = 0; k < in_mesh->mNumBones;++k) {
  225. // check whether the bone exists
  226. BoneWeightList* const weight_list = reinterpret_cast<BoneWeightList*>(out_mesh->mBones[k]);
  227. if (weight_list) {
  228. const aiBone* const bone_in = in_mesh->mBones[k];
  229. aiBone* const bone_out = new aiBone();
  230. *ppCurrent++ = bone_out;
  231. bone_out->mName = aiString(bone_in->mName);
  232. bone_out->mOffsetMatrix =bone_in->mOffsetMatrix;
  233. bone_out->mNumWeights = (unsigned int)weight_list->size();
  234. bone_out->mWeights = new aiVertexWeight[bone_out->mNumWeights];
  235. // copy the vertex weights
  236. ::memcpy(bone_out->mWeights, &(*weight_list)[0],bone_out->mNumWeights * sizeof(aiVertexWeight));
  237. delete weight_list;
  238. out_mesh->mNumBones++;
  239. }
  240. }
  241. }
  242. // copy the face list to the mesh
  243. out_mesh->mFaces = new aiFace[vFaces.size()];
  244. out_mesh->mNumFaces = (unsigned int)vFaces.size();
  245. for (unsigned int p = 0; p < out_mesh->mNumFaces;++p) {
  246. out_mesh->mFaces[p] = vFaces[p];
  247. }
  248. // add the newly created mesh to the list
  249. source_mesh_map.emplace_back(out_mesh,a);
  250. if (base == in_mesh->mNumFaces) {
  251. break;
  252. }
  253. }
  254. // delete the per-vertex weight list again
  255. delete[] avPerVertexWeights;
  256. // now delete the old mesh data
  257. delete in_mesh;
  258. }