From f1f315d629686886e36b9cb099238e4b8d7ee1f3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 9 Nov 2026 23:15:18 +0100 Subject: [PATCH 1/3] 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 Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ --- application/editormode.cpp | 76 +++++++++++++++++++++++--------- application/editormode.h | 7 +++ editor/editorTerrainStreamer.cpp | 39 ++++++++++++++++ editor/editorTerrainStreamer.hpp | 2 + 4 files changed, 102 insertions(+), 22 deletions(-) diff --git a/application/editormode.cpp b/application/editormode.cpp index bb1b10fe5..5b946f28b 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -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 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(Camera.Pos.y + offset.y); + m_terrain_target = static_cast(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() diff --git a/application/editormode.h b/application/editormode.h index ef6202f38..bb695c634 100644 --- a/application/editormode.h +++ b/application/editormode.h @@ -676,6 +676,13 @@ class editor_mode : public application_mode, private editor_track::observer, pri return m_terrain_tool == terrain_tool::sculpt || m_terrain_tool == terrain_tool::smooth || m_terrain_tool == terrain_tool::level || m_terrain_tool == terrain_tool::paint || m_terrain_tool == terrain_tool::restore; } + // the point of the ground of the terrain under the cursor, found along the ray through the cursor, so it's found also over + // a hole and behind the grass. returns: false if the ray meets no terrain + bool terrain_cursor(glm::dvec3 &World) const; + // the point of the ground under the cursor in this frame, for the points put on the ground (outlines, areas, brushes): along the ray + // through the cursor to the terrain; the depth read back (a frame late, and stopped by the models over the ground) only where + // there's none. false: nothing under the cursor + bool cursor_on_ground(glm::dvec3 &World); // converts the point spacing of the selected chunks to m_terrain_convert_spacing void convert_selected_chunks(); // the chunks selected with the spacing tool taken out of the terrain, in one step of the history diff --git a/editor/editorTerrainStreamer.cpp b/editor/editorTerrainStreamer.cpp index 8eca20198..aaf947ba5 100644 --- a/editor/editorTerrainStreamer.cpp +++ b/editor/editorTerrainStreamer.cpp @@ -810,6 +810,45 @@ editor_terrain *terrain_streamer::terrain_at(double X, double Z) const return nullptr; } +bool terrain_streamer::raycast(glm::dvec3 const &Origin, glm::dvec3 const &Direction, double const Reach, glm::dvec3 &Hit) const +{ + if (glm::length(Direction) < 1e-9) + return false; + auto const direction{glm::normalize(Direction)}; + // above the ground (or where there's none) at the start of a step, below it at its end: the crossing is in between + auto const above = [&](double const T) { + auto const point{Origin + direction * T}; + double height; + return false == height_at(point.x, point.z, height) || point.y > height; + }; + if (false == above(0.0)) + return false; // starts under the ground + double previous{0.0}; + double t{0.0}; + while (t < Reach) + { + // finer near the camera, where the brush is aimed most + t = std::min(Reach, t + std::max(0.25, t * 0.004)); + if (above(t)) + { + previous = t; + continue; + } + auto low{previous}, high{t}; + for (int i = 0; i < 24; ++i) + { + auto const middle{(low + high) * 0.5}; + if (above(middle)) + low = middle; + else + high = middle; + } + Hit = Origin + direction * high; + return true; + } + return false; +} + bool terrain_streamer::height_at(double X, double Z, double &Height) const { auto const *chunk{terrain_at(X, Z)}; diff --git a/editor/editorTerrainStreamer.hpp b/editor/editorTerrainStreamer.hpp index 72050b500..cf8407ac1 100644 --- a/editor/editorTerrainStreamer.hpp +++ b/editor/editorTerrainStreamer.hpp @@ -69,6 +69,8 @@ class terrain_streamer editor_terrain *terrain_at(double X, double Z) const; // height of the terrain at (X,Z); false where there's no chunk in memory bool height_at(double X, double Z, double &Height) const; + // the first point of the ground along the ray from the origin, within the reach; false if it meets none + bool raycast(glm::dvec3 const &Origin, glm::dvec3 const &Direction, double Reach, glm::dvec3 &Hit) const; // triangles of the full grid of the chunks in memory overlapping the rectangle (x, z) void gather_triangles(glm::dvec2 const &Min, glm::dvec2 const &Max, std::vector> &Out) const; // true if the geometry belongs to the terrain (or its water) From 9a4d5a4688d3f3bf7c2a7f55332ad555539c88b9 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:36:24 +0100 Subject: [PATCH 2/3] fix(editor): the ray to the terrain doesn't come up under a chunk through a gap, and starts behind the camera in the top view Coming down where there's no terrain and finding itself under the ground of the next chunk, the ray gives up and the depth read back says where the ground is. In the top view it starts 10 km behind the plane of the camera, as far as the view sees, so the ground higher than the camera is met too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ --- application/editormode.cpp | 6 +++++- editor/editorTerrainStreamer.cpp | 20 +++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/application/editormode.cpp b/application/editormode.cpp index 5b946f28b..d7424c19e 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -3615,7 +3615,11 @@ bool editor_mode::terrain_cursor(glm::dvec3 &World) const 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); + // the top view sees from 10 km behind the plane of the camera (the near plane of its projection): the ray starts there, + // so the ground higher than the camera is met too + auto const back{Global.EditorOrtho ? 10000.0 : 0.0}; + auto const along{glm::normalize(direction)}; + return m_streamer.raycast(GfxRenderer->Camera_Position() + point(0.0f) - along * back, along, 5000.0 + back, World); } void editor_mode::fill_terrain_area() diff --git a/editor/editorTerrainStreamer.cpp b/editor/editorTerrainStreamer.cpp index aaf947ba5..a681b1a9e 100644 --- a/editor/editorTerrainStreamer.cpp +++ b/editor/editorTerrainStreamer.cpp @@ -815,13 +815,17 @@ bool terrain_streamer::raycast(glm::dvec3 const &Origin, glm::dvec3 const &Direc if (glm::length(Direction) < 1e-9) return false; auto const direction{glm::normalize(Direction)}; - // above the ground (or where there's none) at the start of a step, below it at its end: the crossing is in between - auto const above = [&](double const T) { + // 1 above the ground, -1 under it, 0 where there's no terrain + auto const side = [&](double const T) { auto const point{Origin + direction * T}; double height; - return false == height_at(point.x, point.z, height) || point.y > height; + if (false == height_at(point.x, point.z, height)) + return 0; + return point.y > height ? 1 : -1; }; - if (false == above(0.0)) + auto const above = [&](double const T) { return side(T) >= 0; }; + auto last{side(0.0)}; + if (last < 0) return false; // starts under the ground double previous{0.0}; double t{0.0}; @@ -829,11 +833,17 @@ bool terrain_streamer::raycast(glm::dvec3 const &Origin, glm::dvec3 const &Direc { // finer near the camera, where the brush is aimed most t = std::min(Reach, t + std::max(0.25, t * 0.004)); - if (above(t)) + auto const now{side(t)}; + if (now >= 0) { previous = t; + last = now; continue; } + // under the ground coming from where there's no terrain: the side of a chunk seen through a gap, not the ground + // seen on the screen; the depth read back says where that is + if (last == 0) + return false; auto low{previous}, high{t}; for (int i = 0; i < 24; ++i) { From 4ef4a30ef941485e589b33aec8f3fe702fbf7732 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 22:29:04 +0100 Subject: [PATCH 3/3] fix(editor): the cursor off the terrain on the ground only where the depth read hit something (Mouse_Hit), not where its offset isn't zero Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ --- application/editormode.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/application/editormode.cpp b/application/editormode.cpp index d7424c19e..9cf75ce34 100644 --- a/application/editormode.cpp +++ b/application/editormode.cpp @@ -3591,11 +3591,10 @@ 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) + // no terrain under the ray: the depth read back under the cursor, as before, where it hit anything + if (false == GfxRenderer->Mouse_Hit()) return false; - World = Camera.Pos + offset; + World = Camera.Pos + GfxRenderer->Mouse_Position(); return true; }