ImGuizmo Documentation

repository·master·Indexed 26 days ago

https://github.com/cedricguillemet/imguizmo

A collection of Dear ImGui-based widgets for 3D manipulation and specialized editing. Key components include ImGuizmo for 4x4 float matrix manipulation (translation, rotation, scale), ImViewGizmo for view orientation, ImSequencer for timeline editing, GraphEditor for node graphs, and ImVectorEditor for 2D vector geometry path editing.

Tokens
3.5K
Snippets
10
Records
18
Agent score
88%

What's inside ImGuizmo

  1. Overview of ImGuizmo widgets

    master

    ImGuizmo is a collection of Dear ImGui-based widgets designed for 3D manipulation and specialized editing tasks. Each widget is provided as a standalone .h/.cpp pair for independent use, or can be used as part of a unified static library built via CMake.

    Available widgets include:

    • ImViewGizmo: For manipulating view orientation.
    • ImGuizmo: For manipulating 4x4 float matrices (rotation, translation, and scale).
    • ImSequencer: A timeline sequencer for editing frame start/end ranges across multiple events.
    • GraphEditor: A node graph editor featuring connections and a delegate system for custom node rendering.
    • ImVectorEditor: A 2D vector geometry path editor supporting pen tools, anchor/handle editing, and host-provided transforms.
  2. Configure ImGuizmo for VSCode using CMake Tools

    master

    If you are using the VSCode CMake Tools extension, you can automate the vcpkg integration by adding the toolchain file path to your .vscode/settings.json.

    To ensure compatibility across different machines, it is recommended to use an environment variable (like VCPKG_ROOT) with variable substitution instead of a hardcoded absolute path.

    {
        "cmake.configureArgs": [
            "-DCMAKE_TOOLCHAIN_FILE={$env:VCPKG_ROOT}\\scripts\\buildsystems\\vcpkg.cmake"
        ]
    }
  3. Install ImGuizmo using vcpkg and CMake

    master

    To install ImGuizmo and its dependencies, use the vcpkg toolchain file during the CMake configuration step. This requires that both cmake and vcpkg are already installed on your system. Providing the toolchain file allows CMake to detect the vcpkg.json manifest file, which triggers the automatic download and compilation of all required dependencies.

    mkdir build
    cd build
    cmake .. -DCMAKE_TOOLCHAIN_FILE=C:/dev/vcpkg/scripts/buildsystem/vcpkg.cmake
  4. Full usage example for object transformation

    master

    This example demonstrates a complete workflow: handling input for operation modes (Translate, Rotate, Scale), toggling between Local/World modes, decomposing a matrix for manual float input, and finally calling Manipulate with snapping support.

    void EditTransform(float* cameraView, float* cameraProjection, float* matrix)
    {
        static ImGuizmo::OPERATION mCurrentGizmoOperation(ImGuizmo::ROTATE);
        static ImGuizmo::MODE mCurrentGizmoMode(ImGuizmo::WORLD);
        if (ImGui::IsKeyPressed(ImGuiKey_T))
            mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
        if (ImGui::IsKeyPressed(ImGuiKey_E))
            mCurrentGizmoOperation = ImGuizmo::ROTATE;
        if (ImGui::IsKeyPressed(ImGuiKey_R))
            mCurrentGizmoOperation = ImGuizmo::SCALE;
        if (ImGui::RadioButton("Translate", mCurrentGizmoOperation == ImGuizmo::TRANSLATE))
            mCurrentGizmoOperation = ImGuizmo::TRANSLATE;
        ImGui::SameLine();
        if (ImGui::RadioButton("Rotate", mCurrentGizmoOperation == ImGuizmo::ROTATE))
            mCurrentGizmoOperation = ImGuizmo::ROTATE;
        ImGui::SameLine();
        if (ImGui::RadioButton("Scale", mCurrentGizmoOperation == ImGuizmo::SCALE))
            mCurrentGizmoOperation = ImGuizmo::SCALE;
        float matrixTranslation[3], matrixRotation[3], matrixScale[3];
        ImGuizmo::DecomposeMatrixToComponents(matrix, matrixTranslation, matrixRotation, matrixScale);
        ImGui::InputFloat3("Tr", matrixTranslation);
        ImGui::InputFloat3("Rt", matrixRotation);
        ImGui::InputFloat3("Sc", matrixScale);
        ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, matrix);
    
        if (mCurrentGizmoOperation != ImGuizmo::SCALE)
        {
            if (ImGui::RadioButton("Local", mCurrentGizmoMode == ImGuizmo::LOCAL))
                mCurrentGizmoMode = ImGuizmo::LOCAL;
            ImGui::SameLine();
            if (ImGui::RadioButton("World", mCurrentGizmoMode == ImGuizmo::WORLD))
                mCurrentGizmoMode = ImGuizmo::WORLD;
        }
        static bool useSnap(false);
        if (ImGui::IsKeyPressed(ImGuiKey_S))
            useSnap = !useSnap;
        ImGui::Checkbox("##useSnap", &useSnap);
        ImGui::SameLine();
        vec_t snap;
        switch (mCurrentGizmoOperation)
        {
        case ImGuizmo::TRANSLATE:
            snap = config.mSnapTranslation;
            ImGui::InputFloat3("Snap", &snap.x);
            break;
        case ImGuizmo::ROTATE:
            snap = config.mSnapRotation;
            ImGui::InputFloat("Angle Snap", &snap.x);
            break;
        case ImGuizmo::SCALE:
            snap = config.mSnapScale;
            ImGui::InputFloat("Scale Snap", &snap.x);
            break;
        default:
            break;
        }
        ImGuiIO& io = ImGui::GetIO();
        ImGuizmo::SetRect(0, 0, io.DisplaySize.x, io.DisplaySize.y);
        ImGuizmo::Manipulate(cameraView, cameraProjection, mCurrentGizmoOperation, 
                             mCurrentGizmoMode, matrix, NULL, useSnap ? &snap.x : NULL);
    }
  5. Manipulate objects with a gizmo

    master

    The ImGuizmo::Manipulate function draws and handles a gizmo for a specific matrix.

    Key Details:

    • Matrix Input/Output: The matrix parameter is both an input (defines where the gizmo is drawn) and an output (updated when the user interacts with it).
    • Coordinate Systems: Supports both left-handed and right-handed coordinate systems, as well as both finite and infinite far plane projection matrices.
    • Snapping: The snap parameter accepts a float[3] for translation snapping, or a single float for rotation (in degrees) or scale snapping.

    Enums:

    • OPERATION: TRANSLATE, ROTATE, SCALE
    • MODE: LOCAL, WORLD

    Signature:

    void Manipulate(const float* view, const float* projection, OPERATION operation, MODE mode, float* matrix, float* deltaMatrix = 0, float* snap = 0);
  6. Decompose and Recompose matrices using component helpers

    master

    ImGuizmo provides helper functions to convert a transformation matrix into translation, rotation, and scale components, and back again. This is useful for manual editing via float inputs.

    Parameters:

    • translation, rotation, scale: Arrays of 3 floats.
    • Angles are in degrees.

    Warning: These functions have numerical stability limitations and should be used with caution.

    Functions:

    • void DecomposeMatrixToComponents(const float* matrix, float* translation, float* rotation, float* scale)
    • void RecomposeMatrixFromComponents(const float* translation, const float* rotation, const float* scale, float* matrix)
    float matrixTranslation[3], matrixRotation[3], matrixScale[3];
    ImGuizmo::DecomposeMatrixToComponents(gizmoMatrix.m16, matrixTranslation, matrixRotation, matrixScale);
    ImGui::InputFloat3("Tr", matrixTranslation, 3);
    ImGui::InputFloat3("Rt", matrixRotation, 3);
    ImGui::InputFloat3("Sc", matrixScale, 3);
    ImGuizmo::RecomposeMatrixFromComponents(matrixTranslation, matrixRotation, matrixScale, gizmoMatrix.m16);
  7. Check gizmo interaction state with IsOver and IsUsing

    master

    Use these functions to determine how the user is interacting with the gizmo:

    • IsOver(): Returns true if the mouse cursor is currently over any gizmo control (axis, plane, or screen component).
    • IsUsing(): Returns true if the mouse is over the gizmo OR if the gizmo is currently being actively moved/manipulated.
  8. Configure the ImVectorEditor::Editor

    master

    The ImVectorEditor::Config struct allows you to customize the editor's appearance, input handling, and coordinate system. Key configuration areas include:

    • Canvas & Tools: Set canvasSize and the active tool (Tool::Select or Tool::Pen).
    • Transform: Provide a Transform object to handle pan, zoom, and object-level transformations (rotation, scale, translation).
    • Style: Customize colors (e.g., pathColor, anchorColor, handleColor), thicknesses, and shapes (ControlPointShape).
    • Behavior: Toggle showGrid, allowKeyboardShortcuts, or set readOnly mode.
    • Callbacks: Provide a Delegate* to receive BeginEdit and EndEdit notifications.