Files
o3de/Gems/GradientSignal/Code/Source/Components/PerlinGradientComponent.h
T
Mike Balfour 48260486fb Change gradients to use cached GradientTransform instance (#6591)
* Change flow so that TerrainSystem stops responding during deactivation.
Some systems might accidentally try to call back to the TerrainSystem inside a DestroyBegin notification, so make sure it stops listening before sending out the notification.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Change gradients to cache and use a GradientTransform instance.
In my local test case, calling EBus on every call took 337 ms, using a lambda to wrap the calls took 197 ms, and using the fully cached version took 170 ms.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Remove the wrappingTransform function and go back to the switch statement.
There was a bit of overhead to each function call due to using AZStd::function that just isn't necessary for this use case.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Add profile markers to the heightfield updates so that they're more visible.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Cleared state while component is deactivated.
The state was getting refreshed even while the component was in a deactivated state, which meant that it wasn't properly notifying of state changes when it became active since it wasn't detecting an actual change.  By clearing the state when deactivated, and ensuring the state isn't getting refreshed *while* deactivated, the notifications work properly.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Fixed compile warning on unit test.

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>

* Addressed PR feedback - changed comments, reduced mutex scope

Signed-off-by: Mike Balfour <82224783+mbalfour-amzn@users.noreply.github.com>
2021-12-30 12:43:00 -06:00

97 lines
3.3 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#include <AzCore/Component/Component.h>
#include <AzCore/Component/ComponentBus.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <GradientSignal/Ebuses/GradientRequestBus.h>
#include <GradientSignal/Ebuses/GradientTransformRequestBus.h>
#include <GradientSignal/Ebuses/PerlinGradientRequestBus.h>
#include <GradientSignal/PerlinImprovedNoise.h>
namespace AZ
{
class Job;
}
namespace LmbrCentral
{
template<typename, typename>
class EditorWrappedComponentBase;
}
namespace GradientSignal
{
class PerlinGradientConfig
: public AZ::ComponentConfig
{
public:
AZ_CLASS_ALLOCATOR(PerlinGradientConfig, AZ::SystemAllocator, 0);
AZ_RTTI(PerlinGradientConfig, "{A746CFD0-7288-42F4-837D-1CDE2EAA6923}", AZ::ComponentConfig);
static void Reflect(AZ::ReflectContext* context);
int m_randomSeed = 1;
int m_octave = 1;
float m_amplitude = 1.0f;
float m_frequency = 1.0f;
};
static const AZ::Uuid PerlinGradientComponentTypeId = "{A293D617-C0F2-4D96-9DA0-791A5564878C}";
class PerlinGradientComponent
: public AZ::Component
, private GradientRequestBus::Handler
, private PerlinGradientRequestBus::Handler
, private GradientTransformNotificationBus::Handler
{
public:
template<typename, typename> friend class LmbrCentral::EditorWrappedComponentBase;
AZ_COMPONENT(PerlinGradientComponent, PerlinGradientComponentTypeId);
static void GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& services);
static void GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& services);
static void GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& services);
static void Reflect(AZ::ReflectContext* context);
PerlinGradientComponent(const PerlinGradientConfig& configuration);
PerlinGradientComponent() = default;
~PerlinGradientComponent() = default;
// AZ::Component overrides...
void Activate() override;
void Deactivate() override;
bool ReadInConfig(const AZ::ComponentConfig* baseConfig) override;
bool WriteOutConfig(AZ::ComponentConfig* outBaseConfig) const override;
// GradientRequestBus overrides...
float GetValue(const GradientSampleParams& sampleParams) const override;
private:
PerlinGradientConfig m_configuration;
AZStd::unique_ptr<PerlinImprovedNoise> m_perlinImprovedNoise;
GradientTransform m_gradientTransform;
mutable AZStd::shared_mutex m_transformMutex;
// GradientTransformNotificationBus overrides...
void OnGradientTransformChanged(const GradientTransform& newTransform) override;
// PerlinGradientRequestBus overrides...
int GetRandomSeed() const override;
void SetRandomSeed(int seed) override;
int GetOctaves() const override;
void SetOctaves(int octaves) override;
float GetAmplitude() const override;
void SetAmplitude(float amp) override;
float GetFrequency() const override;
void SetFrequency(float frequency) override;
};
}