3
0

TerrainSystemComponent.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <Components/TerrainSystemComponent.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/EditContextConstants.inl>
  12. #include <TerrainSystem/TerrainSystem.h>
  13. #include <Atom/RPI.Public/Pass/PassSystemInterface.h>
  14. #include <TerrainRenderer/Passes/TerrainClipmapComputePass.h>
  15. #include <TerrainRenderer/Passes/TerrainClipmapDebugPass.h>
  16. namespace Terrain
  17. {
  18. void TerrainSystemComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  21. {
  22. serialize->Class<TerrainSystemComponent, AZ::Component>()
  23. ->Version(0);
  24. if (AZ::EditContext* ec = serialize->GetEditContext())
  25. {
  26. ec->Class<TerrainSystemComponent>("Terrain", "The Terrain System Component enables Terrain.")
  27. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  28. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  29. ;
  30. }
  31. }
  32. }
  33. void TerrainSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  34. {
  35. provided.push_back(AZ_CRC_CE("TerrainService"));
  36. }
  37. void TerrainSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  38. {
  39. incompatible.push_back(AZ_CRC_CE("TerrainService"));
  40. }
  41. void TerrainSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  42. {
  43. }
  44. void TerrainSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  45. {
  46. }
  47. void TerrainSystemComponent::Init()
  48. {
  49. }
  50. void TerrainSystemComponent::Activate()
  51. {
  52. // Currently, the Terrain System Component owns the Terrain System instance because the Terrain World component gets recreated
  53. // every time an entity is added or removed to a level. If this ever changes, the Terrain System ownership could move into
  54. // the level component.
  55. m_terrainSystem = new TerrainSystem();
  56. auto* passSystem = AZ::RPI::PassSystemInterface::Get();
  57. AZ_Assert(passSystem, "Cannot get the pass system.");
  58. // Setup handler for load pass templates mappings
  59. m_loadTemplatesHandler = AZ::RPI::PassSystemInterface::OnReadyLoadTemplatesEvent::Handler([this]() { this->LoadPassTemplateMappings(); });
  60. passSystem->ConnectEvent(m_loadTemplatesHandler);
  61. // Register terrain system related passes
  62. passSystem->AddPassCreator(AZ::Name("TerrainMacroClipmapGenerationPass"), &TerrainMacroClipmapGenerationPass::Create);
  63. passSystem->AddPassCreator(AZ::Name("TerrainDetailClipmapGenerationPass"), &TerrainDetailClipmapGenerationPass::Create);
  64. passSystem->AddPassCreator(AZ::Name("TerrainClipmapDebugPass"), &TerrainClipmapDebugPass::Create);
  65. }
  66. void TerrainSystemComponent::Deactivate()
  67. {
  68. m_loadTemplatesHandler.Disconnect();
  69. delete m_terrainSystem;
  70. m_terrainSystem = nullptr;
  71. }
  72. void TerrainSystemComponent::LoadPassTemplateMappings()
  73. {
  74. auto* passSystem = AZ::RPI::PassSystemInterface::Get();
  75. AZ_Assert(passSystem, "Cannot get the pass system.");
  76. const char* passTemplatesFile = "Passes/TerrainPassTemplates.azasset";
  77. passSystem->LoadPassTemplateMappings(passTemplatesFile);
  78. }
  79. }