BehaviorsMeshGroup.cpp 8.0 KB

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