BehaviorsMeshGroup.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/algorithm.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. #include <SceneAPI/SceneCore/Containers/Scene.h>
  12. #include <SceneAPI/SceneCore/Containers/SceneGraph.h>
  13. #include <SceneAPI/SceneCore/Containers/Utilities/Filters.h>
  14. #include <SceneAPI/SceneCore/Containers/Utilities/SceneGraphUtilities.h>
  15. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  16. #include <SceneAPI/SceneCore/Containers/Views/FilterIterator.h>
  17. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphChildIterator.h>
  18. #include <SceneAPI/SceneCore/DataTypes/GraphData/IMeshData.h>
  19. #include <SceneAPI/SceneCore/DataTypes/GraphData/IBoneData.h>
  20. #include <SceneAPI/SceneCore/DataTypes/DataTypeUtilities.h>
  21. #include <SceneAPI/SceneCore/Events/GraphMetaInfoBus.h>
  22. #include <SceneAPI/SceneCore/Utilities/SceneGraphSelector.h>
  23. #include <SceneAPI/SceneData/Groups/MeshGroup.h>
  24. #include <SceneAPI/SceneData/Behaviors/MeshGroup.h>
  25. namespace AZ
  26. {
  27. namespace SceneAPI
  28. {
  29. namespace Behaviors
  30. {
  31. void MeshGroup::Activate()
  32. {
  33. Events::ManifestMetaInfoBus::Handler::BusConnect();
  34. Events::AssetImportRequestBus::Handler::BusConnect();
  35. }
  36. void MeshGroup::Deactivate()
  37. {
  38. Events::AssetImportRequestBus::Handler::BusDisconnect();
  39. Events::ManifestMetaInfoBus::Handler::BusDisconnect();
  40. }
  41. void MeshGroup::Reflect(ReflectContext* context)
  42. {
  43. SerializeContext* serializeContext = azrtti_cast<SerializeContext*>(context);
  44. if (serializeContext)
  45. {
  46. serializeContext->Class<MeshGroup, BehaviorComponent>()->Version(1);
  47. }
  48. }
  49. void MeshGroup::GetCategoryAssignments(CategoryRegistrationList& categories, const Containers::Scene& scene)
  50. {
  51. if (SceneHasMeshGroup(scene) || Utilities::DoesSceneGraphContainDataLike<DataTypes::IMeshData>(scene, false))
  52. {
  53. categories.emplace_back("Meshes", SceneData::MeshGroup::TYPEINFO_Uuid(), s_meshGroupPreferredTabOrder);
  54. }
  55. }
  56. void MeshGroup::InitializeObject(const Containers::Scene& scene, DataTypes::IManifestObject& target)
  57. {
  58. if (!target.RTTI_IsTypeOf(SceneData::MeshGroup::TYPEINFO_Uuid()))
  59. {
  60. return;
  61. }
  62. SceneData::MeshGroup* group = azrtti_cast<SceneData::MeshGroup*>(&target);
  63. group->SetName(DataTypes::Utilities::CreateUniqueName<DataTypes::IMeshGroup>(scene.GetName(), scene.GetManifest()));
  64. Utilities::SceneGraphSelector::SelectAll(scene.GetGraph(), group->GetSceneNodeSelectionList());
  65. const Containers::SceneGraph& graph = scene.GetGraph();
  66. auto nameStorage = graph.GetNameStorage();
  67. auto contentStorage = graph.GetContentStorage();
  68. auto keyValueView = Containers::Views::MakePairView(nameStorage, contentStorage);
  69. auto filteredView = Containers::Views::MakeFilterView(keyValueView, Containers::DerivedTypeFilter<DataTypes::IMeshData>());
  70. for (auto it = filteredView.begin(); it != filteredView.end(); ++it)
  71. {
  72. Events::GraphMetaInfo::VirtualTypesSet types;
  73. auto keyValueIterator = it.GetBaseIterator();
  74. Containers::SceneGraph::NodeIndex index = graph.ConvertToNodeIndex(keyValueIterator.GetFirstIterator());
  75. EBUS_EVENT(Events::GraphMetaInfoBus, GetVirtualTypes, types, scene, index);
  76. if (!types.empty())
  77. {
  78. // Mesh is not a standard static mesh, but a special type so remove it from the selected list.
  79. group->GetSceneNodeSelectionList().RemoveSelectedNode(it->first.GetPath());
  80. }
  81. }
  82. }
  83. Events::ProcessingResult MeshGroup::UpdateManifest(Containers::Scene& scene, ManifestAction action,
  84. RequestingApplication /*requester*/)
  85. {
  86. if (action == ManifestAction::ConstructDefault)
  87. {
  88. return BuildDefault(scene);
  89. }
  90. else if (action == ManifestAction::Update)
  91. {
  92. return UpdateMeshGroups(scene);
  93. }
  94. else
  95. {
  96. return Events::ProcessingResult::Ignored;
  97. }
  98. }
  99. Events::ProcessingResult MeshGroup::BuildDefault(Containers::Scene& scene) const
  100. {
  101. if (SceneHasMeshGroup(scene) ||
  102. !Utilities::DoesSceneGraphContainDataLike<DataTypes::IMeshData>(scene, true))
  103. {
  104. return Events::ProcessingResult::Ignored;
  105. }
  106. // There are meshes but no mesh group, so add a default mesh group to the manifest.
  107. AZStd::shared_ptr<SceneData::MeshGroup> group = AZStd::make_shared<SceneData::MeshGroup>();
  108. // This is a group that's generated automatically so may not be saved to disk but would need to be recreated
  109. // in the same way again. To guarantee the same uuid, generate a stable one instead.
  110. group->OverrideId(DataTypes::Utilities::CreateStableUuid(scene, MeshGroup::TYPEINFO_Uuid()));
  111. EBUS_EVENT(Events::ManifestMetaInfoBus, InitializeObject, scene, *group);
  112. scene.GetManifest().AddEntry(AZStd::move(group));
  113. return Events::ProcessingResult::Success;
  114. }
  115. Events::ProcessingResult MeshGroup::UpdateMeshGroups(Containers::Scene& scene) const
  116. {
  117. bool updated = false;
  118. Containers::SceneManifest& manifest = scene.GetManifest();
  119. auto valueStorage = manifest.GetValueStorage();
  120. auto view = Containers::MakeDerivedFilterView<SceneData::MeshGroup>(valueStorage);
  121. for (SceneData::MeshGroup& group : view)
  122. {
  123. if (group.GetName().empty())
  124. {
  125. group.SetName(DataTypes::Utilities::CreateUniqueName<DataTypes::IMeshGroup>(scene.GetName(), scene.GetManifest()));
  126. }
  127. if (group.GetId().IsNull())
  128. {
  129. // When the uuid it's null is likely because the manifest has been updated from an older version. Include the
  130. // name of the group as there could be multiple groups.
  131. group.OverrideId(DataTypes::Utilities::CreateStableUuid(scene, MeshGroup::TYPEINFO_Uuid(), group.GetName()));
  132. }
  133. Utilities::SceneGraphSelector::UpdateNodeSelection(scene.GetGraph(), group.GetSceneNodeSelectionList());
  134. updated = true;
  135. }
  136. return updated ? Events::ProcessingResult::Success : Events::ProcessingResult::Ignored;
  137. }
  138. bool MeshGroup::SceneHasMeshGroup(const Containers::Scene& scene) const
  139. {
  140. const Containers::SceneManifest& manifest = scene.GetManifest();
  141. Containers::SceneManifest::ValueStorageConstData manifestData = manifest.GetValueStorage();
  142. auto meshGroup = AZStd::find_if(manifestData.begin(), manifestData.end(), Containers::DerivedTypeFilter<DataTypes::IMeshGroup>());
  143. return meshGroup != manifestData.end();
  144. }
  145. } // namespace Behaviors
  146. } // namespace SceneAPI
  147. } // namespace AZ