mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-20 04:35:02 +01:00
Merge branch 'master' into feat/renderers-cleanup
This commit is contained in:
@@ -907,6 +907,55 @@ 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)};
|
||||
// 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;
|
||||
if (false == height_at(point.x, point.z, height))
|
||||
return 0;
|
||||
return point.y > height ? 1 : -1;
|
||||
};
|
||||
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};
|
||||
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));
|
||||
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)
|
||||
{
|
||||
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)};
|
||||
@@ -946,45 +995,6 @@ void terrain_streamer::gather_triangles(glm::dvec2 const &Min, glm::dvec2 const
|
||||
}
|
||||
}
|
||||
|
||||
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::owns(gfx::geometry_handle const &Geometry) const
|
||||
{
|
||||
if (Geometry.bank == 0 && Geometry.chunk == 0)
|
||||
@@ -2004,6 +2014,38 @@ bool terrain_streamer::remove_modifier(std::string const &Name)
|
||||
return true;
|
||||
}
|
||||
|
||||
std::size_t terrain_streamer::remove_modifiers(std::vector<std::string> const &Names)
|
||||
{
|
||||
std::unordered_set<std::string> const names(Names.begin(), Names.end());
|
||||
auto &list{m_manifest.modifiers};
|
||||
auto const matches = [&](heightmap::modifier const &Item) { return names.contains(Item.name); };
|
||||
std::vector<std::pair<glm::dvec2, glm::dvec2>> areas;
|
||||
for (auto const &item : list)
|
||||
if (matches(item))
|
||||
areas.push_back(item.bounds());
|
||||
if (areas.empty())
|
||||
return 0;
|
||||
remember_manifest();
|
||||
list.erase(std::remove_if(list.begin(), list.end(), matches), list.end());
|
||||
m_manifestchanged = true;
|
||||
// one by one, the thousands of a terrain made for another version of the scenery took minutes of the terrain led anew each time
|
||||
std::unordered_map<editor_terrain *, std::pair<glm::dvec2, glm::dvec2>> reached;
|
||||
for (auto const &[from, to] : areas)
|
||||
for (auto *chunk : chunks_in(from, to, true))
|
||||
{
|
||||
auto const [found, fresh]{reached.try_emplace(chunk, from, to)};
|
||||
if (false == fresh)
|
||||
{
|
||||
found->second.first = glm::min(found->second.first, from);
|
||||
found->second.second = glm::max(found->second.second, to);
|
||||
}
|
||||
}
|
||||
auto const bounds{modifier_bounds()};
|
||||
for (auto const &[chunk, area] : reached)
|
||||
apply_modifiers(*chunk, area.first, area.second, bounds);
|
||||
return areas.size();
|
||||
}
|
||||
|
||||
bool terrain_streamer::bake_modifier(std::string const &Name)
|
||||
{
|
||||
auto const existing{std::find_if(m_manifest.modifiers.begin(), m_manifest.modifiers.end(), [&](heightmap::modifier const &Item) { return Item.name == Name; })};
|
||||
|
||||
Reference in New Issue
Block a user