ShapeWeightModifierComponentController.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <PostProcess/ShapeWeightModifier/ShapeWeightModifierComponentController.h>
  9. #include <LmbrCentral/Shape/ShapeComponentBus.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Asset/AssetSerializer.h>
  12. namespace AZ
  13. {
  14. namespace Render
  15. {
  16. void ShapeWeightModifierComponentController::Reflect(AZ::ReflectContext* context)
  17. {
  18. ShapeWeightModifierComponentConfig::Reflect(context);
  19. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  20. {
  21. serializeContext->Class<ShapeWeightModifierComponentController>()
  22. ->Version(0)
  23. ->Field("Configuration", &ShapeWeightModifierComponentController::m_configuration);
  24. }
  25. }
  26. void ShapeWeightModifierComponentController::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  27. {
  28. provided.push_back(AZ_CRC_CE("PostFXWeightModifierService"));
  29. }
  30. void ShapeWeightModifierComponentController::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  31. {
  32. incompatible.push_back(AZ_CRC_CE("PostFXWeightModifierService"));
  33. }
  34. void ShapeWeightModifierComponentController::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  35. {
  36. required.push_back(AZ_CRC_CE("PostFXLayerService"));
  37. required.push_back(AZ_CRC("ShapeService", 0xe86aa5fe));
  38. }
  39. ShapeWeightModifierComponentController::ShapeWeightModifierComponentController(const ShapeWeightModifierComponentConfig& config)
  40. : m_configuration(config)
  41. {
  42. }
  43. void ShapeWeightModifierComponentController::Activate(EntityId entityId)
  44. {
  45. m_entityId = entityId;
  46. PostFxWeightRequestBus::Handler::BusConnect(m_entityId);
  47. }
  48. void ShapeWeightModifierComponentController::Deactivate()
  49. {
  50. PostFxWeightRequestBus::Handler::BusDisconnect(m_entityId);
  51. m_entityId.SetInvalid();
  52. }
  53. void ShapeWeightModifierComponentController::SetConfiguration(const ShapeWeightModifierComponentConfig& config)
  54. {
  55. m_configuration = config;
  56. }
  57. // Implementation taken from GradientSignal Gem
  58. float ShapeWeightModifierComponentController::GetWeightAtPosition(const AZ::Vector3& influencerPosition) const
  59. {
  60. float distance = 0.0f;
  61. LmbrCentral::ShapeComponentRequestsBus::EventResult(distance, m_entityId, &LmbrCentral::ShapeComponentRequestsBus::Events::DistanceFromPoint, influencerPosition);
  62. // In the special case of 0 falloff, make sure that all points inside the shape (0 distance) return
  63. // 1.0, and all points outside the shape return 0.
  64. if (m_configuration.m_falloffDistance == 0.0f)
  65. {
  66. return (distance > 0.0f) ? 0.0f : 1.0f;
  67. }
  68. // Since this is outer falloff, distance should give us values from 1.0 at the minimum distance
  69. // to 0.0 at the maximum distance.
  70. return GetRatio(m_configuration.m_falloffDistance, 0.0f, distance);
  71. }
  72. // Implementation taken from GradientSignal Gem
  73. float ShapeWeightModifierComponentController::GetRatio(float maxRange, float minRange, float distance) const
  74. {
  75. // If our min/max range is equal, GetRatio() would end up dividing by infinity, so in this case
  76. // make sure that everything below or equal to the min/max value is 0.0, and everything above it is 1.0.
  77. if (maxRange == minRange)
  78. {
  79. return (distance <= minRange) ? 0.0f : 1.0f;
  80. }
  81. return AZ::GetClamp((distance - maxRange) / (minRange - maxRange), 0.0f, 1.0f);
  82. }
  83. }
  84. }