TerrainWorldComponent.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/Asset/AssetCommon.h>
  10. #include <AzCore/Component/Component.h>
  11. #include <AzCore/Math/Vector3.h>
  12. #include <AzCore/Serialization/Json/BaseJsonSerializer.h>
  13. #include <AzCore/Serialization/Json/RegistrationContext.h>
  14. #include <TerrainSystem/TerrainSystem.h>
  15. namespace LmbrCentral
  16. {
  17. template<typename, typename>
  18. class EditorWrappedComponentBase;
  19. }
  20. namespace Terrain
  21. {
  22. // Custom JSON serializer for TerrainWorldConfig to handle version conversion
  23. class JsonTerrainWorldConfigSerializer : public AZ::BaseJsonSerializer
  24. {
  25. public:
  26. AZ_RTTI(Terrain::JsonTerrainWorldConfigSerializer, "{910BC31F-CD49-488E-8004-227D9FEB5A16}", AZ::BaseJsonSerializer);
  27. AZ_CLASS_ALLOCATOR_DECL;
  28. AZ::JsonSerializationResult::Result Load(
  29. void* outputValue, const AZ::Uuid& outputValueTypeId, const rapidjson::Value& inputValue,
  30. AZ::JsonDeserializerContext& context) override;
  31. };
  32. class TerrainWorldConfig
  33. : public AZ::ComponentConfig
  34. {
  35. public:
  36. AZ_CLASS_ALLOCATOR(TerrainWorldConfig, AZ::SystemAllocator);
  37. AZ_RTTI(TerrainWorldConfig, "{295844DB-20DD-45B2-94DB-4245D5AE9AFF}", AZ::ComponentConfig);
  38. static void Reflect(AZ::ReflectContext* context);
  39. float m_minHeight{ 0.0f };
  40. float m_maxHeight{ 1024.0f };
  41. float m_heightQueryResolution{ 1.0f };
  42. float m_surfaceDataQueryResolution{ 1.0f };
  43. static AZ::Outcome<void, AZStd::string> ValidateHeight(float minHeight, float maxHeight)
  44. {
  45. if (minHeight > maxHeight)
  46. {
  47. return AZ::Failure(AZStd::string("Terrain min height must be less than max height."));
  48. }
  49. return AZ::Success();
  50. }
  51. AZ::Outcome<void, AZStd::string> ValidateHeightMin(void* newValue, [[maybe_unused]] const AZ::Uuid& valueType)
  52. {
  53. return ValidateHeight(*static_cast<float*>(newValue), m_maxHeight);
  54. }
  55. AZ::Outcome<void, AZStd::string> ValidateHeightMax(void* newValue, [[maybe_unused]] const AZ::Uuid& valueType)
  56. {
  57. return ValidateHeight(m_minHeight, *static_cast<float*>(newValue));
  58. }
  59. };
  60. class TerrainWorldComponent
  61. : public AZ::Component
  62. {
  63. public:
  64. template<typename, typename>
  65. friend class LmbrCentral::EditorWrappedComponentBase;
  66. AZ_COMPONENT(TerrainWorldComponent, "{4734EFDC-135D-4BF5-BE57-4F9AD03ADF78}");
  67. static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  68. static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  69. static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
  70. static void Reflect(AZ::ReflectContext* context);
  71. TerrainWorldComponent(const TerrainWorldConfig& configuration);
  72. TerrainWorldComponent() = default;
  73. ~TerrainWorldComponent() override;
  74. //////////////////////////////////////////////////////////////////////////
  75. // AZ::Component interface implementation
  76. void Activate() override;
  77. void Deactivate() override;
  78. bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
  79. bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override;
  80. private:
  81. TerrainWorldConfig m_configuration;
  82. };
  83. }