OptimizeMeshes.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2022, 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() = default;
  57. // ------------------------------------------------------------------------------------------------
  58. // Returns whether the processing step is present in the given flag field.
  59. bool OptimizeMeshesProcess::IsActive( unsigned int pFlags) const
  60. {
  61. // Our behaviour needs to be different if the SortByPType or SplitLargeMeshes
  62. // steps are active. Thus we need to query their flags here and store the
  63. // information, although we're breaking const-correctness.
  64. // That's a serious design flaw, consider redesign.
  65. if( 0 != (pFlags & aiProcess_OptimizeMeshes) ) {
  66. pts = (0 != (pFlags & aiProcess_SortByPType));
  67. max_verts = ( 0 != ( pFlags & aiProcess_SplitLargeMeshes ) ) ? DeadBeef : max_verts;
  68. return true;
  69. }
  70. return false;
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. // Setup properties for the post-processing step
  74. void OptimizeMeshesProcess::SetupProperties(const Importer* pImp)
  75. {
  76. if( max_verts == DeadBeef /* magic hack */ ) {
  77. max_faces = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
  78. max_verts = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
  79. }
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. // Execute step
  83. void OptimizeMeshesProcess::Execute( aiScene* pScene)
  84. {
  85. const unsigned int num_old = pScene->mNumMeshes;
  86. if (num_old <= 1) {
  87. ASSIMP_LOG_DEBUG("Skipping OptimizeMeshesProcess");
  88. return;
  89. }
  90. ASSIMP_LOG_DEBUG("OptimizeMeshesProcess begin");
  91. mScene = pScene;
  92. // need to clear persistent members from previous runs
  93. merge_list.resize( 0 );
  94. output.resize( 0 );
  95. // ensure we have the right sizes
  96. merge_list.reserve(pScene->mNumMeshes);
  97. output.reserve(pScene->mNumMeshes);
  98. // Prepare lookup tables
  99. meshes.resize(pScene->mNumMeshes);
  100. FindInstancedMeshes(pScene->mRootNode);
  101. if( max_verts == DeadBeef ) /* undo the magic hack */
  102. max_verts = NotSet;
  103. // ... instanced meshes are immediately processed and added to the output list
  104. for (unsigned int i = 0, n = 0; i < pScene->mNumMeshes;++i) {
  105. meshes[i].vertex_format = GetMeshVFormatUnique(pScene->mMeshes[i]);
  106. if (meshes[i].instance_cnt > 1 && meshes[i].output_id == NotSet ) {
  107. meshes[i].output_id = n++;
  108. output.push_back(mScene->mMeshes[i]);
  109. }
  110. }
  111. // and process all nodes in the scenegraph recursively
  112. ProcessNode(pScene->mRootNode);
  113. if (!output.size()) {
  114. throw DeadlyImportError("OptimizeMeshes: No meshes remaining; there's definitely something wrong");
  115. }
  116. meshes.resize( 0 );
  117. ai_assert(output.size() <= num_old);
  118. mScene->mNumMeshes = static_cast<unsigned int>(output.size());
  119. std::copy(output.begin(),output.end(),mScene->mMeshes);
  120. if (output.size() != num_old) {
  121. ASSIMP_LOG_DEBUG("OptimizeMeshesProcess finished. Input meshes: ", num_old, ", Output meshes: ", pScene->mNumMeshes);
  122. } else {
  123. ASSIMP_LOG_DEBUG( "OptimizeMeshesProcess finished" );
  124. }
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. // Process meshes for a single node
  128. void OptimizeMeshesProcess::ProcessNode( aiNode* pNode)
  129. {
  130. for (unsigned int i = 0; i < pNode->mNumMeshes;++i) {
  131. unsigned int& im = pNode->mMeshes[i];
  132. if (meshes[im].instance_cnt > 1) {
  133. im = meshes[im].output_id;
  134. }
  135. else {
  136. merge_list.resize( 0 );
  137. unsigned int verts = 0, faces = 0;
  138. // Find meshes to merge with us
  139. for (unsigned int a = i+1; a < pNode->mNumMeshes;++a) {
  140. unsigned int am = pNode->mMeshes[a];
  141. if (meshes[am].instance_cnt == 1 && CanJoin(im,am,verts,faces)) {
  142. merge_list.push_back(mScene->mMeshes[am]);
  143. verts += mScene->mMeshes[am]->mNumVertices;
  144. faces += mScene->mMeshes[am]->mNumFaces;
  145. pNode->mMeshes[a] = pNode->mMeshes[pNode->mNumMeshes - 1];
  146. --pNode->mNumMeshes;
  147. --a;
  148. }
  149. }
  150. // and merge all meshes which we found, replace the old ones
  151. if (!merge_list.empty()) {
  152. merge_list.push_back(mScene->mMeshes[im]);
  153. aiMesh* out;
  154. SceneCombiner::MergeMeshes(&out,0,merge_list.begin(),merge_list.end());
  155. output.push_back(out);
  156. } else {
  157. output.push_back(mScene->mMeshes[im]);
  158. }
  159. im = static_cast<unsigned int>(output.size()-1);
  160. }
  161. }
  162. for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) {
  163. ProcessNode( pNode->mChildren[ i ] );
  164. }
  165. }
  166. // ------------------------------------------------------------------------------------------------
  167. // Check whether two meshes can be joined
  168. bool OptimizeMeshesProcess::CanJoin ( unsigned int a, unsigned int b, unsigned int verts, unsigned int faces )
  169. {
  170. if (meshes[a].vertex_format != meshes[b].vertex_format)
  171. return false;
  172. aiMesh* ma = mScene->mMeshes[a], *mb = mScene->mMeshes[b];
  173. if ((NotSet != max_verts && verts+mb->mNumVertices > max_verts) ||
  174. (NotSet != max_faces && faces+mb->mNumFaces > max_faces)) {
  175. return false;
  176. }
  177. // Never merge unskinned meshes with skinned meshes
  178. if (ma->mMaterialIndex != mb->mMaterialIndex || ma->HasBones() != mb->HasBones())
  179. return false;
  180. // Never merge meshes with different kinds of primitives if SortByPType did already
  181. // do its work. We would destroy everything again ...
  182. if (pts && ma->mPrimitiveTypes != mb->mPrimitiveTypes)
  183. return false;
  184. // If both meshes are skinned, check whether we have many bones defined in both meshes.
  185. // If yes, we can join them.
  186. if (ma->HasBones()) {
  187. // TODO
  188. return false;
  189. }
  190. return true;
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. // Build a LUT of all instanced meshes
  194. void OptimizeMeshesProcess::FindInstancedMeshes (aiNode* pNode)
  195. {
  196. for( unsigned int i = 0; i < pNode->mNumMeshes; ++i ) {
  197. ++meshes[ pNode->mMeshes[ i ] ].instance_cnt;
  198. }
  199. for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) {
  200. FindInstancedMeshes( pNode->mChildren[ i ] );
  201. }
  202. }
  203. // ------------------------------------------------------------------------------------------------
  204. #endif // !! ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS