mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 12:44: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:
@@ -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)};
|
||||
|
||||
Reference in New Issue
Block a user