SplitLargeMeshes.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, 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. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /// @file Implementation of the SplitLargeMeshes postprocessing step
  34. // internal headers of the post-processing framework
  35. #include "SplitLargeMeshes.h"
  36. #include "ProcessHelper.h"
  37. using namespace Assimp;
  38. // ------------------------------------------------------------------------------------------------
  39. SplitLargeMeshesProcess_Triangle::SplitLargeMeshesProcess_Triangle() {
  40. LIMIT = AI_SLM_DEFAULT_MAX_TRIANGLES;
  41. }
  42. // ------------------------------------------------------------------------------------------------
  43. // Returns whether the processing step is present in the given flag field.
  44. bool SplitLargeMeshesProcess_Triangle::IsActive( unsigned int pFlags) const {
  45. return (pFlags & aiProcess_SplitLargeMeshes) != 0;
  46. }
  47. // ------------------------------------------------------------------------------------------------
  48. // Executes the post processing step on the given imported data.
  49. void SplitLargeMeshesProcess_Triangle::Execute( aiScene* pScene) {
  50. if (0xffffffff == this->LIMIT || nullptr == pScene ) {
  51. return;
  52. }
  53. ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Triangle begin");
  54. std::vector<std::pair<aiMesh*, unsigned int> > avList;
  55. for( unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
  56. this->SplitMesh(a, pScene->mMeshes[a],avList);
  57. }
  58. if (avList.size() == pScene->mNumMeshes) {
  59. ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Triangle finished. There was nothing to do");
  60. }
  61. // it seems something has been split. rebuild the mesh list
  62. delete[] pScene->mMeshes;
  63. pScene->mNumMeshes = (unsigned int)avList.size();
  64. pScene->mMeshes = new aiMesh*[avList.size()];
  65. for (unsigned int i = 0; i < avList.size();++i) {
  66. pScene->mMeshes[i] = avList[i].first;
  67. }
  68. // now we need to update all nodes
  69. this->UpdateNode(pScene->mRootNode,avList);
  70. ASSIMP_LOG_INFO("SplitLargeMeshesProcess_Triangle finished. Meshes have been split");
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Setup properties
  74. void SplitLargeMeshesProcess_Triangle::SetupProperties( const Importer* pImp) {
  75. // get the current value of the split property
  76. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
  77. }
  78. // ------------------------------------------------------------------------------------------------
  79. // Update a node after some meshes have been split
  80. void SplitLargeMeshesProcess_Triangle::UpdateNode(aiNode* pcNode, const std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
  81. // for every index in out list build a new entry
  82. std::vector<unsigned int> aiEntries;
  83. aiEntries.reserve(pcNode->mNumMeshes + 1);
  84. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i) {
  85. for (unsigned int a = 0; a < avList.size();++a) {
  86. if (avList[a].second == pcNode->mMeshes[i]) {
  87. aiEntries.push_back(a);
  88. }
  89. }
  90. }
  91. // now build the new list
  92. delete[] pcNode->mMeshes;
  93. pcNode->mNumMeshes = (unsigned int)aiEntries.size();
  94. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  95. for (unsigned int b = 0; b < pcNode->mNumMeshes;++b) {
  96. pcNode->mMeshes[b] = aiEntries[b];
  97. }
  98. // recursively update all other nodes
  99. for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
  100. UpdateNode ( pcNode->mChildren[i], avList );
  101. }
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. // Executes the post processing step on the given imported data.
  105. void SplitLargeMeshesProcess_Triangle::SplitMesh(
  106. unsigned int a,
  107. aiMesh* pMesh,
  108. std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
  109. if (pMesh->mNumFaces > SplitLargeMeshesProcess_Triangle::LIMIT) {
  110. ASSIMP_LOG_INFO("Mesh exceeds the triangle limit. It will be split ...");
  111. // we need to split this mesh into sub meshes
  112. // determine the size of a submesh
  113. const unsigned int iSubMeshes = (pMesh->mNumFaces / LIMIT) + 1;
  114. const unsigned int iOutFaceNum = pMesh->mNumFaces / iSubMeshes;
  115. const unsigned int iOutVertexNum = iOutFaceNum * 3;
  116. // now generate all submeshes
  117. for (unsigned int i = 0; i < iSubMeshes;++i) {
  118. aiMesh* pcMesh = new aiMesh;
  119. pcMesh->mNumFaces = iOutFaceNum;
  120. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  121. // the name carries the adjacency information between the meshes
  122. pcMesh->mName = pMesh->mName;
  123. if (i == iSubMeshes-1) {
  124. pcMesh->mNumFaces = iOutFaceNum + (
  125. pMesh->mNumFaces - iOutFaceNum * iSubMeshes);
  126. }
  127. // copy the list of faces
  128. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  129. const unsigned int iBase = iOutFaceNum * i;
  130. // get the total number of indices
  131. unsigned int iCnt = 0;
  132. for (unsigned int p = iBase; p < pcMesh->mNumFaces + iBase;++p) {
  133. iCnt += pMesh->mFaces[p].mNumIndices;
  134. }
  135. pcMesh->mNumVertices = iCnt;
  136. // allocate storage
  137. if (pMesh->mVertices != nullptr) {
  138. pcMesh->mVertices = new aiVector3D[iCnt];
  139. }
  140. if (pMesh->HasNormals()) {
  141. pcMesh->mNormals = new aiVector3D[iCnt];
  142. }
  143. if (pMesh->HasTangentsAndBitangents()) {
  144. pcMesh->mTangents = new aiVector3D[iCnt];
  145. pcMesh->mBitangents = new aiVector3D[iCnt];
  146. }
  147. // texture coordinates
  148. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
  149. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  150. if (pMesh->HasTextureCoords( c)) {
  151. pcMesh->mTextureCoords[c] = new aiVector3D[iCnt];
  152. }
  153. }
  154. // vertex colors
  155. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
  156. if (pMesh->HasVertexColors( c)) {
  157. pcMesh->mColors[c] = new aiColor4D[iCnt];
  158. }
  159. }
  160. if (pMesh->HasBones()) {
  161. // assume the number of bones won't change in most cases
  162. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  163. // iterate through all bones of the mesh and find those which
  164. // need to be copied to the split mesh
  165. std::vector<aiVertexWeight> avTempWeights;
  166. for (unsigned int p = 0; p < pcMesh->mNumBones;++p) {
  167. aiBone* const bone = pcMesh->mBones[p];
  168. avTempWeights.clear();
  169. avTempWeights.reserve(bone->mNumWeights / iSubMeshes);
  170. for (unsigned int q = 0; q < bone->mNumWeights;++q) {
  171. aiVertexWeight& weight = bone->mWeights[q];
  172. if(weight.mVertexId >= iBase && weight.mVertexId < iBase + iOutVertexNum) {
  173. avTempWeights.push_back(weight);
  174. weight = avTempWeights.back();
  175. weight.mVertexId -= iBase;
  176. }
  177. }
  178. if (!avTempWeights.empty()) {
  179. // we'll need this bone. Copy it ...
  180. aiBone* pc = new aiBone();
  181. pcMesh->mBones[pcMesh->mNumBones++] = pc;
  182. pc->mName = aiString(bone->mName);
  183. pc->mNumWeights = (unsigned int)avTempWeights.size();
  184. pc->mOffsetMatrix = bone->mOffsetMatrix;
  185. // no need to reallocate the array for the last submesh.
  186. // Here we can reuse the (large) source array, although
  187. // we'll waste some memory
  188. if (iSubMeshes-1 == i) {
  189. pc->mWeights = bone->mWeights;
  190. bone->mWeights = nullptr;
  191. } else {
  192. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  193. }
  194. // copy the weights
  195. ::memcpy(pc->mWeights,&avTempWeights[0],sizeof(aiVertexWeight)*pc->mNumWeights);
  196. }
  197. }
  198. }
  199. // (we will also need to copy the array of indices)
  200. unsigned int iCurrent = 0;
  201. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p) {
  202. pcMesh->mFaces[p].mNumIndices = 3;
  203. // allocate a new array
  204. const unsigned int iTemp = p + iBase;
  205. const unsigned int iNumIndices = pMesh->mFaces[iTemp].mNumIndices;
  206. // setup face type and number of indices
  207. pcMesh->mFaces[p].mNumIndices = iNumIndices;
  208. unsigned int* pi = pMesh->mFaces[iTemp].mIndices;
  209. unsigned int* piOut = pcMesh->mFaces[p].mIndices = new unsigned int[iNumIndices];
  210. // need to update the output primitive types
  211. switch (iNumIndices) {
  212. case 1:
  213. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  214. break;
  215. case 2:
  216. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  217. break;
  218. case 3:
  219. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  220. break;
  221. default:
  222. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  223. }
  224. // and copy the contents of the old array, offset by current base
  225. for (unsigned int v = 0; v < iNumIndices;++v) {
  226. unsigned int iIndex = pi[v];
  227. unsigned int iIndexOut = iCurrent++;
  228. piOut[v] = iIndexOut;
  229. // copy positions
  230. if (pMesh->mVertices != nullptr) {
  231. pcMesh->mVertices[iIndexOut] = pMesh->mVertices[iIndex];
  232. }
  233. // copy normals
  234. if (pMesh->HasNormals()) {
  235. pcMesh->mNormals[iIndexOut] = pMesh->mNormals[iIndex];
  236. }
  237. // copy tangents/bitangents
  238. if (pMesh->HasTangentsAndBitangents()) {
  239. pcMesh->mTangents[iIndexOut] = pMesh->mTangents[iIndex];
  240. pcMesh->mBitangents[iIndexOut] = pMesh->mBitangents[iIndex];
  241. }
  242. // texture coordinates
  243. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
  244. if (pMesh->HasTextureCoords( c ) ) {
  245. pcMesh->mTextureCoords[c][iIndexOut] = pMesh->mTextureCoords[c][iIndex];
  246. }
  247. }
  248. // vertex colors
  249. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
  250. if (pMesh->HasVertexColors( c)) {
  251. pcMesh->mColors[c][iIndexOut] = pMesh->mColors[c][iIndex];
  252. }
  253. }
  254. }
  255. }
  256. // add the newly created mesh to the list
  257. avList.emplace_back(pcMesh,a);
  258. }
  259. // now delete the old mesh data
  260. delete pMesh;
  261. } else {
  262. avList.emplace_back(pMesh,a);
  263. }
  264. }
  265. // ------------------------------------------------------------------------------------------------
  266. SplitLargeMeshesProcess_Vertex::SplitLargeMeshesProcess_Vertex() {
  267. LIMIT = AI_SLM_DEFAULT_MAX_VERTICES;
  268. }
  269. // ------------------------------------------------------------------------------------------------
  270. // Returns whether the processing step is present in the given flag field.
  271. bool SplitLargeMeshesProcess_Vertex::IsActive( unsigned int pFlags) const {
  272. return (pFlags & aiProcess_SplitLargeMeshes) != 0;
  273. }
  274. // ------------------------------------------------------------------------------------------------
  275. // Executes the post processing step on the given imported data.
  276. void SplitLargeMeshesProcess_Vertex::Execute( aiScene* pScene) {
  277. if (0xffffffff == this->LIMIT || nullptr == pScene ) {
  278. return;
  279. }
  280. ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Vertex begin");
  281. std::vector<std::pair<aiMesh*, unsigned int> > avList;
  282. //Check for point cloud first,
  283. //Do not process point cloud, splitMesh works only with faces data
  284. for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  285. if ( pScene->mMeshes[a]->mPrimitiveTypes == aiPrimitiveType_POINT ) {
  286. return;
  287. }
  288. }
  289. for( unsigned int a = 0; a < pScene->mNumMeshes; ++a ) {
  290. this->SplitMesh(a, pScene->mMeshes[a], avList);
  291. }
  292. if (avList.size() != pScene->mNumMeshes) {
  293. // it seems something has been split. rebuild the mesh list
  294. delete[] pScene->mMeshes;
  295. pScene->mNumMeshes = (unsigned int)avList.size();
  296. pScene->mMeshes = new aiMesh*[avList.size()];
  297. for (unsigned int i = 0; i < avList.size();++i) {
  298. pScene->mMeshes[i] = avList[i].first;
  299. }
  300. // now we need to update all nodes
  301. SplitLargeMeshesProcess_Triangle::UpdateNode(pScene->mRootNode,avList);
  302. ASSIMP_LOG_INFO("SplitLargeMeshesProcess_Vertex finished. Meshes have been split");
  303. } else {
  304. ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Vertex finished. There was nothing to do");
  305. }
  306. }
  307. // ------------------------------------------------------------------------------------------------
  308. // Setup properties
  309. void SplitLargeMeshesProcess_Vertex::SetupProperties( const Importer* pImp) {
  310. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  311. }
  312. // ------------------------------------------------------------------------------------------------
  313. // Executes the post processing step on the given imported data.
  314. void SplitLargeMeshesProcess_Vertex::SplitMesh(
  315. unsigned int a,
  316. aiMesh* pMesh,
  317. std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
  318. if (pMesh->mNumVertices > SplitLargeMeshesProcess_Vertex::LIMIT) {
  319. typedef std::vector< std::pair<unsigned int,float> > VertexWeightTable;
  320. // build a per-vertex weight list if necessary
  321. VertexWeightTable* avPerVertexWeights = ComputeVertexBoneWeightTable(pMesh);
  322. // we need to split this mesh into sub meshes
  323. // determine the estimated size of a submesh
  324. // (this could be too large. Max waste is a single digit percentage)
  325. const unsigned int iSubMeshes = (pMesh->mNumVertices / SplitLargeMeshesProcess_Vertex::LIMIT) + 1;
  326. // create a std::vector<unsigned int> to indicate which vertices
  327. // have already been copied
  328. std::vector<unsigned int> avWasCopied;
  329. avWasCopied.resize(pMesh->mNumVertices,0xFFFFFFFF);
  330. // try to find a good estimate for the number of output faces
  331. // per mesh. Add 12.5% as buffer
  332. unsigned int iEstimatedSize = pMesh->mNumFaces / iSubMeshes;
  333. iEstimatedSize += iEstimatedSize >> 3;
  334. // now generate all submeshes
  335. unsigned int iBase( 0 );
  336. while (true) {
  337. const unsigned int iOutVertexNum = SplitLargeMeshesProcess_Vertex::LIMIT;
  338. aiMesh* pcMesh = new aiMesh;
  339. pcMesh->mNumVertices = 0;
  340. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  341. // the name carries the adjacency information between the meshes
  342. pcMesh->mName = pMesh->mName;
  343. typedef std::vector<aiVertexWeight> BoneWeightList;
  344. if (pMesh->HasBones()) {
  345. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  346. ::memset(pcMesh->mBones,0,sizeof(void*)*pMesh->mNumBones);
  347. }
  348. // clear the temporary helper array
  349. if (iBase) {
  350. // we can't use memset here we unsigned int needn' be 32 bits
  351. for (auto &elem : avWasCopied) {
  352. elem = 0xffffffff;
  353. }
  354. }
  355. // output vectors
  356. std::vector<aiFace> vFaces;
  357. // reserve enough storage for most cases
  358. if (pMesh->HasPositions()) {
  359. pcMesh->mVertices = new aiVector3D[iOutVertexNum];
  360. }
  361. if (pMesh->HasNormals()) {
  362. pcMesh->mNormals = new aiVector3D[iOutVertexNum];
  363. }
  364. if (pMesh->HasTangentsAndBitangents()) {
  365. pcMesh->mTangents = new aiVector3D[iOutVertexNum];
  366. pcMesh->mBitangents = new aiVector3D[iOutVertexNum];
  367. }
  368. for (unsigned int c = 0; pMesh->HasVertexColors(c);++c) {
  369. pcMesh->mColors[c] = new aiColor4D[iOutVertexNum];
  370. }
  371. for (unsigned int c = 0; pMesh->HasTextureCoords(c);++c) {
  372. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  373. pcMesh->mTextureCoords[c] = new aiVector3D[iOutVertexNum];
  374. }
  375. vFaces.reserve(iEstimatedSize);
  376. // (we will also need to copy the array of indices)
  377. while (iBase < pMesh->mNumFaces) {
  378. // allocate a new array
  379. const unsigned int iNumIndices = pMesh->mFaces[iBase].mNumIndices;
  380. // doesn't catch degenerates but is quite fast
  381. unsigned int iNeed = 0;
  382. for (unsigned int v = 0; v < iNumIndices;++v) {
  383. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  384. // check whether we do already have this vertex
  385. if (0xFFFFFFFF == avWasCopied[iIndex]) {
  386. iNeed++;
  387. }
  388. }
  389. if (pcMesh->mNumVertices + iNeed > iOutVertexNum) {
  390. // don't use this face
  391. break;
  392. }
  393. vFaces.emplace_back();
  394. aiFace& rFace = vFaces.back();
  395. // setup face type and number of indices
  396. rFace.mNumIndices = iNumIndices;
  397. rFace.mIndices = new unsigned int[iNumIndices];
  398. // need to update the output primitive types
  399. switch (rFace.mNumIndices) {
  400. case 1:
  401. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  402. break;
  403. case 2:
  404. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  405. break;
  406. case 3:
  407. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  408. break;
  409. default:
  410. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  411. }
  412. // and copy the contents of the old array, offset by current base
  413. for (unsigned int v = 0; v < iNumIndices;++v) {
  414. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  415. // check whether we do already have this vertex
  416. if (0xFFFFFFFF != avWasCopied[iIndex]) {
  417. rFace.mIndices[v] = avWasCopied[iIndex];
  418. continue;
  419. }
  420. // copy positions
  421. pcMesh->mVertices[pcMesh->mNumVertices] = (pMesh->mVertices[iIndex]);
  422. // copy normals
  423. if (pMesh->HasNormals()) {
  424. pcMesh->mNormals[pcMesh->mNumVertices] = (pMesh->mNormals[iIndex]);
  425. }
  426. // copy tangents/bitangents
  427. if (pMesh->HasTangentsAndBitangents()) {
  428. pcMesh->mTangents[pcMesh->mNumVertices] = (pMesh->mTangents[iIndex]);
  429. pcMesh->mBitangents[pcMesh->mNumVertices] = (pMesh->mBitangents[iIndex]);
  430. }
  431. // texture coordinates
  432. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
  433. if (pMesh->HasTextureCoords( c)) {
  434. pcMesh->mTextureCoords[c][pcMesh->mNumVertices] = pMesh->mTextureCoords[c][iIndex];
  435. }
  436. }
  437. // vertex colors
  438. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
  439. if (pMesh->HasVertexColors( c)) {
  440. pcMesh->mColors[c][pcMesh->mNumVertices] = pMesh->mColors[c][iIndex];
  441. }
  442. }
  443. // check whether we have bone weights assigned to this vertex
  444. rFace.mIndices[v] = pcMesh->mNumVertices;
  445. if (avPerVertexWeights) {
  446. VertexWeightTable& table = avPerVertexWeights[ pcMesh->mNumVertices ];
  447. if( !table.empty() ) {
  448. for (VertexWeightTable::const_iterator iter = table.begin();
  449. iter != table.end();++iter) {
  450. // allocate the bone weight array if necessary
  451. BoneWeightList* pcWeightList = (BoneWeightList*)pcMesh->mBones[(*iter).first];
  452. if (nullptr == pcWeightList) {
  453. pcMesh->mBones[(*iter).first] = (aiBone*)(pcWeightList = new BoneWeightList());
  454. }
  455. pcWeightList->push_back(aiVertexWeight(pcMesh->mNumVertices,(*iter).second));
  456. }
  457. }
  458. }
  459. avWasCopied[iIndex] = pcMesh->mNumVertices;
  460. pcMesh->mNumVertices++;
  461. }
  462. ++iBase;
  463. if(pcMesh->mNumVertices == iOutVertexNum) {
  464. // break here. The face is only added if it was complete
  465. break;
  466. }
  467. }
  468. // check which bones we'll need to create for this submesh
  469. if (pMesh->HasBones()) {
  470. aiBone** ppCurrent = pcMesh->mBones;
  471. for (unsigned int k = 0; k < pMesh->mNumBones;++k) {
  472. // check whether the bone is existing
  473. BoneWeightList* pcWeightList;
  474. pcWeightList = (BoneWeightList *)pcMesh->mBones[k];
  475. if (nullptr != pcWeightList) {
  476. aiBone *pcOldBone = pMesh->mBones[k];
  477. aiBone* pcOut( nullptr );
  478. *ppCurrent++ = pcOut = new aiBone();
  479. pcOut->mName = aiString(pcOldBone->mName);
  480. pcOut->mOffsetMatrix = pcOldBone->mOffsetMatrix;
  481. pcOut->mNumWeights = (unsigned int)pcWeightList->size();
  482. pcOut->mWeights = new aiVertexWeight[pcOut->mNumWeights];
  483. // copy the vertex weights
  484. ::memcpy(pcOut->mWeights,&pcWeightList->operator[](0),
  485. pcOut->mNumWeights * sizeof(aiVertexWeight));
  486. // delete the temporary bone weight list
  487. delete pcWeightList;
  488. pcMesh->mNumBones++;
  489. }
  490. }
  491. }
  492. // copy the face list to the mesh
  493. pcMesh->mFaces = new aiFace[vFaces.size()];
  494. pcMesh->mNumFaces = (unsigned int)vFaces.size();
  495. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p) {
  496. pcMesh->mFaces[p] = vFaces[p];
  497. }
  498. // add the newly created mesh to the list
  499. avList.emplace_back(pcMesh,a);
  500. if (iBase == pMesh->mNumFaces) {
  501. // have all faces ... finish the outer loop, too
  502. break;
  503. }
  504. }
  505. // delete the per-vertex weight list again
  506. delete[] avPerVertexWeights;
  507. // now delete the old mesh data
  508. delete pMesh;
  509. return;
  510. }
  511. avList.emplace_back(pMesh,a);
  512. }