refactor: chunk interpolate (#6)

* refactor: rewrite blend_heightmap_boundaries

* refactor: init_world

* fix: unnatural biome boundary transition
This commit is contained in:
zhenyan121
2026-05-03 16:02:01 +08:00
committed by GitHub
parent a02bfad639
commit 9d200f31be
19 changed files with 553 additions and 319 deletions

View File

@@ -11,8 +11,6 @@
namespace Cubed {
static constexpr ChunkPos CHUNK_DIR[]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct ChunkRenderData {
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
Chunk* chunk;
@@ -67,18 +65,8 @@ Player& World::get_player(const std::string& name) {
}
void World::init_world() {
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE);
m_chunks.reserve(MAX_DISTANCE * MAX_DISTANCE * 4);
auto t1 = std::chrono::system_clock::now();
for (int s = 0; s < PRE_LOAD_DISTANCE; s++) {
for (int t = 0; t < PRE_LOAD_DISTANCE; t++) {
int ns = s - PRE_LOAD_DISTANCE / 2;
int nt = t - PRE_LOAD_DISTANCE / 2;
ChunkPos pos{ns, nt};
m_chunks.emplace(pos, Chunk(*this, pos));
}
}
Logger::info("Max Support Thread is {}",
std::thread::hardware_concurrency());
@@ -93,95 +81,162 @@ void World::init_world() {
start_gen_thread();
hot_reload();
}
/*
void World::init_chunks() {
std::vector<Chunk*> chunk_ptrs;
chunk_ptrs.reserve(m_chunks.size());
for (auto& [pos, chunk] : m_chunks) {
chunk_ptrs.push_back(&chunk);
}
std::for_each(std::execution::par, chunk_ptrs.begin(), chunk_ptrs.end(),
[](auto& chunk){ chunk->init_chunk();
});
std::atomic<int> sync{0};
sync.store(1, std::memory_order_release);
sync.load(std::memory_order_acquire);
std::vector<ChunkRenderData> pending_gen_data;
pending_gen_data.reserve(m_chunks.size());
for (auto& [pos, chunk] : m_chunks) {
ChunkRenderData data;
data.chunk = &chunk;
for (int i = 0; i < 4; i++) {
auto it = m_chunks.find(pos + CHUNK_DIR[i]);
if (it != m_chunks.end()) {
data.neighbor_block[i] = &(it->second.get_chunk_blocks());
} else {
data.neighbor_block[i] = nullptr;
int dis_x = PRE_LOAD_DISTANCE;
int dis_z = PRE_LOAD_DISTANCE;
for (int x = 0; x < dis_x; x++) {
for (int z = 0; z < dis_z; z++) {
int nx = x - dis_x / 2;
int nz = z - dis_z / 2;
ChunkPos pos{nx, nz};
auto it = m_chunks.find(pos);
if (it == m_chunks.end()) {
m_chunks.emplace(pos, Chunk(*this, pos));
}
}
pending_gen_data.emplace_back(std::move(data));
}
std::for_each(std::execution::par, pending_gen_data.begin(),
pending_gen_data.end(), [](ChunkRenderData& data){ if(!data.chunk) { return ;
ChunkHashMap temp_neighbor;
for (int x = 0; x < dis_x + 2; x++) {
for (int z = 0; z < dis_z + 2; z++) {
int nx = x - (dis_x + 2) / 2;
int nz = z - (dis_z + 2) / 2;
ChunkPos pos{nx, nz};
auto it = m_chunks.find(pos);
if (it == m_chunks.end()) {
auto it = temp_neighbor.find(pos);
if (it == temp_neighbor.end()) {
temp_neighbor.emplace(pos, Chunk(*this, pos));
}
}
}
data.chunk->gen_vertex_data(data.neighbor_block);
});
for (auto& chunk_map : m_chunks) {
auto& [chunk_pos, chunk] = chunk_map;
chunk.upload_to_gpu();
}
}
*/
void World::init_chunks() {
for (auto& [pos, chunks] : m_chunks) {
chunks.gen_phase_one();
}
std::array<const Chunk*, 4> neighbor_chunks;
for (auto& [pos, chunks] : temp_neighbor) {
chunks.gen_phase_one();
}
std::array<const Chunk*, 8> neighbor_chunks;
for (auto& [pos, chunks] : m_chunks) {
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 8; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = m_chunks.find(neighbor_pos);
if (it == m_chunks.end()) {
neighbor_chunks[i] = nullptr;
auto it = temp_neighbor.find(neighbor_pos);
if (it == temp_neighbor.end()) {
neighbor_chunks[i] = nullptr;
ASSERT_MSG(false, "Neighbor Chunk is nullptr");
} else {
neighbor_chunks[i] = &it->second;
}
continue;
}
neighbor_chunks[i] = &it->second;
}
chunks.gen_phase_two(neighbor_chunks);
}
for (auto& [pos, chunks] : m_chunks) {
chunks.gen_phase_three();
}
std::array<std::optional<HeightMapArray>, 4> neighbor_chunk_heightmap;
for (auto& [pos, chunks] : m_chunks) {
for (int i = 0; i < 4; i++) {
for (auto& [pos, chunks] : temp_neighbor) {
for (int i = 0; i < 8; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = m_chunks.find(neighbor_pos);
if (it == m_chunks.end()) {
neighbor_chunk_heightmap[i] = std::nullopt;
auto it = temp_neighbor.find(neighbor_pos);
if (it == temp_neighbor.end()) {
neighbor_chunks[i] = nullptr;
} else {
neighbor_chunks[i] = &it->second;
}
continue;
}
neighbor_chunk_heightmap[i] = it->second.get_heightmap();
neighbor_chunks[i] = &it->second;
}
chunks.gen_phase_two(neighbor_chunks);
}
for (auto& [pos, chunks] : m_chunks) {
chunks.gen_phase_three();
}
for (auto& [pos, chunks] : temp_neighbor) {
chunks.gen_phase_three();
}
for (int i = 0; i < 4; i++) {
for (auto& [pos, chunks] : temp_neighbor) {
std::array<std::optional<HeightMapArray>, 8>
neighbor_chunk_heightmap;
std::array<BiomeType, 8> neighbor_biome;
for (int i = 0; i < 4; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = m_chunks.find(neighbor_pos);
if (it == m_chunks.end()) {
auto it = temp_neighbor.find(neighbor_pos);
if (it == temp_neighbor.end()) {
neighbor_chunk_heightmap[i] = std::nullopt;
neighbor_biome[i] = BiomeType::NONE;
} else {
neighbor_chunk_heightmap[i] =
it->second.get_heightmap();
neighbor_biome[i] = it->second.biome();
}
continue;
}
neighbor_chunk_heightmap[i] = it->second.get_heightmap();
neighbor_biome[i] = it->second.biome();
}
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
}
for (auto& [pos, chunks] : m_chunks) {
std::array<std::optional<HeightMapArray>, 8>
neighbor_chunk_heightmap;
std::array<BiomeType, 8> neighbor_biome;
for (int i = 0; i < 8; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = m_chunks.find(neighbor_pos);
if (it == m_chunks.end()) {
auto it = temp_neighbor.find(neighbor_pos);
if (it == temp_neighbor.end()) {
neighbor_chunk_heightmap[i] = std::nullopt;
neighbor_biome[i] = BiomeType::NONE;
ASSERT_MSG(false, "Neighbor Chunk is nullptr");
} else {
neighbor_chunk_heightmap[i] =
it->second.get_heightmap();
neighbor_biome[i] = it->second.biome();
}
continue;
}
neighbor_chunk_heightmap[i] = it->second.get_heightmap();
neighbor_biome[i] = it->second.biome();
}
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
}
chunks.gen_phase_four(neighbor_chunk_heightmap);
}
for (auto& [pos, chunks] : m_chunks) {
chunks.gen_phase_five();
}
for (auto& [pos, chunks] : temp_neighbor) {
chunks.gen_phase_five();
}
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_block;
for (auto& [pos, chunks] : m_chunks) {
for (int i = 0; i < 4; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = m_chunks.find(neighbor_pos);
if (it == m_chunks.end()) {
neighbor_block[i] = std::nullopt;
auto it = temp_neighbor.find(neighbor_pos);
if (it == temp_neighbor.end()) {
neighbor_block[i] = std::nullopt;
ASSERT_MSG(false, "Neighbor Chunk is nullptr");
} else {
neighbor_block[i] = it->second.get_chunk_blocks();
}
continue;
}
neighbor_block[i] = it->second.get_chunk_blocks();
@@ -191,6 +246,7 @@ void World::init_chunks() {
for (auto& [pos, chunks] : m_chunks) {
chunks.gen_phase_seven();
}
std::atomic<int> sync{0};
sync.store(1, std::memory_order_release);
sync.load(std::memory_order_acquire);
@@ -253,16 +309,16 @@ void World::render(const glm::mat4& mvp_matrix) {
ChunkPos World::chunk_pos(int world_x, int world_z) {
int chunk_x, chunk_z;
if (world_x < 0) {
chunk_x = (world_x + 1) / CHUCK_SIZE - 1;
chunk_x = (world_x + 1) / CHUNK_SIZE - 1;
}
if (world_x >= 0) {
chunk_x = world_x / CHUCK_SIZE;
chunk_x = world_x / CHUNK_SIZE;
}
if (world_z < 0) {
chunk_z = (world_z + 1) / CHUCK_SIZE - 1;
chunk_z = (world_z + 1) / CHUNK_SIZE - 1;
}
if (world_z >= 0) {
chunk_z = world_z / CHUCK_SIZE;
chunk_z = world_z / CHUNK_SIZE;
}
return {chunk_x, chunk_z};
}
@@ -292,19 +348,35 @@ void World::gen_chunks_internal() {
ConstChunkMap new_chunks_neighbor;
// affected neighbor
ChunkPtrUpdateList affected_neighbor;
build_neighbor_context_for_new_chunks(new_chunks_neighbor,
affected_neighbor, new_chunks);
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
ChunkHashMap temp_neighbor;
build_neighbor_context_for_new_chunks(
new_chunks_neighbor, affected_neighbor, new_chunks, temp_neighbor);
Logger::info("Temp neighbor sum {}", temp_neighbor.size());
// build new chunk, but the neighbor in m_chunks also need to re-build
for (auto& [pos, chunk] : new_chunks) {
chunk.gen_phase_one();
}
for (auto& [pos, chunk] : temp_neighbor) {
chunk.gen_phase_one();
}
m_chunk_gen_fraction = 0.2f;
std::array<const Chunk*, 4> neighbor_chunks;
std::array<const Chunk*, 8> neighbor_chunks;
for (auto& [pos, chunks] : new_chunks) {
for (int i = 0; i < 4; i++) {
for (int i = 0; i < 8; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = new_chunks_neighbor.find(neighbor_pos);
if (it == new_chunks_neighbor.end()) {
neighbor_chunks[i] = nullptr;
ASSERT_MSG(false, "Cant Find Neighbot");
continue;
}
neighbor_chunks[i] = it->second;
}
chunks.gen_phase_two(neighbor_chunks);
}
for (auto& [pos, chunks] : temp_neighbor) {
for (int i = 0; i < 8; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = new_chunks_neighbor.find(neighbor_pos);
if (it == new_chunks_neighbor.end()) {
@@ -319,27 +391,59 @@ void World::gen_chunks_internal() {
for (auto& [pos, chunks] : new_chunks) {
chunks.gen_phase_three();
}
for (auto& [pos, chunks] : temp_neighbor) {
chunks.gen_phase_three();
}
m_chunk_gen_fraction = 0.4f;
std::array<std::optional<HeightMapArray>, 4> neighbor_chunk_heightmap;
for (auto& [pos, chunks] : new_chunks) {
{
for (int i = 0; i < 4; i++) {
for (auto& [pos, chunks] : temp_neighbor) {
std::array<std::optional<HeightMapArray>, 8>
neighbor_chunk_heightmap;
// std::lock_guard lk(m_chunks_mutex);
for (int i = 0; i < 4; i++) {
std::array<BiomeType, 8> neighbor_biome;
for (int i = 0; i < 8; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = new_chunks_neighbor.find(neighbor_pos);
if (it == new_chunks_neighbor.end()) {
neighbor_chunk_heightmap[i] = std::nullopt;
neighbor_biome[i] = BiomeType::NONE;
continue;
}
neighbor_chunk_heightmap[i] = it->second->get_heightmap();
neighbor_biome[i] = it->second->biome();
}
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
}
for (auto& [pos, chunks] : new_chunks) {
std::array<std::optional<HeightMapArray>, 8>
neighbor_chunk_heightmap;
// std::lock_guard lk(m_chunks_mutex);
std::array<BiomeType, 8> neighbor_biome;
for (int i = 0; i < 8; i++) {
auto neighbor_pos = pos + CHUNK_DIR[i];
auto it = new_chunks_neighbor.find(neighbor_pos);
if (it == new_chunks_neighbor.end()) {
neighbor_chunk_heightmap[i] = std::nullopt;
neighbor_biome[i] = BiomeType::NONE;
ASSERT_MSG(false, "Cant Find Neighbot");
continue;
}
neighbor_chunk_heightmap[i] = it->second->get_heightmap();
neighbor_biome[i] = it->second->biome();
}
chunks.gen_phase_four(neighbor_chunk_heightmap, neighbor_biome);
}
chunks.gen_phase_four(neighbor_chunk_heightmap);
}
m_chunk_gen_fraction = 0.5f;
for (auto& [pos, chunks] : new_chunks) {
chunks.gen_phase_five();
}
for (auto& [pos, chunks] : temp_neighbor) {
chunks.gen_phase_five();
}
std::array<std::optional<std::vector<uint8_t>>, 4> neighbor_blocks_data;
for (auto& [pos, chunks] : new_chunks) {
{
@@ -359,7 +463,9 @@ void World::gen_chunks_internal() {
for (auto& [pos, chunks] : new_chunks) {
chunks.gen_phase_seven();
}
m_chunk_gen_fraction = 0.6f;
std::array<const std::vector<uint8_t>*, 4> neighbor_block;
for (auto& [pos, chunk] : new_chunks) {
for (int i = 0; i < 4; i++) {
auto it = new_chunks_neighbor.find(pos + CHUNK_DIR[i]);
@@ -441,7 +547,7 @@ void World::sync_and_collect_missing_chunks(
void World::build_neighbor_context_for_new_chunks(
ConstChunkMap& new_chunks_neighbor, ChunkPtrUpdateList& affected_neighbor,
const ChunkUpdateList& new_chunks) {
const ChunkUpdateList& new_chunks, ChunkHashMap& temp_neighbor) {
{
std::lock_guard lk(m_chunks_mutex);
for (auto& [pos, chunk] : new_chunks) {
@@ -450,6 +556,8 @@ void World::build_neighbor_context_for_new_chunks(
if (it != m_chunks.end()) {
new_chunks_neighbor.insert({it->first, &(it->second)});
affected_neighbor.push_back({it->first, &(it->second)});
} else {
temp_neighbor.emplace(pos + dir, Chunk(*this, pos + dir));
}
}
}
@@ -457,6 +565,9 @@ void World::build_neighbor_context_for_new_chunks(
for (auto& [pos, chunk] : new_chunks) {
new_chunks_neighbor.insert({pos, &chunk});
}
for (auto& [pos, chunk] : temp_neighbor) {
new_chunks_neighbor.insert({pos, &chunk});
}
}
void World::build_neighbor_context_for_affected_neighbors(
@@ -544,10 +655,10 @@ int World::get_block(const glm::ivec3& block_pos) const {
const auto& chunk_blocks = it->second.get_chunk_blocks();
int x, y, z;
y = block_pos.y;
x = block_pos.x - chunk_x * CHUCK_SIZE;
z = block_pos.z - chunk_z * CHUCK_SIZE;
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
z >= CHUCK_SIZE) {
x = block_pos.x - chunk_x * CHUNK_SIZE;
z = block_pos.z - chunk_z * CHUNK_SIZE;
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
z >= CHUNK_SIZE) {
return 0;
}
return chunk_blocks[Chunk::get_index(x, y, z)];
@@ -564,10 +675,10 @@ bool World::is_block(const glm::ivec3& block_pos) const {
const auto& chunk_blocks = it->second.get_chunk_blocks();
int x, y, z;
y = block_pos.y;
x = block_pos.x - chunk_x * CHUCK_SIZE;
z = block_pos.z - chunk_z * CHUCK_SIZE;
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
z >= CHUCK_SIZE) {
x = block_pos.x - chunk_x * CHUNK_SIZE;
z = block_pos.z - chunk_z * CHUNK_SIZE;
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
z >= CHUNK_SIZE) {
return false;
}
auto id = chunk_blocks[Chunk::get_index(x, y, z)];
@@ -595,10 +706,10 @@ void World::set_block(const glm::ivec3& block_pos, unsigned id) {
int x, y, z;
y = world_y;
x = world_x - chunk_x * CHUCK_SIZE;
z = world_z - chunk_z * CHUCK_SIZE;
if (x < 0 || y < 0 || z < 0 || x >= CHUCK_SIZE || y >= WORLD_SIZE_Y ||
z >= CHUCK_SIZE) {
x = world_x - chunk_x * CHUNK_SIZE;
z = world_z - chunk_z * CHUNK_SIZE;
if (x < 0 || y < 0 || z < 0 || x >= CHUNK_SIZE || y >= WORLD_SIZE_Y ||
z >= CHUNK_SIZE) {
return;
}
@@ -677,14 +788,14 @@ void World::update(float delta_time) {
}
m_render_snapshots.push_back(
{chunk.get_vbo(), chunk.get_vertex_sum(),
glm::vec3(static_cast<float>(pos.x * CHUCK_SIZE) +
static_cast<float>(CHUCK_SIZE / 2),
glm::vec3(static_cast<float>(pos.x * CHUNK_SIZE) +
static_cast<float>(CHUNK_SIZE / 2),
static_cast<float>(WORLD_SIZE_Y / 2),
static_cast<float>(pos.z * CHUCK_SIZE) +
static_cast<float>(CHUCK_SIZE / 2)),
glm::vec3(static_cast<float>(CHUCK_SIZE / 2),
static_cast<float>(pos.z * CHUNK_SIZE) +
static_cast<float>(CHUNK_SIZE / 2)),
glm::vec3(static_cast<float>(CHUNK_SIZE / 2),
static_cast<float>(WORLD_SIZE_Y / 2),
static_cast<float>(CHUCK_SIZE / 2))});
static_cast<float>(CHUNK_SIZE / 2))});
}
}
}