Files
o3de/Gems/FastNoise/Code/Source/FastNoiseGradientComponent.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

132 lines
5.2 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/Asset/AssetCommon.h>
#include <AzCore/RTTI/TypeInfo.h>
#include <GradientSignal/Ebuses/GradientRequestBus.h>
#include <GradientSignal/Ebuses/GradientTransformRequestBus.h>
#include <FastNoise/Ebuses/FastNoiseGradientRequestBus.h>
#include <External/FastNoise/FastNoise.h>
namespace AZ
{
AZ_TYPE_INFO_SPECIALIZE(FastNoise::Interp, "{E1D450B5-CE30-450F-BD29-382DA64A469B}");
AZ_TYPE_INFO_SPECIALIZE(FastNoise::NoiseType, "{0B54F0FB-F5D2-49DF-B5F8-30B209978D59}");
AZ_TYPE_INFO_SPECIALIZE(FastNoise::FractalType, "{AAC4BD68-217B-4247-A1CE-E5E98B15956F}");
AZ_TYPE_INFO_SPECIALIZE(FastNoise::CellularDistanceFunction, "{761E3584-FACD-4355-BAD6-DA4D2DAFFD8C}");
AZ_TYPE_INFO_SPECIALIZE(FastNoise::CellularReturnType, "{31CDBAC6-882C-4330-8C68-4039FE4D1A48}");
}
namespace LmbrCentral
{
template<typename, typename>
class EditorWrappedComponentBase;
}
namespace FastNoiseGem
{
class FastNoiseGradientConfig
: public AZ::ComponentConfig
{
public:
AZ_CLASS_ALLOCATOR(FastNoiseGradientConfig, AZ::SystemAllocator, 0);
AZ_RTTI(FastNoiseGradientConfig, "{831C1F11-5898-4FBF-B4CF-92B757A907A8}", AZ::ComponentConfig);
static void Reflect(AZ::ReflectContext* context);
AZ::u32 GetCellularParameterVisibility() const;
AZ::u32 GetFractalParameterVisbility() const;
AZ::u32 GetFrequencyParameterVisbility() const;
AZ::u32 GetInterpParameterVisibility() const;
int m_seed = 1;
float m_frequency = 1.f;
FastNoise::Interp m_interp = FastNoise::Interp::Quintic;
FastNoise::NoiseType m_noiseType = FastNoise::NoiseType::PerlinFractal;
int m_octaves = 4;
float m_lacunarity = 2.f;
float m_gain = 0.5;
FastNoise::FractalType m_fractalType = FastNoise::FractalType::FBM;
FastNoise::CellularDistanceFunction m_cellularDistanceFunction = FastNoise::CellularDistanceFunction::Euclidean;
FastNoise::CellularReturnType m_cellularReturnType = FastNoise::CellularReturnType::CellValue;
float m_cellularJitter = 0.45f;
};
static const AZ::Uuid FastNoiseGradientComponentTypeId = "{81449CDF-D6DE-46DA-A50C-576B0B921311}";
class FastNoiseGradientComponent
: public AZ::Component
, private GradientSignal::GradientRequestBus::Handler
, private FastNoiseGradientRequestBus::Handler
, private GradientSignal::GradientTransformNotificationBus::Handler
{
public:
friend class EditorFastNoiseGradientComponent;
template<typename, typename> friend class LmbrCentral::EditorWrappedComponentBase;
AZ_COMPONENT(FastNoiseGradientComponent, FastNoiseGradientComponentTypeId);
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);
FastNoiseGradientComponent(const FastNoiseGradientConfig& configuration);
FastNoiseGradientComponent() = 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 GradientSignal::GradientSampleParams& sampleParams) const override;
protected:
FastNoiseGradientConfig m_configuration;
FastNoise m_generator;
GradientSignal::GradientTransform m_gradientTransform;
mutable AZStd::shared_mutex m_transformMutex;
// GradientTransformNotificationBus overrides...
void OnGradientTransformChanged(const GradientSignal::GradientTransform& newTransform) override;
// FastNoiseGradientRequest overrides...
int GetRandomSeed() const override;
void SetRandomSeed(int seed) override;
float GetFrequency() const override;
void SetFrequency(float freq) override;
FastNoise::Interp GetInterpolation() const override;
void SetInterpolation(FastNoise::Interp interp) override;
FastNoise::NoiseType GetNoiseType() const override;
void SetNoiseType(FastNoise::NoiseType type) override;
int GetOctaves() const override;
void SetOctaves(int octaves) override;
float GetLacunarity() const override;
void SetLacunarity(float lacunarity) override;
float GetGain() const override;
void SetGain(float gain) override;
FastNoise::FractalType GetFractalType() const override;
void SetFractalType(FastNoise::FractalType type) override;
template <typename TValueType, TValueType FastNoiseGradientConfig::*TConfigMember, void (FastNoise::*TMethod)(TValueType)>
void SetConfigValue(TValueType value);
};
} //namespace FastNoiseGem