Better 3D navigator (#5620)

* Initial working impl of face labels

* Improve color

* Remove background
This commit is contained in:
Noisyfox 2024-06-15 12:17:57 +08:00 committed by GitHub
parent aa692b815e
commit dbac0f2919
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 80 additions and 4 deletions

View file

@ -662,10 +662,18 @@ namespace IMGUIZMO_NAMESPACE
Colors[HATCHED_AXIS_LINES] = ImVec4(0.000f, 0.000f, 0.000f, 0.500f);
Colors[TEXT] = ImVec4(1.000f, 1.000f, 1.000f, 1.000f);
Colors[TEXT_SHADOW] = ImVec4(0.000f, 0.000f, 0.000f, 1.000f);
Colors[FACE] = ImVec4(0.776f, 0.804f, 0.839f, 1.000f);
strcpy(AxisLabels[Axis_X], "x");
strcpy(AxisLabels[Axis_Y], "y");
strcpy(AxisLabels[Axis_Z], "z");
strcpy(FaceLabels[FACE_BACK], "back");
strcpy(FaceLabels[FACE_TOP], "top");
strcpy(FaceLabels[FACE_RIGHT], "right");
strcpy(FaceLabels[FACE_FRONT], "front");
strcpy(FaceLabels[FACE_BOTTOM], "bottom");
strcpy(FaceLabels[FACE_LEFT], "left");
}
struct Context
@ -2885,6 +2893,7 @@ namespace IMGUIZMO_NAMESPACE
const vec_t dx = directionUnary[perpXIndex];
const vec_t dy = directionUnary[perpYIndex];
const vec_t origin = directionUnary[normalIndex] - dx - dy;
ImU32 faceColor = GetColorU32(FACE);
for (int iPanel = 0; iPanel < 9; iPanel++)
{
vec_t boxCoord = boxOrigin + indexVectorX * float(iPanel % 3) + indexVectorY * float(iPanel / 3) + makeVect(1.f, 1.f, 1.f);
@ -2910,8 +2919,7 @@ namespace IMGUIZMO_NAMESPACE
// draw face with lighter color
if (iPass)
{
ImU32 directionColor = GetColorU32(DIRECTION_X + normalIndex);
gContext.mDrawList->AddConvexPolyFilled(faceCoordsScreen, 4, (directionColor | IM_COL32(0x80, 0x80, 0x80, 0x80)) | (isInside ? IM_COL32(0x08, 0x08, 0x08, 0) : 0));
gContext.mDrawList->AddConvexPolyFilled(faceCoordsScreen, 4, faceColor);
if (boxes[boxCoordInt])
{
gContext.mDrawList->AddConvexPolyFilled(faceCoordsScreen, 4, IM_COL32(0xF0, 0xA0, 0x60, 0x80));
@ -2929,6 +2937,53 @@ namespace IMGUIZMO_NAMESPACE
}
}
}
if (iPass) {
// Draw face label
ImDrawList* drawList = gContext.mDrawList;
ImDrawVert* vtx_write_start = drawList->_VtxWritePtr;
const auto label = gContext.mStyle.FaceLabels[iFace];
ImVec2 labelSize = ImGui::CalcTextSize(label);
float scaleFactor = 2 / size.y;
auto labelOrigin = labelSize * 0.5;
drawList->AddText(ImVec2(0, 0), GetColorU32(TEXT), label);
ImDrawVert* vtx_write_end = drawList->_VtxWritePtr;
vec_t tdx = directionUnary[perpXIndex];
vec_t tdy = directionUnary[perpYIndex];
ImVec2 invert2 = {1, 1};
switch (iFace) {
case 0: // Back
tdx = directionUnary[2];
tdy = directionUnary[1];
invert2 = {-1, - 1};
break;
case 3: // Front
tdx = directionUnary[2];
tdy = directionUnary[1];
invert2.x = -1;
break;
case 1: // Top
invert2.y = -1;
break;
case 4: // Bottom
invert2 = {-1, -1};
break;
case 2: // Right
invert2.y = -1;
break;
case 5: // Left
break;
}
for (auto v = vtx_write_start; v < vtx_write_end; v++) {
auto pp = ((v->pos - labelOrigin) * scaleFactor * invert2 + ImVec2{0.5, 0.5}) * 2.f;
vec_t pt = tdx * pp.x + tdy * pp.y;
v->pos = worldToPos((pt + origin) * 0.5 * invert, res, position, size);
}
}
}
}
@ -2962,7 +3017,7 @@ namespace IMGUIZMO_NAMESPACE
if (!visible) {
directionColorV.w *= 0.3f;
}
ImU32 directionColor = ImGui::ColorConvertFloat4ToU32(directionColorV);
ImU32 directionColor = ImGui::ColorConvertFloat4ToU32(directionColorV) | IM_COL32(0x80, 0x80, 0x80, 0x00);
drawList->AddLine(baseSSpace, worldDirSSpace, directionColor, gContext.mStyle.TranslationLineThickness);
// Arrow head begin

View file

@ -249,6 +249,7 @@ namespace IMGUIZMO_NAMESPACE
HATCHED_AXIS_LINES,
TEXT,
TEXT_SHADOW,
FACE,
COUNT
};
@ -260,6 +261,17 @@ namespace IMGUIZMO_NAMESPACE
Axis_COUNT,
};
enum FACES
{
FACE_BACK,
FACE_TOP,
FACE_RIGHT,
FACE_FRONT,
FACE_BOTTOM,
FACE_LEFT,
FACES_COUNT
};
struct Style
{
IMGUI_API Style();
@ -276,6 +288,7 @@ namespace IMGUIZMO_NAMESPACE
ImVec4 Colors[COLOR::COUNT];
char AxisLabels[Axis::Axis_COUNT][32];
char FaceLabels[FACES::FACES_COUNT][32];
};
IMGUI_API Style& GetStyle();

View file

@ -5662,9 +5662,17 @@ void GLCanvas3D::_render_3d_navigator()
style.Colors[ImGuizmo::COLOR::DIRECTION_X] = ImGuiWrapper::to_ImVec4(ColorRGBA::Y());
style.Colors[ImGuizmo::COLOR::DIRECTION_Y] = ImGuiWrapper::to_ImVec4(ColorRGBA::Z());
style.Colors[ImGuizmo::COLOR::DIRECTION_Z] = ImGuiWrapper::to_ImVec4(ColorRGBA::X());
style.Colors[ImGuizmo::COLOR::TEXT] = m_is_dark ? ImVec4(224 / 255.f, 224 / 255.f, 224 / 255.f, 1.f) : ImVec4(.2f, .2f, .2f, 1.0f);
style.Colors[ImGuizmo::COLOR::FACE] = m_is_dark ? ImVec4(45 / 255.f, 45 / 255.f, 49 / 255.f, 1.f) : ImVec4(1, 1, 1, 1);
strcpy(style.AxisLabels[ImGuizmo::Axis::Axis_X], "y");
strcpy(style.AxisLabels[ImGuizmo::Axis::Axis_Y], "z");
strcpy(style.AxisLabels[ImGuizmo::Axis::Axis_Z], "x");
strcpy(style.FaceLabels[ImGuizmo::FACES::FACE_FRONT], _utf8("Front").c_str());
strcpy(style.FaceLabels[ImGuizmo::FACES::FACE_BACK], _utf8("Back").c_str());
strcpy(style.FaceLabels[ImGuizmo::FACES::FACE_TOP], _utf8("Top").c_str());
strcpy(style.FaceLabels[ImGuizmo::FACES::FACE_BOTTOM], _utf8("Bottom").c_str());
strcpy(style.FaceLabels[ImGuizmo::FACES::FACE_LEFT], _utf8("Left").c_str());
strcpy(style.FaceLabels[ImGuizmo::FACES::FACE_RIGHT], _utf8("Right").c_str());
float sc = get_scale();
#ifdef WIN32
@ -5694,7 +5702,7 @@ void GLCanvas3D::_render_3d_navigator()
const float size = 128 * sc;
const bool dirty = ImGuizmo::ViewManipulate(cameraView, cameraProjection, ImGuizmo::OPERATION::ROTATE, ImGuizmo::MODE::WORLD,
identityMatrix, camDistance, ImVec2(viewManipulateLeft, viewManipulateTop - size),
ImVec2(size, size), 0x10101010);
ImVec2(size, size), 0x00101010);
if (dirty) {
for (unsigned int c = 0; c < 4; ++c) {