2
0

OptimizeMeshes.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file OptimizeMeshes.cpp
  35. * @brief Implementation of the aiProcess_OptimizeMeshes step
  36. */
  37. #ifndef ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS
  38. #include "OptimizeMeshes.h"
  39. #include "ProcessHelper.h"
  40. #include <assimp/SceneCombiner.h>
  41. #include <assimp/Exceptional.h>
  42. using namespace Assimp;
  43. static const unsigned int NotSet = 0xffffffff;
  44. static const unsigned int DeadBeef = 0xdeadbeef;
  45. // ------------------------------------------------------------------------------------------------
  46. // Constructor to be privately used by Importer
  47. OptimizeMeshesProcess::OptimizeMeshesProcess()
  48. : mScene()
  49. , pts(false)
  50. , max_verts( NotSet )
  51. , max_faces( NotSet ) {
  52. // empty
  53. }
  54. // ------------------------------------------------------------------------------------------------
  55. // Destructor, private as well
  56. OptimizeMeshesProcess::~OptimizeMeshesProcess() {
  57. // empty
  58. }
  59. // ------------------------------------------------------------------------------------------------
  60. // Returns whether the processing step is present in the given flag field.
  61. bool OptimizeMeshesProcess::IsActive( unsigned int pFlags) const
  62. {
  63. // Our behaviour needs to be different if the SortByPType or SplitLargeMeshes
  64. // steps are active. Thus we need to query their flags here and store the
  65. // information, although we're breaking const-correctness.
  66. // That's a serious design flaw, consider redesign.
  67. if( 0 != (pFlags & aiProcess_OptimizeMeshes) ) {
  68. pts = (0 != (pFlags & aiProcess_SortByPType));
  69. max_verts = ( 0 != ( pFlags & aiProcess_SplitLargeMeshes ) ) ? DeadBeef : max_verts;
  70. return true;
  71. }
  72. return false;
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // Setup properties for the post-processing step
  76. void OptimizeMeshesProcess::SetupProperties(const Importer* pImp)
  77. {
  78. if( max_verts == DeadBeef /* magic hack */ ) {
  79. max_faces = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
  80. max_verts = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  81. }
  82. }
  83. // ------------------------------------------------------------------------------------------------
  84. // Execute step
  85. void OptimizeMeshesProcess::Execute( aiScene* pScene)
  86. {
  87. const unsigned int num_old = pScene->mNumMeshes;
  88. if (num_old <= 1) {
  89. ASSIMP_LOG_DEBUG("Skipping OptimizeMeshesProcess");
  90. return;
  91. }
  92. ASSIMP_LOG_DEBUG("OptimizeMeshesProcess begin");
  93. mScene = pScene;
  94. // need to clear persistent members from previous runs
  95. merge_list.resize( 0 );
  96. output.resize( 0 );
  97. // ensure we have the right sizes
  98. merge_list.reserve(pScene->mNumMeshes);
  99. output.reserve(pScene->mNumMeshes);
  100. // Prepare lookup tables
  101. meshes.resize(pScene->mNumMeshes);
  102. FindInstancedMeshes(pScene->mRootNode);
  103. if( max_verts == DeadBeef ) /* undo the magic hack */
  104. max_verts = NotSet;
  105. // ... instanced meshes are immediately processed and added to the output list
  106. for (unsigned int i = 0, n = 0; i < pScene->mNumMeshes;++i) {
  107. meshes[i].vertex_format = GetMeshVFormatUnique(pScene->mMeshes[i]);
  108. if (meshes[i].instance_cnt > 1 && meshes[i].output_id == NotSet ) {
  109. meshes[i].output_id = n++;
  110. output.push_back(mScene->mMeshes[i]);
  111. }
  112. }
  113. // and process all nodes in the scenegraph recursively
  114. ProcessNode(pScene->mRootNode);
  115. if (!output.size()) {
  116. throw DeadlyImportError("OptimizeMeshes: No meshes remaining; there's definitely something wrong");
  117. }
  118. meshes.resize( 0 );
  119. ai_assert(output.size() <= num_old);
  120. mScene->mNumMeshes = static_cast<unsigned int>(output.size());
  121. std::copy(output.begin(),output.end(),mScene->mMeshes);
  122. if (output.size() != num_old) {
  123. ASSIMP_LOG_DEBUG_F("OptimizeMeshesProcess finished. Input meshes: ", num_old, ", Output meshes: ", pScene->mNumMeshes);
  124. } else {
  125. ASSIMP_LOG_DEBUG( "OptimizeMeshesProcess finished" );
  126. }
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. // Process meshes for a single node
  130. void OptimizeMeshesProcess::ProcessNode( aiNode* pNode)
  131. {
  132. for (unsigned int i = 0; i < pNode->mNumMeshes;++i) {
  133. unsigned int& im = pNode->mMeshes[i];
  134. if (meshes[im].instance_cnt > 1) {
  135. im = meshes[im].output_id;
  136. }
  137. else {
  138. merge_list.resize( 0 );
  139. unsigned int verts = 0, faces = 0;
  140. // Find meshes to merge with us
  141. for (unsigned int a = i+1; a < pNode->mNumMeshes;++a) {
  142. unsigned int am = pNode->mMeshes[a];
  143. if (meshes[am].instance_cnt == 1 && CanJoin(im,am,verts,faces)) {
  144. merge_list.push_back(mScene->mMeshes[am]);
  145. verts += mScene->mMeshes[am]->mNumVertices;
  146. faces += mScene->mMeshes[am]->mNumFaces;
  147. pNode->mMeshes[a] = pNode->mMeshes[pNode->mNumMeshes - 1];
  148. --pNode->mNumMeshes;
  149. --a;
  150. }
  151. }
  152. // and merge all meshes which we found, replace the old ones
  153. if (!merge_list.empty()) {
  154. merge_list.push_back(mScene->mMeshes[im]);
  155. aiMesh* out;
  156. SceneCombiner::MergeMeshes(&out,0,merge_list.begin(),merge_list.end());
  157. output.push_back(out);
  158. } else {
  159. output.push_back(mScene->mMeshes[im]);
  160. }
  161. im = static_cast<unsigned int>(output.size()-1);
  162. }
  163. }
  164. for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) {
  165. ProcessNode( pNode->mChildren[ i ] );
  166. }
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. // Check whether two meshes can be joined
  170. bool OptimizeMeshesProcess::CanJoin ( unsigned int a, unsigned int b, unsigned int verts, unsigned int faces )
  171. {
  172. if (meshes[a].vertex_format != meshes[b].vertex_format)
  173. return false;
  174. aiMesh* ma = mScene->mMeshes[a], *mb = mScene->mMeshes[b];
  175. if ((NotSet != max_verts && verts+mb->mNumVertices > max_verts) ||
  176. (NotSet != max_faces && faces+mb->mNumFaces > max_faces)) {
  177. return false;
  178. }
  179. // Never merge unskinned meshes with skinned meshes
  180. if (ma->mMaterialIndex != mb->mMaterialIndex || ma->HasBones() != mb->HasBones())
  181. return false;
  182. // Never merge meshes with different kinds of primitives if SortByPType did already
  183. // do its work. We would destroy everything again ...
  184. if (pts && ma->mPrimitiveTypes != mb->mPrimitiveTypes)
  185. return false;
  186. // If both meshes are skinned, check whether we have many bones defined in both meshes.
  187. // If yes, we can join them.
  188. if (ma->HasBones()) {
  189. // TODO
  190. return false;
  191. }
  192. return true;
  193. }
  194. // ------------------------------------------------------------------------------------------------
  195. // Build a LUT of all instanced meshes
  196. void OptimizeMeshesProcess::FindInstancedMeshes (aiNode* pNode)
  197. {
  198. for( unsigned int i = 0; i < pNode->mNumMeshes; ++i ) {
  199. ++meshes[ pNode->mMeshes[ i ] ].instance_cnt;
  200. }
  201. for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) {
  202. FindInstancedMeshes( pNode->mChildren[ i ] );
  203. }
  204. }
  205. // ------------------------------------------------------------------------------------------------
  206. #endif // !! ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS