SplitLargeMeshes.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. if (pcNode == nullptr) {
  82. ASSIMP_LOG_WARN("UpdateNode skipped, nullptr detected.");
  83. return;
  84. }
  85. // for every index in out list build a new entry
  86. std::vector<unsigned int> aiEntries;
  87. aiEntries.reserve(pcNode->mNumMeshes + 1);
  88. for (unsigned int i = 0; i < pcNode->mNumMeshes;++i) {
  89. for (unsigned int a = 0; a < avList.size();++a) {
  90. if (avList[a].second == pcNode->mMeshes[i]) {
  91. aiEntries.push_back(a);
  92. }
  93. }
  94. }
  95. // now build the new list
  96. delete[] pcNode->mMeshes;
  97. pcNode->mNumMeshes = (unsigned int)aiEntries.size();
  98. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  99. for (unsigned int b = 0; b < pcNode->mNumMeshes;++b) {
  100. pcNode->mMeshes[b] = aiEntries[b];
  101. }
  102. // recursively update all other nodes
  103. for (unsigned int i = 0; i < pcNode->mNumChildren;++i) {
  104. UpdateNode ( pcNode->mChildren[i], avList );
  105. }
  106. }
  107. // ------------------------------------------------------------------------------------------------
  108. // Executes the post processing step on the given imported data.
  109. void SplitLargeMeshesProcess_Triangle::SplitMesh(
  110. unsigned int a,
  111. aiMesh* pMesh,
  112. std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
  113. if (pMesh->mNumFaces > SplitLargeMeshesProcess_Triangle::LIMIT) {
  114. ASSIMP_LOG_INFO("Mesh exceeds the triangle limit. It will be split ...");
  115. // we need to split this mesh into sub meshes
  116. // determine the size of a submesh
  117. const unsigned int iSubMeshes = (pMesh->mNumFaces / LIMIT) + 1;
  118. const unsigned int iOutFaceNum = pMesh->mNumFaces / iSubMeshes;
  119. const unsigned int iOutVertexNum = iOutFaceNum * 3;
  120. // now generate all submeshes
  121. for (unsigned int i = 0; i < iSubMeshes;++i) {
  122. aiMesh* pcMesh = new aiMesh;
  123. pcMesh->mNumFaces = iOutFaceNum;
  124. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  125. // the name carries the adjacency information between the meshes
  126. pcMesh->mName = pMesh->mName;
  127. if (i == iSubMeshes-1) {
  128. pcMesh->mNumFaces = iOutFaceNum + (
  129. pMesh->mNumFaces - iOutFaceNum * iSubMeshes);
  130. }
  131. // copy the list of faces
  132. pcMesh->mFaces = new aiFace[pcMesh->mNumFaces];
  133. const unsigned int iBase = iOutFaceNum * i;
  134. // get the total number of indices
  135. unsigned int iCnt = 0;
  136. for (unsigned int p = iBase; p < pcMesh->mNumFaces + iBase;++p) {
  137. iCnt += pMesh->mFaces[p].mNumIndices;
  138. }
  139. pcMesh->mNumVertices = iCnt;
  140. // allocate storage
  141. if (pMesh->mVertices != nullptr) {
  142. pcMesh->mVertices = new aiVector3D[iCnt];
  143. }
  144. if (pMesh->HasNormals()) {
  145. pcMesh->mNormals = new aiVector3D[iCnt];
  146. }
  147. if (pMesh->HasTangentsAndBitangents()) {
  148. pcMesh->mTangents = new aiVector3D[iCnt];
  149. pcMesh->mBitangents = new aiVector3D[iCnt];
  150. }
  151. // texture coordinates
  152. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
  153. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  154. if (pMesh->HasTextureCoords( c)) {
  155. pcMesh->mTextureCoords[c] = new aiVector3D[iCnt];
  156. }
  157. }
  158. // vertex colors
  159. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
  160. if (pMesh->HasVertexColors( c)) {
  161. pcMesh->mColors[c] = new aiColor4D[iCnt];
  162. }
  163. }
  164. if (pMesh->HasBones()) {
  165. // assume the number of bones won't change in most cases
  166. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  167. // iterate through all bones of the mesh and find those which
  168. // need to be copied to the split mesh
  169. std::vector<aiVertexWeight> avTempWeights;
  170. for (unsigned int p = 0; p < pcMesh->mNumBones;++p) {
  171. aiBone* const bone = pcMesh->mBones[p];
  172. avTempWeights.clear();
  173. avTempWeights.reserve(bone->mNumWeights / iSubMeshes);
  174. for (unsigned int q = 0; q < bone->mNumWeights;++q) {
  175. aiVertexWeight& weight = bone->mWeights[q];
  176. if(weight.mVertexId >= iBase && weight.mVertexId < iBase + iOutVertexNum) {
  177. avTempWeights.push_back(weight);
  178. weight = avTempWeights.back();
  179. weight.mVertexId -= iBase;
  180. }
  181. }
  182. if (!avTempWeights.empty()) {
  183. // we'll need this bone. Copy it ...
  184. aiBone* pc = new aiBone();
  185. pcMesh->mBones[pcMesh->mNumBones++] = pc;
  186. pc->mName = aiString(bone->mName);
  187. pc->mNumWeights = (unsigned int)avTempWeights.size();
  188. pc->mOffsetMatrix = bone->mOffsetMatrix;
  189. // no need to reallocate the array for the last submesh.
  190. // Here we can reuse the (large) source array, although
  191. // we'll waste some memory
  192. if (iSubMeshes-1 == i) {
  193. pc->mWeights = bone->mWeights;
  194. bone->mWeights = nullptr;
  195. } else {
  196. pc->mWeights = new aiVertexWeight[pc->mNumWeights];
  197. }
  198. // copy the weights
  199. ::memcpy(pc->mWeights,&avTempWeights[0],sizeof(aiVertexWeight)*pc->mNumWeights);
  200. }
  201. }
  202. }
  203. // (we will also need to copy the array of indices)
  204. unsigned int iCurrent = 0;
  205. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p) {
  206. pcMesh->mFaces[p].mNumIndices = 3;
  207. // allocate a new array
  208. const unsigned int iTemp = p + iBase;
  209. const unsigned int iNumIndices = pMesh->mFaces[iTemp].mNumIndices;
  210. // setup face type and number of indices
  211. pcMesh->mFaces[p].mNumIndices = iNumIndices;
  212. unsigned int* pi = pMesh->mFaces[iTemp].mIndices;
  213. unsigned int* piOut = pcMesh->mFaces[p].mIndices = new unsigned int[iNumIndices];
  214. // need to update the output primitive types
  215. switch (iNumIndices) {
  216. case 1:
  217. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  218. break;
  219. case 2:
  220. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  221. break;
  222. case 3:
  223. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  224. break;
  225. default:
  226. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  227. }
  228. // and copy the contents of the old array, offset by current base
  229. for (unsigned int v = 0; v < iNumIndices;++v) {
  230. unsigned int iIndex = pi[v];
  231. unsigned int iIndexOut = iCurrent++;
  232. piOut[v] = iIndexOut;
  233. // copy positions
  234. if (pMesh->mVertices != nullptr) {
  235. pcMesh->mVertices[iIndexOut] = pMesh->mVertices[iIndex];
  236. }
  237. // copy normals
  238. if (pMesh->HasNormals()) {
  239. pcMesh->mNormals[iIndexOut] = pMesh->mNormals[iIndex];
  240. }
  241. // copy tangents/bitangents
  242. if (pMesh->HasTangentsAndBitangents()) {
  243. pcMesh->mTangents[iIndexOut] = pMesh->mTangents[iIndex];
  244. pcMesh->mBitangents[iIndexOut] = pMesh->mBitangents[iIndex];
  245. }
  246. // texture coordinates
  247. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
  248. if (pMesh->HasTextureCoords( c ) ) {
  249. pcMesh->mTextureCoords[c][iIndexOut] = pMesh->mTextureCoords[c][iIndex];
  250. }
  251. }
  252. // vertex colors
  253. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
  254. if (pMesh->HasVertexColors( c)) {
  255. pcMesh->mColors[c][iIndexOut] = pMesh->mColors[c][iIndex];
  256. }
  257. }
  258. }
  259. }
  260. // add the newly created mesh to the list
  261. avList.emplace_back(pcMesh,a);
  262. }
  263. // now delete the old mesh data
  264. delete pMesh;
  265. } else {
  266. avList.emplace_back(pMesh,a);
  267. }
  268. }
  269. // ------------------------------------------------------------------------------------------------
  270. SplitLargeMeshesProcess_Vertex::SplitLargeMeshesProcess_Vertex() {
  271. LIMIT = AI_SLM_DEFAULT_MAX_VERTICES;
  272. }
  273. // ------------------------------------------------------------------------------------------------
  274. // Returns whether the processing step is present in the given flag field.
  275. bool SplitLargeMeshesProcess_Vertex::IsActive( unsigned int pFlags) const {
  276. return (pFlags & aiProcess_SplitLargeMeshes) != 0;
  277. }
  278. // ------------------------------------------------------------------------------------------------
  279. // Executes the post processing step on the given imported data.
  280. void SplitLargeMeshesProcess_Vertex::Execute( aiScene* pScene) {
  281. if (0xffffffff == this->LIMIT || nullptr == pScene ) {
  282. return;
  283. }
  284. ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Vertex begin");
  285. std::vector<std::pair<aiMesh*, unsigned int> > avList;
  286. //Check for point cloud first,
  287. //Do not process point cloud, splitMesh works only with faces data
  288. for (unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  289. if ( pScene->mMeshes[a]->mPrimitiveTypes == aiPrimitiveType_POINT ) {
  290. return;
  291. }
  292. }
  293. for( unsigned int a = 0; a < pScene->mNumMeshes; ++a ) {
  294. this->SplitMesh(a, pScene->mMeshes[a], avList);
  295. }
  296. if (avList.size() != pScene->mNumMeshes) {
  297. // it seems something has been split. rebuild the mesh list
  298. delete[] pScene->mMeshes;
  299. pScene->mNumMeshes = (unsigned int)avList.size();
  300. pScene->mMeshes = new aiMesh*[avList.size()];
  301. for (unsigned int i = 0; i < avList.size();++i) {
  302. pScene->mMeshes[i] = avList[i].first;
  303. }
  304. // now we need to update all nodes
  305. SplitLargeMeshesProcess_Triangle::UpdateNode(pScene->mRootNode,avList);
  306. ASSIMP_LOG_INFO("SplitLargeMeshesProcess_Vertex finished. Meshes have been split");
  307. } else {
  308. ASSIMP_LOG_DEBUG("SplitLargeMeshesProcess_Vertex finished. There was nothing to do");
  309. }
  310. }
  311. // ------------------------------------------------------------------------------------------------
  312. // Setup properties
  313. void SplitLargeMeshesProcess_Vertex::SetupProperties( const Importer* pImp) {
  314. this->LIMIT = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  315. }
  316. // ------------------------------------------------------------------------------------------------
  317. // Executes the post processing step on the given imported data.
  318. void SplitLargeMeshesProcess_Vertex::SplitMesh(
  319. unsigned int a,
  320. aiMesh* pMesh,
  321. std::vector<std::pair<aiMesh*, unsigned int> >& avList) {
  322. if (pMesh->mNumVertices > SplitLargeMeshesProcess_Vertex::LIMIT) {
  323. typedef std::vector< std::pair<unsigned int,float> > VertexWeightTable;
  324. // build a per-vertex weight list if necessary
  325. VertexWeightTable* avPerVertexWeights = ComputeVertexBoneWeightTable(pMesh);
  326. // we need to split this mesh into sub meshes
  327. // determine the estimated size of a submesh
  328. // (this could be too large. Max waste is a single digit percentage)
  329. const unsigned int iSubMeshes = (pMesh->mNumVertices / SplitLargeMeshesProcess_Vertex::LIMIT) + 1;
  330. // create a std::vector<unsigned int> to indicate which vertices
  331. // have already been copied
  332. std::vector<unsigned int> avWasCopied;
  333. avWasCopied.resize(pMesh->mNumVertices,0xFFFFFFFF);
  334. // try to find a good estimate for the number of output faces
  335. // per mesh. Add 12.5% as buffer
  336. unsigned int iEstimatedSize = pMesh->mNumFaces / iSubMeshes;
  337. iEstimatedSize += iEstimatedSize >> 3;
  338. // now generate all submeshes
  339. unsigned int iBase( 0 );
  340. while (true) {
  341. const unsigned int iOutVertexNum = SplitLargeMeshesProcess_Vertex::LIMIT;
  342. aiMesh* pcMesh = new aiMesh;
  343. pcMesh->mNumVertices = 0;
  344. pcMesh->mMaterialIndex = pMesh->mMaterialIndex;
  345. // the name carries the adjacency information between the meshes
  346. pcMesh->mName = pMesh->mName;
  347. typedef std::vector<aiVertexWeight> BoneWeightList;
  348. if (pMesh->HasBones()) {
  349. pcMesh->mBones = new aiBone*[pMesh->mNumBones];
  350. ::memset(pcMesh->mBones,0,sizeof(void*)*pMesh->mNumBones);
  351. }
  352. // clear the temporary helper array
  353. if (iBase) {
  354. // we can't use memset here we unsigned int needn' be 32 bits
  355. for (auto &elem : avWasCopied) {
  356. elem = 0xffffffff;
  357. }
  358. }
  359. // output vectors
  360. std::vector<aiFace> vFaces;
  361. // reserve enough storage for most cases
  362. if (pMesh->HasPositions()) {
  363. pcMesh->mVertices = new aiVector3D[iOutVertexNum];
  364. }
  365. if (pMesh->HasNormals()) {
  366. pcMesh->mNormals = new aiVector3D[iOutVertexNum];
  367. }
  368. if (pMesh->HasTangentsAndBitangents()) {
  369. pcMesh->mTangents = new aiVector3D[iOutVertexNum];
  370. pcMesh->mBitangents = new aiVector3D[iOutVertexNum];
  371. }
  372. for (unsigned int c = 0; pMesh->HasVertexColors(c);++c) {
  373. pcMesh->mColors[c] = new aiColor4D[iOutVertexNum];
  374. }
  375. for (unsigned int c = 0; pMesh->HasTextureCoords(c);++c) {
  376. pcMesh->mNumUVComponents[c] = pMesh->mNumUVComponents[c];
  377. pcMesh->mTextureCoords[c] = new aiVector3D[iOutVertexNum];
  378. }
  379. vFaces.reserve(iEstimatedSize);
  380. // (we will also need to copy the array of indices)
  381. while (iBase < pMesh->mNumFaces) {
  382. // allocate a new array
  383. const unsigned int iNumIndices = pMesh->mFaces[iBase].mNumIndices;
  384. // doesn't catch degenerates but is quite fast
  385. unsigned int iNeed = 0;
  386. for (unsigned int v = 0; v < iNumIndices;++v) {
  387. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  388. // check whether we do already have this vertex
  389. if (0xFFFFFFFF == avWasCopied[iIndex]) {
  390. iNeed++;
  391. }
  392. }
  393. if (pcMesh->mNumVertices + iNeed > iOutVertexNum) {
  394. // don't use this face
  395. break;
  396. }
  397. vFaces.emplace_back();
  398. aiFace& rFace = vFaces.back();
  399. // setup face type and number of indices
  400. rFace.mNumIndices = iNumIndices;
  401. rFace.mIndices = new unsigned int[iNumIndices];
  402. // need to update the output primitive types
  403. switch (rFace.mNumIndices) {
  404. case 1:
  405. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POINT;
  406. break;
  407. case 2:
  408. pcMesh->mPrimitiveTypes |= aiPrimitiveType_LINE;
  409. break;
  410. case 3:
  411. pcMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  412. break;
  413. default:
  414. pcMesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  415. }
  416. // and copy the contents of the old array, offset by current base
  417. for (unsigned int v = 0; v < iNumIndices;++v) {
  418. unsigned int iIndex = pMesh->mFaces[iBase].mIndices[v];
  419. // check whether we do already have this vertex
  420. if (0xFFFFFFFF != avWasCopied[iIndex]) {
  421. rFace.mIndices[v] = avWasCopied[iIndex];
  422. continue;
  423. }
  424. // copy positions
  425. pcMesh->mVertices[pcMesh->mNumVertices] = (pMesh->mVertices[iIndex]);
  426. // copy normals
  427. if (pMesh->HasNormals()) {
  428. pcMesh->mNormals[pcMesh->mNumVertices] = (pMesh->mNormals[iIndex]);
  429. }
  430. // copy tangents/bitangents
  431. if (pMesh->HasTangentsAndBitangents()) {
  432. pcMesh->mTangents[pcMesh->mNumVertices] = (pMesh->mTangents[iIndex]);
  433. pcMesh->mBitangents[pcMesh->mNumVertices] = (pMesh->mBitangents[iIndex]);
  434. }
  435. // texture coordinates
  436. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS;++c) {
  437. if (pMesh->HasTextureCoords( c)) {
  438. pcMesh->mTextureCoords[c][pcMesh->mNumVertices] = pMesh->mTextureCoords[c][iIndex];
  439. }
  440. }
  441. // vertex colors
  442. for (unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS;++c) {
  443. if (pMesh->HasVertexColors( c)) {
  444. pcMesh->mColors[c][pcMesh->mNumVertices] = pMesh->mColors[c][iIndex];
  445. }
  446. }
  447. // check whether we have bone weights assigned to this vertex
  448. rFace.mIndices[v] = pcMesh->mNumVertices;
  449. if (avPerVertexWeights) {
  450. VertexWeightTable& table = avPerVertexWeights[ pcMesh->mNumVertices ];
  451. if( !table.empty() ) {
  452. for (VertexWeightTable::const_iterator iter = table.begin();
  453. iter != table.end();++iter) {
  454. // allocate the bone weight array if necessary
  455. BoneWeightList* pcWeightList = (BoneWeightList*)pcMesh->mBones[(*iter).first];
  456. if (nullptr == pcWeightList) {
  457. pcMesh->mBones[(*iter).first] = (aiBone*)(pcWeightList = new BoneWeightList());
  458. }
  459. pcWeightList->push_back(aiVertexWeight(pcMesh->mNumVertices,(*iter).second));
  460. }
  461. }
  462. }
  463. avWasCopied[iIndex] = pcMesh->mNumVertices;
  464. pcMesh->mNumVertices++;
  465. }
  466. ++iBase;
  467. if(pcMesh->mNumVertices == iOutVertexNum) {
  468. // break here. The face is only added if it was complete
  469. break;
  470. }
  471. }
  472. // check which bones we'll need to create for this submesh
  473. if (pMesh->HasBones()) {
  474. aiBone** ppCurrent = pcMesh->mBones;
  475. for (unsigned int k = 0; k < pMesh->mNumBones;++k) {
  476. // check whether the bone is existing
  477. BoneWeightList* pcWeightList;
  478. pcWeightList = (BoneWeightList *)pcMesh->mBones[k];
  479. if (nullptr != pcWeightList) {
  480. aiBone *pcOldBone = pMesh->mBones[k];
  481. aiBone* pcOut( nullptr );
  482. *ppCurrent++ = pcOut = new aiBone();
  483. pcOut->mName = aiString(pcOldBone->mName);
  484. pcOut->mOffsetMatrix = pcOldBone->mOffsetMatrix;
  485. pcOut->mNumWeights = (unsigned int)pcWeightList->size();
  486. pcOut->mWeights = new aiVertexWeight[pcOut->mNumWeights];
  487. // copy the vertex weights
  488. ::memcpy(pcOut->mWeights,&pcWeightList->operator[](0),
  489. pcOut->mNumWeights * sizeof(aiVertexWeight));
  490. // delete the temporary bone weight list
  491. delete pcWeightList;
  492. pcMesh->mNumBones++;
  493. }
  494. }
  495. }
  496. // copy the face list to the mesh
  497. pcMesh->mFaces = new aiFace[vFaces.size()];
  498. pcMesh->mNumFaces = (unsigned int)vFaces.size();
  499. for (unsigned int p = 0; p < pcMesh->mNumFaces;++p) {
  500. pcMesh->mFaces[p] = vFaces[p];
  501. }
  502. // add the newly created mesh to the list
  503. avList.emplace_back(pcMesh,a);
  504. if (iBase == pMesh->mNumFaces) {
  505. // have all faces ... finish the outer loop, too
  506. break;
  507. }
  508. }
  509. // delete the per-vertex weight list again
  510. delete[] avPerVertexWeights;
  511. // now delete the old mesh data
  512. delete pMesh;
  513. return;
  514. }
  515. avList.emplace_back(pMesh,a);
  516. }