DefaultProceduralPrefab.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #pragma once
  9. #include <AzCore/EBus/EBus.h>
  10. #include <PrefabGroup/PrefabGroup.h>
  11. #include <PrefabGroup/PrefabGroupBus.h>
  12. #include <SceneAPI/SceneCore/Containers/SceneGraph.h>
  13. #include <AzCore/Component/EntityId.h>
  14. #include <AzToolsFramework/Prefab/Instance/Instance.h>
  15. namespace AZ::SceneAPI
  16. {
  17. namespace SceneData
  18. {
  19. class MeshGroup;
  20. }
  21. using PrefabGroup = AZ::SceneAPI::SceneData::PrefabGroup;
  22. using Scene = AZ::SceneAPI::Containers::Scene;
  23. //! Handler for the Prefab Group event logic
  24. class DefaultProceduralPrefabGroup
  25. : protected PrefabGroupEventBus::Handler
  26. {
  27. public:
  28. AZ_RTTI(DefaultProceduralPrefabGroup, "{6BAAB306-01EE-42E8-AAFE-C9EE0BF4CFDF}");
  29. DefaultProceduralPrefabGroup();
  30. virtual ~DefaultProceduralPrefabGroup();
  31. static void Reflect(ReflectContext* context);
  32. // PrefabGroupEventBus::Handler
  33. AZStd::optional<ManifestUpdates> GeneratePrefabGroupManifestUpdates(const Scene& scene) const override;
  34. AZStd::vector<AZStd::shared_ptr<DataTypes::IManifestObject>> GenerateDefaultPrefabMeshGroups(const Scene& scene) const override;
  35. protected:
  36. // this stores the data related with nodes that will translate to entities in the prefab group
  37. struct NodeDataForEntity
  38. {
  39. Containers::SceneGraph::NodeIndex m_meshIndex = {};
  40. Containers::SceneGraph::NodeIndex m_transformIndex = {};
  41. Containers::SceneGraph::NodeIndex m_propertyMapIndex = {};
  42. };
  43. using NodeDataMapEntry = AZStd::pair<Containers::SceneGraph::NodeIndex, NodeDataForEntity>;
  44. using NodeDataMap = AZStd::unordered_map<Containers::SceneGraph::NodeIndex, NodeDataForEntity>; // NodeIndex -> NodeDataForEntity
  45. using ManifestUpdates = AZStd::vector<AZStd::shared_ptr<DataTypes::IManifestObject>>;
  46. using NodeEntityMap = AZStd::unordered_map<Containers::SceneGraph::NodeIndex, AZStd::pair<AZ::EntityId, AzToolsFramework::Prefab::EntityAlias>>;
  47. using EntityIdMap = AZStd::unordered_map<AZ::EntityId, AzToolsFramework::Prefab::EntityAlias>;
  48. AZStd::shared_ptr<SceneData::MeshGroup> BuildMeshGroupForNode(
  49. const Scene& scene,
  50. const NodeDataForEntity& nodeData,
  51. const NodeDataMap& nodeDataMap) const;
  52. NodeDataMap CalculateNodeDataMap(const Containers::Scene& scene) const;
  53. bool AddEditorMaterialComponent(
  54. const AZ::EntityId& entityId,
  55. const DataTypes::ICustomPropertyData& propertyData) const;
  56. bool AddEditorMeshComponent(
  57. const AZ::EntityId& entityId,
  58. const AZStd::string& relativeSourcePath,
  59. const AZStd::string& meshGroupName,
  60. const AZStd::string& sourceFileExtension) const;
  61. bool CreateMeshGroupAndComponents(
  62. ManifestUpdates& manifestUpdates,
  63. AZ::EntityId entityId,
  64. const NodeDataForEntity& nodeData,
  65. const NodeDataMap& nodeDataMap,
  66. const Containers::Scene& scene,
  67. const AZStd::string& relativeSourcePath) const;
  68. NodeEntityMap CreateNodeEntityMap(
  69. ManifestUpdates& manifestUpdates,
  70. const NodeDataMap& nodeDataMap,
  71. const Containers::Scene& scene,
  72. const AZStd::string& relativeSourcePath) const;
  73. EntityIdMap FixUpEntityParenting(
  74. const NodeEntityMap& nodeEntityMap,
  75. const Containers::SceneGraph& graph,
  76. const NodeDataMap& nodeDataMap) const;
  77. bool CreatePrefabGroupManifestUpdates(
  78. ManifestUpdates& manifestUpdates,
  79. const Containers::Scene& scene,
  80. const EntityIdMap& entities,
  81. const AZStd::string& filenameOnly,
  82. const AZStd::string& relativeSourcePath) const;
  83. };
  84. }
  85. namespace AZStd
  86. {
  87. template<>
  88. struct hash<AZ::SceneAPI::Containers::SceneGraph::NodeIndex>
  89. {
  90. inline size_t operator()(const AZ::SceneAPI::Containers::SceneGraph::NodeIndex& nodeIndex) const
  91. {
  92. size_t hashValue{ 0 };
  93. hash_combine(hashValue, nodeIndex.AsNumber());
  94. return hashValue;
  95. }
  96. };
  97. template<>
  98. struct hash<AZ::SceneAPI::PrefabGroupRequests::ManifestUpdates>
  99. {
  100. inline size_t operator()(const AZ::SceneAPI::PrefabGroupRequests::ManifestUpdates& updates) const
  101. {
  102. size_t hashValue{ 0 };
  103. hash_combine(hashValue, updates.size());
  104. return hashValue;
  105. }
  106. };
  107. }