From cbe220664fa021d2be6542776fa9181a0e466316 Mon Sep 17 00:00:00 2001 From: Orange Date: Sat, 11 Jul 2026 22:39:18 +0300 Subject: [PATCH] added outside glow --- examples/example_hud/gui/main_window.cpp | 18 +++-- examples/example_hud/gui/main_window.hpp | 3 + include/omath/hud/entity_overlay_widgets.hpp | 10 +++ include/omath/hud/hud_renderer_interface.hpp | 6 ++ .../renderer_realizations/imgui_renderer.hpp | 2 + source/hud/entity_overlay.cpp | 75 ++++++++++++++----- .../renderer_realizations/imgui_renderer.cpp | 7 ++ 7 files changed, 98 insertions(+), 23 deletions(-) diff --git a/examples/example_hud/gui/main_window.cpp b/examples/example_hud/gui/main_window.cpp index cb3cd5b..89269fd 100644 --- a/examples/example_hud/gui/main_window.cpp +++ b/examples/example_hud/gui/main_window.cpp @@ -66,7 +66,7 @@ namespace imgui_desktop::gui ImGui::Begin("Controls", &m_opened, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse); ImGui::PushItemWidth(160.f); - const auto draw_glow_controls = [](const char* label, GlowSettings& settings, const float max_radius = 30.f) + const auto draw_glow_controls = [](const char* label, GlowSettings& settings) { ImGui::PushID(label); ImGui::TextUnformatted(label); @@ -74,8 +74,8 @@ namespace imgui_desktop::gui if (settings.enabled) { ImGui::ColorEdit4("Color", reinterpret_cast(&settings.color), ImGuiColorEditFlags_NoInputs); - ImGui::SliderFloat("Radius", &settings.radius, 1.f, max_radius); - ImGui::SliderFloat("Intensity", &settings.intensity, 0.f, 1.f); + ImGui::InputFloat("Radius", &settings.radius); + ImGui::InputFloat("Intensity", &settings.intensity); } ImGui::PopID(); }; @@ -86,7 +86,13 @@ namespace imgui_desktop::gui ImGui::SliderFloat("Top Y", &m_entity_top_y, 20.f, m_entity_bottom_y - 20.f); ImGui::SliderFloat("Bottom Y", &m_entity_bottom_y, m_entity_top_y + 20.f, vp->Size.y - 20.f); ImGui::SliderFloat("Aspect", &m_entity_aspect, 1.f, 10.f); - draw_glow_controls("Canvas light", m_canvas_glow, 500.f); + draw_glow_controls("Canvas light", m_canvas_glow); + if (m_canvas_glow.enabled) + { + ImGui::InputInt("Blur layers##canvas", &m_canvas_glow_layers); + ImGui::Combo("Mode##canvas", &m_canvas_glow_mode, "Inside\0Outside\0Both\0"); + ImGui::InputFloat("Rounding##canvas", &m_canvas_glow_rounding); + } } if (ImGui::CollapsingHeader("Box", ImGuiTreeNodeFlags_DefaultOpen)) @@ -285,7 +291,9 @@ namespace imgui_desktop::gui omath::hud::EntityOverlay({m_entity_x, m_entity_top_y}, {m_entity_x, m_entity_bottom_y}, m_entity_aspect, std::make_shared()) .contents( - when(canvas_glow.has_value(), CanvasGlow{canvas_glow.value_or(Glow{m_canvas_glow.color})}), + when(canvas_glow.has_value(), + CanvasGlow{canvas_glow.value_or(Glow{m_canvas_glow.color}), m_canvas_glow_layers, + static_cast(m_canvas_glow_mode), m_canvas_glow_rounding}), // ── Boxes ──────────────────────────────────────────────────── when(m_show_box, Box{m_box_color, box_fill, m_box_outline, m_box_thickness, box_glow}), when(m_show_cornered_box, diff --git a/examples/example_hud/gui/main_window.hpp b/examples/example_hud/gui/main_window.hpp index c6b6678..1f1fd3e 100644 --- a/examples/example_hud/gui/main_window.hpp +++ b/examples/example_hud/gui/main_window.hpp @@ -77,6 +77,9 @@ namespace imgui_desktop::gui GlowSettings m_bar_glow{{1.f, 0.f, 0.f, 0.8f}}; GlowSettings m_label_glow; GlowSettings m_canvas_glow{{1.f, 0.5f, 0.f, 0.8f}, 100.f}; + int m_canvas_glow_layers = 64; + int m_canvas_glow_mode = 2; + float m_canvas_glow_rounding = 40.f; // Skeleton omath::Color m_skel_color = omath::Color::from_rgba(255, 255, 255, 200); diff --git a/include/omath/hud/entity_overlay_widgets.hpp b/include/omath/hud/entity_overlay_widgets.hpp index 21dfdbc..8563f0c 100644 --- a/include/omath/hud/entity_overlay_widgets.hpp +++ b/include/omath/hud/entity_overlay_widgets.hpp @@ -39,9 +39,19 @@ namespace omath::hud::widget float intensity = 1.f; }; + enum class CanvasGlowMode + { + INSIDE, + OUTSIDE, + BOTH, + }; + struct CanvasGlow { Glow glow; + int layers = 64; + CanvasGlowMode mode = CanvasGlowMode::INSIDE; + float rounding = 20.f; }; // ── Standalone widgets ──────────────────────────────────────────────────── diff --git a/include/omath/hud/hud_renderer_interface.hpp b/include/omath/hud/hud_renderer_interface.hpp index 3ebc5f4..3501246 100644 --- a/include/omath/hud/hud_renderer_interface.hpp +++ b/include/omath/hud/hud_renderer_interface.hpp @@ -38,6 +38,12 @@ namespace omath::hud virtual void add_filled_circle(const Vector2& center, float radius, const Color& color, int segments = 0) = 0; + virtual void add_filled_ellipse(const Vector2& center, const Vector2& radius, const Color& color, + int segments = 0) + { + add_filled_circle(center, std::min(radius.x, radius.y), color, segments); + } + /// Draw an arc (partial circle outline). Angles in radians, 0 = right (+X), counter-clockwise. virtual void add_arc(const Vector2& center, float radius, float a_min, float a_max, const Color& color, float thickness, int segments = 0) = 0; diff --git a/include/omath/hud/renderer_realizations/imgui_renderer.hpp b/include/omath/hud/renderer_realizations/imgui_renderer.hpp index 6632a48..a509a74 100644 --- a/include/omath/hud/renderer_realizations/imgui_renderer.hpp +++ b/include/omath/hud/renderer_realizations/imgui_renderer.hpp @@ -24,6 +24,8 @@ namespace omath::hud int segments = 0) override; void add_filled_circle(const Vector2& center, float radius, const Color& color, int segments = 0) override; + void add_filled_ellipse(const Vector2& center, const Vector2& radius, const Color& color, + int segments = 0) override; void add_arc(const Vector2& center, float radius, float a_min, float a_max, const Color& color, float thickness, int segments = 0) override; void add_image(const std::any& texture_id, const Vector2& min, const Vector2& max, diff --git a/source/hud/entity_overlay.cpp b/source/hud/entity_overlay.cpp index 9bad505..8ea3913 100644 --- a/source/hud/entity_overlay.cpp +++ b/source/hud/entity_overlay.cpp @@ -388,7 +388,7 @@ namespace omath::hud const int radius = static_cast(std::ceil(std::max(glow->radius, 0.f))); for (int layer = radius; layer > 0; --layer) { - const float alpha = glow->color.value().w * std::clamp(glow->intensity, 0.f, 1.f) + const float alpha = glow->color.value().w * std::max(glow->intensity, 0.f) * (1.f - static_cast(layer - 1) / static_cast(radius + 1)); const auto value = glow->color.value(); const Color color{value.x, value.y, value.z, alpha / static_cast(radius)}; @@ -442,7 +442,7 @@ namespace omath::hud { const float falloff = 1.f - static_cast(layer - 1) / static_cast(radius + 1); const Color color{value.x, value.y, value.z, - value.w * std::clamp(glow.intensity, 0.f, 1.f) * falloff / static_cast(radius)}; + value.w * std::max(glow.intensity, 0.f) * falloff / static_cast(radius)}; m_renderer->add_polyline(points, color, thickness + static_cast(layer) * 2.f); } } @@ -456,7 +456,7 @@ namespace omath::hud { const float falloff = 1.f - static_cast(layer - 1) / static_cast(radius + 1); const Color color{value.x, value.y, value.z, - value.w * std::clamp(glow.intensity, 0.f, 1.f) * falloff / static_cast(radius)}; + value.w * std::max(glow.intensity, 0.f) * falloff / static_cast(radius)}; m_renderer->add_line(from, to, color, thickness + static_cast(layer) * 2.f); } } @@ -478,10 +478,58 @@ namespace omath::hud void EntityOverlay::draw_canvas_glow(const widget::CanvasGlow& canvas_glow) const { - constexpr int layers = 32; - constexpr int segments = 64; + constexpr int segments = 96; + const int layers = std::max(canvas_glow.layers, 2); const auto min = m_canvas.top_left_corner; const auto max = m_canvas.bottom_right_corner; + const auto value = canvas_glow.glow.color.value(); + const float intensity = std::max(canvas_glow.glow.intensity, 0.f); + + if (canvas_glow.mode == widget::CanvasGlowMode::OUTSIDE || canvas_glow.mode == widget::CanvasGlowMode::BOTH) + { + constexpr int corner_segments = 12; + const float max_rounding = std::min(std::abs(max.x - min.x), std::abs(max.y - min.y)) * 0.5f; + const float rounding = std::clamp(canvas_glow.rounding, 0.f, max_rounding); + const float extent = std::max(canvas_glow.glow.radius, 0.f); + const float stroke = std::max(extent / static_cast(layers - 1) * 2.f, 1.f); + for (int layer = 0; layer < layers; ++layer) + { + const float progress = static_cast(layer) / static_cast(layers - 1); + const float expansion = extent * (1.f - progress); + const float falloff = progress * progress * (3.f - 2.f * progress); + const Color color{value.x, value.y, value.z, value.w * intensity * falloff * 0.35f}; + const auto expanded_min = min - Vector2{expansion, expansion}; + const auto expanded_max = max + Vector2{expansion, expansion}; + const float expanded_rounding = rounding + expansion; + const std::array corner_centers = { + expanded_min + Vector2{expanded_rounding, expanded_rounding}, + Vector2{expanded_max.x - expanded_rounding, expanded_min.y + expanded_rounding}, + expanded_max - Vector2{expanded_rounding, expanded_rounding}, + Vector2{expanded_min.x + expanded_rounding, expanded_max.y - expanded_rounding}, + }; + std::array, corner_segments * 4> points; + for (int corner = 0; corner < 4; ++corner) + { + const float start_angle = + std::numbers::pi_v + static_cast(corner) * std::numbers::pi_v * 0.5f; + for (int segment = 0; segment < corner_segments; ++segment) + { + const float arc_progress = + static_cast(segment) / static_cast(corner_segments - 1); + const float angle = start_angle + arc_progress * std::numbers::pi_v * 0.5f; + points[corner * corner_segments + segment] = + corner_centers[corner] + + Vector2{std::cos(angle) * expanded_rounding, + std::sin(angle) * expanded_rounding}; + } + } + m_renderer->add_polyline(points, color, stroke); + } + } + + if (canvas_glow.mode == widget::CanvasGlowMode::OUTSIDE) + return; + const auto center = (min + max) * 0.5f; const float half_width = std::abs(max.x - min.x) * 0.5f; const float half_height = std::abs(max.y - min.y) * 0.5f; @@ -492,24 +540,15 @@ namespace omath::hud if (vertical_radius <= 0.f || horizontal_radius <= 0.f) return; - const auto value = canvas_glow.glow.color.value(); - const float intensity = std::clamp(canvas_glow.glow.intensity, 0.f, 1.f); for (int layer = 0; layer < layers; ++layer) { const float progress = static_cast(layer) / static_cast(layers - 1); const float scale = 1.f - progress * 0.95f; - const float alpha = value.w * intensity / static_cast(layers); + const float falloff = progress * progress; + const float alpha = value.w * intensity * falloff * 3.f / static_cast(layers); const Color color{value.x, value.y, value.z, alpha}; - std::array, segments> points; - for (int segment = 0; segment < segments; ++segment) - { - const float angle = - static_cast(segment) / static_cast(segments) * 2.f * std::numbers::pi_v; - points[segment] = center - + Vector2{std::cos(angle) * horizontal_radius * scale, - std::sin(angle) * vertical_radius * scale}; - } - m_renderer->add_filled_polyline(points, color); + m_renderer->add_filled_ellipse(center, {horizontal_radius * scale, vertical_radius * scale}, color, + segments); } } diff --git a/source/hud/renderer_realizations/imgui_renderer.cpp b/source/hud/renderer_realizations/imgui_renderer.cpp index 65e15b3..6f8f9c0 100644 --- a/source/hud/renderer_realizations/imgui_renderer.cpp +++ b/source/hud/renderer_realizations/imgui_renderer.cpp @@ -65,6 +65,13 @@ namespace omath::hud ImGui::GetBackgroundDrawList()->AddCircleFilled(center.to_im_vec2(), radius, color.to_im_color(), segments); } + void ImguiHudRenderer::add_filled_ellipse(const Vector2& center, const Vector2& radius, + const Color& color, const int segments) + { + ImGui::GetBackgroundDrawList()->AddEllipseFilled(center.to_im_vec2(), radius.to_im_vec2(), color.to_im_color(), + 0.f, segments); + } + void ImguiHudRenderer::add_arc(const Vector2& center, const float radius, const float a_min, const float a_max, const Color& color, const float thickness, const int segments) {