Files
o3de/Gems/GradientSignal/Code/Source/GradientSampler.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

219 lines
11 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
*
*/
#include <GradientSignal/GradientSampler.h>
#include <AzCore/Debug/Profiler.h>
#include <AzCore/Math/Vector3.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Serialization/SerializeContext.h>
#include <GradientSignal/Ebuses/GradientRequestBus.h>
#include <GradientSignal/Ebuses/GradientTransformRequestBus.h>
#include <GradientSignal/Util.h>
namespace GradientSignal
{
void GradientSampler::Reflect(AZ::ReflectContext* context)
{
AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context);
if (serialize)
{
serialize->Class<GradientSampler>()
->Version(1)
->Field("GradientId", &GradientSampler::m_gradientId)
->Field("Opacity", &GradientSampler::m_opacity)
->Field("InvertInput", &GradientSampler::m_invertInput)
->Field("EnableTransform", &GradientSampler::m_enableTransform)
->Field("Translate", &GradientSampler::m_translate)
->Field("Scale", &GradientSampler::m_scale)
->Field("Rotate", &GradientSampler::m_rotate)
->Field("EnableLevels", &GradientSampler::m_enableLevels)
->Field("InputMid", &GradientSampler::m_inputMid)
->Field("InputMin", &GradientSampler::m_inputMin)
->Field("InputMax", &GradientSampler::m_inputMax)
->Field("OutputMin", &GradientSampler::m_outputMin)
->Field("OutputMax", &GradientSampler::m_outputMax)
;
AZ::EditContext* edit = serialize->GetEditContext();
if (edit)
{
edit->Class<GradientSampler>("Gradient Sampler", "")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
->DataElement(0, &GradientSampler::m_gradientId, "Gradient Entity Id", "Entity with attached gradient component")
->Attribute(AZ::Edit::Attributes::RequiredService, AZ_CRC("GradientService", 0x21c18d23))
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->Attribute(AZ::Edit::Attributes::ChangeValidate, &GradientSampler::ValidatePotentialEntityId)
->DataElement(AZ::Edit::UIHandlers::Slider, &GradientSampler::m_opacity, "Opacity", "Factor multiplied by the current gradient before mixing.")
->Attribute(AZ::Edit::Attributes::Min, 0.0f)
->Attribute(AZ::Edit::Attributes::Max, 1.0f)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->ClassElement(AZ::Edit::ClassElements::Group, "Advanced")
->Attribute(AZ::Edit::Attributes::AutoExpand, false)
->DataElement(0, &GradientSampler::m_invertInput, "Invert Input", "")
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(0, &GradientSampler::m_enableTransform, "Enable Transform", "")
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(0, &GradientSampler::m_translate, "Translate", "")
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreTransformSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(0, &GradientSampler::m_scale, "Scale", "")
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreTransformSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(0, &GradientSampler::m_rotate, "Rotate", "Rotation in degrees.")
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreTransformSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(0, &GradientSampler::m_enableLevels, "Enable Levels", "")
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(AZ::Edit::UIHandlers::Slider, &GradientSampler::m_inputMid, "Input Mid", "")
->Attribute(AZ::Edit::Attributes::Min, 0.0f)
->Attribute(AZ::Edit::Attributes::Max, 10.0f)
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreLevelSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(AZ::Edit::UIHandlers::Slider, &GradientSampler::m_inputMin, "Input Min", "")
->Attribute(AZ::Edit::Attributes::Min, 0.0f)
->Attribute(AZ::Edit::Attributes::Max, 1.0f)
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreLevelSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(AZ::Edit::UIHandlers::Slider, &GradientSampler::m_inputMax, "Input Max", "")
->Attribute(AZ::Edit::Attributes::Min, 0.0f)
->Attribute(AZ::Edit::Attributes::Max, 1.0f)
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreLevelSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(AZ::Edit::UIHandlers::Slider, &GradientSampler::m_outputMin, "Output Min", "")
->Attribute(AZ::Edit::Attributes::Min, 0.0f)
->Attribute(AZ::Edit::Attributes::Max, 1.0f)
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreLevelSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->DataElement(AZ::Edit::UIHandlers::Slider, &GradientSampler::m_outputMax, "Output Max", "")
->Attribute(AZ::Edit::Attributes::Min, 0.0f)
->Attribute(AZ::Edit::Attributes::Max, 1.0f)
->Attribute(AZ::Edit::Attributes::ReadOnly, &GradientSampler::AreLevelSettingsDisabled)
->Attribute(AZ::Edit::Attributes::ChangeNotify, &GradientSampler::ChangeNotify)
->ClassElement(AZ::Edit::ClassElements::Group, "Preview (Inbound)")
->Attribute(AZ::Edit::Attributes::AutoExpand, false)
->UIElement("GradientPreviewer", "Previewer")
->Attribute(AZ::Edit::Attributes::NameLabelOverride, "")
->Attribute(AZ_CRC("GradientSampler", 0xaec97010), &GradientSampler::GetSampler)
;
}
}
if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
{
behaviorContext->Class<GradientSampler>()
->Constructor()
->Property("gradientId", BehaviorValueProperty(&GradientSampler::m_gradientId))
->Property("opacity", BehaviorValueProperty(&GradientSampler::m_opacity))
->Property("invertInput", BehaviorValueProperty(&GradientSampler::m_invertInput))
->Property("enableLevels", BehaviorValueProperty(&GradientSampler::m_enableLevels))
->Property("inputMid", BehaviorValueProperty(&GradientSampler::m_inputMid))
->Property("inputMin", BehaviorValueProperty(&GradientSampler::m_inputMin))
->Property("inputMax", BehaviorValueProperty(&GradientSampler::m_inputMax))
->Property("outputMin", BehaviorValueProperty(&GradientSampler::m_outputMin))
->Property("outputMax", BehaviorValueProperty(&GradientSampler::m_outputMax))
->Property("enableTransforms", BehaviorValueProperty(&GradientSampler::m_enableTransform))
->Property("translation", BehaviorValueProperty(&GradientSampler::m_translate))
->Property("scale", BehaviorValueProperty(&GradientSampler::m_scale))
->Property("rotation", BehaviorValueProperty(&GradientSampler::m_rotate))
;
}
}
GradientSampler* GradientSampler::GetSampler()
{
return this;
}
AZ::u32 GradientSampler::ChangeNotify() const
{
return AZ::Edit::PropertyRefreshLevels::AttributesAndValues;
}
AZ::Outcome<void, AZStd::string> GradientSampler::ValidatePotentialEntityId(void* newValue, const AZ::Uuid& valueType) const
{
if (azrtti_typeid<AZ::EntityId>() != valueType)
{
AZ_Assert(false, "Unexpected value type");
return AZ::Failure(AZStd::string("Trying to set an entity ID to something that isn't an entity ID!"));
}
AZ::EntityId potentialEntityId = static_cast<AZ::EntityId>(*((AZ::EntityId*)newValue));
// Always allow a clear, no need to check
if (!potentialEntityId.IsValid())
{
return AZ::Success();
}
// Prevent setting the parent to the entity itself.
if (potentialEntityId == m_ownerEntityId)
{
return AZ::Failure(AZStd::string("You cannot set a gradient or modifier to point to itself!"));
}
else
{
bool loopCreated = false;
// See if we are in the new connections network already
GradientRequestBus::EventResult(loopCreated, potentialEntityId, &GradientRequestBus::Events::IsEntityInHierarchy, m_ownerEntityId);
if (loopCreated)
{
return AZ::Failure(AZStd::string("Setting this entity reference will cause a cyclical loop, which is not allowed!"));
}
}
return AZ::Success();
}
bool GradientSampler::ValidateGradientEntityId()
{
AZ::ComponentValidationResult result = ValidatePotentialEntityId(&m_gradientId, azrtti_typeid<AZ::EntityId>());
if (!result.IsSuccess())
{
AZ_Warning("GradientSignal", false, "Gradient Sampler refers to an entity that will cause a cyclical loop, which is not allowed! Clearing gradient entity id!");
m_gradientId = AZ::EntityId();
return false;
}
return true;
}
bool GradientSampler::IsEntityInHierarchy(const AZ::EntityId& entityId) const
{
if (entityId == m_gradientId)
{
return true;
}
bool inHierarchy = false;
GradientRequestBus::EventResult(inHierarchy, m_gradientId, &GradientRequestBus::Events::IsEntityInHierarchy, entityId);
return inHierarchy;
}
bool GradientSampler::AreLevelSettingsDisabled() const
{
return !m_enableLevels;
}
bool GradientSampler::AreTransformSettingsDisabled() const
{
return !m_enableTransform;
}
}