diff --git a/tools/editor/editor_ui.cpp b/tools/editor/editor_ui.cpp index 58646c91..62392635 100644 --- a/tools/editor/editor_ui.cpp +++ b/tools/editor/editor_ui.cpp @@ -603,6 +603,19 @@ void EditorUI::renderBrushPanel(EditorApp& app) { ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1), "No stamp copied"); } + if (ImGui::CollapsingHeader("Thermal Erosion")) { + static int erosionIters = 10; + static float talusAngle = 40.0f; + ImGui::SliderInt("Iterations##therm", &erosionIters, 1, 50); + ImGui::SliderFloat("Talus Angle##therm", &talusAngle, 10.0f, 80.0f, "%.0f deg"); + if (ImGui::Button("Apply Thermal Erosion", ImVec2(-1, 0))) { + app.getTerrainEditor().thermalErosion(erosionIters, talusAngle); + app.showToast("Thermal erosion applied"); + } + ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1), + "Material slides downhill. Lower angle = more erosion."); + } + if (ImGui::CollapsingHeader("Terrace / Steps")) { static int terraceSteps = 6; ImGui::SliderInt("Steps##terrace", &terraceSteps, 2, 20); diff --git a/tools/editor/terrain_editor.cpp b/tools/editor/terrain_editor.cpp index a949458f..5cc0d803 100644 --- a/tools/editor/terrain_editor.cpp +++ b/tools/editor/terrain_editor.cpp @@ -860,6 +860,42 @@ void TerrainEditor::createHill(const glm::vec3& center, float radius, float heig dirty_ = true; } +void TerrainEditor::thermalErosion(int iterations, float talusAngle) { + if (!terrain_) return; + float unitSize = CHUNK_SIZE / 8.0f; + float maxDelta = std::tan(talusAngle * 3.14159f / 180.0f) * unitSize; + + for (int iter = 0; iter < iterations; iter++) { + for (int ci = 0; ci < 256; ci++) { + auto& chunk = terrain_->chunks[ci]; + if (!chunk.hasHeightMap()) continue; + for (int v = 0; v < 145; v++) { + int row = v / 17, col = v % 17; + if (col > 8) continue; + float h = chunk.heightMap.heights[v]; + int neighbors[] = {v - 17, v + 17, v - 1, v + 1}; + for (int n : neighbors) { + if (n < 0 || n >= 145) continue; + int nRow = n / 17, nCol = n % 17; + if (nCol > 8 || std::abs(nRow - row) > 1 || std::abs(nCol - col) > 1) continue; + float nh = chunk.heightMap.heights[n]; + float delta = h - nh; + if (delta > maxDelta) { + float transfer = (delta - maxDelta) * 0.25f; + chunk.heightMap.heights[v] -= transfer; + chunk.heightMap.heights[n] += transfer; + } + } + } + } + } + for (int ci = 0; ci < 256; ci++) { + stitchEdges(ci); + dirtyChunks_.push_back(ci); + } + dirty_ = true; +} + void TerrainEditor::terraceHeights(int steps) { if (!terrain_ || steps < 2) return; diff --git a/tools/editor/terrain_editor.hpp b/tools/editor/terrain_editor.hpp index 04925140..4ddf48c6 100644 --- a/tools/editor/terrain_editor.hpp +++ b/tools/editor/terrain_editor.hpp @@ -102,6 +102,9 @@ public: // Terrace/quantize heights into N steps void terraceHeights(int steps); + // Thermal erosion: material falls downhill based on angle of repose + void thermalErosion(int iterations, float talusAngle); + // Import/export heightmap (raw 16-bit grayscale, 129x129) bool importHeightmap(const std::string& path, float heightScale); bool exportHeightmap(const std::string& path, float heightScale);