diff options
| author | Mel <einebeere@gmail.com> | 2023-07-21 02:17:03 +0200 |
|---|---|---|
| committer | Mel <einebeere@gmail.com> | 2023-07-21 02:17:03 +0200 |
| commit | 23d88e5f1c8f0c8652a07050fcfa8ff126e85d4a (patch) | |
| tree | d2979c12a9675885b7ed969d5f51dbd69d969286 /src/World/Generation/ChunkMeshing.cpp | |
| parent | c0556f76fc5c8271c2eaa7ca91ad1c92c691d8bc (diff) | |
| download | meowcraft-23d88e5f1c8f0c8652a07050fcfa8ff126e85d4a.tar.zst meowcraft-23d88e5f1c8f0c8652a07050fcfa8ff126e85d4a.zip | |
Extremely simple chunk-limited lighting
Diffstat (limited to 'src/World/Generation/ChunkMeshing.cpp')
| -rw-r--r-- | src/World/Generation/ChunkMeshing.cpp | 54 |
1 files changed, 17 insertions, 37 deletions
diff --git a/src/World/Generation/ChunkMeshing.cpp b/src/World/Generation/ChunkMeshing.cpp index 2e9f191..002dd79 100644 --- a/src/World/Generation/ChunkMeshing.cpp +++ b/src/World/Generation/ChunkMeshing.cpp @@ -124,6 +124,14 @@ Face<Normal> DefaultMeshDecisions::face_normals(BlockSide side) { return {normal, normal, normal, normal}; } +Face<Light> DefaultMeshDecisions::face_light(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) { + auto neighbor = get_opposing_neighbor(chunk, neighbors, x, y, z, side); + if (!neighbor.type.is_translucent()) return {}; + + auto light = (F32)neighbor.light / (F32)255; + return {light, light, light, light}; +} + Face<AO> DefaultMeshDecisions::face_ao_values(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) { std::array<Vector<3, I32>, 8> offsets{}; // Given a block position, these offsets can be added to it to get the 8 blocks necessary to calculate AO. @@ -177,35 +185,6 @@ Face<AO> DefaultMeshDecisions::face_ao_values(Chunk& chunk, const ChunkNeighbors return vertex_ao; } -Chunk::BlockData DefaultMeshDecisions::get_block_wrapping(const Chunk& chunk, const ChunkNeighbors& neighbors, Vector<3, I32> pos) { - const Chunk* chunk_to_ask; - - auto overflow = [](I32& c, I32 max) -> I8 { - if (c < 0) { c += max; return -1; } - if (c >= max) { c -= max; return 1; } - return 0; - }; - - auto xo = overflow(pos.x(), Chunk::Width); - auto yo = overflow(pos.y(), Chunk::Height); - auto zo = overflow(pos.z(), Chunk::Width); - - // Blocks above and below a chunk are always Air. - if (yo != 0) return {}; - - if (xo == 1 && zo == 1) { chunk_to_ask = neighbors.south_east; } - else if (xo == 1 && zo == -1) { chunk_to_ask = neighbors.north_east; } - else if (xo == -1 && zo == 1) { chunk_to_ask = neighbors.south_west; } - else if (xo == -1 && zo == -1) { chunk_to_ask = neighbors.north_west; } - else if (xo == 1) { chunk_to_ask = neighbors.east; } - else if (xo == -1) { chunk_to_ask = neighbors.west; } - else if (zo == 1) { chunk_to_ask = neighbors.south; } - else if (zo == -1) { chunk_to_ask = neighbors.north; } - else { chunk_to_ask = &chunk; } - - return chunk_to_ask->get(pos.x(), pos.y(), pos.z()); -} - Vector<3, I32> DefaultMeshDecisions::get_face_normal(BlockSide side) { auto is_side = [=](BlockSide s) -> I8 { return s == side; }; @@ -216,12 +195,16 @@ Vector<3, I32> DefaultMeshDecisions::get_face_normal(BlockSide side) { }; } -Bool DefaultMeshDecisions::is_face_visible(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) { +Chunk::BlockData DefaultMeshDecisions::get_opposing_neighbor(const Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) { Vector<3, I32> offset = get_face_normal(side); auto neighbor_position = offset + Vector<3, I32>{x, y, z}; - auto block = get_block_wrapping(chunk, neighbors, neighbor_position); - return block.type.is_transparent(); + return get_block_wrapping(chunk, neighbors, neighbor_position); +} + +Bool DefaultMeshDecisions::is_face_visible(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) { + auto block = get_opposing_neighbor(chunk, neighbors, x, y, z, side); + return block.type.is_translucent(); } Bool DefaultMeshDecisions::should_ignore_block(Chunk::BlockData block) { @@ -229,11 +212,8 @@ Bool DefaultMeshDecisions::should_ignore_block(Chunk::BlockData block) { } Bool WaterMeshDecisions::is_face_visible(Chunk& chunk, const ChunkNeighbors& neighbors, U32 x, U32 y, U32 z, BlockSide side) { - Vector<3, I32> offset = get_face_normal(side); - auto neighbor_position = offset + Vector<3, I32>{x, y, z}; - - auto [neighbor] = get_block_wrapping(chunk, neighbors, neighbor_position); - return neighbor.is_transparent() && neighbor != BlockType::Water; + auto block = get_opposing_neighbor(chunk, neighbors, x, y, z, side); + return block.type.is_translucent() && block.type != BlockType::Water; } Bool WaterMeshDecisions::should_ignore_block(Chunk::BlockData block) { |
