18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 22:04:59 +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

@@ -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)};

View File

@@ -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<std::array<glm::dvec3, 3>> &Out) const;
// true if the geometry belongs to the terrain (or its water)