From 1c58911da0ca2ae1493d6a34718969479926b583 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Tue, 5 May 2026 07:00:05 -0700 Subject: [PATCH] feat(editor): ridge/mountain range generator between two points - Ridge Generator: creates mountain ranges by setting start and end points, with configurable width and height - Cross-section uses quadratic falloff, along-axis uses sqrt taper for natural mountain range silhouette (wider at center, tapering at both ends) - Same start/end workflow as river/road tools --- tools/editor/editor_ui.cpp | 23 ++++++++++++++++++++++ tools/editor/terrain_editor.cpp | 35 +++++++++++++++++++++++++++++++++ tools/editor/terrain_editor.hpp | 3 +++ 3 files changed, 61 insertions(+) diff --git a/tools/editor/editor_ui.cpp b/tools/editor/editor_ui.cpp index 0a94587f..95c414ae 100644 --- a/tools/editor/editor_ui.cpp +++ b/tools/editor/editor_ui.cpp @@ -551,6 +551,29 @@ void EditorUI::renderBrushPanel(EditorApp& app) { ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1), "No stamp copied"); } + if (ImGui::CollapsingHeader("Ridge / Mountain Range")) { + static glm::vec3 ridgeStart{0}, ridgeEnd{0}; + static float ridgeWidth = 20.0f, ridgeHeight = 30.0f; + static bool ridgeStartSet = false; + ImGui::SliderFloat("Width##ridge", &ridgeWidth, 5.0f, 80.0f); + ImGui::SliderFloat("Height##ridge", &ridgeHeight, 5.0f, 100.0f); + auto& brushR = app.getTerrainEditor().brush(); + if (ImGui::Button("Set Start##ridge", ImVec2(120, 0)) && brushR.isActive()) { + ridgeStart = brushR.getPosition(); + ridgeStartSet = true; + app.showToast("Ridge start set"); + } + ImGui::SameLine(); + if (ImGui::Button("Set End + Create##ridge", ImVec2(140, 0)) && brushR.isActive() && ridgeStartSet) { + ridgeEnd = brushR.getPosition(); + app.getTerrainEditor().createRidge(ridgeStart, ridgeEnd, ridgeWidth, ridgeHeight); + app.showToast("Ridge created"); + ridgeStartSet = false; + } + if (ridgeStartSet) + ImGui::TextColored(ImVec4(0.5f, 0.9f, 0.5f, 1), "Start set"); + } + if (ImGui::CollapsingHeader("Hill Generator")) { static float hillRadius = 40.0f, hillHeight = 25.0f; ImGui::SliderFloat("Radius##hill", &hillRadius, 10.0f, 150.0f); diff --git a/tools/editor/terrain_editor.cpp b/tools/editor/terrain_editor.cpp index a2f39e8d..ac994ea5 100644 --- a/tools/editor/terrain_editor.cpp +++ b/tools/editor/terrain_editor.cpp @@ -860,6 +860,41 @@ void TerrainEditor::createHill(const glm::vec3& center, float radius, float heig dirty_ = true; } +void TerrainEditor::createRidge(const glm::vec3& start, const glm::vec3& end, + float width, float height) { + if (!terrain_) return; + glm::vec2 lineStart(start.x, start.y); + glm::vec2 lineEnd(end.x, end.y); + glm::vec2 lineDir = glm::normalize(lineEnd - lineStart); + float lineLen = glm::length(lineEnd - lineStart); + + for (int ci = 0; ci < 256; ci++) { + auto& chunk = terrain_->chunks[ci]; + if (!chunk.hasHeightMap()) continue; + bool modified = false; + for (int v = 0; v < 145; v++) { + glm::vec3 pos = chunkVertexWorldPos(ci, v); + glm::vec2 p(pos.x, pos.y); + glm::vec2 toP = p - lineStart; + float along = glm::dot(toP, lineDir); + along = std::clamp(along, 0.0f, lineLen); + glm::vec2 closest = lineStart + lineDir * along; + float dist = glm::length(p - closest); + if (dist >= width) continue; + + float crossFalloff = 1.0f - (dist / width); + crossFalloff = crossFalloff * crossFalloff; + float alongFalloff = 1.0f - 2.0f * std::abs(along / lineLen - 0.5f); + alongFalloff = std::max(0.0f, alongFalloff); + float h = height * crossFalloff * std::sqrt(alongFalloff); + chunk.heightMap.heights[v] += h; + modified = true; + } + if (modified) { stitchEdges(ci); dirtyChunks_.push_back(ci); } + } + dirty_ = true; +} + void TerrainEditor::flattenRoad(const glm::vec3& start, const glm::vec3& end, float width) { if (!terrain_) return; glm::vec2 lineStart(start.x, start.y); diff --git a/tools/editor/terrain_editor.hpp b/tools/editor/terrain_editor.hpp index 5cb47f16..9e91a732 100644 --- a/tools/editor/terrain_editor.hpp +++ b/tools/editor/terrain_editor.hpp @@ -90,6 +90,9 @@ public: // Create a smooth hill/mountain void createHill(const glm::vec3& center, float radius, float height); + // Create a ridge/mountain range between two points + void createRidge(const glm::vec3& start, const glm::vec3& end, float width, float height); + // Import/export heightmap (raw 16-bit grayscale, 129x129) bool importHeightmap(const std::string& path, float heightScale); bool exportHeightmap(const std::string& path, float heightScale);