AssImpMaterialImporter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <API/EditorAssetSystemAPI.h>
  9. #include <SceneAPI/SceneBuilder/Importers/AssImpMaterialImporter.h>
  10. #include <AzCore/IO/FileIO.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/std/smart_ptr/make_shared.h>
  13. #include <AzCore/std/containers/unordered_map.h>
  14. #include <AzCore/StringFunc/StringFunc.h>
  15. #include <AzToolsFramework/Debug/TraceContext.h>
  16. #include <SceneAPI/SceneBuilder/Importers/ImporterUtilities.h>
  17. #include <SceneAPI/SceneBuilder/Importers/Utilities/AssImpMeshImporterUtilities.h>
  18. #include <SceneAPI/SceneBuilder/Importers/Utilities/RenamedNodesMap.h>
  19. #include <SceneAPI/SDKWrapper/AssImpNodeWrapper.h>
  20. #include <SceneAPI/SDKWrapper/AssImpSceneWrapper.h>
  21. #include <SceneAPI/SDKWrapper/AssImpMaterialWrapper.h>
  22. #include <SceneAPI/SceneData/GraphData/MaterialData.h>
  23. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  24. #include <SceneAPI/SceneCore/Events/ProcessingResult.h>
  25. #include <assimp/scene.h>
  26. namespace AZ
  27. {
  28. namespace SceneAPI
  29. {
  30. namespace SceneBuilder
  31. {
  32. AssImpMaterialImporter::AssImpMaterialImporter()
  33. {
  34. BindToCall(&AssImpMaterialImporter::ImportMaterials);
  35. }
  36. void AssImpMaterialImporter::Reflect(ReflectContext* context)
  37. {
  38. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  39. if (serializeContext)
  40. {
  41. serializeContext->Class<AssImpMaterialImporter, SceneCore::LoadingComponent>()->Version(2);
  42. }
  43. }
  44. Events::ProcessingResult AssImpMaterialImporter::ImportMaterials(AssImpSceneNodeAppendedContext& context)
  45. {
  46. AZ_TraceContext("Importer", "Material");
  47. if (!context.m_sourceNode.ContainsMesh())
  48. {
  49. return Events::ProcessingResult::Ignored;
  50. }
  51. Events::ProcessingResultCombiner combinedMaterialImportResults;
  52. AZStd::unordered_map<int, AZStd::shared_ptr<SceneData::GraphData::MaterialData>> materialMap;
  53. for (unsigned int idx = 0; idx < context.m_sourceNode.GetAssImpNode()->mNumMeshes; ++idx)
  54. {
  55. int meshIndex = context.m_sourceNode.GetAssImpNode()->mMeshes[idx];
  56. const aiMesh* assImpMesh = context.m_sourceScene.GetAssImpScene()->mMeshes[meshIndex];
  57. AZ_Assert(assImpMesh, "Asset Importer Mesh should not be null.");
  58. int materialIndex = assImpMesh->mMaterialIndex;
  59. AZ_TraceContext("Material Index", materialIndex);
  60. auto matFound = materialMap.find(materialIndex);
  61. AZStd::shared_ptr<SceneData::GraphData::MaterialData> material;
  62. AZStd::string materialName;
  63. if (matFound == materialMap.end())
  64. {
  65. std::shared_ptr<AssImpSDKWrapper::AssImpMaterialWrapper> assImpMaterial =
  66. std::shared_ptr<AssImpSDKWrapper::AssImpMaterialWrapper>(new AssImpSDKWrapper::AssImpMaterialWrapper(context.m_sourceScene.GetAssImpScene()->mMaterials[materialIndex]));
  67. materialName = assImpMaterial->GetName().c_str();
  68. RenamedNodesMap::SanitizeNodeName(materialName, context.m_scene.GetGraph(), context.m_currentGraphPosition, "Material");
  69. AZ_TraceContext("Material Name", materialName);
  70. material = AZStd::make_shared<SceneData::GraphData::MaterialData>();
  71. material->SetMaterialName(assImpMaterial->GetName());
  72. material->SetTexture(DataTypes::IMaterialData::TextureMapType::Diffuse,
  73. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  74. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::Diffuse)).c_str());
  75. material->SetTexture(DataTypes::IMaterialData::TextureMapType::Specular,
  76. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  77. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::Specular)).c_str());
  78. material->SetTexture(DataTypes::IMaterialData::TextureMapType::Bump,
  79. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  80. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::Bump)).c_str());
  81. material->SetTexture(DataTypes::IMaterialData::TextureMapType::Normal,
  82. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  83. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::Normal)).c_str());
  84. material->SetUniqueId(assImpMaterial->GetUniqueId());
  85. material->SetDiffuseColor(assImpMaterial->GetDiffuseColor());
  86. material->SetSpecularColor(assImpMaterial->GetSpecularColor());
  87. material->SetEmissiveColor(assImpMaterial->GetEmissiveColor());
  88. material->SetShininess(assImpMaterial->GetShininess());
  89. material->SetOpacity(assImpMaterial->GetOpacity());
  90. material->SetUseColorMap(assImpMaterial->GetUseColorMap());
  91. material->SetBaseColor(assImpMaterial->GetBaseColor());
  92. material->SetUseMetallicMap(assImpMaterial->GetUseMetallicMap());
  93. material->SetMetallicFactor(assImpMaterial->GetMetallicFactor());
  94. material->SetTexture(
  95. DataTypes::IMaterialData::TextureMapType::Metallic,
  96. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  97. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::Metallic).c_str()));
  98. material->SetUseRoughnessMap(assImpMaterial->GetUseRoughnessMap());
  99. material->SetRoughnessFactor(assImpMaterial->GetRoughnessFactor());
  100. material->SetTexture(
  101. DataTypes::IMaterialData::TextureMapType::Roughness,
  102. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  103. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::Roughness).c_str()));
  104. material->SetUseEmissiveMap(assImpMaterial->GetUseEmissiveMap());
  105. material->SetEmissiveIntensity(assImpMaterial->GetEmissiveIntensity());
  106. material->SetUseAOMap(assImpMaterial->GetUseAOMap());
  107. material->SetTexture(
  108. DataTypes::IMaterialData::TextureMapType::AmbientOcclusion,
  109. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  110. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::AmbientOcclusion).c_str()));
  111. material->SetTexture(
  112. DataTypes::IMaterialData::TextureMapType::Emissive,
  113. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  114. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::Emissive).c_str()));
  115. material->SetTexture(
  116. DataTypes::IMaterialData::TextureMapType::BaseColor,
  117. ResolveTexturePath(materialName, context.m_sourceScene.GetSceneFileName(),
  118. assImpMaterial->GetTextureFileName(SDKMaterial::MaterialWrapper::MaterialMapType::BaseColor).c_str()));
  119. AZ_Assert(material, "Failed to allocate scene material data.");
  120. if (!material)
  121. {
  122. combinedMaterialImportResults += Events::ProcessingResult::Failure;
  123. continue;
  124. }
  125. materialMap[materialIndex] = material;
  126. Events::ProcessingResult materialResult;
  127. Containers::SceneGraph::NodeIndex newIndex =
  128. context.m_scene.GetGraph().AddChild(context.m_currentGraphPosition, materialName.c_str());
  129. AZ_Assert(newIndex.IsValid(), "Failed to create SceneGraph node for attribute.");
  130. if (!newIndex.IsValid())
  131. {
  132. combinedMaterialImportResults += Events::ProcessingResult::Failure;
  133. continue;
  134. }
  135. AssImpSceneAttributeDataPopulatedContext dataPopulated(context, material, newIndex, materialName);
  136. materialResult = Events::Process(dataPopulated);
  137. if (materialResult != Events::ProcessingResult::Failure)
  138. {
  139. materialResult = SceneAPI::SceneBuilder::AddAttributeDataNodeWithContexts(dataPopulated);
  140. }
  141. combinedMaterialImportResults += materialResult;
  142. }
  143. else
  144. {
  145. material = matFound->second;
  146. materialName = material.get()->GetMaterialName();
  147. AZ_Info(AZ::SceneAPI::Utilities::LogWindow, "Duplicate material references to %s from node %s",
  148. materialName.c_str(), context.m_sourceNode.GetName());
  149. }
  150. }
  151. return combinedMaterialImportResults.GetResult();
  152. }
  153. AZStd::string AssImpMaterialImporter::ResolveTexturePath([[maybe_unused]] const AZStd::string& materialName, const AZStd::string& sceneFilePath, const AZStd::string& textureFilePath) const
  154. {
  155. if (textureFilePath.empty())
  156. {
  157. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow, "Material %.*s has no associated texture.", AZ_STRING_ARG(materialName));
  158. return textureFilePath;
  159. }
  160. AZStd::string cleanedUpSceneFilePath(sceneFilePath);
  161. AZ::StringFunc::Path::StripFullName(cleanedUpSceneFilePath);
  162. AZ::IO::PathView textureFilePathView(textureFilePath.c_str());
  163. if (textureFilePathView.IsAbsolute())
  164. {
  165. // Don't try to resolve the absolute path of the texture relative to the scene file,
  166. // because it may resolve differently on different machines if the path happens to be
  167. // correct on one person's machine but not another.
  168. // Example: Texture is at C:\path\to\MyProject\assets\MyTexture.tga.
  169. // On one person's machine, if their scene file is in that folder, example C:\path\to\MyProject\assets\MyFBX.fbx
  170. // then comparing the path of the scene file and texture might properly resolve here.
  171. // However, if a second person on the team's machine has the project at D:\path2\to2\MyProject,
  172. // the same logic won't resolve.
  173. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow,
  174. "Material %.*s has a texture with absolute path '%.*s'. This path will not be resolved, this texture will not be found or used by this material.",
  175. AZ_STRING_ARG(materialName), AZ_STRING_ARG(textureFilePath));
  176. return textureFilePath;
  177. }
  178. AZStd::string texturePathRelativeToScene;
  179. AZ::StringFunc::Path::Join(cleanedUpSceneFilePath.c_str(), textureFilePath.c_str(), texturePathRelativeToScene);
  180. // If the texture did start with marker to change directories upward, then it's relative to the scene file,
  181. // and that path needs to be resolved. It can't be resolved later. This was handled internally by FBX SDK,
  182. // but is not handled by AssImp.
  183. if (textureFilePath.starts_with(".."))
  184. {
  185. // Not checking for the file existing because it may not be there yet.
  186. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow,
  187. "Material %.*s has a texture '%.*s' with a directory change marker. This may not resolve correctly, the texture may not be found or used by this material.",
  188. AZ_STRING_ARG(materialName), AZ_STRING_ARG(textureFilePath));
  189. return texturePathRelativeToScene;
  190. }
  191. bool generatedRelativeSourcePath = false;
  192. AZStd::string relativePath, rootPath;
  193. AzToolsFramework::AssetSystemRequestBus::BroadcastResult(
  194. generatedRelativeSourcePath, &AzToolsFramework::AssetSystemRequestBus::Events::GenerateRelativeSourcePath,
  195. texturePathRelativeToScene.c_str(), relativePath, rootPath);
  196. // The engine only supports relative paths to scan folders.
  197. // Scene files may have paths to textures, relative to the scene file.
  198. // Try to use a scanfolder relative path instead
  199. if (generatedRelativeSourcePath)
  200. {
  201. return relativePath;
  202. }
  203. return textureFilePath;
  204. }
  205. } // namespace SceneBuilder
  206. } // namespace SceneAPI
  207. } // namespace AZ