From 941f5630599a020aad463f45d73234660b1ffc16 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Thu, 26 Jan 2023 03:50:38 +0100 Subject: [PATCH] block textures --- src/world/block.rs | 22 ++++++++++++++++++++++ src/world/mesh.rs | 33 ++++++++++++++++++--------------- src/world/worldgen.rs | 15 ++++++++------- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/world/block.rs b/src/world/block.rs index dc3de5d..e16641c 100644 --- a/src/world/block.rs +++ b/src/world/block.rs @@ -25,6 +25,28 @@ impl Block { collision: CollisionType::Solid, raycast_collision: true, }, + Self::Dirt => BlockDescriptor { + name: "dirt", + render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Dirt)), + collision: CollisionType::Solid, + raycast_collision: true, + }, + Self::Grass => BlockDescriptor { + name: "grass", + render: RenderType::SolidBlock(CubeTexture::top_sides_bottom( + BlockTexture::GrassTop, + BlockTexture::GrassSide, + BlockTexture::Dirt + )), + collision: CollisionType::Solid, + raycast_collision: true, + }, + Self::Sand => BlockDescriptor { + name: "sand", + render: RenderType::SolidBlock(CubeTexture::all(BlockTexture::Sand)), + collision: CollisionType::Solid, + raycast_collision: true, + }, _ => todo!() } } diff --git a/src/world/mesh.rs b/src/world/mesh.rs index 4074cb8..6d6b6f2 100644 --- a/src/world/mesh.rs +++ b/src/world/mesh.rs @@ -1,6 +1,6 @@ use strum::{EnumIter, IntoEnumIterator}; use glam::{Vec3A, vec3a, IVec3, ivec3}; -use super::{render::ChunkVertex, chunk::CHUNK_SIZE, block::Block}; +use super::{render::ChunkVertex, chunk::CHUNK_SIZE, block::{Block, RenderType}}; pub mod data; use data::MeshGenData; @@ -103,26 +103,29 @@ pub fn generate_mesh(data: MeshGenData) -> (Vec, Vec) { for z in 0..CHUNK_SIZE { let coord = ivec3(x as i32, y as i32, z as i32); let block = get_block(coord); - if block == Block::Air { + let descriptor = block.descriptor(); + if matches!(descriptor.render, RenderType::None) { continue } for face in CubeFace::iter() { let facing = CUBE_FACE_NORMALS[face as usize].as_ivec3(); let facing_coord = coord + facing; - let show = { - get_block(facing_coord) == Block::Air - }; + let show = matches!(get_block(facing_coord).descriptor().render, RenderType::None); if show { - // let texures = descriptor.render.unwrap().1; - // let block_texture = match face { - // CubeFace::Top => texures.top, - // CubeFace::Front => texures.front, - // CubeFace::Left => texures.left, - // CubeFace::Right => texures.right, - // CubeFace::Back => texures.back, - // CubeFace::Bottom => texures.bottom, - // }; - builder.add_face(face, coord, 0); + match descriptor.render { + RenderType::SolidBlock(textures) => { + let face_texture = match face { + CubeFace::Top => textures.top, + CubeFace::Front => textures.front, + CubeFace::Left => textures.left, + CubeFace::Right => textures.right, + CubeFace::Back => textures.back, + CubeFace::Bottom => textures.bottom, + }; + builder.add_face(face, coord, face_texture as u8); + }, + _ => unimplemented!() + } } } } diff --git a/src/world/worldgen.rs b/src/world/worldgen.rs index 8c49489..fa72ca6 100644 --- a/src/world/worldgen.rs +++ b/src/world/worldgen.rs @@ -6,17 +6,18 @@ use super::{ pub fn generate_world(position: IVec3, _seed: u32) -> BlockData { let mut blocks = Box::new([[[Block::Air; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE]); - blocks[0][0][0] = Block::Stone; - blocks[1][0][0] = Block::Stone; - blocks[0][1][0] = Block::Stone; - blocks[0][2][0] = Block::Stone; - blocks[0][0][1] = Block::Stone; - if position.y == 0 { + if position.y == -1 { for x in 0..CHUNK_SIZE { for z in 0..CHUNK_SIZE { - blocks[x][0][z] = Block::Stone; + blocks[x][0][z] = Block::Grass; } } + } else { + blocks[0][0][0] = Block::Stone; + blocks[1][0][0] = Block::Stone; + blocks[0][1][0] = Block::Stone; + blocks[0][2][0] = Block::Stone; + blocks[0][0][1] = Block::Stone; } //TODO actual world generation blocks