AssImpTangentStreamImporter.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <AzCore/Serialization/SerializeContext.h>
  9. #include <AzCore/std/numeric.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <AzToolsFramework/Debug/TraceContext.h>
  12. #include <SceneAPI/SceneBuilder/Importers/AssImpTangentStreamImporter.h>
  13. #include <SceneAPI/SceneBuilder/Importers/ImporterUtilities.h>
  14. #include <SceneAPI/SceneBuilder/Importers/Utilities/AssImpMeshImporterUtilities.h>
  15. #include <SceneAPI/SceneData/GraphData/MeshVertexTangentData.h>
  16. #include <SceneAPI/SDKWrapper/AssImpNodeWrapper.h>
  17. #include <SceneAPI/SDKWrapper/AssImpSceneWrapper.h>
  18. #include <SceneAPI/SDKWrapper/AssImpTypeConverter.h>
  19. #include <SceneAPI/SceneData/GraphData/MeshData.h>
  20. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  21. #include <assimp/scene.h>
  22. #include <assimp/mesh.h>
  23. namespace AZ
  24. {
  25. namespace SceneAPI
  26. {
  27. namespace SceneBuilder
  28. {
  29. const char* AssImpTangentStreamImporter::m_defaultNodeName = "Tangent";
  30. AssImpTangentStreamImporter::AssImpTangentStreamImporter()
  31. {
  32. BindToCall(&AssImpTangentStreamImporter::ImportTangentStreams);
  33. }
  34. void AssImpTangentStreamImporter::Reflect(ReflectContext* context)
  35. {
  36. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  37. if (serializeContext)
  38. {
  39. serializeContext->Class<AssImpTangentStreamImporter, SceneCore::LoadingComponent>()->Version(3); // LYN-3250
  40. }
  41. }
  42. Events::ProcessingResult AssImpTangentStreamImporter::ImportTangentStreams(AssImpSceneNodeAppendedContext& context)
  43. {
  44. AZ_TraceContext("Importer", m_defaultNodeName);
  45. if (!context.m_sourceNode.ContainsMesh())
  46. {
  47. return Events::ProcessingResult::Ignored;
  48. }
  49. const aiNode* currentNode = context.m_sourceNode.GetAssImpNode();
  50. const aiScene* scene = context.m_sourceScene.GetAssImpScene();
  51. const auto meshHasTangentsAndBitangents = [&scene](const unsigned int meshIndex)
  52. {
  53. return scene->mMeshes[meshIndex]->HasTangentsAndBitangents();
  54. };
  55. // If there are no tangents on any meshes, there's nothing to import in this function.
  56. const bool anyMeshHasTangentsAndBitangents = AZStd::any_of(currentNode->mMeshes, currentNode->mMeshes + currentNode->mNumMeshes, meshHasTangentsAndBitangents);
  57. if (!anyMeshHasTangentsAndBitangents)
  58. {
  59. return Events::ProcessingResult::Ignored;
  60. }
  61. // AssImp nodes with multiple meshes on them occur when AssImp split a mesh on material.
  62. // This logic recombines those meshes to minimize the changes needed to replace FBX SDK with AssImp, FBX SDK did not separate meshes,
  63. // and the engine has code to do this later.
  64. const bool allMeshesHaveTangentsAndBitangents = AZStd::all_of(currentNode->mMeshes, currentNode->mMeshes + currentNode->mNumMeshes, meshHasTangentsAndBitangents);
  65. if (!allMeshesHaveTangentsAndBitangents)
  66. {
  67. AZ_Error(
  68. Utilities::ErrorWindow, false,
  69. "Node with name %s has meshes with and without tangents. "
  70. "Placeholder incorrect tangents will be generated to allow the data to process, "
  71. "but the source art needs to be fixed to correct this. Either apply tangents to all meshes on this node, "
  72. "or remove all tangents from all meshes on this node.",
  73. currentNode->mName.C_Str());
  74. }
  75. const uint64_t vertexCount = GetVertexCountForAllMeshesOnNode(*currentNode, *scene);
  76. AZStd::shared_ptr<SceneData::GraphData::MeshVertexTangentData> tangentStream =
  77. AZStd::make_shared<AZ::SceneData::GraphData::MeshVertexTangentData>();
  78. // AssImp only has one tangentStream per mesh.
  79. tangentStream->SetTangentSetIndex(0);
  80. tangentStream->SetGenerationMethod(AZ::SceneAPI::DataTypes::TangentGenerationMethod::FromSourceScene);
  81. tangentStream->ReserveContainerSpace(vertexCount);
  82. for (unsigned int sdkMeshIndex = 0; sdkMeshIndex < currentNode->mNumMeshes; ++sdkMeshIndex)
  83. {
  84. const aiMesh* mesh = scene->mMeshes[currentNode->mMeshes[sdkMeshIndex]];
  85. for (unsigned int v = 0; v < mesh->mNumVertices; ++v)
  86. {
  87. if (!mesh->HasTangentsAndBitangents())
  88. {
  89. // This node has mixed meshes with and without tangents.
  90. // An error was already thrown above. Output stub tangents so
  91. // the mesh can still be output in some form, even if the data isn't correct.
  92. // The tangent count needs to match the vertex count on the associated mesh node.
  93. tangentStream->AppendTangent(Vector4(0.f, 1.f, 0.f, 1.f));
  94. }
  95. else
  96. {
  97. const Vector4 tangent(
  98. AssImpSDKWrapper::AssImpTypeConverter::ToVector3(mesh->mTangents[v]));
  99. tangentStream->AppendTangent(tangent);
  100. }
  101. }
  102. }
  103. Containers::SceneGraph::NodeIndex newIndex =
  104. context.m_scene.GetGraph().AddChild(context.m_currentGraphPosition, m_defaultNodeName);
  105. Events::ProcessingResult tangentResults;
  106. AssImpSceneAttributeDataPopulatedContext dataPopulated(context, tangentStream, newIndex, m_defaultNodeName);
  107. tangentResults = Events::Process(dataPopulated);
  108. if (tangentResults != Events::ProcessingResult::Failure)
  109. {
  110. tangentResults = AddAttributeDataNodeWithContexts(dataPopulated);
  111. }
  112. return tangentResults;
  113. }
  114. } // namespace SceneBuilder
  115. } // namespace SceneAPI
  116. } // namespace AZ