Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

214 Zeilen
7.6 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 RemoveRedundantMaterials.cpp
  35. * @brief Implementation of the "RemoveRedundantMaterials" post processing step
  36. */
  37. // internal headers
  38. #include "AssimpPCH.h"
  39. #include "RemoveRedundantMaterials.h"
  40. #include "ParsingUtils.h"
  41. #include "ProcessHelper.h"
  42. #include "MaterialSystem.h"
  43. using namespace Assimp;
  44. // ------------------------------------------------------------------------------------------------
  45. // Constructor to be privately used by Importer
  46. RemoveRedundantMatsProcess::RemoveRedundantMatsProcess()
  47. {
  48. // nothing to do here
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. // Destructor, private as well
  52. RemoveRedundantMatsProcess::~RemoveRedundantMatsProcess()
  53. {
  54. // nothing to do here
  55. }
  56. // ------------------------------------------------------------------------------------------------
  57. // Returns whether the processing step is present in the given flag field.
  58. bool RemoveRedundantMatsProcess::IsActive( unsigned int pFlags) const
  59. {
  60. return (pFlags & aiProcess_RemoveRedundantMaterials) != 0;
  61. }
  62. // ------------------------------------------------------------------------------------------------
  63. // Setup import properties
  64. void RemoveRedundantMatsProcess::SetupProperties(const Importer* pImp)
  65. {
  66. // Get value of AI_CONFIG_PP_RRM_EXCLUDE_LIST
  67. configFixedMaterials = pImp->GetPropertyString(AI_CONFIG_PP_RRM_EXCLUDE_LIST,"");
  68. }
  69. // ------------------------------------------------------------------------------------------------
  70. // Executes the post processing step on the given imported data.
  71. void RemoveRedundantMatsProcess::Execute( aiScene* pScene)
  72. {
  73. DefaultLogger::get()->debug("RemoveRedundantMatsProcess begin");
  74. unsigned int redundantRemoved = 0, unreferencedRemoved = 0;
  75. if (pScene->mNumMaterials)
  76. {
  77. // Find out which materials are referenced by meshes
  78. std::vector<bool> abReferenced(pScene->mNumMaterials,false);
  79. for (unsigned int i = 0;i < pScene->mNumMeshes;++i)
  80. abReferenced[pScene->mMeshes[i]->mMaterialIndex] = true;
  81. // If a list of materials to be excluded was given, match the list with
  82. // our imported materials and 'salt' all positive matches to ensure that
  83. // we get unique hashes later.
  84. if (configFixedMaterials.length()) {
  85. std::list<std::string> strings;
  86. ConvertListToStrings(configFixedMaterials,strings);
  87. for (unsigned int i = 0; i < pScene->mNumMaterials;++i) {
  88. aiMaterial* mat = pScene->mMaterials[i];
  89. aiString name;
  90. mat->Get(AI_MATKEY_NAME,name);
  91. if (name.length) {
  92. std::list<std::string>::const_iterator it = std::find(strings.begin(), strings.end(), name.data);
  93. if (it != strings.end()) {
  94. // Our brilliant 'salt': A single material property with ~ as first
  95. // character to mark it as internal and temporary.
  96. const int dummy = 1;
  97. ((aiMaterial*)mat)->AddProperty(&dummy,1,"~RRM.UniqueMaterial",0,0);
  98. // Keep this material even if no mesh references it
  99. abReferenced[i] = true;
  100. DefaultLogger::get()->debug(std::string("Found positive match in exclusion list: \'") + name.data + "\'");
  101. }
  102. }
  103. }
  104. }
  105. // TODO: reimplement this algorithm to work in-place
  106. unsigned int* aiMappingTable = new unsigned int[pScene->mNumMaterials];
  107. unsigned int iNewNum = 0;
  108. // Iterate through all materials and calculate a hash for them
  109. // store all hashes in a list and so a quick search whether
  110. // we do already have a specific hash. This allows us to
  111. // determine which materials are identical.
  112. uint32_t* aiHashes;
  113. aiHashes = new uint32_t[pScene->mNumMaterials];
  114. for (unsigned int i = 0; i < pScene->mNumMaterials;++i)
  115. {
  116. // No mesh is referencing this material, remove it.
  117. if (!abReferenced[i]) {
  118. ++unreferencedRemoved;
  119. delete pScene->mMaterials[i];
  120. continue;
  121. }
  122. // Check all previously mapped materials for a matching hash.
  123. // On a match we can delete this material and just make it ref to the same index.
  124. uint32_t me = aiHashes[i] = ComputeMaterialHash(pScene->mMaterials[i]);
  125. for (unsigned int a = 0; a < i;++a)
  126. {
  127. if (abReferenced[a] && me == aiHashes[a]) {
  128. ++redundantRemoved;
  129. me = 0;
  130. aiMappingTable[i] = aiMappingTable[a];
  131. delete pScene->mMaterials[i];
  132. break;
  133. }
  134. }
  135. // This is a new material that is referenced, add to the map.
  136. if (me) {
  137. aiMappingTable[i] = iNewNum++;
  138. }
  139. }
  140. // If the new material count differs from the original,
  141. // we need to rebuild the material list and remap mesh material indexes.
  142. if (iNewNum != pScene->mNumMaterials) {
  143. aiMaterial** ppcMaterials = new aiMaterial*[iNewNum];
  144. ::memset(ppcMaterials,0,sizeof(void*)*iNewNum);
  145. for (unsigned int p = 0; p < pScene->mNumMaterials;++p)
  146. {
  147. // if the material is not referenced ... remove it
  148. if (!abReferenced[p]) {
  149. continue;
  150. }
  151. // generate new names for all modified materials
  152. const unsigned int idx = aiMappingTable[p];
  153. if (ppcMaterials[idx])
  154. {
  155. aiString sz;
  156. sz.length = ::sprintf(sz.data,"JoinedMaterial_#%i",p);
  157. ((aiMaterial*)ppcMaterials[idx])->AddProperty(&sz,AI_MATKEY_NAME);
  158. }
  159. else
  160. ppcMaterials[idx] = pScene->mMaterials[p];
  161. }
  162. // update all material indices
  163. for (unsigned int p = 0; p < pScene->mNumMeshes;++p) {
  164. aiMesh* mesh = pScene->mMeshes[p];
  165. mesh->mMaterialIndex = aiMappingTable[mesh->mMaterialIndex];
  166. }
  167. // delete the old material list
  168. delete[] pScene->mMaterials;
  169. pScene->mMaterials = ppcMaterials;
  170. pScene->mNumMaterials = iNewNum;
  171. }
  172. // delete temporary storage
  173. delete[] aiHashes;
  174. delete[] aiMappingTable;
  175. }
  176. if (redundantRemoved == 0 && unreferencedRemoved == 0)
  177. {
  178. DefaultLogger::get()->debug("RemoveRedundantMatsProcess finished ");
  179. }
  180. else
  181. {
  182. char szBuffer[128]; // should be sufficiently large
  183. ::sprintf(szBuffer,"RemoveRedundantMatsProcess finished. Removed %i redundant and %i unused materials.",
  184. redundantRemoved,unreferencedRemoved);
  185. DefaultLogger::get()->info(szBuffer);
  186. }
  187. }