add overlay when submerged

This commit is contained in:
griffi-gh 2024-05-02 11:03:38 +02:00
parent 8e39fc24fd
commit f157e2dab3
9 changed files with 148 additions and 27 deletions

View file

@ -1,3 +1,4 @@
use glam::{vec4, Vec4};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use strum::EnumIter; use strum::EnumIter;
use num_enum::TryFromPrimitive; use num_enum::TryFromPrimitive;
@ -53,6 +54,7 @@ impl Block {
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: false, raycast_collision: false,
drops: None, drops: None,
submerge: None,
}, },
Self::Marker => BlockDescriptor { Self::Marker => BlockDescriptor {
name: "marker", name: "marker",
@ -60,6 +62,7 @@ impl Block {
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: false, raycast_collision: false,
drops: None, drops: None,
submerge: None,
}, },
Self::Stone => BlockDescriptor { Self::Stone => BlockDescriptor {
name: "stone", name: "stone",
@ -70,6 +73,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Dirt => BlockDescriptor { Self::Dirt => BlockDescriptor {
name: "dirt", name: "dirt",
@ -80,6 +84,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Grass => BlockDescriptor { Self::Grass => BlockDescriptor {
name: "grass", name: "grass",
@ -94,6 +99,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Sand => BlockDescriptor { Self::Sand => BlockDescriptor {
name: "sand", name: "sand",
@ -104,6 +110,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Cobblestone => BlockDescriptor { Self::Cobblestone => BlockDescriptor {
name: "cobblestone", name: "cobblestone",
@ -114,6 +121,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::TallGrass => BlockDescriptor { Self::TallGrass => BlockDescriptor {
name: "tall grass", name: "tall grass",
@ -121,6 +129,7 @@ impl Block {
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Planks => BlockDescriptor { Self::Planks => BlockDescriptor {
name: "planks", name: "planks",
@ -131,6 +140,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Torch => BlockDescriptor { Self::Torch => BlockDescriptor {
name: "torch", name: "torch",
@ -138,6 +148,7 @@ impl Block {
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Wood => BlockDescriptor { Self::Wood => BlockDescriptor {
name: "leaf", name: "leaf",
@ -148,6 +159,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Leaf => BlockDescriptor { Self::Leaf => BlockDescriptor {
name: "leaf", name: "leaf",
@ -158,6 +170,7 @@ impl Block {
collision: CollisionType::Solid, collision: CollisionType::Solid,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: None,
}, },
Self::Water => BlockDescriptor { Self::Water => BlockDescriptor {
name: "water", name: "water",
@ -168,6 +181,7 @@ impl Block {
collision: CollisionType::None, collision: CollisionType::None,
raycast_collision: true, raycast_collision: true,
drops: None, drops: None,
submerge: Some(vec4(0., 0., 0.25, 0.75)),
}, },
} }
} }
@ -180,6 +194,7 @@ pub struct BlockDescriptor {
pub collision: CollisionType, pub collision: CollisionType,
pub raycast_collision: bool, pub raycast_collision: bool,
pub drops: Option<Item>, pub drops: Option<Item>,
pub submerge: Option<Vec4>,
} }
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]

View file

@ -0,0 +1,10 @@
#version 300 es
precision highp float;
out vec4 out_color;
uniform vec4 color;
void main() {
out_color = color;
}

View file

@ -0,0 +1,9 @@
#version 300 es
precision highp float;
in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}

View file

@ -76,6 +76,7 @@ use rendering::{
world::{draw_world, draw_current_chunk_border}, world::{draw_world, draw_current_chunk_border},
selection_box::render_selection_box, selection_box::render_selection_box,
entities::render_entities, entities::render_entities,
sumberge::render_submerged_view,
}; };
use block_placement::update_block_placement; use block_placement::update_block_placement;
use delta_time::{DeltaTime, init_delta_time}; use delta_time::{DeltaTime, init_delta_time};
@ -178,6 +179,7 @@ fn render() -> Workload {
draw_current_chunk_border, draw_current_chunk_border,
render_selection_box, render_selection_box,
render_entities, render_entities,
render_submerged_view,
).into_sequential_workload().run_if(is_ingame), ).into_sequential_workload().run_if(is_ingame),
kubi_ui_draw, kubi_ui_draw,
).into_sequential_workload() ).into_sequential_workload()

View file

@ -51,6 +51,10 @@ pub struct ChunkShaderPrefab(pub Program);
#[repr(transparent)] #[repr(transparent)]
pub struct ColoredShaderPrefab(pub Program); pub struct ColoredShaderPrefab(pub Program);
#[derive(Unique)]
#[repr(transparent)]
pub struct Colored2ShaderPrefab(pub Program);
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]
pub struct UiFontPrefab(pub FontHandle); pub struct UiFontPrefab(pub FontHandle);
@ -98,6 +102,14 @@ pub fn load_prefabs(
&renderer.display &renderer.display
) )
)); ));
storages.add_unique_non_send_sync(Colored2ShaderPrefab(
include_shader_prefab!(
"colored",
"../shaders/colored2.vert",
"../shaders/colored2.frag",
&renderer.display
)
));
log::info!("releasing shader compiler"); log::info!("releasing shader compiler");

View file

@ -22,6 +22,7 @@ pub mod primitives;
pub mod world; pub mod world;
pub mod selection_box; pub mod selection_box;
pub mod entities; pub mod entities;
pub mod sumberge;
#[derive(Unique)] #[derive(Unique)]
#[repr(transparent)] #[repr(transparent)]

View file

@ -1,27 +1,30 @@
use shipyard::{Workload, IntoWorkload}; use shipyard::{Workload, IntoWorkload};
use glium::implement_vertex; use glium::implement_vertex;
pub mod cube; pub mod cube;
pub mod rect; pub mod rect;
pub mod stri;
use cube::init_cube_primitive;
use rect::init_rect_primitive; use cube::init_cube_primitive;
use rect::init_rect_primitive;
#[derive(Clone, Copy, Default)] use stri::init_stri_primitive;
pub struct PositionOnlyVertex {
pub position: [f32; 3], #[derive(Clone, Copy, Default)]
} pub struct PositionOnlyVertex {
implement_vertex!(PositionOnlyVertex, position); pub position: [f32; 3],
}
#[derive(Clone, Copy, Default)] implement_vertex!(PositionOnlyVertex, position);
pub struct PositionOnlyVertex2d {
pub position: [f32; 2], #[derive(Clone, Copy, Default)]
} pub struct PositionOnlyVertex2d {
implement_vertex!(PositionOnlyVertex2d, position); pub position: [f32; 2],
}
pub fn init_primitives() -> Workload { implement_vertex!(PositionOnlyVertex2d, position);
(
init_cube_primitive, pub fn init_primitives() -> Workload {
init_rect_primitive, (
).into_sequential_workload() init_cube_primitive,
} init_rect_primitive,
init_stri_primitive,
).into_workload()
}

View file

@ -0,0 +1,30 @@
use shipyard::{Unique, AllStoragesView, NonSendSync, UniqueView};
use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType};
use crate::rendering::Renderer;
use super::PositionOnlyVertex2d;
#[derive(Unique)]
pub struct STriPrimitive(pub VertexBuffer<PositionOnlyVertex2d>, pub IndexBuffer<u16>);
const STRI_VERTEX: &[PositionOnlyVertex2d] = &[
PositionOnlyVertex2d { position: [-1., -1.] },
PositionOnlyVertex2d { position: [ 3., -1.] },
PositionOnlyVertex2d { position: [-1., 3.] },
];
const STRI_INDEX: &[u16] = &[0, 1, 2];
pub(super) fn init_stri_primitive(
storages: AllStoragesView,
display: NonSendSync<UniqueView<Renderer>>
) {
let vert = VertexBuffer::immutable(
&display.display,
STRI_VERTEX
).unwrap();
let index = IndexBuffer::immutable(
&display.display,
PrimitiveType::TrianglesList,
STRI_INDEX
).unwrap();
storages.add_unique_non_send_sync(STriPrimitive(vert, index));
}

View file

@ -0,0 +1,39 @@
use glium::{uniform, Blend, DrawParameters, Surface};
use kubi_shared::transform::Transform;
use shipyard::{IntoIter, NonSendSync, UniqueView, UniqueViewMut, View};
use crate::{
player::MainPlayer,
prefabs::Colored2ShaderPrefab,
rendering::primitives::stri::STriPrimitive,
world::ChunkStorage,
};
use super::RenderTarget;
pub fn render_submerged_view(
mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
primitive: NonSendSync<UniqueView<STriPrimitive>>,
program: NonSendSync<UniqueView<Colored2ShaderPrefab>>,
plr: View<MainPlayer>,
trans: View<Transform>,
world: UniqueView<ChunkStorage>,
) {
let (_, plr_trans) = (&plr, &trans).iter().next().expect("Main player MIA");
let plr_pos = plr_trans.0.to_scale_rotation_translation().2;
let block_at_pos = world.get_block(plr_pos.floor().as_ivec3());
let Some(block_at_pos) = block_at_pos else { return };
let Some(color) = block_at_pos.descriptor().submerge else { return };
let draw_parameters = DrawParameters {
blend: Blend::alpha_blending(),
..Default::default()
};
target.0.draw(
&primitive.0,
&primitive.1,
&program.0,
&uniform! {
color: color.to_array(),
},
&draw_parameters,
).unwrap();
}