From 2877a9a45a6138cb7f47ef9e24c2926740989885 Mon Sep 17 00:00:00 2001 From: VTui22 Date: Wed, 28 Feb 2024 16:13:36 +0800 Subject: [PATCH 1/2] fix inspector rotation use XYZ rotate --- Engine/Source/Editor/UILayers/Inspector.cpp | 2 +- .../Runtime/ECWorld/TransformComponent.h | 3 ++ Engine/Source/Runtime/ImGui/ImGuiUtils.hpp | 42 +++++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index 8ed3b818..b580910a 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -79,7 +79,7 @@ void UpdateComponentWidget(engine::SceneWorld* pScen if (isHeaderOpen) { - if (ImGuiUtils::ImGuiTransformProperty("Transform", pTransformComponent->GetTransform())) + if (ImGuiUtils::ImGuiTransformProperty("Transform", pTransformComponent->GetTransform(), pTransformComponent->GetInspectorEular())) { pTransformComponent->Dirty(); pTransformComponent->Build(); diff --git a/Engine/Source/Runtime/ECWorld/TransformComponent.h b/Engine/Source/Runtime/ECWorld/TransformComponent.h index 3d80f128..871c446a 100644 --- a/Engine/Source/Runtime/ECWorld/TransformComponent.h +++ b/Engine/Source/Runtime/ECWorld/TransformComponent.h @@ -37,6 +37,8 @@ class TransformComponent final #ifdef EDITOR_MODE static bool DoUseUniformScale() { return m_doUseUniformScale; } static void SetUseUniformScale(bool use) { m_doUseUniformScale = use; } + cd::Vec3f& GetInspectorEular() { return m_inspectorEular; } + void SetInspectorRotation(const cd::Vec3f& eular) { m_inspectorEular = eular; } #endif private: @@ -51,6 +53,7 @@ class TransformComponent final #ifdef EDITOR_MODE static bool m_doUseUniformScale; + cd::Vec3f m_inspectorEular = cd::Vec3f::Zero(); #endif }; diff --git a/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp b/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp index 09a5bb40..a2a80ce1 100644 --- a/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp +++ b/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp @@ -11,6 +11,36 @@ namespace ImGuiUtils { +static cd::Matrix3x3 RotateX(float rad) +{ + cd::Matrix3x3 matrixRot = cd::Matrix3x3::Identity(); + matrixRot.Data(4) = std::cos(rad); + matrixRot.Data(5) = -std::sin(rad); + matrixRot.Data(7) = std::sin(rad); + matrixRot.Data(8) = std::cos(rad); + return matrixRot; +} + +static cd::Matrix3x3 RotateY(float rad) +{ + cd::Matrix3x3 matrixRot = cd::Matrix3x3::Identity(); + matrixRot.Data(0) = std::cos(rad); + matrixRot.Data(2) = std::sin(rad); + matrixRot.Data(6) = -std::sin(rad); + matrixRot.Data(8) = std::cos(rad); + return matrixRot; +} + +static cd::Matrix3x3 RotateZ(float rad) +{ + cd::Matrix3x3 matrixRot = cd::Matrix3x3::Identity(); + matrixRot.Data(0) = std::cos(rad); + matrixRot.Data(1) = -std::sin(rad); + matrixRot.Data(3) = std::sin(rad); + matrixRot.Data(4) = std::cos(rad); + return matrixRot; +} + static bool ImGuiBoolProperty(const char* pName, bool& value) { return ImGui::Checkbox(pName, &value); @@ -185,7 +215,7 @@ static bool ImGuiVectorProperty(const char* pName, T& value, cd::Unit unit = cd: return dirty; } -static bool ImGuiTransformProperty(const char* pName, cd::Transform& value) +static bool ImGuiTransformProperty(const char* pName, cd::Transform& value, cd::Vec3f& inspectorEular) { ImGui::PushID(pName); @@ -195,13 +225,11 @@ static bool ImGuiTransformProperty(const char* pName, cd::Transform& value) dirty = true; } - cd::Vec3f eularAngles = value.GetRotation().ToEulerAngles(); - if (ImGuiVectorProperty("Rotation", eularAngles, cd::Unit::Degree, cd::Vec3f::Zero(), cd::Vec3f(360.0f))) + if (ImGuiVectorProperty("Rotation", inspectorEular, cd::Unit::Degree, cd::Vec3f(-360.0f), cd::Vec3f(360.0f), false, 0.2f)) { - float pitch = std::min(eularAngles.x(), 89.9f); - pitch = std::max(pitch, -89.9f); - - value.SetRotation(cd::Quaternion::FromPitchYawRoll(pitch, eularAngles.y(), eularAngles.z())); + // XYZ Rotate + cd::Matrix3x3 matrix = RotateX(cd::Math::DegreeToRadian(inspectorEular.x())) * RotateY(cd::Math::DegreeToRadian(inspectorEular.y())) * RotateZ(cd::Math::DegreeToRadian(inspectorEular.z())); + value.SetRotation(cd::Quaternion::FromMatrix(matrix)); dirty = true; } constexpr float labelIndetation = 10.0f; From 572acfa733f89a3aef0181581192e25dcc4305db Mon Sep 17 00:00:00 2001 From: VTui22 Date: Thu, 29 Feb 2024 16:08:43 +0800 Subject: [PATCH 2/2] update --- Engine/Source/Editor/UILayers/Inspector.cpp | 2 +- .../Runtime/ECWorld/TransformComponent.h | 8 ++- Engine/Source/Runtime/ImGui/ImGuiUtils.hpp | 60 +++++++++++-------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/Engine/Source/Editor/UILayers/Inspector.cpp b/Engine/Source/Editor/UILayers/Inspector.cpp index b580910a..6990edaa 100644 --- a/Engine/Source/Editor/UILayers/Inspector.cpp +++ b/Engine/Source/Editor/UILayers/Inspector.cpp @@ -79,7 +79,7 @@ void UpdateComponentWidget(engine::SceneWorld* pScen if (isHeaderOpen) { - if (ImGuiUtils::ImGuiTransformProperty("Transform", pTransformComponent->GetTransform(), pTransformComponent->GetInspectorEular())) + if (ImGuiUtils::ImGuiTransformProperty("Transform", pTransformComponent->GetTransform(), pTransformComponent->GetRotateEular())) { pTransformComponent->Dirty(); pTransformComponent->Build(); diff --git a/Engine/Source/Runtime/ECWorld/TransformComponent.h b/Engine/Source/Runtime/ECWorld/TransformComponent.h index 871c446a..66374032 100644 --- a/Engine/Source/Runtime/ECWorld/TransformComponent.h +++ b/Engine/Source/Runtime/ECWorld/TransformComponent.h @@ -37,8 +37,10 @@ class TransformComponent final #ifdef EDITOR_MODE static bool DoUseUniformScale() { return m_doUseUniformScale; } static void SetUseUniformScale(bool use) { m_doUseUniformScale = use; } - cd::Vec3f& GetInspectorEular() { return m_inspectorEular; } - void SetInspectorRotation(const cd::Vec3f& eular) { m_inspectorEular = eular; } + + cd::Vec3f& GetRotateEular() { return m_rotateEular; } + const cd::Vec3f& GetRotateEular() const { return m_rotateEular; } + void SetRotateEular(cd::Vec3f eular) { m_rotateEular = cd::MoveTemp(eular); } #endif private: @@ -53,7 +55,7 @@ class TransformComponent final #ifdef EDITOR_MODE static bool m_doUseUniformScale; - cd::Vec3f m_inspectorEular = cd::Vec3f::Zero(); + cd::Vec3f m_rotateEular = cd::Vec3f::Zero(); #endif }; diff --git a/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp b/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp index a2a80ce1..cbdc4ef7 100644 --- a/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp +++ b/Engine/Source/Runtime/ImGui/ImGuiUtils.hpp @@ -11,33 +11,44 @@ namespace ImGuiUtils { -static cd::Matrix3x3 RotateX(float rad) +enum class AxisDirection { - cd::Matrix3x3 matrixRot = cd::Matrix3x3::Identity(); - matrixRot.Data(4) = std::cos(rad); - matrixRot.Data(5) = -std::sin(rad); - matrixRot.Data(7) = std::sin(rad); - matrixRot.Data(8) = std::cos(rad); - return matrixRot; -} + X, + Y, + Z +}; -static cd::Matrix3x3 RotateY(float rad) +template +static cd::Matrix3x3 Rotate(float rad) { cd::Matrix3x3 matrixRot = cd::Matrix3x3::Identity(); - matrixRot.Data(0) = std::cos(rad); - matrixRot.Data(2) = std::sin(rad); - matrixRot.Data(6) = -std::sin(rad); - matrixRot.Data(8) = std::cos(rad); - return matrixRot; -} - -static cd::Matrix3x3 RotateZ(float rad) -{ - cd::Matrix3x3 matrixRot = cd::Matrix3x3::Identity(); - matrixRot.Data(0) = std::cos(rad); - matrixRot.Data(1) = -std::sin(rad); - matrixRot.Data(3) = std::sin(rad); - matrixRot.Data(4) = std::cos(rad); + float cosRad = std::cos(rad); + float sinRad = std::sin(rad); + if constexpr (AD == AxisDirection::X) + { + matrixRot.Data(4) = cosRad; + matrixRot.Data(5) = -sinRad; + matrixRot.Data(7) = sinRad; + matrixRot.Data(8) = cosRad; + } + else if constexpr (AD == AxisDirection::Y) + { + matrixRot.Data(0) = cosRad; + matrixRot.Data(2) = sinRad; + matrixRot.Data(6) = -sinRad; + matrixRot.Data(8) = cosRad; + } + else if constexpr (AD == AxisDirection::Z) + { + matrixRot.Data(0) = cosRad; + matrixRot.Data(1) = -sinRad; + matrixRot.Data(3) = sinRad; + matrixRot.Data(4) = cosRad; + } + else + { + static_assert("Unsupported axis direction."); + } return matrixRot; } @@ -227,8 +238,7 @@ static bool ImGuiTransformProperty(const char* pName, cd::Transform& value, cd:: if (ImGuiVectorProperty("Rotation", inspectorEular, cd::Unit::Degree, cd::Vec3f(-360.0f), cd::Vec3f(360.0f), false, 0.2f)) { - // XYZ Rotate - cd::Matrix3x3 matrix = RotateX(cd::Math::DegreeToRadian(inspectorEular.x())) * RotateY(cd::Math::DegreeToRadian(inspectorEular.y())) * RotateZ(cd::Math::DegreeToRadian(inspectorEular.z())); + cd::Matrix3x3 matrix = Rotate(cd::Math::DegreeToRadian(inspectorEular.x())) * Rotate(cd::Math::DegreeToRadian(inspectorEular.y())) * Rotate(cd::Math::DegreeToRadian(inspectorEular.z())); value.SetRotation(cd::Quaternion::FromMatrix(matrix)); dirty = true; }