Merge pull request #1039 from aws-lumberyard-dev/transform-float-scale-3

refactor vector scale in Transform to float scale
This commit is contained in:
greerdv
2021-05-28 21:20:03 +01:00
committed by GitHub
38 changed files with 254 additions and 386 deletions
@@ -39,7 +39,7 @@ namespace AzToolsFramework
AZ::Transform result;
result.SetRotation(m_space.GetRotation() * localTransform.GetRotation());
result.SetTranslation(m_space.TransformPoint(m_nonUniformScale * localTransform.GetTranslation()));
result.SetScale(m_space.GetScale() * localTransform.GetUniformScale());
result.SetUniformScale(m_space.GetUniformScale() * localTransform.GetUniformScale());
return result;
}
@@ -28,7 +28,7 @@ namespace AzToolsFramework
AZ::Transform worldFromLocal = AZ::Transform::CreateIdentity();
AZ::TransformBus::EventResult(worldFromLocal, m_entityComponentIdPair.GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
worldFromLocal.ExtractScale();
worldFromLocal.ExtractUniformScale();
m_manipulators = AZStd::make_unique<ScaleManipulators>(worldFromLocal);
m_manipulators->Register(g_mainManipulatorManagerId);
m_manipulators->SetAxes(AZ::Vector3::CreateAxisX(), AZ::Vector3::CreateAxisY(), AZ::Vector3::CreateAxisZ());
@@ -32,7 +32,6 @@
#include <AzToolsFramework/Entity/EditorEntityContextBus.h>
#include <AzToolsFramework/Prefab/PrefabPublicInterface.h>
#include <AzToolsFramework/ToolsComponents/TransformComponentBus.h>
#include <AzToolsFramework/ToolsComponents/TransformScalePropertyHandler.h>
#include <AzToolsFramework/ToolsComponents/EditorInspectorComponentBus.h>
#include <AzToolsFramework/ToolsComponents/EditorPendingCompositionBus.h>
#include <AzToolsFramework/ViewportSelection/EditorSelectionUtil.h>
@@ -50,10 +49,10 @@ namespace AzToolsFramework
{
const AZ::u32 ParentEntityCRC = AZ_CRC("Parent Entity", 0x5b1b276c);
// Decompose a transform into euler angles in degrees, scale (along basis, any shear will be dropped), and translation.
void DecomposeTransform(const AZ::Transform& transform, AZ::Vector3& translation, AZ::Vector3& rotation, AZ::Vector3& scale)
// Decompose a transform into euler angles in degrees, uniform scale, and translation.
void DecomposeTransform(const AZ::Transform& transform, AZ::Vector3& translation, AZ::Vector3& rotation, float& scale)
{
scale = transform.GetScale();
scale = transform.GetUniformScale();
translation = transform.GetTranslation();
rotation = transform.GetRotation().GetEulerDegrees();
}
@@ -120,7 +119,7 @@ namespace AzToolsFramework
// Decompose the old slice-relative transform and set it as a our editor transform,
// since the entity is now our parent.
EditorTransform editorTransform;
DecomposeTransform(sliceRelTransform, editorTransform.m_translate, editorTransform.m_rotate, editorTransform.m_scale);
DecomposeTransform(sliceRelTransform, editorTransform.m_translate, editorTransform.m_rotate, editorTransform.m_uniformScale);
editorTransformElement.Convert<EditorTransform>(context);
editorTransformElement.SetData(context, editorTransform);
}
@@ -170,6 +169,23 @@ namespace AzToolsFramework
return true;
}
bool EditorTransformDataConverter(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
{
if (classElement.GetVersion() < 3)
{
// version 3 replaces vector scale with uniform scale but does not yet delete the legacy scale data
// in order to allow for migration
AZ::Vector3 vectorScale;
if (classElement.FindSubElementAndGetData<AZ::Vector3>(AZ_CRC_CE("Scale"), vectorScale))
{
const float uniformScale = vectorScale.GetMaxElement();
classElement.AddElementWithData(context, "UniformScale", uniformScale);
}
}
return true;
}
} // namespace Internal
TransformComponent::TransformComponent()
@@ -357,7 +373,7 @@ namespace AzToolsFramework
AZ::Transform TransformComponent::GetLocalScaleTM() const
{
return AZ::Transform::CreateUniformScale(m_editorTransform.m_scale.GetMaxElement());
return AZ::Transform::CreateUniformScale(m_editorTransform.m_uniformScale);
}
const AZ::Transform& TransformComponent::GetLocalTM()
@@ -374,12 +390,13 @@ namespace AzToolsFramework
// given a local transform, update local transform.
void TransformComponent::SetLocalTM(const AZ::Transform& finalTx)
{
AZ::Vector3 tx, rot, scale;
Internal::DecomposeTransform(finalTx, tx, rot, scale);
AZ::Vector3 tx, rot;
float uniformScale;
Internal::DecomposeTransform(finalTx, tx, rot, uniformScale);
m_editorTransform.m_translate = tx;
m_editorTransform.m_rotate = rot;
m_editorTransform.m_scale = scale;
m_editorTransform.m_uniformScale = uniformScale;
TransformChanged();
}
@@ -599,31 +616,21 @@ namespace AzToolsFramework
return result;
}
void TransformComponent::SetLocalScale(const AZ::Vector3& scale)
{
m_editorTransform.m_scale = scale;
TransformChanged();
}
AZ::Vector3 TransformComponent::GetLocalScale()
{
return m_editorTransform.m_scale;
}
AZ::Vector3 TransformComponent::GetWorldScale()
{
return GetWorldTM().GetScale();
AZ_WarningOnce("TransformComponent", false, "GetLocalScale is deprecated, please use GetLocalUniformScale instead");
return m_editorTransform.m_legacyScale;
}
void TransformComponent::SetLocalUniformScale(float scale)
{
m_editorTransform.m_scale = AZ::Vector3(scale);
m_editorTransform.m_uniformScale = scale;
TransformChanged();
}
float TransformComponent::GetLocalUniformScale()
{
return m_editorTransform.m_scale.GetMaxElement();
return m_editorTransform.m_uniformScale;
}
float TransformComponent::GetWorldUniformScale()
@@ -1141,9 +1148,10 @@ namespace AzToolsFramework
serializeContext->Class<EditorTransform>()->
Field("Translate", &EditorTransform::m_translate)->
Field("Rotate", &EditorTransform::m_rotate)->
Field("Scale", &EditorTransform::m_scale)->
Field("Scale", &EditorTransform::m_legacyScale)->
Field("Locked", &EditorTransform::m_locked)->
Version(2);
Field("UniformScale", &EditorTransform::m_uniformScale)->
Version(3, &Internal::EditorTransformDataConverter);
serializeContext->Class<Components::TransformComponent, EditorComponentBase>()->
Field("Parent Entity", &TransformComponent::m_parentEntityId)->
@@ -1202,7 +1210,7 @@ namespace AzToolsFramework
Attribute(AZ::Edit::Attributes::Suffix, " deg")->
Attribute(AZ::Edit::Attributes::ReadOnly, &EditorTransform::m_locked)->
Attribute(AZ::Edit::Attributes::SliceFlags, AZ::Edit::SliceFlags::NotPushableOnSliceRoot)->
DataElement(TransformScaleHandler, &EditorTransform::m_scale, "Scale", "Local Scale")->
DataElement(AZ::Edit::UIHandlers::Default, &EditorTransform::m_uniformScale, "Uniform Scale", "Local Uniform Scale")->
Attribute(AZ::Edit::Attributes::Step, 0.1f)->
Attribute(AZ::Edit::Attributes::ReadOnly, &EditorTransform::m_locked)
;
@@ -1230,7 +1238,8 @@ namespace AzToolsFramework
{
AzToolsFramework::ScopedUndoBatch undo("Reset transform values");
m_editorTransform.m_translate = AZ::Vector3::CreateZero();
m_editorTransform.m_scale = AZ::Vector3::CreateOne();
m_editorTransform.m_legacyScale = AZ::Vector3::CreateOne();
m_editorTransform.m_uniformScale = 1.0f;
m_editorTransform.m_rotate = AZ::Vector3::CreateZero();
OnTransformChanged();
SetDirty();
@@ -115,9 +115,7 @@ namespace AzToolsFramework
AZ::Quaternion GetLocalRotationQuaternion() override;
// Scale Modifiers
void SetLocalScale(const AZ::Vector3& scale) override;
AZ::Vector3 GetLocalScale() override;
AZ::Vector3 GetWorldScale() override;
void SetLocalUniformScale(float scale) override;
float GetLocalUniformScale() override;
@@ -30,7 +30,8 @@ namespace AzToolsFramework
EditorTransform()
{
m_translate = AZ::Vector3::CreateZero();
m_scale = AZ::Vector3::CreateOne();
m_legacyScale = AZ::Vector3::CreateOne();
m_uniformScale = 1.0f;
m_rotate = AZ::Vector3::CreateZero();
m_locked = false;
}
@@ -40,9 +41,10 @@ namespace AzToolsFramework
return EditorTransform();
}
AZ::Vector3 m_translate; //! Translation in engine units (meters)
AZ::Vector3 m_scale;
AZ::Vector3 m_rotate; //! Rotation in degrees
AZ::Vector3 m_translate; //!< Translation in engine units (meters)
AZ::Vector3 m_legacyScale; //!< Legacy vector scale value, retained only for migration.
float m_uniformScale; //!< Single scale value applied uniformly.
AZ::Vector3 m_rotate; //!< Rotation in degrees
bool m_locked;
};
@@ -1,82 +0,0 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "AzToolsFramework_precompiled.h"
#include <ToolsComponents/TransformScalePropertyHandler.h>
#include <AzCore/Math/Transform.h>
#include <AzCore/Math/Vector3.h>
namespace AzToolsFramework
{
void RegisterTransformScaleHandler()
{
PropertyTypeRegistrationMessages::Bus::Broadcast(&PropertyTypeRegistrationMessages::RegisterPropertyType, aznew Components::TransformScalePropertyHandler());
}
namespace Components
{
AZ::u32 TransformScalePropertyHandler::GetHandlerName(void) const
{
return TransformScaleHandler;
}
QWidget* TransformScalePropertyHandler::CreateGUI(QWidget* parent)
{
AzQtComponents::DoubleSpinBox* newCtrl = new AzQtComponents::DoubleSpinBox(parent);
connect(newCtrl, QOverload<double>::of(&AzQtComponents::DoubleSpinBox::valueChanged), newCtrl, [newCtrl]()
{
AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestWrite, newCtrl);
});
newCtrl->setMinimum(AZ::MinTransformScale);
newCtrl->setMaximum(AZ::MaxTransformScale);
return newCtrl;
}
void TransformScalePropertyHandler::ConsumeAttribute(AzQtComponents::DoubleSpinBox* GUI, AZ::u32 attrib,
AzToolsFramework::PropertyAttributeReader* attrValue, [[maybe_unused]] const char* debugName)
{
if (attrib == AZ::Edit::Attributes::Suffix)
{
AZStd::string label;
if (attrValue->Read<AZStd::string>(label))
{
GUI->setSuffix(label.c_str());
}
}
}
void TransformScalePropertyHandler::WriteGUIValuesIntoProperty([[maybe_unused]] size_t index, AzQtComponents::DoubleSpinBox* GUI,
AZ::Vector3& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
{
const float value = aznumeric_cast<float>(GUI->value());
const float currentMaxElement = instance.GetMaxElement();
if (currentMaxElement != 0.0f)
{
instance *= value / currentMaxElement;
}
else
{
instance = AZ::Vector3(value);
}
}
bool TransformScalePropertyHandler::ReadValuesIntoGUI([[maybe_unused]] size_t index, AzQtComponents::DoubleSpinBox* GUI,
const AZ::Vector3& instance, [[maybe_unused]] AzToolsFramework::InstanceDataNode* node)
{
QSignalBlocker signalBlocker(GUI);
GUI->setValue(instance.GetMaxElement());
return true;
}
} // namespace Components
} // namespace AzToolsFramework
@@ -1,56 +0,0 @@
/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#pragma once
#if !defined(Q_MOC_RUN)
#include <AzQtComponents/Components/Widgets/SpinBox.h>
#include <AzToolsFramework/UI/PropertyEditor/PropertyEditorAPI.h>
#include <AzCore/Math/Vector3.h>
#endif
namespace AzToolsFramework
{
namespace Components
{
static const AZ::Crc32 TransformScaleHandler = AZ_CRC_CE("TransformScale");
//! Handler to allow the scale field inside the Transform Component to be represented as a single value in
//! the editor, but stored internally as a Vector3.
//! The purpose for this is to prevent any new entities being created with non-uniform scale on the Transform
//! Component, but preserve the data required for migrating any existing entities to use the Non-Uniform Scale
//! Component, until all migration work is completed.
//! The value shown in the editor will be the maximum value from the scale vector, and changing the value in
//! the editor will update the vector so that its maximum value matches the newly edited value, but its
//! components retain their existing proportion.
//! For example, if the current vector scale is (2, 3, 4), the value in the editor will appear as 4. If the value
//! in the editor is updated to 2, then the vector scale will update to (1, 1.5, 2), keeping the same proportion
//! between the x, y and z components.
class TransformScalePropertyHandler
: public QObject
, public AzToolsFramework::PropertyHandler<AZ::Vector3, AzQtComponents::DoubleSpinBox>
{
Q_OBJECT //AUTOMOC
public:
AZ_CLASS_ALLOCATOR(TransformScalePropertyHandler, AZ::SystemAllocator, 0);
AZ::u32 GetHandlerName(void) const override;
QWidget* CreateGUI(QWidget* parent) override;
void ConsumeAttribute(AzQtComponents::DoubleSpinBox* GUI, AZ::u32 attrib,
AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName) override;
void WriteGUIValuesIntoProperty(size_t index, AzQtComponents::DoubleSpinBox* GUI,
AZ::Vector3& instance, AzToolsFramework::InstanceDataNode* node) override;
bool ReadValuesIntoGUI(size_t index, AzQtComponents::DoubleSpinBox* GUI,
const AZ::Vector3& instance, AzToolsFramework::InstanceDataNode* node) override;
};
} // namespace Components
} // namespace AzToolsFramework
@@ -16,7 +16,6 @@
#include <AzToolsFramework/ToolsComponents/EditorEntityIdContainer.h>
#include <AzToolsFramework/UI/PropertyEditor/PropertyAudioCtrlTypes.h>
#include <AzToolsFramework/UI/PropertyEditor/GenericComboBoxCtrl.h>
#include <AzToolsFramework/ToolsComponents/TransformScalePropertyHandler.h>
namespace AzToolsFramework
{
@@ -38,7 +37,6 @@ namespace AzToolsFramework
void RegisterButtonPropertyHandlers();
void RegisterMultiLineEditHandler();
void RegisterCrcHandler();
void RegisterTransformScaleHandler();
void ReflectPropertyEditor(AZ::ReflectContext* context);
namespace Components
@@ -192,7 +190,6 @@ namespace AzToolsFramework
RegisterVectorHandlers();
RegisterButtonPropertyHandlers();
RegisterMultiLineEditHandler();
RegisterTransformScaleHandler();
// GenericComboBoxHandlers
RegisterGenericComboBoxHandler<AZ::Crc32>();
@@ -293,8 +293,6 @@ set(FILES
ToolsComponents/TransformComponent.h
ToolsComponents/TransformComponent.cpp
ToolsComponents/TransformComponentBus.h
ToolsComponents/TransformScalePropertyHandler.cpp
ToolsComponents/TransformScalePropertyHandler.h
ToolsComponents/ScriptEditorComponent.cpp
ToolsComponents/ScriptEditorComponent.h
ToolsComponents/ToolsAssetCatalogComponent.cpp
@@ -141,7 +141,7 @@ namespace UnitTest
// Set the new entity's transform to non zero values
// This helps validate in comparison tests that the transform values of created entities persist during slice operations
entityTransform->SetLocalScale(AZ::Vector3(5, 5, 5));
entityTransform->SetLocalUniformScale(5);
entityTransform->SetLocalRotation(AZ::Vector3RadToDeg(AZ::Vector3(90, 90, 90)));
entityTransform->SetLocalTranslation(AZ::Vector3(100, 100, 100));