GradientTransformComponent.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 <GradientSignal/Components/GradientTransformComponent.h>
  9. #include <AzCore/Math/Vector2.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/Serialization/SerializeContext.h>
  13. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  14. #include <GradientSignal/Ebuses/GradientTransformRequestBus.h>
  15. #include <AzCore/Component/TransformBus.h>
  16. #include <GradientSignal/Util.h>
  17. namespace GradientSignal
  18. {
  19. void GradientTransformConfig::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<GradientTransformConfig, AZ::ComponentConfig>()
  24. ->Version(0)
  25. ->Field("TransformType", &GradientTransformConfig::m_transformType)
  26. ->Field("WrappingType", &GradientTransformConfig::m_wrappingType)
  27. ->Field("FrequencyZoom", &GradientTransformConfig::m_frequencyZoom)
  28. ->Field("AdvancedMode", &GradientTransformConfig::m_advancedMode)
  29. ->Field("Is3d", &GradientTransformConfig::m_is3d)
  30. ->Field("AllowReference", &GradientTransformConfig::m_allowReference)
  31. ->Field("ShapeReference", &GradientTransformConfig::m_shapeReference)
  32. ->Field("OverrideBounds", &GradientTransformConfig::m_overrideBounds)
  33. ->Field("Bounds", &GradientTransformConfig::m_bounds)
  34. ->Field("Center", &GradientTransformConfig::m_center)
  35. ->Field("OverrideTranslate", &GradientTransformConfig::m_overrideTranslate)
  36. ->Field("Translate", &GradientTransformConfig::m_translate)
  37. ->Field("OverrideRotate", &GradientTransformConfig::m_overrideRotate)
  38. ->Field("Rotate", &GradientTransformConfig::m_rotate)
  39. ->Field("OverrideScale", &GradientTransformConfig::m_overrideScale)
  40. ->Field("Scale", &GradientTransformConfig::m_scale)
  41. ;
  42. if (auto editContext = serializeContext->GetEditContext())
  43. {
  44. editContext->Class<GradientTransformConfig>("Gradient Transform", "")
  45. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  46. ->Attribute(AZ::Edit::Attributes::Visibility, AZ::Edit::PropertyVisibility::ShowChildrenOnly)
  47. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  48. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &GradientTransformConfig::m_transformType, "Transform Type", "Determines how shape transforms are interpreted")
  49. ->EnumAttribute(TransformType::World_Origin, "Origin")
  50. ->EnumAttribute(TransformType::World_ThisEntity, "World Transform")
  51. ->EnumAttribute(TransformType::Local_ThisEntity, "Relative to Parent")
  52. ->EnumAttribute(TransformType::World_ReferenceEntity, "World Transform (of Reference)")
  53. ->EnumAttribute(TransformType::Relative, "Relative to Reference")
  54. ->DataElement(AZ::Edit::UIHandlers::ComboBox, &GradientTransformConfig::m_wrappingType, "Wrapping Type", "")
  55. ->EnumAttribute(WrappingType::None, "None (unbounded)")
  56. ->EnumAttribute(WrappingType::ClampToEdge, "Clamp To Edge")
  57. ->EnumAttribute(WrappingType::ClampToZero, "Clamp To Zero")
  58. ->EnumAttribute(WrappingType::Mirror, "Mirror")
  59. ->EnumAttribute(WrappingType::Repeat, "Repeat")
  60. ->DataElement(AZ::Edit::UIHandlers::Slider, &GradientTransformConfig::m_frequencyZoom, "Frequency Zoom", "Rescales coordinates based on a multiplied factor")
  61. ->Attribute(AZ::Edit::Attributes::DisplayDecimals, 4)
  62. ->Attribute(AZ::Edit::Attributes::Min, 0.0001f)
  63. ->Attribute(AZ::Edit::Attributes::Max, std::numeric_limits<float>::max())
  64. ->Attribute(AZ::Edit::Attributes::SoftMax, 8.0f)
  65. ->Attribute(AZ::Edit::Attributes::Step, 0.25f)
  66. ->Attribute(AZ::Edit::Attributes::SliderCurveMidpoint, 0.25) // Give the frequency zoom a non-linear scale slider with higher precision at the low end
  67. ->GroupElementToggle("Advanced", &GradientTransformConfig::m_advancedMode)
  68. ->Attribute(AZ::Edit::Attributes::AutoExpand, false)
  69. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  70. ->DataElement(0, &GradientTransformConfig::m_allowReference, "Allow Reference", "When enabled, the shape reference can be overridden. When disabled, all operations are relative to this entity.")
  71. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsAdvancedModeReadOnly)
  72. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  73. ->DataElement(0, &GradientTransformConfig::m_shapeReference, "Shape Reference", "An optional shape reference that can be used to drive bounds and transform")
  74. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsReferenceReadOnly)
  75. ->DataElement(0, &GradientTransformConfig::m_overrideBounds, "Override Bounds", "Allow manual override of the associated parameter")
  76. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsAdvancedModeReadOnly)
  77. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  78. ->DataElement(0, &GradientTransformConfig::m_bounds, "Bounds", "Local (untransformed) bounds of a box used to remap, clamp, wrap, scale incoming coordinates")
  79. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsBoundsReadOnly)
  80. ->DataElement(0, &GradientTransformConfig::m_center, "Center", "Local (untransformed) center of a box used to remap, clamp, wrap, scale incoming coordinates")
  81. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsBoundsReadOnly)
  82. ->DataElement(0, &GradientTransformConfig::m_overrideTranslate, "Override Translate", "Allow manual override of the associated parameter")
  83. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsAdvancedModeReadOnly)
  84. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  85. ->DataElement(0, &GradientTransformConfig::m_translate, "Translate", "")
  86. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsTranslateReadOnly)
  87. ->DataElement(0, &GradientTransformConfig::m_overrideRotate, "Override Rotate", "Allow manual override of the associated parameter")
  88. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsAdvancedModeReadOnly)
  89. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  90. ->DataElement(0, &GradientTransformConfig::m_rotate, "Rotate", "")
  91. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsRotateReadOnly)
  92. ->DataElement(0, &GradientTransformConfig::m_overrideScale, "Override Scale", "Allow manual override of the associated parameter")
  93. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsAdvancedModeReadOnly)
  94. ->Attribute(AZ::Edit::Attributes::ChangeNotify, AZ::Edit::PropertyRefreshLevels::AttributesAndValues)
  95. ->DataElement(0, &GradientTransformConfig::m_scale, "Scale", "")
  96. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsScaleReadOnly)
  97. ->DataElement(0, &GradientTransformConfig::m_is3d, "Sample in 3D", "Check if the UVW should be mapped based on three dimensional world space")
  98. ->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientTransformConfig::IsAdvancedModeReadOnly)
  99. ;
  100. }
  101. }
  102. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  103. {
  104. behaviorContext->Class<GradientTransformConfig>()
  105. ->Constructor()
  106. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  107. ->Property("advancedMode", BehaviorValueProperty(&GradientTransformConfig::m_advancedMode))
  108. ->Property("allowReference", BehaviorValueProperty(&GradientTransformConfig::m_allowReference))
  109. ->Property("shapeReference", BehaviorValueProperty(&GradientTransformConfig::m_shapeReference))
  110. ->Property("overrideBounds", BehaviorValueProperty(&GradientTransformConfig::m_overrideBounds))
  111. ->Property("bounds", BehaviorValueProperty(&GradientTransformConfig::m_bounds))
  112. ->Property("center", BehaviorValueProperty(&GradientTransformConfig::m_center))
  113. ->Property("transformType",
  114. [](GradientTransformConfig* config) { return (AZ::u8&)(config->m_transformType); },
  115. [](GradientTransformConfig* config, const AZ::u8& i) { config->m_transformType = (TransformType)i; })
  116. ->Property("overrideTranslate", BehaviorValueProperty(&GradientTransformConfig::m_overrideTranslate))
  117. ->Property("translate", BehaviorValueProperty(&GradientTransformConfig::m_translate))
  118. ->Property("overrideRotate", BehaviorValueProperty(&GradientTransformConfig::m_overrideRotate))
  119. ->Property("rotate", BehaviorValueProperty(&GradientTransformConfig::m_rotate))
  120. ->Property("overrideScale", BehaviorValueProperty(&GradientTransformConfig::m_overrideScale))
  121. ->Property("scale", BehaviorValueProperty(&GradientTransformConfig::m_scale))
  122. ->Property("frequencyZoom", BehaviorValueProperty(&GradientTransformConfig::m_frequencyZoom))
  123. ->Property("wrappingType",
  124. [](GradientTransformConfig* config) { return (AZ::u8&)(config->m_wrappingType); },
  125. [](GradientTransformConfig* config, const AZ::u8& i) { config->m_wrappingType = (WrappingType)i; })
  126. ->Property("is3d", BehaviorValueProperty(&GradientTransformConfig::m_is3d))
  127. ;
  128. }
  129. }
  130. bool GradientTransformConfig::operator==(const GradientTransformConfig& other) const
  131. {
  132. return
  133. m_advancedMode == other.m_advancedMode &&
  134. m_allowReference == other.m_allowReference &&
  135. m_shapeReference == other.m_shapeReference &&
  136. m_overrideBounds == other.m_overrideBounds &&
  137. m_bounds.IsClose(other.m_bounds) &&
  138. m_center.IsClose(other.m_center) &&
  139. m_transformType == other.m_transformType &&
  140. m_overrideTranslate == other.m_overrideTranslate &&
  141. m_translate.IsClose(other.m_translate) &&
  142. m_overrideRotate == other.m_overrideRotate &&
  143. m_rotate.IsClose(other.m_rotate) &&
  144. m_overrideScale == other.m_overrideScale &&
  145. m_scale.IsClose(other.m_scale) &&
  146. m_frequencyZoom == other.m_frequencyZoom &&
  147. m_wrappingType == other.m_wrappingType &&
  148. m_is3d == other.m_is3d;
  149. }
  150. bool GradientTransformConfig::operator!=(const GradientTransformConfig& other) const
  151. {
  152. return !(*this == other);
  153. }
  154. bool GradientTransformConfig::IsAdvancedModeReadOnly() const
  155. {
  156. return !m_advancedMode;
  157. }
  158. bool GradientTransformConfig::IsReferenceReadOnly() const
  159. {
  160. return !m_allowReference || !m_advancedMode;
  161. }
  162. bool GradientTransformConfig::IsBoundsReadOnly() const
  163. {
  164. return !m_overrideBounds || !m_advancedMode;
  165. }
  166. bool GradientTransformConfig::IsTranslateReadOnly() const
  167. {
  168. return !m_overrideTranslate || !m_advancedMode;
  169. }
  170. bool GradientTransformConfig::IsRotateReadOnly() const
  171. {
  172. return !m_overrideRotate || !m_advancedMode;
  173. }
  174. bool GradientTransformConfig::IsScaleReadOnly() const
  175. {
  176. return !m_overrideScale || !m_advancedMode;
  177. }
  178. void GradientTransformComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  179. {
  180. services.push_back(AZ_CRC_CE("GradientTransformService"));
  181. }
  182. void GradientTransformComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  183. {
  184. services.push_back(AZ_CRC_CE("GradientTransformService"));
  185. services.push_back(AZ_CRC_CE("NonUniformScaleService"));
  186. }
  187. void GradientTransformComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services)
  188. {
  189. services.push_back(AZ_CRC_CE("ShapeService"));
  190. }
  191. void GradientTransformComponent::Reflect(AZ::ReflectContext* context)
  192. {
  193. GradientTransformConfig::Reflect(context);
  194. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  195. {
  196. serializeContext->Class<GradientTransformComponent, AZ::Component>()
  197. ->Version(0)
  198. ->Field("Configuration", &GradientTransformComponent::m_configuration)
  199. ;
  200. }
  201. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  202. {
  203. behaviorContext->Constant("GradientTransformComponentTypeId", BehaviorConstant(GradientTransformComponentTypeId));
  204. behaviorContext->Class<GradientTransformComponent>()->RequestBus("GradientTransformModifierRequestBus");
  205. behaviorContext->EBus<GradientTransformModifierRequestBus>("GradientTransformModifierRequestBus")
  206. ->Attribute(AZ::Script::Attributes::Category, "Vegetation")
  207. ->Event("GetAllowReference", &GradientTransformModifierRequestBus::Events::GetAllowReference)
  208. ->Event("SetAllowReference", &GradientTransformModifierRequestBus::Events::SetAllowReference)
  209. ->VirtualProperty("AllowReference", "GetAllowReference", "SetAllowReference")
  210. ->Event("GetShapeReference", &GradientTransformModifierRequestBus::Events::GetShapeReference)
  211. ->Event("SetShapeReference", &GradientTransformModifierRequestBus::Events::SetShapeReference)
  212. ->VirtualProperty("ShapeReference", "GetShapeReference", "SetShapeReference")
  213. ->Event("GetOverrideBounds", &GradientTransformModifierRequestBus::Events::GetOverrideBounds)
  214. ->Event("SetOverrideBounds", &GradientTransformModifierRequestBus::Events::SetOverrideBounds)
  215. ->VirtualProperty("OverrideBounds", "GetOverrideBounds", "SetOverrideBounds")
  216. ->Event("GetBounds", &GradientTransformModifierRequestBus::Events::GetBounds)
  217. ->Event("SetBounds", &GradientTransformModifierRequestBus::Events::SetBounds)
  218. ->VirtualProperty("Bounds", "GetBounds", "SetBounds")
  219. ->Event("GetCenter", &GradientTransformModifierRequestBus::Events::GetCenter)
  220. ->Event("SetCenter", &GradientTransformModifierRequestBus::Events::SetCenter)
  221. ->VirtualProperty("Center", "GetCenter", "SetCenter")
  222. ->Event("GetTransformType", &GradientTransformModifierRequestBus::Events::GetTransformType)
  223. ->Event("SetTransformType", &GradientTransformModifierRequestBus::Events::SetTransformType)
  224. ->VirtualProperty("TransformType", "GetTransformType", "SetTransformType")
  225. ->Event("GetOverrideTranslate", &GradientTransformModifierRequestBus::Events::GetOverrideTranslate)
  226. ->Event("SetOverrideTranslate", &GradientTransformModifierRequestBus::Events::SetOverrideTranslate)
  227. ->VirtualProperty("OverrideTranslate", "GetOverrideTranslate", "SetOverrideTranslate")
  228. ->Event("GetTranslate", &GradientTransformModifierRequestBus::Events::GetTranslate)
  229. ->Event("SetTranslate", &GradientTransformModifierRequestBus::Events::SetTranslate)
  230. ->VirtualProperty("Translate", "GetTranslate", "SetTranslate")
  231. ->Event("GetOverrideRotate", &GradientTransformModifierRequestBus::Events::GetOverrideRotate)
  232. ->Event("SetOverrideRotate", &GradientTransformModifierRequestBus::Events::SetOverrideRotate)
  233. ->VirtualProperty("OverrideRotate", "GetOverrideRotate", "SetOverrideRotate")
  234. ->Event("GetRotate", &GradientTransformModifierRequestBus::Events::GetRotate)
  235. ->Event("SetRotate", &GradientTransformModifierRequestBus::Events::SetRotate)
  236. ->VirtualProperty("Rotate", "GetRotate", "SetRotate")
  237. ->Event("GetOverrideScale", &GradientTransformModifierRequestBus::Events::GetOverrideScale)
  238. ->Event("SetOverrideScale", &GradientTransformModifierRequestBus::Events::SetOverrideScale)
  239. ->VirtualProperty("OverrideScale", "GetOverrideScale", "SetOverrideScale")
  240. ->Event("GetScale", &GradientTransformModifierRequestBus::Events::GetScale)
  241. ->Event("SetScale", &GradientTransformModifierRequestBus::Events::SetScale)
  242. ->VirtualProperty("Scale", "GetScale", "SetScale")
  243. ->Event("GetFrequencyZoom", &GradientTransformModifierRequestBus::Events::GetFrequencyZoom)
  244. ->Event("SetFrequencyZoom", &GradientTransformModifierRequestBus::Events::SetFrequencyZoom)
  245. ->VirtualProperty("FrequencyZoom", "GetFrequencyZoom", "SetFrequencyZoom")
  246. ->Event("GetWrappingType", &GradientTransformModifierRequestBus::Events::GetWrappingType)
  247. ->Event("SetWrappingType", &GradientTransformModifierRequestBus::Events::SetWrappingType)
  248. ->VirtualProperty("WrappingType", "GetWrappingType", "SetWrappingType")
  249. ->Event("GetIs3D", &GradientTransformModifierRequestBus::Events::GetIs3D)
  250. ->Event("SetIs3D", &GradientTransformModifierRequestBus::Events::SetIs3D)
  251. ->VirtualProperty("Is3D", "GetIs3D", "SetIs3D")
  252. ;
  253. }
  254. }
  255. GradientTransformComponent::GradientTransformComponent(const GradientTransformConfig& configuration)
  256. : m_configuration(configuration)
  257. {
  258. }
  259. void GradientTransformComponent::Activate()
  260. {
  261. m_dirty = false;
  262. m_gradientTransform = GradientTransform();
  263. // Update our GradientTransform to be configured correctly. We don't need to notify dependents of the change though.
  264. // If anyone is listening, they're already getting notified below.
  265. const bool notifyDependentsOfChange = false;
  266. UpdateFromShape(notifyDependentsOfChange);
  267. GradientTransformRequestBus::Handler::BusConnect(GetEntityId());
  268. LmbrCentral::DependencyNotificationBus::Handler::BusConnect(GetEntityId());
  269. AZ::TickBus::Handler::BusConnect();
  270. GradientTransformModifierRequestBus::Handler::BusConnect(GetEntityId());
  271. m_dependencyMonitor.Reset();
  272. m_dependencyMonitor.SetRegionChangedEntityNotificationFunction();
  273. m_dependencyMonitor.ConnectOwner(GetEntityId());
  274. m_dependencyMonitor.ConnectDependency(GetEntityId());
  275. m_dependencyMonitor.ConnectDependency(GetShapeEntityId());
  276. }
  277. void GradientTransformComponent::Deactivate()
  278. {
  279. m_dirty = false;
  280. m_gradientTransform = GradientTransform();
  281. m_dependencyMonitor.Reset();
  282. GradientTransformRequestBus::Handler::BusDisconnect();
  283. LmbrCentral::DependencyNotificationBus::Handler::BusDisconnect();
  284. AZ::TickBus::Handler::BusDisconnect();
  285. GradientTransformModifierRequestBus::Handler::BusDisconnect();
  286. }
  287. bool GradientTransformComponent::ReadInConfig(const AZ::ComponentConfig* baseConfig)
  288. {
  289. if (auto config = azrtti_cast<const GradientTransformConfig*>(baseConfig))
  290. {
  291. m_configuration = *config;
  292. return true;
  293. }
  294. return false;
  295. }
  296. bool GradientTransformComponent::WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const
  297. {
  298. if (auto config = azrtti_cast<GradientTransformConfig*>(outBaseConfig))
  299. {
  300. *config = m_configuration;
  301. return true;
  302. }
  303. return false;
  304. }
  305. const GradientTransform& GradientTransformComponent::GetGradientTransform() const
  306. {
  307. AZStd::lock_guard<decltype(m_cacheMutex)> lock(m_cacheMutex);
  308. return m_gradientTransform;
  309. }
  310. void GradientTransformComponent::OnCompositionChanged()
  311. {
  312. m_dirty = true;
  313. }
  314. void GradientTransformComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  315. {
  316. if (m_dirty)
  317. {
  318. // Updating on tick to query transform bus on main thread.
  319. // Also, if the GradientTransform configuration changes, notify listeners so they can refresh themselves.
  320. const bool notifyDependentsOfChange = true;
  321. UpdateFromShape(notifyDependentsOfChange);
  322. m_dirty = false;
  323. }
  324. }
  325. void GradientTransformComponent::UpdateFromShape(bool notifyDependentsOfChange)
  326. {
  327. AZ_PROFILE_FUNCTION(Entity);
  328. AZStd::lock_guard<decltype(m_cacheMutex)> lock(m_cacheMutex);
  329. AZ::EntityId shapeReference = GetShapeEntityId();
  330. if (!shapeReference.IsValid())
  331. {
  332. return;
  333. }
  334. const GradientTransform oldGradientTransform = m_gradientTransform;
  335. AZ::Aabb shapeBounds = AZ::Aabb::CreateNull();
  336. AZ::Matrix3x4 shapeTransformInverse = AZ::Matrix3x4::CreateIdentity();
  337. AZ::Transform shapeTransform = AZ::Transform::CreateIdentity();
  338. switch (m_configuration.m_transformType)
  339. {
  340. default:
  341. case TransformType::World_Origin:
  342. {
  343. break;
  344. }
  345. case TransformType::Local_ThisEntity:
  346. {
  347. AZ::TransformBus::EventResult(shapeTransform, GetEntityId(), &AZ::TransformBus::Events::GetLocalTM);
  348. break;
  349. }
  350. case TransformType::World_ThisEntity:
  351. {
  352. AZ::TransformBus::EventResult(shapeTransform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  353. break;
  354. }
  355. case TransformType::Local_ReferenceEntity:
  356. {
  357. AZ::TransformBus::EventResult(shapeTransform, shapeReference, &AZ::TransformBus::Events::GetLocalTM);
  358. break;
  359. }
  360. case TransformType::World_ReferenceEntity:
  361. {
  362. AZ::TransformBus::EventResult(shapeTransform, shapeReference, &AZ::TransformBus::Events::GetWorldTM);
  363. break;
  364. }
  365. case TransformType::Relative:
  366. {
  367. AZ::Transform entityWorldTransform = AZ::Transform::CreateIdentity();
  368. AZ::TransformBus::EventResult(entityWorldTransform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  369. AZ::Transform referenceWorldTransform = AZ::Transform::CreateIdentity();
  370. AZ::TransformBus::EventResult(referenceWorldTransform, shapeReference, &AZ::TransformBus::Events::GetWorldTM);
  371. shapeTransform = referenceWorldTransform.GetInverse() * entityWorldTransform;
  372. break;
  373. }
  374. }
  375. // only update shape bounds with a responsive Entity reference
  376. if (!m_configuration.m_advancedMode || !m_configuration.m_overrideBounds)
  377. {
  378. // If we have a shape reference, grab its local space bounds and (inverse) transform into that local space
  379. GetObbParamsFromShape(shapeReference, shapeBounds, shapeTransformInverse);
  380. if (shapeBounds.IsValid())
  381. {
  382. m_configuration.m_bounds = shapeBounds.GetExtents();
  383. m_configuration.m_center = shapeBounds.GetCenter();
  384. }
  385. }
  386. //support overriding/ignoring elements of transform
  387. if (!m_configuration.m_advancedMode || !m_configuration.m_overrideTranslate)
  388. {
  389. m_configuration.m_translate = shapeTransform.GetTranslation();
  390. }
  391. if (!m_configuration.m_advancedMode || !m_configuration.m_overrideRotate)
  392. {
  393. m_configuration.m_rotate = shapeTransform.GetRotation().GetEulerDegrees();
  394. }
  395. if (!m_configuration.m_advancedMode || !m_configuration.m_overrideScale)
  396. {
  397. m_configuration.m_scale = AZ::Vector3(shapeTransform.GetUniformScale());
  398. }
  399. //rebuild bounds from parameters
  400. m_configuration.m_bounds = m_configuration.m_bounds.GetAbs();
  401. shapeBounds = AZ::Aabb::CreateCenterHalfExtents(m_configuration.m_center, 0.5f * m_configuration.m_bounds);
  402. //rebuild transform from parameters
  403. AZ::Matrix3x4 shapeTransformFinal;
  404. shapeTransformFinal.SetFromEulerDegrees(m_configuration.m_rotate);
  405. shapeTransformFinal.SetTranslation(m_configuration.m_translate);
  406. shapeTransformFinal.MultiplyByScale(m_configuration.m_scale);
  407. shapeTransformInverse = shapeTransformFinal.GetInverseFull();
  408. // Set everything up on the Gradient Transform
  409. const bool use3dGradients = m_configuration.m_advancedMode && m_configuration.m_is3d;
  410. m_gradientTransform = GradientTransform(
  411. shapeBounds, shapeTransformFinal, use3dGradients, m_configuration.m_frequencyZoom, m_configuration.m_wrappingType);
  412. // If the transform has changed, send out notifications.
  413. if (oldGradientTransform != m_gradientTransform)
  414. {
  415. // Always notify on the GradientTransformNotificationBus.
  416. GradientTransformNotificationBus::Event(
  417. GetEntityId(), &GradientTransformNotificationBus::Events::OnGradientTransformChanged, m_gradientTransform);
  418. // Only notify the DependencyNotificationBus when requested by the caller.
  419. if (notifyDependentsOfChange)
  420. {
  421. LmbrCentral::DependencyNotificationBus::Event(
  422. GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  423. }
  424. }
  425. }
  426. AZ::EntityId GradientTransformComponent::GetShapeEntityId() const
  427. {
  428. return m_configuration.m_advancedMode && m_configuration.m_allowReference && m_configuration.m_shapeReference.IsValid() ? m_configuration.m_shapeReference : GetEntityId();
  429. }
  430. bool GradientTransformComponent::GetAllowReference() const
  431. {
  432. return m_configuration.m_allowReference;
  433. }
  434. void GradientTransformComponent::SetAllowReference(bool value)
  435. {
  436. m_configuration.m_allowReference = value;
  437. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  438. }
  439. AZ::EntityId GradientTransformComponent::GetShapeReference() const
  440. {
  441. return m_configuration.m_shapeReference;
  442. }
  443. void GradientTransformComponent::SetShapeReference(AZ::EntityId shapeReference)
  444. {
  445. m_configuration.m_shapeReference = shapeReference;
  446. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  447. }
  448. bool GradientTransformComponent::GetOverrideBounds() const
  449. {
  450. return m_configuration.m_overrideBounds;
  451. }
  452. void GradientTransformComponent::SetOverrideBounds(bool value)
  453. {
  454. m_configuration.m_overrideBounds = value;
  455. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  456. }
  457. AZ::Vector3 GradientTransformComponent::GetBounds() const
  458. {
  459. return m_configuration.m_bounds;
  460. }
  461. void GradientTransformComponent::SetBounds(const AZ::Vector3& bounds)
  462. {
  463. m_configuration.m_bounds = bounds;
  464. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  465. }
  466. AZ::Vector3 GradientTransformComponent::GetCenter() const
  467. {
  468. return m_configuration.m_center;
  469. }
  470. void GradientTransformComponent::SetCenter(const AZ::Vector3& center)
  471. {
  472. m_configuration.m_center = center;
  473. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  474. }
  475. TransformType GradientTransformComponent::GetTransformType() const
  476. {
  477. return m_configuration.m_transformType;
  478. }
  479. void GradientTransformComponent::SetTransformType(TransformType type)
  480. {
  481. m_configuration.m_transformType = type;
  482. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  483. }
  484. bool GradientTransformComponent::GetOverrideTranslate() const
  485. {
  486. return m_configuration.m_overrideTranslate;
  487. }
  488. void GradientTransformComponent::SetOverrideTranslate(bool value)
  489. {
  490. m_configuration.m_overrideTranslate = value;
  491. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  492. }
  493. AZ::Vector3 GradientTransformComponent::GetTranslate() const
  494. {
  495. return m_configuration.m_translate;
  496. }
  497. void GradientTransformComponent::SetTranslate(const AZ::Vector3& translate)
  498. {
  499. m_configuration.m_translate = translate;
  500. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  501. }
  502. bool GradientTransformComponent::GetOverrideRotate() const
  503. {
  504. return m_configuration.m_overrideRotate;
  505. }
  506. void GradientTransformComponent::SetOverrideRotate(bool value)
  507. {
  508. m_configuration.m_overrideRotate = value;
  509. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  510. }
  511. AZ::Vector3 GradientTransformComponent::GetRotate() const
  512. {
  513. return m_configuration.m_rotate;
  514. }
  515. void GradientTransformComponent::SetRotate(const AZ::Vector3& rotate)
  516. {
  517. m_configuration.m_rotate = rotate;
  518. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  519. }
  520. bool GradientTransformComponent::GetOverrideScale() const
  521. {
  522. return m_configuration.m_overrideScale;
  523. }
  524. void GradientTransformComponent::SetOverrideScale(bool value)
  525. {
  526. m_configuration.m_overrideScale = value;
  527. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  528. }
  529. AZ::Vector3 GradientTransformComponent::GetScale() const
  530. {
  531. return m_configuration.m_scale;
  532. }
  533. void GradientTransformComponent::SetScale(const AZ::Vector3& scale)
  534. {
  535. m_configuration.m_scale = scale;
  536. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  537. }
  538. float GradientTransformComponent::GetFrequencyZoom() const
  539. {
  540. return m_configuration.m_frequencyZoom;
  541. }
  542. void GradientTransformComponent::SetFrequencyZoom(float frequencyZoom)
  543. {
  544. m_configuration.m_frequencyZoom = frequencyZoom;
  545. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  546. }
  547. WrappingType GradientTransformComponent::GetWrappingType() const
  548. {
  549. return m_configuration.m_wrappingType;
  550. }
  551. void GradientTransformComponent::SetWrappingType(WrappingType type)
  552. {
  553. m_configuration.m_wrappingType = type;
  554. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  555. }
  556. bool GradientTransformComponent::GetIs3D() const
  557. {
  558. return m_configuration.m_is3d;
  559. }
  560. void GradientTransformComponent::SetIs3D(bool value)
  561. {
  562. m_configuration.m_is3d = value;
  563. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  564. }
  565. bool GradientTransformComponent::GetAdvancedMode() const
  566. {
  567. return m_configuration.m_advancedMode;
  568. }
  569. void GradientTransformComponent::SetAdvancedMode(bool value)
  570. {
  571. m_configuration.m_advancedMode = value;
  572. LmbrCentral::DependencyNotificationBus::Event(GetEntityId(), &LmbrCentral::DependencyNotificationBus::Events::OnCompositionChanged);
  573. }
  574. } //namespace GradientSignal