fix: resolve transparency rendering issues

This commit is contained in:
2026-04-18 11:55:53 +08:00
parent bb888fd7b7
commit 955c4ddec7
4 changed files with 58 additions and 128 deletions

View File

@@ -66,62 +66,6 @@ int Chunk::get_index(const glm::vec3& pos) {
return Chunk::get_index(pos.x, pos.y, pos.z);
}
// this is thread-unsafe!
void Chunk::gen_vertex_data() {
m_vertexs_data.clear();
static const glm::ivec3 DIR[6] = {
{0,0,1},{1,0,0},{0,0,-1},{-1,0,0},{0,1,0},{0,-1,0}
};
for (int x = 0; x < SIZE_X; x++) {
for (int y = 0; y < SIZE_Y; y++) {
for (int z = 0; z < SIZE_Z; z++) {
int world_x = x + m_chunk_pos.x * CHUCK_SIZE;
int world_z = z + m_chunk_pos.z * CHUCK_SIZE;
int world_y = y;
int id = m_blocks[get_index(x, y, z)];
// air
if (id == 0) {
continue;
}
for (int face = 0; face < 6; face++) {
int nx = x + DIR[face].x;
int ny = y + DIR[face].y;
int nz = z + DIR[face].z;
bool neighbor_soild = false;
if (nx < 0 || nx >= SIZE_X || ny < 0 || ny >= SIZE_Y || nz < 0 || nz>= SIZE_Z) {
neighbor_soild = m_world.is_block(glm::ivec3(world_x, world_y, world_z) + DIR[face]);
} else {
if (m_blocks[get_index(nx, ny, nz)] != 0) {
neighbor_soild = true;
}
}
if (neighbor_soild) {
continue;
}
for (int i = 0; i < 6; i++) {
Vertex vex = {
VERTICES_POS[face][i][0] + (float)world_x * 1.0f,
VERTICES_POS[face][i][1] + (float)world_y * 1.0f,
VERTICES_POS[face][i][2] + (float)world_z * 1.0f,
TEX_COORDS[face][i][0],
TEX_COORDS[face][i][1],
static_cast<float>(id * 6 + face)
};
m_vertexs_data.emplace_back(vex);
}
}
}
}
}
}
void Chunk::gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neighbor_block) {
m_vertexs_data.clear();
@@ -135,16 +79,17 @@ void Chunk::gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neig
int world_x = x + m_chunk_pos.x * CHUCK_SIZE;
int world_z = z + m_chunk_pos.z * CHUCK_SIZE;
int world_y = y;
int id = m_blocks[get_index(x, y, z)];
int cur_id = m_blocks[get_index(x, y, z)];
// air
if (id == 0) {
if (cur_id == 0) {
continue;
}
for (int face = 0; face < 6; face++) {
int nx = x + DIR[face].x;
int ny = y + DIR[face].y;
int nz = z + DIR[face].z;
bool neighbor_soild = false;
bool neighbor_cull = false;
if (nx < 0 || nx >= SIZE_X || ny < 0 || ny >= SIZE_Y || nz < 0 || nz>= SIZE_Z) {
@@ -154,7 +99,7 @@ void Chunk::gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neig
auto [neighbor_x, neighbor_z] = World::chunk_pos(world_nx, world_nz);
auto is_block = [&](const std::vector<uint8_t>* chunk_blocks){
auto is_cull = [&](const std::vector<uint8_t>* chunk_blocks){
if (chunk_blocks == nullptr) {
return false;
}
@@ -173,30 +118,42 @@ void Chunk::gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neig
return false;
}
auto id = (*chunk_blocks)[idx];
if (id == 0) {
return false;
if (is_in_transparent_map(id)) {
if (id == cur_id) {
return true;
} else {
return false;
}
} else {
return true;
}
};
if (m_chunk_pos.x + 1 == neighbor_x) {
neighbor_soild = is_block(neighbor_block[0]);
neighbor_cull = is_cull(neighbor_block[0]);
} else if (m_chunk_pos.x - 1 == neighbor_x) {
neighbor_soild = is_block(neighbor_block[1]);
neighbor_cull = is_cull(neighbor_block[1]);
} else if (m_chunk_pos.z + 1 == neighbor_z) {
neighbor_soild = is_block(neighbor_block[2]);
neighbor_cull = is_cull(neighbor_block[2]);
} else if (m_chunk_pos.z - 1 == neighbor_z) {
neighbor_soild = is_block(neighbor_block[3]);
neighbor_cull = is_cull(neighbor_block[3]);
}
//neighbor_soild = m_world.is_block(glm::ivec3(world_x, world_y, world_z) + DIR[face]);
//neighbor_cull = m_world.is_block(glm::ivec3(world_x, world_y, world_z) + DIR[face]);
} else {
if (m_blocks[get_index(nx, ny, nz)] != 0) {
neighbor_soild = true;
auto id = m_blocks[get_index(nx, ny, nz)];
if (!is_in_transparent_map(id)) {
neighbor_cull = true;
} else {
if (id == cur_id) {
neighbor_cull = true;
} else {
neighbor_cull = false;
}
}
}
if (neighbor_soild) {
if (neighbor_cull) {
continue;
}
for (int i = 0; i < 6; i++) {
@@ -206,7 +163,7 @@ void Chunk::gen_vertex_data(const std::vector<const std::vector<uint8_t>*>& neig
VERTICES_POS[face][i][2] + (float)world_z * 1.0f,
TEX_COORDS[face][i][0],
TEX_COORDS[face][i][1],
static_cast<float>(id * 6 + face)
static_cast<float>(cur_id * 6 + face)
};
m_vertexs_data.emplace_back(vex);

View File

@@ -9,6 +9,10 @@
#include <unordered_set>
static constexpr ChunkPos CHUNK_DIR[] {
{1, 0}, {-1, 0}, {0, 1}, {0, -1}
};
World::World() {
}
@@ -29,44 +33,7 @@ bool World::can_move(const AABB& player_box) const{
return true;
}
/*
const BlockRenderData& World::get_block_render_data(int world_x, int world_y ,int world_z) {
auto [chunk_x, chunk_z] = chunk_pos(world_x, world_z);
//Logger::info("Chunk PosX : {} Chuch PosZ : {}", chunk_x, chunk_z);
auto it = m_chunks.find(ChunkPos{chunk_x, chunk_z});
CUBED_ASSERT_MSG(it != m_chunks.end(), "Chunk not find");
const auto& chunk_blocks = it->second.get_chunk_blocks();
int x, y, z;
y = world_y;
x = world_x - chunk_x * CHUCK_SIZE;
z = world_z - chunk_z * CHUCK_SIZE;
//BlockRenderData m_block_render_data;
// block id
m_block_render_data.block_id = chunk_blocks[Chunk::get_index(x, y, z)];
if (m_block_render_data.block_id == 0) {
return m_block_render_data;
}
// draw_face
m_block_render_data.draw_face.assign(6, true);
static const std::vector<glm::ivec3> DIR = {
glm::ivec3(0, 0, 1),
glm::ivec3(1, 0, 0),
glm::ivec3(0 ,0, -1),
glm::ivec3(-1, 0, 0),
glm::ivec3(0, 1, 0),
glm::ivec3(0, -1, 0)
};
glm::ivec3 world_pos = glm::ivec3(world_x, world_y, world_z);
for (int i = 0; i < 6; i++) {
if (is_block(world_pos + DIR[i])) {
m_block_render_data.draw_face[i] = false;
}
}
return m_block_render_data;
}
*/
const std::optional<LookBlock>& World::get_look_block_pos(const std::string& name) const{
static std::optional<LookBlock> null_look_block = std::nullopt;
auto it = m_players.find(HASH::str(name));
@@ -119,10 +86,20 @@ void World::init_world() {
}
// After block gen fininshed
std::vector<const std::vector<uint8_t>*> neighbor_block(4);
for (auto& [pos, chunk] : m_chunks) {
for (int i = 0; i < 4; i++) {
auto it = m_chunks.find(pos + CHUNK_DIR[i]);
if (it != m_chunks.end()) {
neighbor_block[i] = &(it->second.get_chunk_blocks());
} else {
neighbor_block[i] = nullptr;
}
}
chunk.gen_vertex_data(neighbor_block);
}
for (auto& chunk_map : m_chunks) {
auto& [chunk_pos, chunk] = chunk_map;
chunk.gen_vertex_data();
chunk.upload_to_gpu();
}
@@ -230,10 +207,6 @@ void World::gen_chunks_internal() {
new_chunks.push_back({pos, Chunk(*this, pos)});
}
static const ChunkPos CHUNK_DIR[] {
{1, 0}, {-1, 0}, {0, 1}, {0, -1}
};
std::unordered_map<ChunkPos, const Chunk&, ChunkPos::Hash> neighbor;
{
@@ -454,7 +427,16 @@ void World::update(float delta_time) {
for (auto& [pos, chunk] : m_chunks) {
if (chunk.is_dirty()) {
// the curial fator influence
chunk.gen_vertex_data();
std::vector<const std::vector<uint8_t>*> neighbor_block(4);
for (int i = 0; i < 4; i++) {
auto it = m_chunks.find(pos + CHUNK_DIR[i]);
if (it != m_chunks.end()) {
neighbor_block[i] = &(it->second.get_chunk_blocks());
} else {
neighbor_block[i] = nullptr;
}
}
chunk.gen_vertex_data(neighbor_block);
chunk.upload_to_gpu();
}
if (!chunk.is_dirty()) {