DecalBus.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/Math/Transform.h>
  10. #include <AzCore/EBus/EBus.h>
  11. #include <Atom/RPI.Public/Scene.h>
  12. namespace MultiplayerSample
  13. {
  14. struct SpawnDecalConfig
  15. {
  16. AZ_RTTI(MultiplayerSample::SpawnDecalConfig, "{FC3DA616-174B-48FD-9BFB-BC277132FB47}");
  17. inline static void Reflect(AZ::ReflectContext* context);
  18. AZ::Data::AssetId m_materialAssetId; // Asset Id of the material.
  19. float m_scale = 1.0f; // Scale in meters.
  20. float m_opacity = 1.0f; // How visible the decal is.
  21. float m_attenuationAngle = 1.0f; // How much to attenuate based on the angle of the geometry vs the decal.
  22. float m_lifeTimeSec = 0.0f; // Length of time the decal lives between fading in and out, in seconds.
  23. float m_fadeInTimeSec = 0.1f; // Time it takes the decal to fade in, in seconds.
  24. float m_fadeOutTimeSec = 1.0f; // Time it takes the decal to fade out, in seconds.
  25. float m_thickness = 1.0f; // How thick the decal should be on the z axis.
  26. uint8_t m_sortKey = 0; // Higher numbers sort in front of lower numbers.
  27. };
  28. void SpawnDecalConfig::Reflect(AZ::ReflectContext* context)
  29. {
  30. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  31. {
  32. serializeContext->Class<MultiplayerSample::SpawnDecalConfig>()
  33. ->Version(0)
  34. ->Field("MaterialAssetId", &SpawnDecalConfig::m_materialAssetId)
  35. ->Field("Scale", &SpawnDecalConfig::m_scale)
  36. ->Field("Opacity", &SpawnDecalConfig::m_opacity)
  37. ->Field("AttenuationAngle", &SpawnDecalConfig::m_attenuationAngle)
  38. ->Field("LifeTimeSec", &SpawnDecalConfig::m_lifeTimeSec)
  39. ->Field("FadeInTimeSec", &SpawnDecalConfig::m_fadeInTimeSec)
  40. ->Field("FadeOutTimeSec", &SpawnDecalConfig::m_fadeOutTimeSec)
  41. ->Field("Thickness", &SpawnDecalConfig::m_thickness)
  42. ->Field("SortKey", &SpawnDecalConfig::m_sortKey)
  43. ;
  44. if (auto editContext = serializeContext->GetEditContext())
  45. {
  46. editContext->Class<SpawnDecalConfig>("SpawnDecalConfig", "Configuration settings for spawning a decal.")
  47. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  48. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  49. ->DataElement(AZ::Edit::UIHandlers::Default, &SpawnDecalConfig::m_materialAssetId, "Material", "The material for the decal.")
  50. ->DataElement(AZ::Edit::UIHandlers::Slider, &SpawnDecalConfig::m_scale, "Scale", "The scale of the decal.")
  51. ->Attribute(AZ::Edit::Attributes::Min, 0.01f)
  52. ->Attribute(AZ::Edit::Attributes::Max, 100.0f)
  53. ->Attribute(AZ::Edit::Attributes::SoftMin, 0.01f)
  54. ->Attribute(AZ::Edit::Attributes::SoftMax, 5.0f)
  55. ->DataElement(AZ::Edit::UIHandlers::Slider, &SpawnDecalConfig::m_opacity, "Opacity", "The opacity of the decal.")
  56. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  57. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  58. ->DataElement(AZ::Edit::UIHandlers::Slider, &SpawnDecalConfig::m_attenuationAngle, "Angle attenuation", "How much to attenuate the opacity of the decal based on the different in the angle between the decal and the surface.")
  59. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  60. ->Attribute(AZ::Edit::Attributes::Max, 1.0f)
  61. ->DataElement(AZ::Edit::UIHandlers::Default, &SpawnDecalConfig::m_lifeTimeSec, "Life time", "Length of time the decal lives between fading in and out, in seconds")
  62. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  63. ->DataElement(AZ::Edit::UIHandlers::Default, &SpawnDecalConfig::m_fadeInTimeSec, "Fade in time", "How long the decal should spend fading in when it is first spawned, in seconds.")
  64. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  65. ->DataElement(AZ::Edit::UIHandlers::Default, &SpawnDecalConfig::m_fadeOutTimeSec, "Fade out time", "How long the decal should spend fading out at the end of its life time, in seconds.")
  66. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  67. ->DataElement(AZ::Edit::UIHandlers::Default, &SpawnDecalConfig::m_thickness, "Thickness", "How thick the decal should be on the z axis.")
  68. ->Attribute(AZ::Edit::Attributes::Min, 0.0f)
  69. ->DataElement(AZ::Edit::UIHandlers::Default, &SpawnDecalConfig::m_sortKey, "Sort key", "Used to sort the decal with other decals. Higher numbered decals show on top of lower number decals.")
  70. ;
  71. }
  72. }
  73. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  74. {
  75. behaviorContext->Class<SpawnDecalConfig>("SpawnDecalConfig")
  76. ->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Common)
  77. ->Attribute(AZ::Script::Attributes::Category, "Graphics")
  78. ->Attribute(AZ::Script::Attributes::Module, "decals")
  79. ->Constructor()
  80. ->Constructor<const SpawnDecalConfig&>()
  81. ->Property("material", BehaviorValueProperty(&SpawnDecalConfig::m_materialAssetId))
  82. ->Property("scale", BehaviorValueProperty(&SpawnDecalConfig::m_scale))
  83. ->Property("opacity", BehaviorValueProperty(&SpawnDecalConfig::m_opacity))
  84. ->Property("attenuationAngle", BehaviorValueProperty(&SpawnDecalConfig::m_attenuationAngle))
  85. ->Property("lifeTimeSec", BehaviorValueProperty(&SpawnDecalConfig::m_lifeTimeSec))
  86. ->Property("fadeInTimeSec", BehaviorValueProperty(&SpawnDecalConfig::m_fadeInTimeSec))
  87. ->Property("fadeOutTimeSec", BehaviorValueProperty(&SpawnDecalConfig::m_fadeOutTimeSec))
  88. ->Property("thickness", BehaviorValueProperty(&SpawnDecalConfig::m_thickness))
  89. ->Property("sortKey", BehaviorValueProperty(&SpawnDecalConfig::m_sortKey))
  90. ;
  91. }
  92. }
  93. class DecalRequests
  94. : public AZ::EBusTraits
  95. {
  96. public:
  97. static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
  98. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById;
  99. using BusIdType = AZ::RPI::SceneId;
  100. AZ_RTTI(MultiplayerSample::DecalRequests, "{A3643473-25ED-4F86-8BEE-D65A1A54867B}");
  101. virtual ~DecalRequests() = default;
  102. /**
  103. * \brief Spawn a decal
  104. * \param worldTm Where to spawn the decal.
  105. * \param materialAssetId The asset ID of the material to use for the decal
  106. * \param config The configuration of the decal to spawn (opacity, scale, etc).
  107. */
  108. virtual void SpawnDecal(const AZ::Transform& worldTm, const SpawnDecalConfig& config) = 0;
  109. };
  110. using DecalRequestBus = AZ::EBus<DecalRequests>;
  111. }