Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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