diff --git a/tools/editor/editor_ui.cpp b/tools/editor/editor_ui.cpp index 012c5f33..0c88cf27 100644 --- a/tools/editor/editor_ui.cpp +++ b/tools/editor/editor_ui.cpp @@ -603,6 +603,21 @@ void EditorUI::renderBrushPanel(EditorApp& app) { ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1), "No stamp copied"); } + if (ImGui::CollapsingHeader("Canyon Generator")) { + static float canyonWidth = 15.0f, canyonDepth = 20.0f; + static int canyonSeed = 1; + ImGui::SliderFloat("Width##canyon", &canyonWidth, 5.0f, 50.0f); + ImGui::SliderFloat("Depth##canyon", &canyonDepth, 5.0f, 60.0f); + ImGui::InputInt("Seed##canyon", &canyonSeed); + ImGui::SameLine(); + if (ImGui::SmallButton("Rnd##cs")) canyonSeed = std::rand(); + if (ImGui::Button("Create Canyon", ImVec2(-1, 0))) { + app.getTerrainEditor().createCanyon(canyonWidth, canyonDepth, static_cast(canyonSeed)); + app.showToast("Canyon carved"); + } + ImGui::TextColored(ImVec4(0.6f, 0.6f, 0.6f, 1), "Winding canyon across tile. Fill with water for river."); + } + if (ImGui::CollapsingHeader("Island Generator")) { static float islandHeight = 30.0f, islandDrop = 20.0f; ImGui::SliderFloat("Center Height##island", &islandHeight, 5.0f, 100.0f); diff --git a/tools/editor/terrain_editor.cpp b/tools/editor/terrain_editor.cpp index 4f0f40c2..f165e910 100644 --- a/tools/editor/terrain_editor.cpp +++ b/tools/editor/terrain_editor.cpp @@ -860,6 +860,50 @@ void TerrainEditor::createHill(const glm::vec3& center, float radius, float heig dirty_ = true; } +void TerrainEditor::createCanyon(float width, float depth, uint32_t seed) { + if (!terrain_) return; + + float tileNW_X = (32.0f - static_cast(terrain_->coord.y)) * TILE_SIZE; + float tileNW_Y = (32.0f - static_cast(terrain_->coord.x)) * TILE_SIZE; + float tileCenter_X = tileNW_X - TILE_SIZE * 0.5f; + float tileCenter_Y = tileNW_Y - TILE_SIZE * 0.5f; + + // Generate a winding path using sine waves + auto canyonPath = [&](float t) -> glm::vec2 { + float px = tileCenter_X + (t - 0.5f) * TILE_SIZE * 0.9f; + float py = tileCenter_Y + std::sin(t * 6.28f * 1.5f + seed * 0.1f) * TILE_SIZE * 0.2f + + std::sin(t * 6.28f * 3.0f + seed * 0.3f) * TILE_SIZE * 0.05f; + return glm::vec2(px, py); + }; + + 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); + + // Find nearest point on canyon path + float bestDist = 1e30f; + for (float t = 0.0f; t <= 1.0f; t += 0.005f) { + glm::vec2 cp = canyonPath(t); + float d = glm::length(p - cp); + bestDist = std::min(bestDist, d); + } + + if (bestDist < width) { + float falloff = 1.0f - (bestDist / width); + falloff = falloff * falloff; + chunk.heightMap.heights[v] -= depth * falloff; + modified = true; + } + } + if (modified) { stitchEdges(ci); dirtyChunks_.push_back(ci); } + } + dirty_ = true; +} + void TerrainEditor::createIsland(float centerHeight, float edgeDropoff) { if (!terrain_) return; diff --git a/tools/editor/terrain_editor.hpp b/tools/editor/terrain_editor.hpp index 3f8a22f9..78086b23 100644 --- a/tools/editor/terrain_editor.hpp +++ b/tools/editor/terrain_editor.hpp @@ -96,6 +96,9 @@ public: // Create an island shape (raised center, dropping to base at edges) void createIsland(float centerHeight, float edgeDropoff); + // Create a winding canyon across the tile + void createCanyon(float width, float depth, uint32_t seed); + // Import/export heightmap (raw 16-bit grayscale, 129x129) bool importHeightmap(const std::string& path, float heightScale); bool exportHeightmap(const std::string& path, float heightScale);