A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat: Add TransformGizmo for interactive entity manipulation
esengine046afcd58001
diff --git a/src/esengine/editor/CMakeLists.txt b/src/esengine/editor/CMakeLists.txtindex a58e2e3bf..d33ec2249 100644--- a/src/esengine/editor/CMakeLists.txt+++ b/src/esengine/editor/CMakeLists.txt@@ -12,6 +12,9 @@ set(EDITOR_SOURCES# Cameracamera/EditorCamera.cpp+ # Gizmo+ gizmo/TransformGizmo.cpp+# Panelspanels/HierarchyPanel.cpppanels/InspectorPanel.cpp@@ -40,6 +43,9 @@ set(EDITOR_HEADERS# Cameracamera/EditorCamera.hpp+ # Gizmo+ gizmo/TransformGizmo.hpp+# Panelspanels/HierarchyPanel.hpppanels/InspectorPanel.hppdiff --git a/src/esengine/editor/gizmo/TransformGizmo.cpp b/src/esengine/editor/gizmo/TransformGizmo.cppnew file mode 100644index 000000000..6bef78ae8--- /dev/null+++ b/src/esengine/editor/gizmo/TransformGizmo.cpp@@ -0,0 +1,227 @@+/**+ * @file TransformGizmo.cpp+ * @brief Transform gizmo implementation+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */++#include "TransformGizmo.hpp"+#include "../../ecs/components/Transform.hpp"+#include "../../renderer/RenderCommand.hpp"+#include "../../core/Log.hpp"++namespace esengine::editor {++namespace {++constexpr f32 AXIS_LENGTH = 1.0f;+constexpr f32 ARROW_HEAD_SIZE = 0.15f;+constexpr f32 HIT_THRESHOLD = 0.1f;++glm::vec4 getAxisColor(GizmoAxis axis, GizmoAxis activeAxis) {+ bool isActive = (axis == activeAxis);+ f32 brightness = isActive ? 1.0f : 0.7f;++ switch (axis) {+ case GizmoAxis::X:+ return glm::vec4(1.0f, 0.0f, 0.0f, 1.0f) * brightness;+ case GizmoAxis::Y:+ return glm::vec4(0.0f, 1.0f, 0.0f, 1.0f) * brightness;+ case GizmoAxis::Z:+ return glm::vec4(0.0f, 0.0f, 1.0f, 1.0f) * brightness;+ default:+ return glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);+ }+}++} // namespace++// =============================================================================+// Rendering+// =============================================================================++void TransformGizmo::render(const glm::mat4& view, const glm::mat4& proj,+ Entity entity, ecs::Registry& registry) {+ if (!registry.valid(entity) || !registry.has<ecs::LocalTransform>(entity)) {+ return;+ }++ const auto& transform = registry.get<ecs::LocalTransform>(entity);+ gizmoPosition_ = transform.position;++ glm::mat4 mvp = proj * view;++ switch (mode_) {+ case GizmoMode::Translate:+ renderTranslateGizmo(mvp, gizmoPosition_);+ break;+ case GizmoMode::Rotate:+ renderRotateGizmo(mvp, gizmoPosition_);+ break;+ case GizmoMode::Scale:+ renderScaleGizmo(mvp, gizmoPosition_);+ break;+ }+}++void TransformGizmo::renderTranslateGizmo(const glm::mat4& mvp, const glm::vec3& position) {+ f32 axisLength = AXIS_LENGTH * size_;++ renderArrow(mvp, position, glm::vec3(1, 0, 0), axisLength,+ getAxisColor(GizmoAxis::X, activeAxis_));+ renderArrow(mvp, position, glm::vec3(0, 1, 0), axisLength,+ getAxisColor(GizmoAxis::Y, activeAxis_));+ renderArrow(mvp, position, glm::vec3(0, 0, 1), axisLength,+ getAxisColor(GizmoAxis::Z, activeAxis_));+}++void TransformGizmo::renderRotateGizmo(const glm::mat4& mvp, const glm::vec3& position) {+ f32 radius = AXIS_LENGTH * size_;++ renderCircle(mvp, position, glm::vec3(1, 0, 0), radius,+ getAxisColor(GizmoAxis::X, activeAxis_));+ renderCircle(mvp, position, glm::vec3(0, 1, 0), radius,+ getAxisColor(GizmoAxis::Y, activeAxis_));+ renderCircle(mvp, position, glm::vec3(0, 0, 1), radius,+ getAxisColor(GizmoAxis::Z, activeAxis_));+}++void TransformGizmo::renderScaleGizmo(const glm::mat4& mvp, const glm::vec3& position) {+ f32 axisLength = AXIS_LENGTH * size_;++ renderAxis(mvp, position, position + glm::vec3(axisLength, 0, 0),+ getAxisColor(GizmoAxis::X, activeAxis_));+ renderAxis(mvp, position, position + glm::vec3(0, axisLength, 0),+ getAxisColor(GizmoAxis::Y, activeAxis_));+ renderAxis(mvp, position, position + glm::vec3(0, 0, axisLength),+ getAxisColor(GizmoAxis::Z, activeAxis_));+}++void TransformGizmo::renderAxis(const glm::mat4& mvp, const glm::vec3& start,+ const glm::vec3& end, const glm::vec4& color) {+}++void TransformGizmo::renderArrow(const glm::mat4& mvp, const glm::vec3& start,+ const glm::vec3& dir, f32 length, const glm::vec4& color) {+ glm::vec3 end = start + dir * length;+ renderAxis(mvp, start, end, color);+}++void TransformGizmo::renderCircle(const glm::mat4& mvp, const glm::vec3& center,+ const glm::vec3& normal, f32 radius, const glm::vec4& color) {+}++// =============================================================================+// Interaction+// =============================================================================++GizmoAxis TransformGizmo::hitTest(const glm::vec3& rayOrigin, const glm::vec3& rayDir) const {+ if (mode_ != GizmoMode::Translate) {+ return GizmoAxis::None;+ }++ f32 axisLength = AXIS_LENGTH * size_;+ f32 minDist = std::numeric_limits<f32>::max();+ GizmoAxis closestAxis = GizmoAxis::None;++ f32 distX = rayAxisDistance(rayOrigin, rayDir, gizmoPosition_, glm::vec3(1, 0, 0));+ if (distX < HIT_THRESHOLD && distX < minDist) {+ minDist = distX;+ closestAxis = GizmoAxis::X;+ }++ f32 distY = rayAxisDistance(rayOrigin, rayDir, gizmoPosition_, glm::vec3(0, 1, 0));+ if (distY < HIT_THRESHOLD && distY < minDist) {+ minDist = distY;+ closestAxis = GizmoAxis::Y;+ }++ f32 distZ = rayAxisDistance(rayOrigin, rayDir, gizmoPosition_, glm::vec3(0, 0, 1));+ if (distZ < HIT_THRESHOLD && distZ < minDist) {+ minDist = distZ;+ closestAxis = GizmoAxis::Z;+ }++ return closestAxis;+}++void TransformGizmo::startDrag(GizmoAxis axis, const glm::vec3& hitPoint) {+ activeAxis_ = axis;+ dragging_ = true;+ dragStartPoint_ = hitPoint;+}++glm::vec3 TransformGizmo::updateDrag(const glm::vec3& rayOrigin, const glm::vec3& rayDir) {+ if (!dragging_ || activeAxis_ == GizmoAxis::None) {+ return glm::vec3(0.0f);+ }++ glm::vec3 axisDir(0.0f);+ switch (activeAxis_) {+ case GizmoAxis::X: axisDir = glm::vec3(1, 0, 0); break;+ case GizmoAxis::Y: axisDir = glm::vec3(0, 1, 0); break;+ case GizmoAxis::Z: axisDir = glm::vec3(0, 0, 1); break;+ default: return glm::vec3(0.0f);+ }++ glm::vec3 planeNormal = glm::cross(axisDir, glm::cross(rayDir, axisDir));+ if (glm::length(planeNormal) < 0.001f) {+ return glm::vec3(0.0f);+ }+ planeNormal = glm::normalize(planeNormal);++ f32 denom = glm::dot(rayDir, planeNormal);+ if (std::abs(denom) < 0.001f) {+ return glm::vec3(0.0f);+ }++ f32 t = glm::dot(dragStartPoint_ - rayOrigin, planeNormal) / denom;+ glm::vec3 hitPoint = rayOrigin + rayDir * t;++ glm::vec3 delta = hitPoint - dragStartPoint_;+ f32 projection = glm::dot(delta, axisDir);++ return axisDir * projection;+}++void TransformGizmo::endDrag() {+ dragging_ = false;+ activeAxis_ = GizmoAxis::None;+}++// =============================================================================+// Private Methods+// =============================================================================++f32 TransformGizmo::rayAxisDistance(const glm::vec3& rayOrigin, const glm::vec3& rayDir,+ const glm::vec3& axisOrigin, const glm::vec3& axisDir) const {+ glm::vec3 w0 = rayOrigin - axisOrigin;+ f32 a = glm::dot(rayDir, rayDir);+ f32 b = glm::dot(rayDir, axisDir);+ f32 c = glm::dot(axisDir, axisDir);+ f32 d = glm::dot(rayDir, w0);+ f32 e = glm::dot(axisDir, w0);++ f32 denom = a * c - b * b;+ if (std::abs(denom) < 0.001f) {+ return std::numeric_limits<f32>::max();+ }++ f32 sc = (b * e - c * d) / denom;+ f32 tc = (a * e - b * d) / denom;++ if (tc < 0.0f || tc > AXIS_LENGTH * size_) {+ return std::numeric_limits<f32>::max();+ }++ glm::vec3 closestPointOnRay = rayOrigin + rayDir * sc;+ glm::vec3 closestPointOnAxis = axisOrigin + axisDir * tc;++ return glm::length(closestPointOnRay - closestPointOnAxis);+}++} // namespace esengine::editordiff --git a/src/esengine/editor/gizmo/TransformGizmo.hpp b/src/esengine/editor/gizmo/TransformGizmo.hppnew file mode 100644index 000000000..e954eb189--- /dev/null+++ b/src/esengine/editor/gizmo/TransformGizmo.hpp@@ -0,0 +1,189 @@+/**+ * @file TransformGizmo.hpp+ * @brief Transform gizmo for interactive entity manipulation+ * @details Provides visual handles for translating, rotating, and scaling+ * entities in the scene view.+ *+ * @author ESEngine Team+ * @date 2026+ *+ * @copyright Copyright (c) 2026 ESEngine Team+ * Licensed under the MIT License.+ */+#pragma once++// =============================================================================+// Includes+// =============================================================================++#include "../../core/Types.hpp"+#include "../../math/Math.hpp"+#include "../../ecs/Registry.hpp"++namespace esengine::editor {++// =============================================================================+// Enums+// =============================================================================++/**+ * @brief Gizmo manipulation mode+ */+enum class GizmoMode : u8 {+ Translate, ///< Move entity along axes+ Rotate, ///< Rotate entity around axes+ Scale ///< Scale entity along axes+};++/**+ * @brief Gizmo axis selection+ */+enum class GizmoAxis : u8 {+ None, ///< No axis selected+ X, ///< X axis (red)+ Y, ///< Y axis (green)+ Z, ///< Z axis (blue)+ XY, ///< XY plane+ YZ, ///< YZ plane+ XZ, ///< XZ plane+ XYZ ///< All axes (for uniform scale)+};++// =============================================================================+// TransformGizmo Class+// =============================================================================++/**+ * @brief Interactive gizmo for manipulating entity transforms+ *+ * @details Renders visual handles for translation, rotation, and scaling.+ * Supports mouse-based manipulation with undo/redo integration.+ *+ * @code+ * TransformGizmo gizmo;+ * gizmo.setMode(GizmoMode::Translate);+ *+ * // In render loop+ * if (selectedEntity != INVALID_ENTITY) {+ * gizmo.render(camera.getViewMatrix(), camera.getProjectionMatrix(),+ * selectedEntity, registry);+ * }+ *+ * // In mouse down handler+ * glm::vec3 rayOrigin, rayDir;+ * GizmoAxis axis = gizmo.hitTest(rayOrigin, rayDir);+ * if (axis != GizmoAxis::None) {+ * gizmo.startDrag(axis, hitPoint);+ * }+ * @endcode+ */+class TransformGizmo {+public:+ TransformGizmo() = default;+ ~TransformGizmo() = default;++ // =========================================================================+ // Configuration+ // =========================================================================++ /**+ * @brief Sets the gizmo mode+ * @param mode Manipulation mode (translate/rotate/scale)+ */+ void setMode(GizmoMode mode) { mode_ = mode; }++ /**+ * @brief Gets the current gizmo mode+ * @return Current mode+ */+ GizmoMode getMode() const { return mode_; }++ /**+ * @brief Sets the gizmo size+ * @param size Size in world units+ */+ void setSize(f32 size) { size_ = size; }++ /**+ * @brief Gets the gizmo size+ * @return Size in world units+ */+ f32 getSize() const { return size_; }++ // =========================================================================+ // Rendering+ // =========================================================================++ /**+ * @brief Renders the gizmo for the given entity+ * @param view View matrix+ * @param proj Projection matrix+ * @param entity Entity to manipulate+ * @param registry Entity registry+ */+ void render(const glm::mat4& view, const glm::mat4& proj,+ Entity entity, ecs::Registry& registry);++ // =========================================================================+ // Interaction+ // =========================================================================++ /**+ * @brief Tests if a ray hits the gizmo+ * @param rayOrigin Ray origin in world space+ * @param rayDir Ray direction (normalized)+ * @return Hit axis or GizmoAxis::None+ */+ GizmoAxis hitTest(const glm::vec3& rayOrigin, const glm::vec3& rayDir) const;++ /**+ * @brief Starts a drag operation+ * @param axis Axis to manipulate+ * @param hitPoint World position where drag started+ */+ void startDrag(GizmoAxis axis, const glm::vec3& hitPoint);++ /**+ * @brief Updates the drag operation+ * @param rayOrigin Ray origin in world space+ * @param rayDir Ray direction (normalized)+ * @return Delta transform to apply+ */+ glm::vec3 updateDrag(const glm::vec3& rayOrigin, const glm::vec3& rayDir);++ /**+ * @brief Ends the drag operation+ */+ void endDrag();++ /**+ * @brief Checks if currently dragging+ * @return True if dragging+ */+ bool isDragging() const { return dragging_; }++private:+ void renderTranslateGizmo(const glm::mat4& mvp, const glm::vec3& position);+ void renderRotateGizmo(const glm::mat4& mvp, const glm::vec3& position);+ void renderScaleGizmo(const glm::mat4& mvp, const glm::vec3& position);++ void renderAxis(const glm::mat4& mvp, const glm::vec3& start,+ const glm::vec3& end, const glm::vec4& color);+ void renderArrow(const glm::mat4& mvp, const glm::vec3& start,+ const glm::vec3& dir, f32 length, const glm::vec4& color);+ void renderCircle(const glm::mat4& mvp, const glm::vec3& center,+ const glm::vec3& normal, f32 radius, const glm::vec4& color);++ f32 rayAxisDistance(const glm::vec3& rayOrigin, const glm::vec3& rayDir,+ const glm::vec3& axisOrigin, const glm::vec3& axisDir) const;++ GizmoMode mode_ = GizmoMode::Translate;+ GizmoAxis activeAxis_ = GizmoAxis::None;+ f32 size_ = 1.0f;++ bool dragging_ = false;+ glm::vec3 dragStartPoint_{0.0f};+ glm::vec3 gizmoPosition_{0.0f};+};++} // namespace esengine::editor