refactor: rewrite blend_heightmap_boundaries

This commit is contained in:
2026-05-02 14:55:52 +08:00
parent a02bfad639
commit 11af0d9f01
3 changed files with 88 additions and 65 deletions

View File

@@ -26,6 +26,6 @@ static constexpr int SIZE_X = CHUCK_SIZE;
static constexpr int SIZE_Y = WORLD_SIZE_Y; static constexpr int SIZE_Y = WORLD_SIZE_Y;
static constexpr int SIZE_Z = CHUCK_SIZE; static constexpr int SIZE_Z = CHUCK_SIZE;
using HeightMapArray = std::array<std::array<float, CHUCK_SIZE>, CHUCK_SIZE>; using HeightMapArray = std::array<std::array<int, CHUCK_SIZE>, CHUCK_SIZE>;
} // namespace Cubed } // namespace Cubed

View File

@@ -19,7 +19,6 @@ private:
static constexpr int SIZE_Y = WORLD_SIZE_Y; static constexpr int SIZE_Y = WORLD_SIZE_Y;
static constexpr int SIZE_Z = CHUCK_SIZE; static constexpr int SIZE_Z = CHUCK_SIZE;
using HeightMapArray = std::array<std::array<float, SIZE_Z>, SIZE_X>;
std::atomic<bool> m_dirty{false}; std::atomic<bool> m_dirty{false};
std::atomic<bool> m_need_upload{true}; std::atomic<bool> m_need_upload{true};
std::atomic<bool> m_is_on_gen_vertex_data{false}; std::atomic<bool> m_is_on_gen_vertex_data{false};

View File

@@ -96,7 +96,7 @@ void ChunkGenerator::generate_heightmap() {
float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE); float world_x = static_cast<float>(x + m_chunk_pos.x * CHUCK_SIZE);
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE); float world_z = static_cast<float>(z + m_chunk_pos.z * CHUCK_SIZE);
auto sample_height = [&](BiomeType b) -> float { auto sample_height = [&](BiomeType b) -> int {
auto range = get_biome_height_range(b); auto range = get_biome_height_range(b);
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b); auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f, float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f,
@@ -106,7 +106,7 @@ void ChunkGenerator::generate_heightmap() {
0.25f * PerlinNoise::noise(world_x * f3, 0.5f, 0.25f * PerlinNoise::noise(world_x * f3, 0.5f,
world_z * f3); world_z * f3);
n /= 1.75f; n /= 1.75f;
return range.base_y + n * range.amplitude; return range.base_y + std::round(n * range.amplitude);
}; };
m_heightmap[x][z] = sample_height(m_biome); m_heightmap[x][z] = sample_height(m_biome);
} }
@@ -117,76 +117,100 @@ void ChunkGenerator::blend_heightmap_boundaries(
const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap) { const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap) {
auto& m_heightmap = m_chunk.heightmap(); auto& m_heightmap = m_chunk.heightmap();
auto m_biome = m_chunk.biome(); auto m_biome = m_chunk.biome();
// Width of interpolation influence (in number of cells)
for (int x = 0; x < SIZE_X; x++) {
for (int z = 0; z < SIZE_Z; z++) {
float h = static_cast<float>(m_heightmap[x][z]);
float total_weight = 1.0f;
float blended = h;
// --- Right neighbor neighbor[0]: (1, 0) --- // --- Right neighbor neighbor[0]: (1, 0) ---
// Blend when x is close to SIZE_X-1 for (int z = 0; z < SIZE_Z; z++) {
if (neighbor_heightmap[0] != std::nullopt && if (neighbor_heightmap[0] != std::nullopt &&
neighbor_biome[0] != m_biome) { neighbor_biome[0] != m_biome) {
int dist = (SIZE_X - 1) - x; // distance from right border int edge_x = CHUCK_SIZE - 1;
if (dist < BLEND_RADIUS) { int h = m_heightmap[edge_x][z];
// Neighbor's boundary row is its x=0 column int neighbor_h = (*neighbor_heightmap[0])[0][z];
float neighbor_h = if (h <= neighbor_h) {
static_cast<float>((*neighbor_heightmap[0])[0][z]); continue;
float t =
1.0f - static_cast<float>(dist) /
BLEND_RADIUS; // larger weight when closer
// Use smoothstep for a more natural transition
t = t * t * (3.0f - 2.0f * t);
blended += t * neighbor_h;
total_weight += t;
}
} }
const int DIR = (edge_x == 0) ? 1 : -1;
for (int i = 0; i < BLEND_RADIUS; i++) {
int x = edge_x + DIR * i;
float t = static_cast<float>(i) / BLEND_RADIUS;
// float smooth_t = t * t * (3.0f - 2.0f * t);
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
m_heightmap[x][z] = static_cast<int>(
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
}
}
}
// --- Left neighbor neighbor[1]: (-1, 0) --- // --- Left neighbor neighbor[1]: (-1, 0) ---
for (int z = 0; z < SIZE_Z; z++) {
if (neighbor_heightmap[1] != std::nullopt && if (neighbor_heightmap[1] != std::nullopt &&
neighbor_biome[1] != m_biome) { neighbor_biome[1] != m_biome) {
int dist = x; // distance from left border int edge_x = 0;
if (dist < BLEND_RADIUS) { int h = m_heightmap[edge_x][z];
float neighbor_h = static_cast<float>( int neighbor_h = (*neighbor_heightmap[1])[CHUCK_SIZE - 1][z];
(*neighbor_heightmap[1])[SIZE_X - 1][z]); if (h <= neighbor_h) {
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS; continue;
t = t * t * (3.0f - 2.0f * t);
blended += t * neighbor_h;
total_weight += t;
}
} }
const int DIR = (edge_x == 0) ? 1 : -1;
for (int i = 0; i < BLEND_RADIUS; i++) {
int x = edge_x + DIR * i;
float t = static_cast<float>(i) / BLEND_RADIUS;
// float smooth_t = t * t * (3.0f - 2.0f * t);
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
m_heightmap[x][z] = static_cast<int>(
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
}
}
}
// --- Front neighbor neighbor[2]: (0, 1) --- // --- Front neighbor neighbor[2]: (0, 1) ---
for (int x = 0; x < SIZE_X; x++) {
if (neighbor_heightmap[2] != std::nullopt && if (neighbor_heightmap[2] != std::nullopt &&
neighbor_biome[2] != m_biome) { neighbor_biome[2] != m_biome) {
int dist = (SIZE_Z - 1) - z; int edge_z = CHUCK_SIZE - 1;
if (dist < BLEND_RADIUS) { int h = m_heightmap[x][edge_z];
float neighbor_h = int neighbor_h = (*neighbor_heightmap[2])[x][0];
static_cast<float>((*neighbor_heightmap[2])[x][0]); if (h <= neighbor_h) {
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS; continue;
t = t * t * (3.0f - 2.0f * t); }
blended += t * neighbor_h; const int DIR = (edge_z == 0) ? 1 : -1;
total_weight += t; for (int i = 0; i < BLEND_RADIUS; i++) {
int z = edge_z + DIR * i;
float t = static_cast<float>(i) / BLEND_RADIUS;
// float smooth_t = t * t * (3.0f - 2.0f * t);
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
m_heightmap[x][z] = static_cast<int>(
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
}
} }
} }
// --- Back neighbor neighbor[3]: (0, -1) --- // --- Back neighbor neighbor[3]: (0, -1) ---
for (int x = 0; x < SIZE_X; x++) {
if (neighbor_heightmap[3] != std::nullopt && if (neighbor_heightmap[3] != std::nullopt &&
neighbor_biome[3] != m_biome) { neighbor_biome[3] != m_biome) {
int dist = z; int edge_z = 0;
if (dist < BLEND_RADIUS) { int h = m_heightmap[x][edge_z];
float neighbor_h = static_cast<float>( int neighbor_h = (*neighbor_heightmap[3])[x][CHUCK_SIZE - 1];
(*neighbor_heightmap[3])[x][SIZE_Z - 1]); if (h <= neighbor_h) {
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS; continue;
t = t * t * (3.0f - 2.0f * t);
blended += t * neighbor_h;
total_weight += t;
} }
int delta_h = h - neighbor_h;
int step = delta_h / BLEND_RADIUS;
if (step < 1) {
continue;
} }
const int DIR = (edge_z == 0) ? 1 : -1;
for (int i = 0; i < BLEND_RADIUS; i++) {
int z = edge_z + DIR * i;
float t = static_cast<float>(i) / BLEND_RADIUS;
m_heightmap[x][z] = static_cast<int>(blended / total_weight); // float smooth_t = t * t * (3.0f - 2.0f * t);
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
m_heightmap[x][z] = static_cast<int>(
std::round(neighbor_h + (h - neighbor_h) * smooth_t));
}
} }
} }
} }