refactor transparency descriptor

This commit is contained in:
griffi-gh 2024-05-02 01:21:17 +02:00
parent 8ebc94fd9e
commit 4a24f4ac56
2 changed files with 64 additions and 35 deletions

View file

@ -63,81 +63,108 @@ impl Block {
}, },
Self::Stone => BlockDescriptor { Self::Stone => BlockDescriptor {
name: "stone", name: "stone",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Stone)), render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Stone)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Dirt => BlockDescriptor { Self::Dirt => BlockDescriptor {
name: "dirt", name: "dirt",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Dirt)), render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Dirt)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Grass => BlockDescriptor { Self::Grass => BlockDescriptor {
name: "grass", name: "grass",
render: RenderType::SolidBlock(CubeTexture::top_sides_bottom( render: RenderType::Cube(
BlockTexture::GrassTop, Transparency::Solid,
BlockTexture::GrassSide, CubeTexture::top_sides_bottom(
BlockTexture::Dirt BlockTexture::GrassTop,
)), BlockTexture::GrassSide,
BlockTexture::Dirt
)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Sand => BlockDescriptor { Self::Sand => BlockDescriptor {
name: "sand", name: "sand",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Sand)), render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Sand)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Cobblestone => BlockDescriptor { Self::Cobblestone => BlockDescriptor {
name: "cobblestone", name: "cobblestone",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Cobblestone)), render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Cobblestone)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::TallGrass => BlockDescriptor { Self::TallGrass => BlockDescriptor {
name: "tall grass", name: "tall grass",
render: RenderType::CrossShape(CrossTexture::all(BlockTexture::TallGrass)), render: RenderType::Cross(CrossTexture::all(BlockTexture::TallGrass)),
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Planks => BlockDescriptor { Self::Planks => BlockDescriptor {
name: "planks", name: "planks",
render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Planks)), render: RenderType::Cube(
Transparency::Solid,
CubeTexture::all(BlockTexture::Planks)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Torch => BlockDescriptor { Self::Torch => BlockDescriptor {
name: "torch", name: "torch",
render: RenderType::CrossShape(CrossTexture::all(BlockTexture::Torch)), render: RenderType::Cross(CrossTexture::all(BlockTexture::Torch)),
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Wood => BlockDescriptor { Self::Wood => BlockDescriptor {
name: "leaf", name: "leaf",
render: RenderType::SolidBlock(CubeTexture::horizontal_vertical(BlockTexture::Wood, BlockTexture::WoodTop)), render: RenderType::Cube(
Transparency::Solid,
CubeTexture::horizontal_vertical(BlockTexture::Wood, BlockTexture::WoodTop)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Leaf => BlockDescriptor { Self::Leaf => BlockDescriptor {
name: "leaf", name: "leaf",
render: RenderType::BinaryTransparency(CubeTexture::all(BlockTexture::Leaf)), render: RenderType::Cube(
Transparency::Binary,
CubeTexture::all(BlockTexture::Leaf)
),
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
}, },
Self::Water => BlockDescriptor { Self::Water => BlockDescriptor {
name: "water", name: "water",
render: RenderType::TransBlock(CubeTexture::all(BlockTexture::Water)), render: RenderType::Cube(
Transparency::Trans,
CubeTexture::all(BlockTexture::Water)
),
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
@ -214,11 +241,16 @@ pub enum CollisionType {
Solid, Solid,
} }
#[derive(Clone, Copy, Debug)]
pub enum Transparency {
Solid,
Binary,
Trans,
}
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub enum RenderType { pub enum RenderType {
None, None,
SolidBlock(CubeTexture), Cube(Transparency, CubeTexture),
TransBlock(CubeTexture), Cross(CrossTexture),
BinaryTransparency(CubeTexture),
CrossShape(CrossTexture),
} }

View file

@ -1,6 +1,6 @@
use glam::{IVec3, ivec3}; use glam::{IVec3, ivec3};
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use kubi_shared::block::{Block, BlockTexture, RenderType}; use kubi_shared::block::{Block, BlockTexture, RenderType, Transparency};
use crate::world::chunk::CHUNK_SIZE; use crate::world::chunk::CHUNK_SIZE;
use crate::rendering::world::ChunkVertex; use crate::rendering::world::ChunkVertex;
@ -43,26 +43,23 @@ pub fn generate_mesh(data: MeshGenData) -> (
let descriptor = block.descriptor(); let descriptor = block.descriptor();
match descriptor.render { match descriptor.render {
RenderType::None => continue, RenderType::None => continue,
RenderType::SolidBlock(textures) | RenderType::Cube(trans_type, textures) => {
RenderType::BinaryTransparency(textures) |
RenderType::TransBlock(textures) => {
for face in CubeFace::iter() { for face in CubeFace::iter() {
let facing_direction = face.normal(); let facing_direction = face.normal();
let facing_coord = coord + facing_direction; let facing_coord = coord + facing_direction;
let facing_block = get_block(facing_coord); let facing_block = get_block(facing_coord);
let facing_descriptor = facing_block.descriptor(); let facing_descriptor = facing_block.descriptor();
let face_obstructed = match descriptor.render { let face_obstructed = match trans_type {
RenderType::SolidBlock(_) => matches!(facing_descriptor.render, RenderType::SolidBlock(_)), Transparency::Solid => matches!(facing_descriptor.render, RenderType::Cube(Transparency::Solid, _)),
RenderType::BinaryTransparency(_) | Transparency::Binary | Transparency::Trans => {
RenderType::TransBlock(_) => {
match facing_descriptor.render { match facing_descriptor.render {
RenderType::SolidBlock(_) => true, RenderType::Cube(trans_type, _) => match trans_type {
RenderType::BinaryTransparency(_) | Transparency::Solid => true,
RenderType::TransBlock(_) => block == facing_block, Transparency::Binary | Transparency::Trans => block == facing_block,
},
_ => false, _ => false,
} }
}, },
_ => unreachable!(),
}; };
if !face_obstructed { if !face_obstructed {
let face_texture = match face { let face_texture = match face {
@ -73,15 +70,15 @@ pub fn generate_mesh(data: MeshGenData) -> (
CubeFace::Back => textures.back, CubeFace::Back => textures.back,
CubeFace::Bottom => textures.bottom, CubeFace::Bottom => textures.bottom,
}; };
let cur_builder = match descriptor.render { let target_builder = match trans_type {
RenderType::TransBlock(_) => &mut trans_builder, Transparency::Trans => &mut trans_builder,
_ => &mut builder, _ => &mut builder,
}; };
cur_builder.add_face(face, coord, face_texture as u8); target_builder.add_face(face, coord, face_texture as u8);
} }
} }
}, },
RenderType::CrossShape(textures) => { RenderType::Cross(textures) => {
builder.add_diagonal_face( builder.add_diagonal_face(
coord, coord,
DiagonalFace::LeftZ, DiagonalFace::LeftZ,