Skip to content

Commit

Permalink
fix inspector rotation use XYZ rotate (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
VTui22 authored Feb 29, 2024
1 parent 25b8341 commit 096981e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Engine/Source/Editor/UILayers/Inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void UpdateComponentWidget<engine::TransformComponent>(engine::SceneWorld* pScen

if (isHeaderOpen)
{
if (ImGuiUtils::ImGuiTransformProperty("Transform", pTransformComponent->GetTransform()))
if (ImGuiUtils::ImGuiTransformProperty("Transform", pTransformComponent->GetTransform(), pTransformComponent->GetRotateEular()))
{
pTransformComponent->Dirty();
pTransformComponent->Build();
Expand Down
5 changes: 5 additions & 0 deletions Engine/Source/Runtime/ECWorld/TransformComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class TransformComponent final
#ifdef EDITOR_MODE
static bool DoUseUniformScale() { return m_doUseUniformScale; }
static void SetUseUniformScale(bool use) { m_doUseUniformScale = use; }

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:
Expand All @@ -51,6 +55,7 @@ class TransformComponent final

#ifdef EDITOR_MODE
static bool m_doUseUniformScale;
cd::Vec3f m_rotateEular = cd::Vec3f::Zero();
#endif
};

Expand Down
52 changes: 45 additions & 7 deletions Engine/Source/Runtime/ImGui/ImGuiUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,47 @@
namespace ImGuiUtils
{

enum class AxisDirection
{
X,
Y,
Z
};

template<AxisDirection AD>
static cd::Matrix3x3 Rotate(float rad)
{
cd::Matrix3x3 matrixRot = cd::Matrix3x3::Identity();
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;
}

static bool ImGuiBoolProperty(const char* pName, bool& value)
{
return ImGui::Checkbox(pName, &value);
Expand Down Expand Up @@ -185,7 +226,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);

Expand All @@ -195,13 +236,10 @@ 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()));
cd::Matrix3x3 matrix = Rotate<AxisDirection::X>(cd::Math::DegreeToRadian(inspectorEular.x())) * Rotate<AxisDirection::Y>(cd::Math::DegreeToRadian(inspectorEular.y())) * Rotate<AxisDirection::Z>(cd::Math::DegreeToRadian(inspectorEular.z()));
value.SetRotation(cd::Quaternion::FromMatrix(matrix));
dirty = true;
}
constexpr float labelIndetation = 10.0f;
Expand Down

0 comments on commit 096981e

Please sign in to comment.