feat(editor): winding canyon generator with seeded sine-wave path

- Canyon Generator: carves a winding canyon across the entire tile
  using layered sine waves for natural serpentine shape
- Configurable width, depth, and seed for different canyon shapes
- Quadratic falloff at edges for smooth cliff walls
- Random seed button for quick shape exploration
- Fill with Water mode for instant river canyon
This commit is contained in:
Kelsi 2026-05-05 07:37:27 -07:00
parent 7971fd7989
commit f3846919a4
3 changed files with 62 additions and 0 deletions

View file

@ -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<uint32_t>(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);

View file

@ -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<float>(terrain_->coord.y)) * TILE_SIZE;
float tileNW_Y = (32.0f - static_cast<float>(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;

View file

@ -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);