From 1502c2ed852db8b14123b65ec071ccbe86480d7d Mon Sep 17 00:00:00 2001 From: Kelsi Date: Tue, 5 May 2026 06:50:24 -0700 Subject: [PATCH] feat(editor): crater generator for lakes, arenas, impact sites - Crater Generator in Sculpt panel: creates a bowl-shaped depression with configurable radius, depth, and raised rim height - Parabolic bowl interior with sinusoidal rim and smooth outer falloff - Perfect for creating lakes (fill with Water mode), arenas, or impact craters for volcanic zones - One click at cursor position, uses brush position for center --- tools/editor/editor_ui.cpp | 13 +++++++++++ tools/editor/terrain_editor.cpp | 41 +++++++++++++++++++++++++++++++++ tools/editor/terrain_editor.hpp | 3 +++ 3 files changed, 57 insertions(+) diff --git a/tools/editor/editor_ui.cpp b/tools/editor/editor_ui.cpp index 3865fc29..d517c4fe 100644 --- a/tools/editor/editor_ui.cpp +++ b/tools/editor/editor_ui.cpp @@ -551,6 +551,19 @@ void EditorUI::renderBrushPanel(EditorApp& app) { ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1), "No stamp copied"); } + if (ImGui::CollapsingHeader("Crater Generator")) { + static float craterRadius = 30.0f, craterDepth = 10.0f, craterRim = 3.0f; + ImGui::SliderFloat("Radius##crater", &craterRadius, 5.0f, 100.0f); + ImGui::SliderFloat("Depth##crater", &craterDepth, 2.0f, 50.0f); + ImGui::SliderFloat("Rim Height##crater", &craterRim, 0.0f, 15.0f); + auto& brush5 = app.getTerrainEditor().brush(); + if (ImGui::Button("Create Crater at Cursor", ImVec2(-1, 0)) && brush5.isActive()) { + app.getTerrainEditor().createCrater(brush5.getPosition(), craterRadius, craterDepth, craterRim); + app.showToast("Crater created"); + } + ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1), "Bowl with raised rim. Fill with water for a lake."); + } + if (ImGui::CollapsingHeader("Flatten Platform")) { auto& brush3 = app.getTerrainEditor().brush(); if (ImGui::Button("Create Flat Platform at Cursor", ImVec2(-1, 0)) && brush3.isActive()) { diff --git a/tools/editor/terrain_editor.cpp b/tools/editor/terrain_editor.cpp index 557ae608..2ce193d3 100644 --- a/tools/editor/terrain_editor.cpp +++ b/tools/editor/terrain_editor.cpp @@ -764,6 +764,47 @@ void TerrainEditor::carveRiver(const glm::vec3& start, const glm::vec3& end, dirty_ = true; } +void TerrainEditor::createCrater(const glm::vec3& center, float radius, float depth, float rimHeight) { + if (!terrain_) return; + + 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); + float dist = glm::length(glm::vec2(pos.x - center.x, pos.y - center.y)); + if (dist > radius * 1.3f) continue; + + float t = dist / radius; + float delta = 0.0f; + + if (t < 0.8f) { + // Bowl interior: parabolic depression + float bowlT = t / 0.8f; + delta = -depth * (1.0f - bowlT * bowlT); + } else if (t < 1.0f) { + // Rim: raised edge + float rimT = (t - 0.8f) / 0.2f; + delta = rimHeight * std::sin(rimT * 3.14159f); + } else if (t < 1.3f) { + // Outer falloff + float fallT = (t - 1.0f) / 0.3f; + delta = rimHeight * (1.0f - fallT) * 0.3f; + } + + chunk.heightMap.heights[v] += delta; + 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 f35cf6e5..76de52f5 100644 --- a/tools/editor/terrain_editor.hpp +++ b/tools/editor/terrain_editor.hpp @@ -81,6 +81,9 @@ public: // Flatten a road between two points (smooths to average height along path) void flattenRoad(const glm::vec3& start, const glm::vec3& end, float width); + // Create a crater at a position (bowl shape with raised rim) + void createCrater(const glm::vec3& center, float radius, float depth, float rimHeight); + // Import/export heightmap (raw 16-bit grayscale, 129x129) bool importHeightmap(const std::string& path, float heightScale); bool exportHeightmap(const std::string& path, float heightScale);