mirror of
https://github.com/orange-cpp/omath.git
synced 2026-08-07 21:42:05 +00:00
added outside glow
This commit is contained in:
@@ -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<float*>(&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<omath::hud::ImguiHudRenderer>())
|
||||
.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<CanvasGlowMode>(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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 ────────────────────────────────────────────────────
|
||||
|
||||
@@ -38,6 +38,12 @@ namespace omath::hud
|
||||
virtual void add_filled_circle(const Vector2<float>& center, float radius, const Color& color,
|
||||
int segments = 0) = 0;
|
||||
|
||||
virtual void add_filled_ellipse(const Vector2<float>& center, const Vector2<float>& 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<float>& center, float radius, float a_min, float a_max, const Color& color,
|
||||
float thickness, int segments = 0) = 0;
|
||||
|
||||
@@ -24,6 +24,8 @@ namespace omath::hud
|
||||
int segments = 0) override;
|
||||
void add_filled_circle(const Vector2<float>& center, float radius, const Color& color,
|
||||
int segments = 0) override;
|
||||
void add_filled_ellipse(const Vector2<float>& center, const Vector2<float>& radius, const Color& color,
|
||||
int segments = 0) override;
|
||||
void add_arc(const Vector2<float>& 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<float>& min, const Vector2<float>& max,
|
||||
|
||||
@@ -388,7 +388,7 @@ namespace omath::hud
|
||||
const int radius = static_cast<int>(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<float>(layer - 1) / static_cast<float>(radius + 1));
|
||||
const auto value = glow->color.value();
|
||||
const Color color{value.x, value.y, value.z, alpha / static_cast<float>(radius)};
|
||||
@@ -442,7 +442,7 @@ namespace omath::hud
|
||||
{
|
||||
const float falloff = 1.f - static_cast<float>(layer - 1) / static_cast<float>(radius + 1);
|
||||
const Color color{value.x, value.y, value.z,
|
||||
value.w * std::clamp(glow.intensity, 0.f, 1.f) * falloff / static_cast<float>(radius)};
|
||||
value.w * std::max(glow.intensity, 0.f) * falloff / static_cast<float>(radius)};
|
||||
m_renderer->add_polyline(points, color, thickness + static_cast<float>(layer) * 2.f);
|
||||
}
|
||||
}
|
||||
@@ -456,7 +456,7 @@ namespace omath::hud
|
||||
{
|
||||
const float falloff = 1.f - static_cast<float>(layer - 1) / static_cast<float>(radius + 1);
|
||||
const Color color{value.x, value.y, value.z,
|
||||
value.w * std::clamp(glow.intensity, 0.f, 1.f) * falloff / static_cast<float>(radius)};
|
||||
value.w * std::max(glow.intensity, 0.f) * falloff / static_cast<float>(radius)};
|
||||
m_renderer->add_line(from, to, color, thickness + static_cast<float>(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<float>(layers - 1) * 2.f, 1.f);
|
||||
for (int layer = 0; layer < layers; ++layer)
|
||||
{
|
||||
const float progress = static_cast<float>(layer) / static_cast<float>(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<float>{expansion, expansion};
|
||||
const auto expanded_max = max + Vector2<float>{expansion, expansion};
|
||||
const float expanded_rounding = rounding + expansion;
|
||||
const std::array corner_centers = {
|
||||
expanded_min + Vector2<float>{expanded_rounding, expanded_rounding},
|
||||
Vector2<float>{expanded_max.x - expanded_rounding, expanded_min.y + expanded_rounding},
|
||||
expanded_max - Vector2<float>{expanded_rounding, expanded_rounding},
|
||||
Vector2<float>{expanded_min.x + expanded_rounding, expanded_max.y - expanded_rounding},
|
||||
};
|
||||
std::array<Vector2<float>, corner_segments * 4> points;
|
||||
for (int corner = 0; corner < 4; ++corner)
|
||||
{
|
||||
const float start_angle =
|
||||
std::numbers::pi_v<float> + static_cast<float>(corner) * std::numbers::pi_v<float> * 0.5f;
|
||||
for (int segment = 0; segment < corner_segments; ++segment)
|
||||
{
|
||||
const float arc_progress =
|
||||
static_cast<float>(segment) / static_cast<float>(corner_segments - 1);
|
||||
const float angle = start_angle + arc_progress * std::numbers::pi_v<float> * 0.5f;
|
||||
points[corner * corner_segments + segment] =
|
||||
corner_centers[corner]
|
||||
+ Vector2<float>{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<float>(layer) / static_cast<float>(layers - 1);
|
||||
const float scale = 1.f - progress * 0.95f;
|
||||
const float alpha = value.w * intensity / static_cast<float>(layers);
|
||||
const float falloff = progress * progress;
|
||||
const float alpha = value.w * intensity * falloff * 3.f / static_cast<float>(layers);
|
||||
const Color color{value.x, value.y, value.z, alpha};
|
||||
std::array<Vector2<float>, segments> points;
|
||||
for (int segment = 0; segment < segments; ++segment)
|
||||
{
|
||||
const float angle =
|
||||
static_cast<float>(segment) / static_cast<float>(segments) * 2.f * std::numbers::pi_v<float>;
|
||||
points[segment] = center
|
||||
+ Vector2<float>{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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<float>& center, const Vector2<float>& 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<float>& center, const float radius, const float a_min,
|
||||
const float a_max, const Color& color, const float thickness, const int segments)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user