From 907de021e5879df39addd3e4f1c33a30d9112ae7 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 12:30:41 +0100 Subject: [PATCH] Fields: one noise for the edges of the fields and the natural slopes, one filling of the geometry of the water Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ --- editor/editorTerrainStreamer.cpp | 77 +++++++++----------------------- editor/editorTerrainStreamer.hpp | 3 ++ scene/heightmapterrain.cpp | 23 +++++----- scene/heightmapterrain.h | 2 + 4 files changed, 36 insertions(+), 69 deletions(-) diff --git a/editor/editorTerrainStreamer.cpp b/editor/editorTerrainStreamer.cpp index cad7e76ae..a12df4b89 100644 --- a/editor/editorTerrainStreamer.cpp +++ b/editor/editorTerrainStreamer.cpp @@ -1425,35 +1425,6 @@ bool terrain_streamer::paint(double X, double Z, double Radius, double Strength, }); } -namespace -{ -// smooth noise of -1..1 in waves of about Scale metres, the same at the same place every time -double edge_noise(double const X, double const Z, double const Scale) -{ - auto const hash = [](std::int64_t const Ix, std::int64_t const Iz) { - auto h{static_cast(Ix) * 0x9E3779B97F4A7C15ull ^ static_cast(Iz) * 0xC2B2AE3D27D4EB4Full}; - h ^= h >> 31; - h *= 0xBF58476D1CE4E5B9ull; - h ^= h >> 29; - return static_cast(h & 0xffffff) / static_cast(0xffffff) * 2.0 - 1.0; - }; - auto const octave = [&](double const U, double const V) { - auto const iu{std::floor(U)}; - auto const iv{std::floor(V)}; - auto const fu{U - iu}; - auto const fv{V - iv}; - auto const su{fu * fu * (3.0 - 2.0 * fu)}; - auto const sv{fv * fv * (3.0 - 2.0 * fv)}; - auto const x{static_cast(iu)}; - auto const z{static_cast(iv)}; - auto const a{hash(x, z) + (hash(x + 1, z) - hash(x, z)) * su}; - auto const b{hash(x, z + 1) + (hash(x + 1, z + 1) - hash(x, z + 1)) * su}; - return a + (b - a) * sv; - }; - auto const scale{std::max(0.5, Scale)}; - return std::clamp(0.7 * octave(X / scale, Z / scale) + 0.3 * octave(X / scale * 2.7 + 17.3, Z / scale * 2.7 - 9.1), -1.0, 1.0); -} -} // namespace bool terrain_streamer::fill(std::vector const &Outline, double Feather, std::uint16_t Layer, double Strength) { @@ -1498,7 +1469,7 @@ bool terrain_streamer::fill(std::vector const &Outline, double Feath // signed distance from the outline, inside positive, the edge shifted by the noise, led smoothly through the blend auto distance{inside ? nearest : -nearest}; if (Noise > 0.0) - distance += Noise * edge_noise(Px, Pz, NoiseScale); + distance += Noise * heightmap::natural_noise(Px, Pz, NoiseScale, 0); if (Feather <= 0.0) return distance > 0.0 ? Strength : 0.0; auto const t{std::clamp((distance + margin) / Feather, 0.0, 1.0)}; @@ -2472,6 +2443,23 @@ void terrain_streamer::clear_water(bool const Touch) m_watershapes.clear(); } +gfx::geometry_handle terrain_streamer::water_geometry(gfx::vertex_array const &Vertices) +{ + // the geometry of water made before is filled anew, so editing the water doesn't pile up geometry + gfx::userdata_array nouserdata; + gfx::geometry_handle geometry{0, 0}; + if (false == m_waterfree.empty()) + { + geometry = m_waterfree.back(); + m_waterfree.pop_back(); + GfxRenderer->Replace(Vertices, nouserdata, geometry, GL_TRIANGLES); + return geometry; + } + if (m_waterbank.bank == 0 && m_waterbank.chunk == 0) + m_waterbank = GfxRenderer->Create_Bank(); + return GfxRenderer->Insert(Vertices, nouserdata, m_waterbank, GL_TRIANGLES); +} + void terrain_streamer::build_water() { m_waterchanged = false; @@ -2518,21 +2506,8 @@ void terrain_streamer::build_water() vertices.push_back(vertex); reach = std::max(reach, glm::length(point - centroid)); } - // the geometry of water made before is filled anew, so editing the water doesn't pile up geometry - gfx::userdata_array nouserdata; water_shape entry; - if (false == m_waterfree.empty()) - { - entry.geometry = m_waterfree.back(); - m_waterfree.pop_back(); - GfxRenderer->Replace(vertices, nouserdata, entry.geometry, GL_TRIANGLES); - } - else - { - if (m_waterbank.bank == 0 && m_waterbank.chunk == 0) - m_waterbank = GfxRenderer->Create_Bank(); - entry.geometry = GfxRenderer->Insert(vertices, nouserdata, m_waterbank, GL_TRIANGLES); - } + entry.geometry = water_geometry(vertices); if (entry.geometry.chunk == 0) continue; // two bounding vertices give the shape its area, the triangles are in the geometry @@ -2640,20 +2615,8 @@ void terrain_streamer::build_water() vertex.tangent = {static_cast(along.x), 0.f, static_cast(along.y), 1.f}; vertices.push_back(vertex); } - gfx::userdata_array nouserdata; water_shape entry; - if (false == m_waterfree.empty()) - { - entry.geometry = m_waterfree.back(); - m_waterfree.pop_back(); - GfxRenderer->Replace(vertices, nouserdata, entry.geometry, GL_TRIANGLES); - } - else - { - if (m_waterbank.bank == 0 && m_waterbank.chunk == 0) - m_waterbank = GfxRenderer->Create_Bank(); - entry.geometry = GfxRenderer->Insert(vertices, nouserdata, m_waterbank, GL_TRIANGLES); - } + entry.geometry = water_geometry(vertices); if (entry.geometry.chunk == 0) continue; auto const reach{glm::length(high - low) * 0.5}; diff --git a/editor/editorTerrainStreamer.hpp b/editor/editorTerrainStreamer.hpp index 07b9b15ea..8f99fe91f 100644 --- a/editor/editorTerrainStreamer.hpp +++ b/editor/editorTerrainStreamer.hpp @@ -304,6 +304,9 @@ class terrain_streamer void apply_modifiers(glm::dvec2 const &Min, glm::dvec2 const &Max); std::vector> modifier_bounds() const; void build_water(); + // the vertices of a shape of the water or of a field in the geometry of the water: one made before filled anew, or a new one; + // a handle of chunk 0 if it failed + gfx::geometry_handle water_geometry(gfx::vertex_array const &Vertices); void clear_water(bool const Touch); std::string pack_path(heightmap::pack_key const &Key) const; // the state of a chunk (null: no chunk) or of the description is kept for the change in progress, if it's the first time diff --git a/scene/heightmapterrain.cpp b/scene/heightmapterrain.cpp index 78198a6c8..a4a72aab2 100644 --- a/scene/heightmapterrain.cpp +++ b/scene/heightmapterrain.cpp @@ -187,7 +187,17 @@ int floor_div(int const Value, int const Divisor) return (Value >= 0 ? Value / Divisor : -((-Value + Divisor - 1) / Divisor)); } -// smooth noise of -1..1 in waves of about Scale metres, the same at the same place every time; Channel gives another one +// the larger of two values, the corner between them rounded off over K +double soft_max(double const A, double const B, double const K) +{ + if (K <= 1e-6) + return std::max(A, B); + auto const h{std::max(K - std::abs(A - B), 0.0) / K}; + return std::max(A, B) + h * h * K * 0.25; +} + +} // namespace + double natural_noise(double const X, double const Z, double const Scale, int const Channel) { auto const hash = [](std::int64_t const Ix, std::int64_t const Iz) { @@ -216,17 +226,6 @@ double natural_noise(double const X, double const Z, double const Scale, int con return std::clamp(0.7 * octave(u, v) + 0.3 * octave(u * 2.7 + 17.3, v * 2.7 - 9.1), -1.0, 1.0); } -// the larger of two values, the corner between them rounded off over K -double soft_max(double const A, double const B, double const K) -{ - if (K <= 1e-6) - return std::max(A, B); - auto const h{std::max(K - std::abs(A - B), 0.0) / K}; - return std::max(A, B) + h * h * K * 0.25; -} - -} // namespace - int cells_for(float const Spacing) { return static_cast(std::lround(chunk_size / valid_spacing(Spacing))); diff --git a/scene/heightmapterrain.h b/scene/heightmapterrain.h index b41325fca..ec7c1e97a 100644 --- a/scene/heightmapterrain.h +++ b/scene/heightmapterrain.h @@ -301,6 +301,8 @@ struct manifest // past this many the patches are saved to their own file (manifest::patches_file) std::size_t constexpr patches_inline{200}; +// smooth noise of -1..1 in waves of about Scale metres, the same at the same place every time; Channel gives another one +double natural_noise(double X, double Z, double Scale, int Channel); bool load_patches(std::string const &Path, std::vector &Patches); bool save_patches(std::string const &Path, std::vector const &Patches); bool load_manifest(std::string const &Path, manifest &Manifest, std::string *Error = nullptr);