18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 04:35:02 +01:00

fix(editor): the points of the terrain tools put on the ground along the ray through the cursor in this frame

The terrain tools (the outlines of Fill, water and chunks, the target height, the brushes) and the area fill took their point
from the depth read back under the cursor. That depth is a frame late, so the point was off while the camera or the cursor
moved, and it stops at whatever is drawn over the ground (models, the 3D grass), at a grazing angle metres away from where
the ground is. Now the ray through the cursor as it is in this frame is followed to the terrain (terrain_streamer::raycast);
the depth read back is used only where the ray meets no terrain. The brush circle is drawn where the stroke will land.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ
This commit is contained in:
Claude
2026-11-09 23:15:18 +01:00
parent e6fa206e47
commit f1f315d629
4 changed files with 102 additions and 22 deletions

View File

@@ -2203,9 +2203,9 @@ void editor_mode::draw_terrain_overlay()
return;
screen_projection const projection;
auto *drawlist = ImGui::GetBackgroundDrawList();
glm::dvec3 const offset = GfxRenderer->Mouse_Position();
bool const hovering = glm::length(offset) > 1e-3 && false == ImGui::GetIO().WantCaptureMouse;
glm::dvec3 const cursor = Camera.Pos + offset;
// the cursor where a click would put the point: the ground along the ray through the cursor in this frame
glm::dvec3 cursor{Camera.Pos};
bool const hovering = false == ImGui::GetIO().WantCaptureMouse && cursor_on_ground(cursor);
auto const ground = [&](double const X, double const Z, double const Fallback) {
double height;
return m_streamer.height_at(X, Z, height) ? height : Fallback;
@@ -2220,8 +2220,12 @@ void editor_mode::draw_terrain_overlay()
}
};
if (terrain_brush() && hovering)
// the brush aimed at the ground itself along the ray through the cursor
glm::dvec3 brushcentre = cursor;
bool const onground = false == ImGui::GetIO().WantCaptureMouse && terrain_cursor(brushcentre);
if (terrain_brush() && onground)
{
auto const cursor = brushcentre;
std::vector<glm::dvec3> circle;
for (int i = 0; i < 64; ++i)
{
@@ -2827,10 +2831,9 @@ void editor_mode::add_chunks_around(int const Count)
void editor_mode::handle_terrain_click(bool const Shift)
{
glm::dvec3 const offset = GfxRenderer->Mouse_Position();
if (glm::length(offset) < 1e-3)
return; // nothing under the cursor (yet)
glm::dvec3 const world = Camera.Pos + offset;
glm::dvec3 world;
if (false == cursor_on_ground(world))
return; // nothing under the cursor
switch (m_terrain_tool)
{
case terrain_tool::chunks:
@@ -3537,12 +3540,11 @@ void editor_mode::export_scenery()
void editor_mode::handle_terrain_brush(double Deltatime)
{
// world point under the cursor (Mouse_Position is camera-relative, like the brush placement uses)
glm::dvec3 const offset = GfxRenderer->Mouse_Position();
if (glm::length(offset) < 1e-3)
// the ground itself along the ray through the cursor in this frame
glm::dvec3 world;
if (false == terrain_cursor(world))
return;
glm::dvec3 const world = Camera.Pos + offset;
// only when the cursor is over the terrain (avoids editing on a stale depth read)
// only when the cursor is over the terrain
if (terrain_at(world.x, world.z) == nullptr)
return;
@@ -3585,6 +3587,37 @@ void editor_mode::handle_terrain_brush(double Deltatime)
}
}
bool editor_mode::cursor_on_ground(glm::dvec3 &World)
{
if (m_streamer.active() && terrain_cursor(World))
return true;
// no terrain under the ray: the depth read back under the cursor, as before
glm::dvec3 const offset = GfxRenderer->Mouse_Position();
if (glm::length(offset) <= 1e-3)
return false;
World = Camera.Pos + offset;
return true;
}
bool editor_mode::terrain_cursor(glm::dvec3 &World) const
{
// along the ray through the cursor as it is in this frame: the depth read back lags a frame behind
ImGuiIO const &io = ImGui::GetIO();
if (io.DisplaySize.x <= 0.0f || io.DisplaySize.y <= 0.0f)
return false;
auto const inverse = glm::inverse(editor_mode::projection_matrix(io.DisplaySize.x / io.DisplaySize.y) * GfxRenderer->Camera_View_Matrix());
glm::vec2 const ndc{io.MousePos.x / io.DisplaySize.x * 2.0f - 1.0f, 1.0f - io.MousePos.y / io.DisplaySize.y * 2.0f};
// two points of the ray, whatever the depth range of the projection
auto const point = [&](float const Depth) {
auto const clip = inverse * glm::vec4(ndc, Depth, 1.0f);
return glm::dvec3{glm::vec3{clip} / clip.w};
};
auto const direction = point(0.5f) - point(0.0f);
if (glm::length(direction) < 1e-9)
return false;
return m_streamer.raycast(GfxRenderer->Camera_Position() + point(0.0f), glm::normalize(direction), 5000.0, World);
}
void editor_mode::fill_terrain_area()
{
auto const &layers = m_streamer.manifest().layers;
@@ -3641,10 +3674,10 @@ void editor_mode::convert_selected_chunks()
bool editor_mode::pick_terrain_target()
{
glm::dvec3 const offset = GfxRenderer->Mouse_Position();
if (glm::length(offset) < 1e-3)
glm::dvec3 world;
if (false == cursor_on_ground(world))
return false; // nothing under the cursor
m_terrain_target = static_cast<float>(Camera.Pos.y + offset.y);
m_terrain_target = static_cast<float>(world.y);
m_terrain_status = format(STR_C("Target height: %.2f m"), m_terrain_target);
return true;
}
@@ -4468,14 +4501,13 @@ void editor_mode::render_object_menu()
void editor_mode::add_area_fill_point()
{
// like the other placement tools, the point comes from the depth buffer under the cursor
// (ground geometry only), relative to the camera
glm::dvec3 const offset = GfxRenderer->Mouse_Position();
if (glm::length(offset) < 1e-3)
return; // no ground under the cursor (yet)
// the ground along the ray through the cursor in this frame
glm::dvec3 world;
if (false == cursor_on_ground(world))
return; // no ground under the cursor
// the outline gives the fill the height to look for the ground at, and the height of what it finds no ground for
m_fill_points.push_back(placement_on_ground(Camera.Pos + offset));
m_fill_points.push_back(placement_on_ground(world));
}
void editor_mode::undo_last_area_fill()