From 9a4d5a4688d3f3bf7c2a7f55332ad555539c88b9 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 11 Nov 2026 11:36:24 +0100 Subject: [PATCH] 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) {