add trans rendering and crosshair

This commit is contained in:
griffi-gh 2024-05-02 00:32:43 +02:00
parent 772a8ea7db
commit 66d3ea656b
12 changed files with 571 additions and 446 deletions

BIN
assets/blocks/water.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 341 B

View file

@ -22,6 +22,7 @@ pub enum BlockTexture {
Cobblestone, Cobblestone,
Planks, Planks,
WaterSolid, WaterSolid,
Water,
} }
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, EnumIter, TryFromPrimitive)] #[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, EnumIter, TryFromPrimitive)]
@ -136,7 +137,7 @@ impl Block {
}, },
Self::Water => BlockDescriptor { Self::Water => BlockDescriptor {
name: "water", name: "water",
render: RenderType::BinaryTransparency(CubeTexture::all(BlockTexture::WaterSolid)), render: RenderType::TransBlock(CubeTexture::all(BlockTexture::Water)),
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
@ -217,6 +218,7 @@ pub enum CollisionType {
pub enum RenderType { pub enum RenderType {
None, None,
SolidBlock(CubeTexture), SolidBlock(CubeTexture),
TransBlock(CubeTexture),
BinaryTransparency(CubeTexture), BinaryTransparency(CubeTexture),
CrossShape(CrossTexture), CrossShape(CrossTexture),
} }

View file

@ -8,6 +8,7 @@ in vec2 v_uv;
flat in uint v_tex_index; flat in uint v_tex_index;
out vec4 color; out vec4 color;
uniform sampler2DArray tex; uniform sampler2DArray tex;
uniform bool discard_alpha;
// vec4 alpha_drop(vec4 b, vec4 a) { // vec4 alpha_drop(vec4 b, vec4 a) {
// if ((a.w < 1.) || (b.w < 1.)) { // if ((a.w < 1.) || (b.w < 1.)) {
@ -20,10 +21,10 @@ void main() {
// base color from texture // base color from texture
color = texture(tex, vec3(v_uv, v_tex_index)); color = texture(tex, vec3(v_uv, v_tex_index));
// discard transparent pixels // discard transparent pixels
if (color.w < 0.5) discard; if (color.w < (discard_alpha ? 0.01 : 0.5)) discard;
//basic "lighting" //basic "lighting"
float light = abs(v_normal.x) + .8 * abs(v_normal.y) + .6 * abs(v_normal.z); float light = abs(v_normal.x) + .8 * abs(v_normal.y) + .6 * abs(v_normal.z);
color *= vec4(vec3(light), 1.); color *= vec4(vec3(light), 1.);
//discard alpha //discard alpha
color.w = 1.; if (discard_alpha) color.w = 1.;
} }

View file

@ -15,12 +15,13 @@ use std::time::Instant;
pub(crate) use kubi_shared::transform; pub(crate) use kubi_shared::transform;
mod ui { mod ui;
pub(crate) mod loading_screen; pub(crate) use ui::{
pub(crate) mod connecting_screen; loading_screen,
pub(crate) mod chat_ui; connecting_screen,
} chat_ui,
pub(crate) use ui::{loading_screen, connecting_screen, chat_ui}; crosshair_ui,
};
pub(crate) mod rendering; pub(crate) mod rendering;
pub(crate) mod world; pub(crate) mod world;
@ -91,6 +92,7 @@ use filesystem::AssetManager;
use client_physics::{init_client_physics, update_client_physics_late}; use client_physics::{init_client_physics, update_client_physics_late};
use chat_ui::render_chat; use chat_ui::render_chat;
use chat::init_chat_manager; use chat::init_chat_manager;
use crosshair_ui::{init_crosshair_image, draw_crosshair};
/// stuff required to init the renderer and other basic systems /// stuff required to init the renderer and other basic systems
fn pre_startup() -> Workload { fn pre_startup() -> Workload {
@ -116,6 +118,7 @@ fn startup() -> Workload {
init_delta_time, init_delta_time,
init_client_physics, init_client_physics,
init_chat_manager, init_chat_manager,
init_crosshair_image,
).into_sequential_workload() ).into_sequential_workload()
} }
@ -151,6 +154,7 @@ fn update() -> Workload {
update_block_placement, update_block_placement,
apply_queued_blocks, apply_queued_blocks,
render_chat, render_chat,
draw_crosshair,
).into_sequential_workload().run_if(is_ingame), ).into_sequential_workload().run_if(is_ingame),
update_networking_late.run_if(is_multiplayer), update_networking_late.run_if(is_multiplayer),
compute_cameras, compute_cameras,

View file

@ -34,6 +34,7 @@ impl AssetPaths for BlockTexture {
Self::Cobblestone => "cobblestone.png", Self::Cobblestone => "cobblestone.png",
Self::Planks => "planks.png", Self::Planks => "planks.png",
Self::WaterSolid => "solid_water.png", Self::WaterSolid => "solid_water.png",
Self::Water => "water.png",
} }
} }
} }

View file

@ -54,7 +54,7 @@ pub fn draw_world(
settings: UniqueView<GameSettings> settings: UniqueView<GameSettings>
) { ) {
let camera = camera.iter().next().expect("No cameras in the scene"); let camera = camera.iter().next().expect("No cameras in the scene");
let draw_parameters = DrawParameters { let mut draw_parameters = DrawParameters {
depth: Depth { depth: Depth {
test: DepthTest::IfLess, test: DepthTest::IfLess,
write: true, write: true,
@ -75,6 +75,8 @@ pub fn draw_world(
let view = camera.view_matrix.to_cols_array_2d(); let view = camera.view_matrix.to_cols_array_2d();
let perspective = camera.perspective_matrix.to_cols_array_2d(); let perspective = camera.perspective_matrix.to_cols_array_2d();
let mut enqueue_trans = Vec::new();
for (&position, chunk) in &chunks.chunks { for (&position, chunk) in &chunks.chunks {
if let Some(key) = chunk.mesh_index { if let Some(key) = chunk.mesh_index {
let mesh = meshes.get(key).expect("Mesh index pointing to nothing"); let mesh = meshes.get(key).expect("Mesh index pointing to nothing");
@ -107,8 +109,31 @@ pub fn draw_world(
}, },
&draw_parameters &draw_parameters
).unwrap(); ).unwrap();
if mesh.trans_index_buffer.len() > 0 {
enqueue_trans.push((chunk, mesh));
} }
} }
}
draw_parameters.blend = Blend::alpha_blending();
draw_parameters.backface_culling = BackfaceCullingMode::CullingDisabled;
for (chunk, mesh) in enqueue_trans {
let world_position = chunk.position.as_vec3() * CHUNK_SIZE as f32;
target.0.draw(
&mesh.trans_vertex_buffer,
&mesh.trans_index_buffer,
&program.0,
&uniform! {
position_offset: world_position.to_array(),
view: view,
perspective: perspective,
tex: texture_sampler,
},
&draw_parameters
).unwrap();
}
} }
pub fn draw_current_chunk_border( pub fn draw_current_chunk_border(

4
kubi/src/ui.rs Normal file
View file

@ -0,0 +1,4 @@
pub(crate) mod loading_screen;
pub(crate) mod connecting_screen;
pub(crate) mod chat_ui;
pub(crate) mod crosshair_ui;

View file

@ -0,0 +1,60 @@
use std::f32::consts::PI;
use glam::uvec2;
use hui::{
draw::{ImageHandle, TextureFormat},
element::{container::Container, image::Image, transformer::ElementTransformExt, UiElementExt},
layout::Alignment,
size
};
use shipyard::{AllStoragesViewMut, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View};
use crate::{hui_integration::UiState, player::MainPlayer, rendering::WindowSize, world::raycast::LookingAtBlock};
const CROSSHAIR_SIZE: usize = 9;
const CROSSHAIR: &[u8] = &[
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
];
#[derive(Unique)]
pub struct CrosshairImage(ImageHandle);
pub fn init_crosshair_image(storages: AllStoragesViewMut) {
let mut ui = storages.borrow::<NonSendSync<UniqueViewMut<UiState>>>().unwrap();
let image = ui.hui.add_image(TextureFormat::Grayscale, CROSSHAIR, CROSSHAIR_SIZE);
storages.add_unique(CrosshairImage(image));
}
pub fn draw_crosshair(
mut ui: NonSendSync<UniqueViewMut<UiState>>,
crosshair: UniqueView<CrosshairImage>,
size: UniqueView<WindowSize>,
player: View<MainPlayer>,
raycast: View<LookingAtBlock>,
) {
let mut cursor_active = false;
if let Some((_, raycast)) = (&player, &raycast).iter().next() {
cursor_active = raycast.0.is_some();
}
Container::default()
.with_size(size!(100%))
.with_align(Alignment::Center)
.with_children(|ui| {
Image::new(crosshair.0)
.with_color((1., 1., 1., 0.5))
.with_size(size!(18, 18))
.transform()
.scale(glam::Vec2::splat(if cursor_active { 1. } else { 0.66 }))
.rotate(if cursor_active { 0. } else { PI / 4. })
.add_child(ui);
})
.add_root(&mut ui.hui, uvec2(size.0.x & !1, size.0.y & !1).as_vec2());
}

View file

@ -17,6 +17,8 @@ impl ChunkData {
pub struct ChunkMesh { pub struct ChunkMesh {
pub vertex_buffer: VertexBuffer<ChunkVertex>, pub vertex_buffer: VertexBuffer<ChunkVertex>,
pub index_buffer: IndexBuffer<u32>, pub index_buffer: IndexBuffer<u32>,
pub trans_vertex_buffer: VertexBuffer<ChunkVertex>,
pub trans_index_buffer: IndexBuffer<u32>,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]

View file

@ -220,7 +220,11 @@ fn process_completed_tasks(
//increase ops counter //increase ops counter
ops += 1; ops += 1;
}, },
ChunkTaskResponse::GeneratedMesh { position, vertices, indexes } => { ChunkTaskResponse::GeneratedMesh {
position,
vertices, indices,
trans_vertices, trans_indices,
} => {
//check if chunk exists //check if chunk exists
let Some(chunk) = world.chunks.get_mut(&position) else { let Some(chunk) = world.chunks.get_mut(&position) else {
log::warn!("mesh discarded: chunk doesn't exist"); log::warn!("mesh discarded: chunk doesn't exist");
@ -234,11 +238,12 @@ fn process_completed_tasks(
} }
//apply the mesh //apply the mesh
let vertex_buffer = VertexBuffer::immutable(&renderer.display, &vertices).unwrap(); //TODO: Skip if mesh is empty? (i.e. set to None)
let index_buffer = IndexBuffer::immutable(&renderer.display, PrimitiveType::TrianglesList, &indexes).unwrap();
let mesh = ChunkMesh { let mesh = ChunkMesh {
vertex_buffer, vertex_buffer: VertexBuffer::immutable(&renderer.display, &vertices).unwrap(),
index_buffer, index_buffer: IndexBuffer::immutable(&renderer.display, PrimitiveType::TrianglesList, &indices).unwrap(),
trans_vertex_buffer: VertexBuffer::immutable(&renderer.display, &trans_vertices).unwrap(),
trans_index_buffer: IndexBuffer::immutable(&renderer.display, PrimitiveType::TrianglesList, &trans_indices).unwrap(),
}; };
if let Some(index) = chunk.mesh_index { if let Some(index) = chunk.mesh_index {
meshes.update(index, mesh).expect("Mesh update failed"); meshes.update(index, mesh).expect("Mesh update failed");

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, RenderType}; use kubi_shared::block::{Block, BlockTexture, RenderType};
use crate::world::chunk::CHUNK_SIZE; use crate::world::chunk::CHUNK_SIZE;
use crate::rendering::world::ChunkVertex; use crate::rendering::world::ChunkVertex;
@ -10,7 +10,10 @@ mod builder;
use data::MeshGenData; use data::MeshGenData;
use builder::{MeshBuilder, CubeFace, DiagonalFace}; use builder::{MeshBuilder, CubeFace, DiagonalFace};
pub fn generate_mesh(data: MeshGenData) -> (Vec<ChunkVertex>, Vec<u32>) { pub fn generate_mesh(data: MeshGenData) -> (
(Vec<ChunkVertex>, Vec<u32>),
(Vec<ChunkVertex>, Vec<u32>),
) {
let get_block = |pos: IVec3| -> Block { let get_block = |pos: IVec3| -> Block {
if pos.x < 0 { if pos.x < 0 {
data.block_data_neg_x[(CHUNK_SIZE as i32 + pos.x) as usize][pos.y as usize][pos.z as usize] data.block_data_neg_x[(CHUNK_SIZE as i32 + pos.x) as usize][pos.y as usize][pos.z as usize]
@ -30,6 +33,7 @@ pub fn generate_mesh(data: MeshGenData) -> (Vec<ChunkVertex>, Vec<u32>) {
}; };
let mut builder = MeshBuilder::new(); let mut builder = MeshBuilder::new();
let mut trans_builder = MeshBuilder::new();
for x in 0..CHUNK_SIZE as i32 { for x in 0..CHUNK_SIZE as i32 {
for y in 0..CHUNK_SIZE as i32 { for y in 0..CHUNK_SIZE as i32 {
@ -39,7 +43,9 @@ pub fn generate_mesh(data: MeshGenData) -> (Vec<ChunkVertex>, Vec<u32>) {
let descriptor = block.descriptor(); let descriptor = block.descriptor();
match descriptor.render { match descriptor.render {
RenderType::None => continue, RenderType::None => continue,
RenderType::SolidBlock(textures) | RenderType::BinaryTransparency(textures) => { RenderType::SolidBlock(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;
@ -47,10 +53,12 @@ pub fn generate_mesh(data: MeshGenData) -> (Vec<ChunkVertex>, Vec<u32>) {
let facing_descriptor = facing_block.descriptor(); let facing_descriptor = facing_block.descriptor();
let face_obstructed = match descriptor.render { let face_obstructed = match descriptor.render {
RenderType::SolidBlock(_) => matches!(facing_descriptor.render, RenderType::SolidBlock(_)), RenderType::SolidBlock(_) => matches!(facing_descriptor.render, RenderType::SolidBlock(_)),
RenderType::BinaryTransparency(_) => { RenderType::BinaryTransparency(_) |
RenderType::TransBlock(_) => {
match facing_descriptor.render { match facing_descriptor.render {
RenderType::SolidBlock(_) => true, RenderType::SolidBlock(_) => true,
RenderType::BinaryTransparency(_) => block == facing_block, RenderType::BinaryTransparency(_) |
RenderType::TransBlock(_) => block == facing_block,
_ => false, _ => false,
} }
}, },
@ -65,7 +73,11 @@ pub fn generate_mesh(data: MeshGenData) -> (Vec<ChunkVertex>, Vec<u32>) {
CubeFace::Back => textures.back, CubeFace::Back => textures.back,
CubeFace::Bottom => textures.bottom, CubeFace::Bottom => textures.bottom,
}; };
builder.add_face(face, coord, face_texture as u8); let cur_builder = match descriptor.render {
RenderType::TransBlock(_) => &mut trans_builder,
_ => &mut builder,
};
cur_builder.add_face(face, coord, face_texture as u8);
} }
} }
}, },
@ -88,5 +100,5 @@ pub fn generate_mesh(data: MeshGenData) -> (Vec<ChunkVertex>, Vec<u32>) {
} }
} }
builder.finish() (builder.finish(), trans_builder.finish())
} }

View file

@ -29,7 +29,9 @@ pub enum ChunkTaskResponse {
GeneratedMesh { GeneratedMesh {
position: IVec3, position: IVec3,
vertices: Vec<ChunkVertex>, vertices: Vec<ChunkVertex>,
indexes: Vec<u32> indices: Vec<u32>,
trans_vertices: Vec<ChunkVertex>,
trans_indices: Vec<u32>,
}, },
} }
@ -55,8 +57,15 @@ impl ChunkTaskManager {
self.pool.spawn(move || { self.pool.spawn(move || {
let _ = sender.send(match task { let _ = sender.send(match task {
ChunkTask::GenerateMesh { position, data } => { ChunkTask::GenerateMesh { position, data } => {
let (vertices, indexes) = generate_mesh(data); let (
ChunkTaskResponse::GeneratedMesh { position, vertices, indexes } (vertices, indices),
(trans_vertices, trans_indices),
) = generate_mesh(data);
ChunkTaskResponse::GeneratedMesh {
position,
vertices, indices,
trans_vertices, trans_indices,
}
}, },
ChunkTask::LoadChunk { position, seed } => { ChunkTask::LoadChunk { position, seed } => {
let (chunk_data, queued) = generate_world(position, seed); let (chunk_data, queued) = generate_world(position, seed);