SplitLargeMeshes.cpp 26 KB

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