mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-06-18 00:27:02 +08:00
refactor: chunk interpolate (#6)
* refactor: rewrite blend_heightmap_boundaries * refactor: init_world * fix: unnatural biome boundary transition
This commit is contained in:
@@ -57,18 +57,14 @@ void ChunkGenerator::assign_chunk_biome() {
|
||||
}
|
||||
|
||||
void ChunkGenerator::resolve_biome_adjacency_conflict(
|
||||
const std::array<const Chunk*, 4>& adj_chunks) {
|
||||
const std::array<const Chunk*, 8>& adj_chunks) {
|
||||
auto m_biome = m_chunk.biome();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
auto& chunk = adj_chunks[i];
|
||||
if (chunk == nullptr) {
|
||||
continue;
|
||||
}
|
||||
BiomeType biome = chunk->get_biome();
|
||||
neighbor_biome[i] = biome;
|
||||
if (biome == BiomeType::RIVER) {
|
||||
is_neighbor_river = true;
|
||||
}
|
||||
for (const auto& non : NON_ADJACENT) {
|
||||
if (m_biome != non.first) {
|
||||
continue;
|
||||
@@ -90,13 +86,13 @@ void ChunkGenerator::generate_heightmap() {
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
auto m_biome = m_chunk.biome();
|
||||
|
||||
for (int x = 0; x < CHUCK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUCK_SIZE; z++) {
|
||||
for (int x = 0; x < CHUNK_SIZE; x++) {
|
||||
for (int z = 0; z < CHUNK_SIZE; z++) {
|
||||
|
||||
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_x = static_cast<float>(x + m_chunk_pos.x * CHUNK_SIZE);
|
||||
float world_z = static_cast<float>(z + m_chunk_pos.z * CHUNK_SIZE);
|
||||
|
||||
auto sample_height = [&](BiomeType b) -> float {
|
||||
auto sample_height = [&](BiomeType b) -> int {
|
||||
auto range = get_biome_height_range(b);
|
||||
auto [f1, f2, f3] = get_noise_frequencies_for_biome(b);
|
||||
float n = 1.00f * PerlinNoise::noise(world_x * f1, 0.5f,
|
||||
@@ -106,7 +102,7 @@ void ChunkGenerator::generate_heightmap() {
|
||||
0.25f * PerlinNoise::noise(world_x * f3, 0.5f,
|
||||
world_z * f3);
|
||||
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);
|
||||
}
|
||||
@@ -114,79 +110,240 @@ void ChunkGenerator::generate_heightmap() {
|
||||
}
|
||||
|
||||
void ChunkGenerator::blend_heightmap_boundaries(
|
||||
const std::array<std::optional<HeightMapArray>, 4>& neighbor_heightmap) {
|
||||
const std::array<std::optional<HeightMapArray>, 8>& neighbor_heightmap,
|
||||
const std::array<BiomeType, 8>& neighbor_biome) {
|
||||
auto& m_heightmap = m_chunk.heightmap();
|
||||
auto m_biome = m_chunk.biome();
|
||||
// Width of interpolation influence (in number of cells)
|
||||
m_neighbor_biome = neighbor_biome;
|
||||
// --- Right neighbor neighbor[0]: (1, 0) ---
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
if (neighbor_heightmap[0] != std::nullopt &&
|
||||
neighbor_biome[0] != m_biome) {
|
||||
is_cur_chunk_ins = true;
|
||||
int edge_x = CHUNK_SIZE - 1;
|
||||
int h = m_heightmap[edge_x][z];
|
||||
int neighbor_h = (*neighbor_heightmap[0])[0][z];
|
||||
if (h <= neighbor_h) {
|
||||
continue;
|
||||
}
|
||||
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) ---
|
||||
for (int z = 0; z < SIZE_Z; z++) {
|
||||
if (neighbor_heightmap[1] != std::nullopt &&
|
||||
neighbor_biome[1] != m_biome) {
|
||||
is_cur_chunk_ins = true;
|
||||
int edge_x = 0;
|
||||
int h = m_heightmap[edge_x][z];
|
||||
int neighbor_h = (*neighbor_heightmap[1])[CHUNK_SIZE - 1][z];
|
||||
if (h <= neighbor_h) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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) ---
|
||||
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;
|
||||
if (neighbor_heightmap[2] != std::nullopt &&
|
||||
neighbor_biome[2] != m_biome) {
|
||||
is_cur_chunk_ins = true;
|
||||
int edge_z = CHUNK_SIZE - 1;
|
||||
int h = m_heightmap[x][edge_z];
|
||||
int neighbor_h = (*neighbor_heightmap[2])[x][0];
|
||||
if (h <= neighbor_h) {
|
||||
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;
|
||||
|
||||
// --- Right neighbor neighbor[0]: (1, 0) ---
|
||||
// Blend when x is close to SIZE_X-1
|
||||
if (neighbor_heightmap[0] != std::nullopt &&
|
||||
neighbor_biome[0] != m_biome) {
|
||||
int dist = (SIZE_X - 1) - x; // distance from right border
|
||||
if (dist < BLEND_RADIUS) {
|
||||
// Neighbor's boundary row is its x=0 column
|
||||
float neighbor_h =
|
||||
static_cast<float>((*neighbor_heightmap[0])[0][z]);
|
||||
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;
|
||||
}
|
||||
// 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) ---
|
||||
for (int x = 0; x < SIZE_X; x++) {
|
||||
if (neighbor_heightmap[3] != std::nullopt &&
|
||||
neighbor_biome[3] != m_biome) {
|
||||
is_cur_chunk_ins = true;
|
||||
int edge_z = 0;
|
||||
int h = m_heightmap[x][edge_z];
|
||||
int neighbor_h = (*neighbor_heightmap[3])[x][CHUNK_SIZE - 1];
|
||||
if (h <= neighbor_h) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// --- Left neighbor neighbor[1]: (-1, 0) ---
|
||||
if (neighbor_heightmap[1] != std::nullopt &&
|
||||
neighbor_biome[1] != m_biome) {
|
||||
int dist = x; // distance from left border
|
||||
if (dist < BLEND_RADIUS) {
|
||||
float neighbor_h = static_cast<float>(
|
||||
(*neighbor_heightmap[1])[SIZE_X - 1][z]);
|
||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
||||
t = t * t * (3.0f - 2.0f * t);
|
||||
blended += t * neighbor_h;
|
||||
total_weight += t;
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
||||
// --- Front neighbor neighbor[2]: (0, 1) ---
|
||||
if (neighbor_heightmap[2] != std::nullopt &&
|
||||
neighbor_biome[2] != m_biome) {
|
||||
int dist = (SIZE_Z - 1) - z;
|
||||
if (dist < BLEND_RADIUS) {
|
||||
float neighbor_h =
|
||||
static_cast<float>((*neighbor_heightmap[2])[x][0]);
|
||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
||||
t = t * t * (3.0f - 2.0f * t);
|
||||
blended += t * neighbor_h;
|
||||
total_weight += t;
|
||||
}
|
||||
// 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) ---
|
||||
if (neighbor_heightmap[3] != std::nullopt &&
|
||||
neighbor_biome[3] != m_biome) {
|
||||
int dist = z;
|
||||
if (dist < BLEND_RADIUS) {
|
||||
float neighbor_h = static_cast<float>(
|
||||
(*neighbor_heightmap[3])[x][SIZE_Z - 1]);
|
||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
||||
t = t * t * (3.0f - 2.0f * t);
|
||||
blended += t * neighbor_h;
|
||||
total_weight += t;
|
||||
}
|
||||
if (is_cur_chunk_ins) {
|
||||
return;
|
||||
}
|
||||
// --- Right-Front corner neighbor[4]: (1, 1) ---
|
||||
if (neighbor_heightmap[4] != std::nullopt && neighbor_biome[4] != m_biome) {
|
||||
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||
int x = (CHUNK_SIZE - 1) - i;
|
||||
int z = (CHUNK_SIZE - 1) - j;
|
||||
int h = m_heightmap[x][z];
|
||||
|
||||
int h_right = (neighbor_heightmap[0] != std::nullopt)
|
||||
? (*neighbor_heightmap[0])[0][z]
|
||||
: h;
|
||||
|
||||
int h_front = (neighbor_heightmap[2] != std::nullopt)
|
||||
? (*neighbor_heightmap[2])[x][0]
|
||||
: h;
|
||||
|
||||
int h_corner = (*neighbor_heightmap[4])[0][0];
|
||||
|
||||
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||
|
||||
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||
h_front * tx * (1 - tz) +
|
||||
h_right * (1 - tx) * tz + h * tx * tz;
|
||||
|
||||
if (h <= static_cast<int>(std::round(target_h)))
|
||||
continue;
|
||||
|
||||
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||
m_heightmap[x][z] = static_cast<int>(
|
||||
std::round(target_h + (h - target_h) * smooth_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_heightmap[x][z] = static_cast<int>(blended / total_weight);
|
||||
// --- Left-Front corner neighbor[5]: (-1, 1) ---
|
||||
if (neighbor_heightmap[5] != std::nullopt && neighbor_biome[5] != m_biome) {
|
||||
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||
int x = i;
|
||||
int z = (CHUNK_SIZE - 1) - j;
|
||||
int h = m_heightmap[x][z];
|
||||
int h_left = (neighbor_heightmap[1] != std::nullopt)
|
||||
? (*neighbor_heightmap[1])[CHUNK_SIZE - 1][z]
|
||||
: h;
|
||||
int h_front = (neighbor_heightmap[2] != std::nullopt)
|
||||
? (*neighbor_heightmap[2])[x][0]
|
||||
: h;
|
||||
int h_corner = (*neighbor_heightmap[5])[CHUNK_SIZE - 1][0];
|
||||
|
||||
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||
h_front * tx * (1 - tz) +
|
||||
h_left * (1 - tx) * tz + h * tx * tz;
|
||||
|
||||
if (h <= static_cast<int>(std::round(target_h)))
|
||||
continue;
|
||||
|
||||
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||
m_heightmap[x][z] = static_cast<int>(
|
||||
std::round(target_h + (h - target_h) * smooth_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Right-Back corner neighbor[6]: (1, -1) ---
|
||||
if (neighbor_heightmap[6] != std::nullopt && neighbor_biome[6] != m_biome) {
|
||||
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||
int x = (CHUNK_SIZE - 1) - i;
|
||||
int z = j;
|
||||
int h = m_heightmap[x][z];
|
||||
int h_right = (neighbor_heightmap[0] != std::nullopt)
|
||||
? (*neighbor_heightmap[0])[0][z]
|
||||
: h;
|
||||
int h_back = (neighbor_heightmap[3] != std::nullopt)
|
||||
? (*neighbor_heightmap[3])[x][CHUNK_SIZE - 1]
|
||||
: h;
|
||||
int h_corner = (*neighbor_heightmap[6])[0][CHUNK_SIZE - 1];
|
||||
|
||||
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||
h_back * tx * (1 - tz) +
|
||||
h_right * (1 - tx) * tz + h * tx * tz;
|
||||
|
||||
if (h <= static_cast<int>(std::round(target_h)))
|
||||
continue;
|
||||
|
||||
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||
m_heightmap[x][z] = static_cast<int>(
|
||||
std::round(target_h + (h - target_h) * smooth_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Left-Back corner neighbor[7]: (-1, -1) ---
|
||||
if (neighbor_heightmap[7] != std::nullopt && neighbor_biome[7] != m_biome) {
|
||||
for (int i = 0; i < BLEND_RADIUS; i++) {
|
||||
for (int j = 0; j < BLEND_RADIUS; j++) {
|
||||
int x = i;
|
||||
int z = j;
|
||||
int h = m_heightmap[x][z];
|
||||
int h_left = (neighbor_heightmap[1] != std::nullopt)
|
||||
? (*neighbor_heightmap[1])[CHUNK_SIZE - 1][z]
|
||||
: h;
|
||||
int h_back = (neighbor_heightmap[3] != std::nullopt)
|
||||
? (*neighbor_heightmap[3])[x][CHUNK_SIZE - 1]
|
||||
: h;
|
||||
int h_corner =
|
||||
(*neighbor_heightmap[7])[CHUNK_SIZE - 1][CHUNK_SIZE - 1];
|
||||
|
||||
float tx = static_cast<float>(i) / BLEND_RADIUS;
|
||||
float tz = static_cast<float>(j) / BLEND_RADIUS;
|
||||
float target_h = h_corner * (1 - tx) * (1 - tz) +
|
||||
h_back * tx * (1 - tz) +
|
||||
h_left * (1 - tx) * tz + h * tx * tz;
|
||||
|
||||
if (h <= static_cast<int>(std::round(target_h)))
|
||||
continue;
|
||||
|
||||
float t = static_cast<float>(std::max(i, j)) / BLEND_RADIUS;
|
||||
float smooth_t = t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f);
|
||||
m_heightmap[x][z] = static_cast<int>(
|
||||
std::round(target_h + (h - target_h) * smooth_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -197,7 +354,7 @@ void ChunkGenerator::generate_terrain_blocks() {
|
||||
Logger::error("BiomeBuilder is nullptr");
|
||||
return;
|
||||
}
|
||||
m_chunk.blocks().assign(CHUCK_SIZE * CHUCK_SIZE * WORLD_SIZE_Y, 0);
|
||||
m_chunk.blocks().assign(CHUNK_SIZE * CHUNK_SIZE * WORLD_SIZE_Y, 0);
|
||||
m_biome_builder->build_biome();
|
||||
}
|
||||
|
||||
@@ -225,8 +382,8 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
};
|
||||
|
||||
// For each column (x, z)
|
||||
for (int x = 0; x < CHUCK_SIZE; ++x) {
|
||||
for (int z = 0; z < CHUCK_SIZE; ++z) {
|
||||
for (int x = 0; x < CHUNK_SIZE; ++x) {
|
||||
for (int z = 0; z < CHUNK_SIZE; ++z) {
|
||||
// Get the current top block type of this column from m_blocks
|
||||
uint8_t type_self = 0;
|
||||
int top_y = -1;
|
||||
@@ -241,8 +398,8 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
weights[type_self] = 1.0f; // self weight
|
||||
|
||||
// --- Right neighbor (index 0) ---
|
||||
if (neighbor_block[0] && x >= CHUCK_SIZE - BLEND_RADIUS) {
|
||||
int dist = (CHUCK_SIZE - 1) - x;
|
||||
if (neighbor_block[0] && x >= CHUNK_SIZE - BLEND_RADIUS) {
|
||||
int dist = (CHUNK_SIZE - 1) - x;
|
||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
||||
t = t * t * (3.0f - 2.0f * t); // smoothstep
|
||||
if (t > 0.0f) {
|
||||
@@ -259,14 +416,14 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
t = t * t * (3.0f - 2.0f * t);
|
||||
if (t > 0.0f) {
|
||||
uint8_t type_neighbor = get_top_block_from_neighbor(
|
||||
*neighbor_block[1], CHUCK_SIZE - 1, z);
|
||||
*neighbor_block[1], CHUNK_SIZE - 1, z);
|
||||
weights[type_neighbor] += t;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Front neighbor (index 2) ---
|
||||
if (neighbor_block[2] && z >= CHUCK_SIZE - BLEND_RADIUS) {
|
||||
int dist = (CHUCK_SIZE - 1) - z;
|
||||
if (neighbor_block[2] && z >= CHUNK_SIZE - BLEND_RADIUS) {
|
||||
int dist = (CHUNK_SIZE - 1) - z;
|
||||
float t = 1.0f - static_cast<float>(dist) / BLEND_RADIUS;
|
||||
t = t * t * (3.0f - 2.0f * t);
|
||||
if (t > 0.0f) {
|
||||
@@ -283,7 +440,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
t = t * t * (3.0f - 2.0f * t);
|
||||
if (t > 0.0f) {
|
||||
uint8_t type_neighbor = get_top_block_from_neighbor(
|
||||
*neighbor_block[3], x, CHUCK_SIZE - 1);
|
||||
*neighbor_block[3], x, CHUNK_SIZE - 1);
|
||||
weights[type_neighbor] += t;
|
||||
}
|
||||
}
|
||||
@@ -304,13 +461,7 @@ void ChunkGenerator::blend_surface_blocks_borders(
|
||||
if (m_chunk.biome() == BiomeType::RIVER && final_type == 1) {
|
||||
final_type = 2;
|
||||
}
|
||||
if (is_neighbor_river && final_type == 1) {
|
||||
if (top_y < SEA_LEVEL) {
|
||||
final_type = 2;
|
||||
} else {
|
||||
final_type = 1;
|
||||
}
|
||||
}
|
||||
|
||||
m_blocks[Chunk::get_index(x, top_y, z)] = final_type;
|
||||
// bottom block
|
||||
unsigned fill_type = 2;
|
||||
@@ -363,6 +514,7 @@ void ChunkGenerator::make_biome_builder() {
|
||||
Chunk& ChunkGenerator::chunk() { return m_chunk; }
|
||||
|
||||
Random& ChunkGenerator::random() { return m_random; }
|
||||
bool ChunkGenerator::neighbor_river() const { return is_neighbor_river; }
|
||||
|
||||
const std::array<BiomeType, 8>& ChunkGenerator::neighbor_biome() const {
|
||||
return m_neighbor_biome;
|
||||
}
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user