DeboneProcess.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 DeboneProcess.cpp
  34. /** Implementation of the DeboneProcess post processing step */
  35. // internal headers of the post-processing framework
  36. #include "ProcessHelper.h"
  37. #include "DeboneProcess.h"
  38. #include <stdio.h>
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // Constructor to be privately used by Importer
  42. DeboneProcess::DeboneProcess() : mNumBones(0), mNumBonesCanDoWithout(0), mThreshold(AI_DEBONE_THRESHOLD), mAllOrNone(false) {}
  43. // ------------------------------------------------------------------------------------------------
  44. // Returns whether the processing step is present in the given flag field.
  45. bool DeboneProcess::IsActive( unsigned int pFlags) const {
  46. return (pFlags & aiProcess_Debone) != 0;
  47. }
  48. // ------------------------------------------------------------------------------------------------
  49. // Executes the post processing step on the given imported data.
  50. void DeboneProcess::SetupProperties(const Importer* pImp) {
  51. // get the current value of the property
  52. mAllOrNone = pImp->GetPropertyInteger(AI_CONFIG_PP_DB_ALL_OR_NONE,0)?true:false;
  53. mThreshold = pImp->GetPropertyFloat(AI_CONFIG_PP_DB_THRESHOLD,AI_DEBONE_THRESHOLD);
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. // Executes the post processing step on the given imported data.
  57. void DeboneProcess::Execute( aiScene* pScene) {
  58. ASSIMP_LOG_DEBUG("DeboneProcess begin");
  59. if(!pScene->mNumMeshes) {
  60. return;
  61. }
  62. std::vector<bool> splitList(pScene->mNumMeshes);
  63. for( unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  64. splitList[a] = ConsiderMesh( pScene->mMeshes[a] );
  65. }
  66. int numSplits = 0;
  67. if(!!mNumBonesCanDoWithout && (!mAllOrNone||mNumBonesCanDoWithout==mNumBones)) {
  68. for(unsigned int a = 0; a < pScene->mNumMeshes; a++) {
  69. if(splitList[a]) {
  70. ++numSplits;
  71. }
  72. }
  73. }
  74. if(numSplits) {
  75. // we need to do something. Let's go.
  76. //mSubMeshIndices.clear(); // really needed?
  77. mSubMeshIndices.resize(pScene->mNumMeshes); // because we're doing it here anyway
  78. // build a new array of meshes for the scene
  79. std::vector<aiMesh*> meshes;
  80. for (unsigned int a=0;a<pScene->mNumMeshes; ++a) {
  81. aiMesh* srcMesh = pScene->mMeshes[a];
  82. std::vector<std::pair<aiMesh*,const aiBone*> > newMeshes;
  83. if(splitList[a]) {
  84. SplitMesh(srcMesh,newMeshes);
  85. }
  86. // mesh was split
  87. if(!newMeshes.empty()) {
  88. unsigned int out = 0, in = srcMesh->mNumBones;
  89. // store new meshes and indices of the new meshes
  90. for(unsigned int b=0;b<newMeshes.size();b++) {
  91. const aiString *find = newMeshes[b].second ? &newMeshes[b].second->mName : nullptr;
  92. aiNode *theNode = find ? pScene->mRootNode->FindNode(*find) : nullptr;
  93. std::pair<unsigned int,aiNode*> push_pair(static_cast<unsigned int>(meshes.size()),theNode);
  94. mSubMeshIndices[a].emplace_back(push_pair);
  95. meshes.emplace_back(newMeshes[b].first);
  96. out+=newMeshes[b].first->mNumBones;
  97. }
  98. if(!DefaultLogger::isNullLogger()) {
  99. ASSIMP_LOG_INFO("Removed %u bones. Input bones:", in - out, ". Output bones: ", out);
  100. }
  101. // and destroy the source mesh. It should be completely contained inside the new submeshes
  102. delete srcMesh;
  103. } else {
  104. // Mesh is kept unchanged - store it's new place in the mesh array
  105. mSubMeshIndices[a].emplace_back(static_cast<unsigned int>(meshes.size()), (aiNode *)nullptr);
  106. meshes.push_back(srcMesh);
  107. }
  108. }
  109. // rebuild the scene's mesh array
  110. pScene->mNumMeshes = static_cast<unsigned int>(meshes.size());
  111. delete [] pScene->mMeshes;
  112. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  113. std::copy( meshes.begin(), meshes.end(), pScene->mMeshes);
  114. // recurse through all nodes and translate the node's mesh indices to fit the new mesh array
  115. UpdateNode( pScene->mRootNode);
  116. }
  117. ASSIMP_LOG_DEBUG("DeboneProcess end");
  118. }
  119. // ------------------------------------------------------------------------------------------------
  120. // Counts bones total/removable in a given mesh.
  121. bool DeboneProcess::ConsiderMesh(const aiMesh* pMesh) {
  122. if(!pMesh->HasBones()) {
  123. return false;
  124. }
  125. bool split = false;
  126. //interstitial faces not permitted
  127. bool isInterstitialRequired = false;
  128. std::vector<bool> isBoneNecessary(pMesh->mNumBones,false);
  129. std::vector<unsigned int> vertexBones(pMesh->mNumVertices,UINT_MAX);
  130. const unsigned int cUnowned = UINT_MAX;
  131. const unsigned int cCoowned = UINT_MAX-1;
  132. for(unsigned int i=0;i<pMesh->mNumBones;i++) {
  133. for(unsigned int j=0;j<pMesh->mBones[i]->mNumWeights;j++) {
  134. float w = pMesh->mBones[i]->mWeights[j].mWeight;
  135. if (w == 0.0f) {
  136. continue;
  137. }
  138. unsigned int vid = pMesh->mBones[i]->mWeights[j].mVertexId;
  139. if (w >= mThreshold) {
  140. if (vertexBones[vid] != cUnowned) {
  141. //double entry
  142. if(vertexBones[vid]==i) {
  143. ASSIMP_LOG_WARN("Encountered double entry in bone weights");
  144. } else {
  145. //TODO: track attraction in order to break tie
  146. vertexBones[vid] = cCoowned;
  147. }
  148. } else {
  149. vertexBones[vid] = i;
  150. }
  151. }
  152. if(!isBoneNecessary[i]) {
  153. isBoneNecessary[i] = w<mThreshold;
  154. }
  155. }
  156. if(!isBoneNecessary[i]) {
  157. isInterstitialRequired = true;
  158. }
  159. }
  160. if(isInterstitialRequired) {
  161. for(unsigned int i=0;i<pMesh->mNumFaces;i++) {
  162. unsigned int v = vertexBones[pMesh->mFaces[i].mIndices[0]];
  163. for (unsigned int j=1;j<pMesh->mFaces[i].mNumIndices;j++) {
  164. unsigned int w = vertexBones[pMesh->mFaces[i].mIndices[j]];
  165. if (v != w) {
  166. if(v<pMesh->mNumBones) {
  167. isBoneNecessary[v] = true;
  168. }
  169. if (w<pMesh->mNumBones) {
  170. isBoneNecessary[w] = true;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. for(unsigned int i=0;i<pMesh->mNumBones;i++) {
  177. if(!isBoneNecessary[i]) {
  178. mNumBonesCanDoWithout++;
  179. split = true;
  180. }
  181. mNumBones++;
  182. }
  183. return split;
  184. }
  185. // ------------------------------------------------------------------------------------------------
  186. // Splits the given mesh by bone count.
  187. void DeboneProcess::SplitMesh( const aiMesh* pMesh, std::vector< std::pair< aiMesh*,const aiBone* > >& poNewMeshes) const {
  188. // same deal here as ConsiderMesh basically
  189. std::vector<bool> isBoneNecessary(pMesh->mNumBones,false);
  190. std::vector<unsigned int> vertexBones(pMesh->mNumVertices,UINT_MAX);
  191. const unsigned int cUnowned = UINT_MAX;
  192. const unsigned int cCoowned = UINT_MAX-1;
  193. for(unsigned int i=0;i<pMesh->mNumBones;i++) {
  194. for(unsigned int j=0;j<pMesh->mBones[i]->mNumWeights;j++) {
  195. float w = pMesh->mBones[i]->mWeights[j].mWeight;
  196. if(w==0.0f) {
  197. continue;
  198. }
  199. unsigned int vid = pMesh->mBones[i]->mWeights[j].mVertexId;
  200. if(w>=mThreshold) {
  201. if(vertexBones[vid]!=cUnowned) {
  202. if(vertexBones[vid]==i) //double entry
  203. {
  204. ASSIMP_LOG_WARN("Encountered double entry in bone weights");
  205. }
  206. else //TODO: track attraction in order to break tie
  207. {
  208. vertexBones[vid] = cCoowned;
  209. }
  210. }
  211. else vertexBones[vid] = i;
  212. }
  213. if(!isBoneNecessary[i]) {
  214. isBoneNecessary[i] = w<mThreshold;
  215. }
  216. }
  217. }
  218. unsigned int nFacesUnowned = 0;
  219. std::vector<unsigned int> faceBones(pMesh->mNumFaces,UINT_MAX);
  220. std::vector<unsigned int> facesPerBone(pMesh->mNumBones,0);
  221. for(unsigned int i=0;i<pMesh->mNumFaces;i++) {
  222. unsigned int nInterstitial = 1;
  223. unsigned int v = vertexBones[pMesh->mFaces[i].mIndices[0]];
  224. for(unsigned int j=1;j<pMesh->mFaces[i].mNumIndices;j++) {
  225. unsigned int w = vertexBones[pMesh->mFaces[i].mIndices[j]];
  226. if(v!=w) {
  227. if(v<pMesh->mNumBones) isBoneNecessary[v] = true;
  228. if(w<pMesh->mNumBones) isBoneNecessary[w] = true;
  229. }
  230. else nInterstitial++;
  231. }
  232. if(v<pMesh->mNumBones &&nInterstitial==pMesh->mFaces[i].mNumIndices) {
  233. faceBones[i] = v; //primitive belongs to bone #v
  234. facesPerBone[v]++;
  235. }
  236. else nFacesUnowned++;
  237. }
  238. // invalidate any "cojoined" faces
  239. for(unsigned int i=0;i<pMesh->mNumFaces;i++) {
  240. if(faceBones[i]<pMesh->mNumBones&&isBoneNecessary[faceBones[i]])
  241. {
  242. ai_assert(facesPerBone[faceBones[i]]>0);
  243. facesPerBone[faceBones[i]]--;
  244. nFacesUnowned++;
  245. faceBones[i] = cUnowned;
  246. }
  247. }
  248. if(nFacesUnowned) {
  249. std::vector<unsigned int> subFaces;
  250. for(unsigned int i=0;i<pMesh->mNumFaces;i++) {
  251. if(faceBones[i]==cUnowned) {
  252. subFaces.push_back(i);
  253. }
  254. }
  255. aiMesh *baseMesh = MakeSubmesh(pMesh,subFaces,0);
  256. std::pair<aiMesh *, const aiBone *> push_pair(baseMesh, (const aiBone *)nullptr);
  257. poNewMeshes.push_back(push_pair);
  258. }
  259. for(unsigned int i=0;i<pMesh->mNumBones;i++) {
  260. if(!isBoneNecessary[i]&&facesPerBone[i]>0) {
  261. std::vector<unsigned int> subFaces;
  262. for(unsigned int j=0;j<pMesh->mNumFaces;j++) {
  263. if(faceBones[j]==i) {
  264. subFaces.push_back(j);
  265. }
  266. }
  267. unsigned int f = AI_SUBMESH_FLAGS_SANS_BONES;
  268. aiMesh *subMesh =MakeSubmesh(pMesh,subFaces,f);
  269. //Lifted from PretransformVertices.cpp
  270. ApplyTransform(subMesh,pMesh->mBones[i]->mOffsetMatrix);
  271. std::pair<aiMesh*,const aiBone*> push_pair(subMesh,pMesh->mBones[i]);
  272. poNewMeshes.push_back(push_pair);
  273. }
  274. }
  275. }
  276. // ------------------------------------------------------------------------------------------------
  277. // Recursively updates the node's mesh list to account for the changed mesh list
  278. void DeboneProcess::UpdateNode(aiNode* pNode) const {
  279. // rebuild the node's mesh index list
  280. std::vector<unsigned int> newMeshList;
  281. // this will require two passes
  282. unsigned int m = static_cast<unsigned int>(pNode->mNumMeshes), n = static_cast<unsigned int>(mSubMeshIndices.size());
  283. // first pass, look for meshes which have not moved
  284. for(unsigned int a=0;a<m;a++) {
  285. unsigned int srcIndex = pNode->mMeshes[a];
  286. const std::vector< std::pair< unsigned int,aiNode* > > &subMeshes = mSubMeshIndices[srcIndex];
  287. unsigned int nSubmeshes = static_cast<unsigned int>(subMeshes.size());
  288. for(unsigned int b=0;b<nSubmeshes;b++) {
  289. if(!subMeshes[b].second) {
  290. newMeshList.push_back(subMeshes[b].first);
  291. }
  292. }
  293. }
  294. // second pass, collect deboned meshes
  295. for(unsigned int a=0;a<n;a++) {
  296. const std::vector< std::pair< unsigned int,aiNode* > > &subMeshes = mSubMeshIndices[a];
  297. unsigned int nSubmeshes = static_cast<unsigned int>(subMeshes.size());
  298. for(unsigned int b=0;b<nSubmeshes;b++) {
  299. if(subMeshes[b].second == pNode) {
  300. newMeshList.push_back(subMeshes[b].first);
  301. }
  302. }
  303. }
  304. if( pNode->mNumMeshes > 0 ) {
  305. delete[] pNode->mMeshes;
  306. pNode->mMeshes = nullptr;
  307. }
  308. pNode->mNumMeshes = static_cast<unsigned int>(newMeshList.size());
  309. if(pNode->mNumMeshes) {
  310. pNode->mMeshes = new unsigned int[pNode->mNumMeshes];
  311. std::copy( newMeshList.begin(), newMeshList.end(), pNode->mMeshes);
  312. }
  313. // do that also recursively for all children
  314. for( unsigned int a = 0; a < pNode->mNumChildren; ++a ) {
  315. UpdateNode( pNode->mChildren[a]);
  316. }
  317. }
  318. // ------------------------------------------------------------------------------------------------
  319. // Apply the node transformation to a mesh
  320. void DeboneProcess::ApplyTransform(aiMesh* mesh, const aiMatrix4x4& mat)const {
  321. // Check whether we need to transform the coordinates at all
  322. if (!mat.IsIdentity()) {
  323. if (mesh->HasPositions()) {
  324. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  325. mesh->mVertices[i] = mat * mesh->mVertices[i];
  326. }
  327. }
  328. if (mesh->HasNormals() || mesh->HasTangentsAndBitangents()) {
  329. aiMatrix4x4 mWorldIT = mat;
  330. mWorldIT.Inverse().Transpose();
  331. // TODO: implement Inverse() for aiMatrix3x3
  332. aiMatrix3x3 m = aiMatrix3x3(mWorldIT);
  333. if (mesh->HasNormals()) {
  334. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  335. mesh->mNormals[i] = (m * mesh->mNormals[i]).Normalize();
  336. }
  337. }
  338. if (mesh->HasTangentsAndBitangents()) {
  339. for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
  340. mesh->mTangents[i] = (m * mesh->mTangents[i]).Normalize();
  341. mesh->mBitangents[i] = (m * mesh->mBitangents[i]).Normalize();
  342. }
  343. }
  344. }
  345. }
  346. }