18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-21 01:34:59 +01:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ
This commit is contained in:
Claude
2026-11-11 11:36:24 +01:00
parent f1f315d629
commit 9a4d5a4688
2 changed files with 20 additions and 6 deletions

View File

@@ -3615,7 +3615,11 @@ bool editor_mode::terrain_cursor(glm::dvec3 &World) const
auto const direction = point(0.5f) - point(0.0f); auto const direction = point(0.5f) - point(0.0f);
if (glm::length(direction) < 1e-9) if (glm::length(direction) < 1e-9)
return false; 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() void editor_mode::fill_terrain_area()

View File

@@ -815,13 +815,17 @@ bool terrain_streamer::raycast(glm::dvec3 const &Origin, glm::dvec3 const &Direc
if (glm::length(Direction) < 1e-9) if (glm::length(Direction) < 1e-9)
return false; return false;
auto const direction{glm::normalize(Direction)}; 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 // 1 above the ground, -1 under it, 0 where there's no terrain
auto const above = [&](double const T) { auto const side = [&](double const T) {
auto const point{Origin + direction * T}; auto const point{Origin + direction * T};
double height; 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 return false; // starts under the ground
double previous{0.0}; double previous{0.0};
double t{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 // finer near the camera, where the brush is aimed most
t = std::min(Reach, t + std::max(0.25, t * 0.004)); 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; previous = t;
last = now;
continue; 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}; auto low{previous}, high{t};
for (int i = 0; i < 24; ++i) for (int i = 0; i < 24; ++i)
{ {