ReferenceShapeComponent.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 "ReferenceShapeComponent.h"
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/EditContext.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. namespace LmbrCentral
  13. {
  14. void ReferenceShapeConfig::Reflect(AZ::ReflectContext* context)
  15. {
  16. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  17. if (serialize)
  18. {
  19. serialize->Class<ReferenceShapeConfig, AZ::ComponentConfig>()
  20. ->Version(0)
  21. ->Field("ShapeEntityId", &ReferenceShapeConfig::m_shapeEntityId)
  22. ;
  23. AZ::EditContext* edit = serialize->GetEditContext();
  24. if (edit)
  25. {
  26. edit->Class<ReferenceShapeConfig>(
  27. "Shape Reference", "")
  28. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  29. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  30. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  31. ->DataElement(0, &ReferenceShapeConfig::m_shapeEntityId, "Shape Entity Id", "Entity with shape component to reference.")
  32. ->Attribute(AZ::Edit::Attributes::RequiredService, AZ_CRC_CE("ShapeService"))
  33. ;
  34. }
  35. }
  36. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  37. {
  38. behaviorContext->Class<ReferenceShapeConfig>()
  39. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  40. ->Constructor()
  41. ->Property("shapeEntityId", BehaviorValueProperty(&ReferenceShapeConfig::m_shapeEntityId))
  42. ;
  43. }
  44. }
  45. void ReferenceShapeComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  46. {
  47. services.push_back(AZ_CRC_CE("ShapeService"));
  48. services.push_back(AZ_CRC_CE("ReferenceShapeService"));
  49. }
  50. void ReferenceShapeComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  51. {
  52. services.push_back(AZ_CRC_CE("ShapeService"));
  53. }
  54. void ReferenceShapeComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& services)
  55. {
  56. }
  57. void ReferenceShapeComponent::Reflect(AZ::ReflectContext* context)
  58. {
  59. ReferenceShapeConfig::Reflect(context);
  60. AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
  61. if (serialize)
  62. {
  63. serialize->Class<ReferenceShapeComponent, AZ::Component>()
  64. ->Version(0)
  65. ->Field("Configuration", &ReferenceShapeComponent::m_configuration)
  66. ;
  67. }
  68. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  69. {
  70. behaviorContext->Constant("ReferenceShapeComponentTypeId", BehaviorConstant(ReferenceShapeComponentTypeId));
  71. behaviorContext->Class<ReferenceShapeComponent>()->RequestBus("ReferenceShapeRequestBus");
  72. behaviorContext->EBus<ReferenceShapeRequestBus>("ReferenceShapeRequestBus")
  73. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  74. ->Event("GetShapeEntityId", &ReferenceShapeRequestBus::Events::GetShapeEntityId)
  75. ->Event("SetShapeEntityId", &ReferenceShapeRequestBus::Events::SetShapeEntityId)
  76. ->VirtualProperty("ShapeEntityId", "GetShapeEntityId", "SetShapeEntityId")
  77. ;
  78. }
  79. }
  80. ReferenceShapeComponent::ReferenceShapeComponent(const ReferenceShapeConfig& configuration)
  81. : m_configuration(configuration)
  82. {
  83. }
  84. void ReferenceShapeComponent::SetupDependencies()
  85. {
  86. AZ::EntityBus::Handler::BusDisconnect();
  87. AZ::TransformNotificationBus::Handler::BusDisconnect();
  88. LmbrCentral::ShapeComponentNotificationsBus::Handler::BusDisconnect();
  89. if (m_configuration.m_shapeEntityId.IsValid() && m_configuration.m_shapeEntityId != GetEntityId())
  90. {
  91. AZ::EntityBus::Handler::BusConnect(m_configuration.m_shapeEntityId);
  92. AZ::TransformNotificationBus::Handler::BusConnect(m_configuration.m_shapeEntityId);
  93. LmbrCentral::ShapeComponentNotificationsBus::Handler::BusConnect(m_configuration.m_shapeEntityId);
  94. }
  95. }
  96. void ReferenceShapeComponent::Activate()
  97. {
  98. SetupDependencies();
  99. // Only connect to these after we've finished initializing everything else.
  100. ReferenceShapeRequestBus::Handler::BusConnect(GetEntityId());
  101. LmbrCentral::ShapeComponentRequestsBus::Handler::BusConnect(GetEntityId());
  102. // Finally, after everything is set up, broadcast out a "ShapeChanged" event. This is needed because the Editor version
  103. // of ReferenceShapeComponent will internally deactivate/activate a runtime version of this component on ShapeEntityId
  104. // changes instead of going through SetShapeEntityId. Other components may rely on knowing about shape changes, so the
  105. // activation needs to send out this event.
  106. LmbrCentral::ShapeComponentNotificationsBus::Event(
  107. GetEntityId(), &LmbrCentral::ShapeComponentNotificationsBus::Events::OnShapeChanged,
  108. LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  109. }
  110. void ReferenceShapeComponent::Deactivate()
  111. {
  112. // Disconnect from these first so that the component stops accepting new requests.
  113. LmbrCentral::ShapeComponentRequestsBus::Handler::BusDisconnect();
  114. ReferenceShapeRequestBus::Handler::BusDisconnect();
  115. AZ::EntityBus::Handler::BusDisconnect();
  116. AZ::TransformNotificationBus::Handler::BusDisconnect();
  117. LmbrCentral::ShapeComponentNotificationsBus::Handler::BusDisconnect();
  118. }
  119. bool ReferenceShapeComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  120. {
  121. if (auto config = azrtti_cast<const ReferenceShapeConfig*>(baseConfig))
  122. {
  123. m_configuration = *config;
  124. return true;
  125. }
  126. return false;
  127. }
  128. bool ReferenceShapeComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  129. {
  130. if (auto config = azrtti_cast<ReferenceShapeConfig*>(outBaseConfig))
  131. {
  132. *config = m_configuration;
  133. return true;
  134. }
  135. return false;
  136. }
  137. void ReferenceShapeComponent::OnEntityActivated([[maybe_unused]] const AZ::EntityId& entityId)
  138. {
  139. if (AllowNotification())
  140. {
  141. LmbrCentral::ShapeComponentNotificationsBus::Event(
  142. GetEntityId(), &LmbrCentral::ShapeComponentNotificationsBus::Events::OnShapeChanged,
  143. LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  144. }
  145. }
  146. void ReferenceShapeComponent::OnEntityDeactivated([[maybe_unused]] const AZ::EntityId& entityId)
  147. {
  148. if (AllowNotification())
  149. {
  150. LmbrCentral::ShapeComponentNotificationsBus::Event(
  151. GetEntityId(), &LmbrCentral::ShapeComponentNotificationsBus::Events::OnShapeChanged,
  152. LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  153. }
  154. }
  155. void ReferenceShapeComponent::OnTransformChanged([[maybe_unused]] const AZ::Transform& local, [[maybe_unused]] const AZ::Transform& world)
  156. {
  157. if (AllowNotification())
  158. {
  159. LmbrCentral::ShapeComponentNotificationsBus::Event(
  160. GetEntityId(), &LmbrCentral::ShapeComponentNotificationsBus::Events::OnShapeChanged,
  161. LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons::TransformChanged);
  162. }
  163. }
  164. void ReferenceShapeComponent::OnShapeChanged([[maybe_unused]] LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons reasons)
  165. {
  166. if (AllowNotification())
  167. {
  168. LmbrCentral::ShapeComponentNotificationsBus::Event(
  169. GetEntityId(), &LmbrCentral::ShapeComponentNotificationsBus::Events::OnShapeChanged,
  170. LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  171. }
  172. }
  173. AZ::Crc32 ReferenceShapeComponent::GetShapeType() const
  174. {
  175. AZ::Crc32 result = {};
  176. AZStd::shared_lock lock(m_mutex);
  177. if (AllowRequest())
  178. {
  179. LmbrCentral::ShapeComponentRequestsBus::EventResult(result, m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::GetShapeType);
  180. }
  181. return result;
  182. }
  183. AZ::Aabb ReferenceShapeComponent::GetEncompassingAabb() const
  184. {
  185. AZ::Aabb result = AZ::Aabb::CreateNull();
  186. AZStd::shared_lock lock(m_mutex);
  187. if (AllowRequest())
  188. {
  189. LmbrCentral::ShapeComponentRequestsBus::EventResult(result, m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::GetEncompassingAabb);
  190. }
  191. return result;
  192. }
  193. void ReferenceShapeComponent::GetTransformAndLocalBounds(AZ::Transform& transform, AZ::Aabb& bounds) const
  194. {
  195. transform = AZ::Transform::CreateIdentity();
  196. bounds = AZ::Aabb::CreateNull();
  197. AZStd::shared_lock lock(m_mutex);
  198. if (AllowRequest())
  199. {
  200. LmbrCentral::ShapeComponentRequestsBus::Event(m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::GetTransformAndLocalBounds, transform, bounds);
  201. }
  202. }
  203. bool ReferenceShapeComponent::IsPointInside(const AZ::Vector3& point) const
  204. {
  205. bool result = false;
  206. AZStd::shared_lock lock(m_mutex);
  207. if (AllowRequest())
  208. {
  209. LmbrCentral::ShapeComponentRequestsBus::EventResult(result, m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::IsPointInside, point);
  210. }
  211. return result;
  212. }
  213. float ReferenceShapeComponent::DistanceFromPoint(const AZ::Vector3& point) const
  214. {
  215. float result = FLT_MAX;
  216. AZStd::shared_lock lock(m_mutex);
  217. if (AllowRequest())
  218. {
  219. LmbrCentral::ShapeComponentRequestsBus::EventResult(result, m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::DistanceFromPoint, point);
  220. }
  221. return result;
  222. }
  223. float ReferenceShapeComponent::DistanceSquaredFromPoint(const AZ::Vector3& point) const
  224. {
  225. float result = FLT_MAX;
  226. AZStd::shared_lock lock(m_mutex);
  227. if (AllowRequest())
  228. {
  229. LmbrCentral::ShapeComponentRequestsBus::EventResult(result, m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::DistanceSquaredFromPoint, point);
  230. }
  231. return result;
  232. }
  233. AZ::Vector3 ReferenceShapeComponent::GenerateRandomPointInside(AZ::RandomDistributionType randomDistribution) const
  234. {
  235. AZ::Vector3 result = AZ::Vector3::CreateZero();
  236. AZStd::shared_lock lock(m_mutex);
  237. if (AllowRequest())
  238. {
  239. LmbrCentral::ShapeComponentRequestsBus::EventResult(result, m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::GenerateRandomPointInside, randomDistribution);
  240. }
  241. return result;
  242. }
  243. bool ReferenceShapeComponent::IntersectRay(const AZ::Vector3& src, const AZ::Vector3& dir, float& distance) const
  244. {
  245. bool result = false;
  246. AZStd::shared_lock lock(m_mutex);
  247. if (AllowRequest())
  248. {
  249. LmbrCentral::ShapeComponentRequestsBus::EventResult(result, m_configuration.m_shapeEntityId, &LmbrCentral::ShapeComponentRequestsBus::Events::IntersectRay, src, dir, distance);
  250. }
  251. return result;
  252. }
  253. bool ReferenceShapeComponent::AllowRequest() const
  254. {
  255. AZ_ErrorOnce(
  256. "Shape", !LmbrCentral::ShapeComponentRequestsBus::HasReentrantEBusUseThisThread(),
  257. "Detected cyclic dependencies with shape entity references on entity '%s' (%s)", GetEntity()->GetName().c_str(),
  258. GetEntityId().ToString().c_str());
  259. return !LmbrCentral::ShapeComponentRequestsBus::HasReentrantEBusUseThisThread() && m_configuration.m_shapeEntityId.IsValid() &&
  260. m_configuration.m_shapeEntityId != GetEntityId();
  261. }
  262. bool ReferenceShapeComponent::AllowNotification() const
  263. {
  264. AZ_ErrorOnce(
  265. "Shape", !LmbrCentral::ShapeComponentNotificationsBus::HasReentrantEBusUseThisThread(),
  266. "Detected cyclic dependencies with shape entity references on entity '%s' (%s)", GetEntity()->GetName().c_str(),
  267. GetEntityId().ToString().c_str());
  268. return !LmbrCentral::ShapeComponentNotificationsBus::HasReentrantEBusUseThisThread() &&
  269. m_allowNotifications &&
  270. m_configuration.m_shapeEntityId.IsValid() &&
  271. m_configuration.m_shapeEntityId != GetEntityId();
  272. }
  273. AZ::EntityId ReferenceShapeComponent::GetShapeEntityId() const
  274. {
  275. AZStd::shared_lock lock(m_mutex);
  276. return m_configuration.m_shapeEntityId;
  277. }
  278. void ReferenceShapeComponent::SetShapeEntityId(AZ::EntityId entityId)
  279. {
  280. {
  281. AZStd::unique_lock lock(m_mutex);
  282. if (m_configuration.m_shapeEntityId != entityId)
  283. {
  284. m_configuration.m_shapeEntityId = entityId;
  285. }
  286. // Temporarily disable notifications so that we don't get an "Entity Activated" notification when setting up
  287. // the dependencies. The notification would both cause a redundant OnShapeChanged call and would be potentially
  288. // problematic because it would occur while still holding the unique_lock above. Instead we can just skip that
  289. // notification and send a single OnShapeChanged notification below.
  290. m_allowNotifications = false;
  291. SetupDependencies();
  292. m_allowNotifications = true;
  293. }
  294. // Broadcast out a "ShapeChanged" event. In some cases, this might be excessive, but in the specific
  295. // case that the entity ID gets cleared out of this component in the Editor, there are no other events
  296. // that fire to notify upstream shape consumers that something has changed about the shape.
  297. LmbrCentral::ShapeComponentNotificationsBus::Event(
  298. GetEntityId(), &LmbrCentral::ShapeComponentNotificationsBus::Events::OnShapeChanged,
  299. LmbrCentral::ShapeComponentNotifications::ShapeChangeReasons::ShapeChanged);
  300. }
  301. }