EditorMeshComponent.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <Mesh/EditorMeshComponent.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  11. #include <AzToolsFramework/Entity/EditorEntityInfoBus.h>
  12. #include <AzToolsFramework/API/EntityCompositionRequestBus.h>
  13. #include <AtomLyIntegration/CommonFeatures/Material/MaterialComponentConstants.h>
  14. namespace AZ
  15. {
  16. namespace Render
  17. {
  18. void EditorMeshComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. BaseClass::Reflect(context);
  21. EditorMeshStats::Reflect(context);
  22. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->RegisterGenericType<EditorMeshStats>();
  25. serializeContext->Class<EditorMeshComponent, BaseClass>()
  26. ->Version(2, ConvertToEditorRenderComponentAdapter<1>)
  27. ->Field("meshStats", &EditorMeshComponent::m_stats)
  28. ;
  29. // This shouldn't be registered here, but is required to make a vector from EditorMeshComponentTypeId. This can be removed when one of the following happens:
  30. // - The generic type for AZStd::vector<AZ::Uuid> is registered in a more generic place
  31. // - EditorLevelComponentAPIComponent has a version of AddComponentsOfType that takes a single Uuid instead of a vector
  32. serializeContext->RegisterGenericType<AZStd::vector<AZ::Uuid>>();
  33. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  34. {
  35. editContext->Class<EditorMeshComponent>(
  36. "Mesh", "The mesh component is the primary method of adding visual geometry to entities")
  37. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  38. ->Attribute(AZ::Edit::Attributes::Category, "Graphics/Mesh")
  39. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/Mesh.svg")
  40. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/Mesh.svg")
  41. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  42. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  43. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/atom/mesh/")
  44. ->Attribute(AZ::Edit::Attributes::PrimaryAssetType, AZ::AzTypeInfo<RPI::ModelAsset>::Uuid())
  45. ->UIElement(AZ::Edit::UIHandlers::Button, "Add Material Component", "Add Material Component")
  46. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
  47. ->Attribute(AZ::Edit::Attributes::ButtonText, "Add Material Component")
  48. ->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorMeshComponent::AddEditorMaterialComponent)
  49. ->Attribute(AZ::Edit::Attributes::Visibility, &EditorMeshComponent::GetEditorMaterialComponentVisibility)
  50. ->DataElement(AZ::Edit::UIHandlers::Default, &EditorMeshComponent::m_stats, "Model Stats", "")
  51. ->Attribute(AZ::Edit::Attributes::AutoExpand, false)
  52. ;
  53. editContext->Class<MeshComponentController>(
  54. "MeshComponentController", "")
  55. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  56. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  57. ->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentController::m_configuration, "Configuration", "")
  58. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  59. ;
  60. editContext->Class<MeshComponentConfig>(
  61. "MeshComponentConfig", "")
  62. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  63. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  64. ->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentConfig::m_modelAsset, "Model Asset", "Model asset reference", "Mesh Asset")
  65. ->Attribute(AZ_CRC_CE("EditButton"), "")
  66. ->Attribute(AZ_CRC_CE("EditDescription"), "Open in Scene Settings")
  67. ->Attribute(AZ_CRC_CE("DisableEditButtonWhenNoAssetSelected"), true)
  68. ->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentConfig::m_sortKey, "Sort Key", "Transparent meshes are first drawn by sort key, then depth. Use this to force certain transparent meshes to draw before or after others.")
  69. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_excludeFromReflectionCubeMaps, "Exclude from reflection cubemaps", "Model will not be visible in baked reflection probe cubemaps")
  70. ->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
  71. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_useForwardPassIblSpecular, "Use Forward Pass IBL Specular",
  72. "Renders image-based lighting (IBL) specular reflections in the forward pass, by using only the most influential probe (based on the position of the entity) and the global IBL cubemap. It can reduce rendering costs, but is only recommended for static objects that are affected by at most one reflection probe. Note that this will also disable SSR on the mesh.")
  73. ->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
  74. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_isRayTracingEnabled, "Use ray tracing",
  75. "Includes this mesh in ray tracing calculations.")
  76. ->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
  77. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_enableRayIntersection, "Support ray intersection",
  78. "Set to true when the entity has UiCanvasOnMeshComponent")
  79. ->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::ValuesOnly)
  80. ->DataElement(AZ::Edit::UIHandlers::CheckBox, &MeshComponentConfig::m_isAlwaysDynamic, "Always Moving", "Forces this mesh to be considered to always be moving, even if the transform didn't update. Useful for meshes with vertex shader animation.")
  81. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &MeshComponentConfig::m_lodType, "Lod Type", "Determines how level of detail (LOD) will be selected during rendering.")
  82. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "LOD Type")
  83. ->EnumAttribute(RPI::Cullable::LodType::Default, "Default")
  84. ->EnumAttribute(RPI::Cullable::LodType::ScreenCoverage, "Screen Coverage")
  85. ->EnumAttribute(RPI::Cullable::LodType::SpecificLod, "Specific LOD")
  86. ->Attribute(AZ::Edit::Attributes::ChangeNotify, Edit::PropertyRefreshLevels::EntireTree)
  87. ->DataElement(AZ::Edit::UIHandlers::Default, &MeshComponentConfig::m_lightingChannelConfig, "Lighting Channels", "")
  88. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  89. ->ClassElement(AZ::Edit::ClassElements::Group, "Lod Configuration")
  90. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "LOD Configuration")
  91. ->Attribute(AZ::Edit::Attributes::AutoExpand, false)
  92. ->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::ShowLodConfig)
  93. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &MeshComponentConfig::m_lodOverride, "Lod Override", "Specifies the LOD to render, overriding the automatic LOD calculations")
  94. ->Attribute(AZ::Edit::Attributes::NameLabelOverride, "LOD Override")
  95. ->Attribute(AZ::Edit::Attributes::EnumValues, &MeshComponentConfig::GetLodOverrideValues)
  96. ->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::LodTypeIsSpecificLOD)
  97. ->DataElement(AZ::Edit::UIHandlers::Slider, &MeshComponentConfig::m_minimumScreenCoverage, "Minimum Screen Coverage", "Minimum proportion of the screen that the entity will cover. If the entity is smaller than the minimum coverage, it is culled.")
  98. ->Attribute(AZ::Edit::Attributes::Min, 0.f)
  99. ->Attribute(AZ::Edit::Attributes::Max, 1.f)
  100. ->Attribute(AZ::Edit::Attributes::Suffix, " percent")
  101. ->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::LodTypeIsScreenCoverage)
  102. ->DataElement(AZ::Edit::UIHandlers::Slider, &MeshComponentConfig::m_qualityDecayRate, "Quality Decay Rate",
  103. "Rate at which the mesh quality decays. 0 - Always stays at highest quality LOD. 1 - Immediately falls off to lowest quality LOD.")
  104. ->Attribute(AZ::Edit::Attributes::Min, 0.f)
  105. ->Attribute(AZ::Edit::Attributes::Max, 1.f)
  106. ->Attribute(AZ::Edit::Attributes::Visibility, &MeshComponentConfig::LodTypeIsScreenCoverage)
  107. ;
  108. }
  109. }
  110. if (BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  111. {
  112. behaviorContext->ConstantProperty("EditorMeshComponentTypeId", BehaviorConstant(Uuid(EditorMeshComponentTypeId)))
  113. ->Attribute(AZ::Script::Attributes::Module, "render")
  114. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation);
  115. behaviorContext->Class<EditorMeshComponent>()->RequestBus("RenderMeshComponentRequestBus");
  116. }
  117. }
  118. EditorMeshComponent::EditorMeshComponent(const MeshComponentConfig& config)
  119. : BaseClass(config)
  120. {
  121. }
  122. void EditorMeshComponent::Activate()
  123. {
  124. m_controller.m_configuration.m_editorRayIntersection = true;
  125. BaseClass::Activate();
  126. AzToolsFramework::EditorComponentSelectionRequestsBus::Handler::BusConnect(GetEntityId());
  127. AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(GetEntityId());
  128. MeshComponentNotificationBus::Handler::BusConnect(GetEntityId());
  129. }
  130. void EditorMeshComponent::Deactivate()
  131. {
  132. MeshComponentNotificationBus::Handler::BusDisconnect();
  133. AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
  134. AzToolsFramework::EditorComponentSelectionRequestsBus::Handler::BusDisconnect();
  135. BaseClass::Deactivate();
  136. }
  137. void EditorMeshComponent::SetPrimaryAsset(const AZ::Data::AssetId& assetId)
  138. {
  139. m_controller.SetModelAssetId(assetId);
  140. }
  141. AZ::Aabb EditorMeshComponent::GetEditorSelectionBoundsViewport(
  142. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo)
  143. {
  144. return m_controller.GetWorldBounds();
  145. }
  146. bool EditorMeshComponent::EditorSelectionIntersectRayViewport(
  147. [[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo, const AZ::Vector3& src,
  148. const AZ::Vector3& dir, float& distance)
  149. {
  150. if (!m_controller.GetModel())
  151. {
  152. return false;
  153. }
  154. AZ::Transform transform = AZ::Transform::CreateIdentity();
  155. AZ::TransformBus::EventResult(transform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  156. AZ::Vector3 nonUniformScale = AZ::Vector3::CreateOne();
  157. AZ::NonUniformScaleRequestBus::EventResult(nonUniformScale, GetEntityId(), &AZ::NonUniformScaleRequests::GetScale);
  158. float t;
  159. AZ::Vector3 ignoreNormal;
  160. constexpr float rayLength = 1000.0f;
  161. if (m_controller.GetModel()->RayIntersection(transform, nonUniformScale, src, dir * rayLength, t, ignoreNormal))
  162. {
  163. distance = rayLength * t;
  164. return true;
  165. }
  166. return false;
  167. }
  168. bool EditorMeshComponent::SupportsEditorRayIntersect()
  169. {
  170. return true;
  171. }
  172. void EditorMeshComponent::DisplayEntityViewport(
  173. const AzFramework::ViewportInfo&, AzFramework::DebugDisplayRequests& debugDisplay)
  174. {
  175. if (!IsSelected())
  176. {
  177. return;
  178. }
  179. const AZ::Aabb localAabb = m_controller.GetLocalBounds();
  180. if (!localAabb.IsValid())
  181. {
  182. return;
  183. }
  184. AZ::Transform worldTM;
  185. AZ::TransformBus::EventResult(worldTM, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  186. debugDisplay.PushMatrix(worldTM);
  187. debugDisplay.SetColor(AZ::Colors::White);
  188. debugDisplay.DrawWireBox(localAabb.GetMin(), localAabb.GetMax());
  189. debugDisplay.PopMatrix();
  190. }
  191. AZ::Crc32 EditorMeshComponent::AddEditorMaterialComponent()
  192. {
  193. const AZStd::vector<AZ::EntityId> entityList = { GetEntityId() };
  194. const AZ::ComponentTypeList componentsToAdd = { AZ::Uuid(AZ::Render::EditorMaterialComponentTypeId) };
  195. AzToolsFramework::EntityCompositionRequests::AddComponentsOutcome outcome =
  196. AZ::Failure(AZStd::string("Failed to add AZ::Render::EditorMaterialComponentTypeId"));
  197. AzToolsFramework::EntityCompositionRequestBus::BroadcastResult(outcome, &AzToolsFramework::EntityCompositionRequests::AddComponentsToEntities, entityList, componentsToAdd);
  198. return Edit::PropertyRefreshLevels::EntireTree;
  199. }
  200. bool EditorMeshComponent::HasEditorMaterialComponent() const
  201. {
  202. return GetEntity() && GetEntity()->FindComponent(AZ::Uuid(AZ::Render::EditorMaterialComponentTypeId)) != nullptr;
  203. }
  204. AZ::u32 EditorMeshComponent::GetEditorMaterialComponentVisibility() const
  205. {
  206. return HasEditorMaterialComponent() ? AZ::Edit::PropertyVisibility::Hide : AZ::Edit::PropertyVisibility::Show;
  207. }
  208. void EditorMeshComponent::OnModelReady(const Data::Asset<RPI::ModelAsset>& /*modelAsset*/, const Data::Instance<RPI::Model>& /*model*/)
  209. {
  210. const auto& lodAssets = m_controller.GetConfiguration().m_modelAsset->GetLodAssets();
  211. m_stats.m_meshStatsForLod.clear();
  212. m_stats.m_meshStatsForLod.reserve(lodAssets.size());
  213. for (const auto& lodAsset : lodAssets)
  214. {
  215. EditorMeshStatsForLod stats;
  216. const auto& meshes = lodAsset->GetMeshes();
  217. stats.m_meshCount = static_cast<AZ::u32>(meshes.size());
  218. for (const auto& mesh : meshes)
  219. {
  220. stats.m_vertCount += mesh.GetVertexCount();
  221. stats.m_triCount += mesh.GetIndexCount() / 3;
  222. }
  223. m_stats.m_meshStatsForLod.emplace_back(AZStd::move(stats));
  224. }
  225. // Refresh the tree when the model loads to update UI based on the model.
  226. InvalidatePropertyDisplay(AzToolsFramework::Refresh_EntireTree);
  227. }
  228. AZ::u32 EditorMeshComponent::OnConfigurationChanged()
  229. {
  230. // temp variable is needed to hold reference to m_modelAsset while it's being loaded.
  231. // Otherwise it gets released in Deactivate function, and instantly re-activating the component
  232. // places it in a bad state, which happens in OnConfigurationChanged base function.
  233. // This is a bug with AssetManager [LYN-2249]
  234. auto temp = m_controller.m_configuration.m_modelAsset;
  235. m_stats.m_meshStatsForLod = {};
  236. SetDirty();
  237. return BaseClass::OnConfigurationChanged();
  238. }
  239. void EditorMeshComponent::OnEntityVisibilityChanged(bool visibility)
  240. {
  241. m_controller.SetVisibility(visibility);
  242. }
  243. bool EditorMeshComponent::ShouldActivateController() const
  244. {
  245. // By default, components using the EditorRenderComponentAdapter will only activate if the component is visible
  246. // Since the mesh component handles visibility changes by not rendering the mesh, rather than deactivating the component entirely,
  247. // it can be activated even if it is not visible
  248. return true;
  249. }
  250. } // namespace Render
  251. } // namespace AZ