mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-24 21:08:19 -06:00
Selection Box
This commit is contained in:
parent
241dc8ea7e
commit
0d41a9f14d
27
kubi/shaders/selection_box.wgsl
Normal file
27
kubi/shaders/selection_box.wgsl
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
struct CameraUniform {
|
||||||
|
view_proj: mat4x4<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
@group(0) @binding(0)
|
||||||
|
var<uniform> camera: CameraUniform;
|
||||||
|
|
||||||
|
struct SboxUniform {
|
||||||
|
position: vec3<f32>,
|
||||||
|
};
|
||||||
|
|
||||||
|
@group(1) @binding(0)
|
||||||
|
var<uniform> sbox: SboxUniform;
|
||||||
|
|
||||||
|
@vertex
|
||||||
|
fn vs_main(
|
||||||
|
@location(0) position: vec3<f32>,
|
||||||
|
) -> @builtin(position) vec4<f32> {
|
||||||
|
return camera.view_proj * vec4<f32>(position + sbox.position, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn fs_main(
|
||||||
|
@builtin(position) in: vec4<f32>,
|
||||||
|
) -> @location(0) vec4<f32> {
|
||||||
|
return vec4<f32>(0.0, 0.0, 0.0, 0.5);
|
||||||
|
}
|
|
@ -1,15 +1,16 @@
|
||||||
use shipyard::{AllStoragesViewMut, IntoIter, IntoWorkload, Unique, UniqueView, UniqueViewMut, View, Workload, WorkloadModificator};
|
use shipyard::{AllStoragesViewMut, IntoIter, IntoWorkload, SystemModificator, Unique, UniqueView, UniqueViewMut, View, Workload, WorkloadModificator};
|
||||||
use winit::dpi::PhysicalSize;
|
use winit::dpi::PhysicalSize;
|
||||||
use glam::Vec3;
|
use glam::Vec3;
|
||||||
use crate::{events::WindowResizedEvent, state::is_ingame};
|
use crate::{events::WindowResizedEvent, state::is_ingame};
|
||||||
|
|
||||||
mod renderer;
|
mod renderer;
|
||||||
mod primitives;
|
mod primitives;
|
||||||
|
mod selection_box;
|
||||||
pub use renderer::Renderer;
|
pub use renderer::Renderer;
|
||||||
|
|
||||||
pub mod background;
|
pub mod background;
|
||||||
pub mod world;
|
pub mod world;
|
||||||
pub mod camera;
|
pub mod camera_uniform;
|
||||||
pub mod depth;
|
pub mod depth;
|
||||||
|
|
||||||
pub struct BufferPair {
|
pub struct BufferPair {
|
||||||
|
@ -28,14 +29,15 @@ pub struct RenderCtx<'a> {
|
||||||
pub surface_view: &'a wgpu::TextureView,
|
pub surface_view: &'a wgpu::TextureView,
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO run init_world_render_state only once ingame?
|
//TODO run init_world_render_state, init_selection_box_state, etc. only once ingame?
|
||||||
|
|
||||||
pub fn init_rendering() -> Workload {
|
pub fn init_rendering() -> Workload {
|
||||||
(
|
(
|
||||||
depth::init_depth_texture,
|
depth::init_depth_texture,
|
||||||
camera::init_camera_uniform_buffer,
|
camera_uniform::init_camera_uniform_buffer,
|
||||||
world::init_world_render_state, //requires depth and camera buffers
|
world::init_world_render_state, //req: depth, camera
|
||||||
primitives::init_primitives,
|
primitives::init_primitives,
|
||||||
|
selection_box::init_selection_box_render_state, //req: depth, camera, primitives
|
||||||
).into_sequential_workload()
|
).into_sequential_workload()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +50,8 @@ pub fn update_rendering_early() -> Workload {
|
||||||
|
|
||||||
pub fn update_rendering_late() -> Workload {
|
pub fn update_rendering_late() -> Workload {
|
||||||
(
|
(
|
||||||
camera::update_camera_uniform_buffer,
|
camera_uniform::update_camera_uniform_buffer,
|
||||||
|
selection_box::update_selection_box_render_state.run_if(is_ingame),
|
||||||
).into_workload()
|
).into_workload()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,6 +73,7 @@ pub fn render_master(storages: AllStoragesViewMut) {
|
||||||
storages.run_with_data(background::clear_bg, &mut data);
|
storages.run_with_data(background::clear_bg, &mut data);
|
||||||
if storages.run(is_ingame) {
|
if storages.run(is_ingame) {
|
||||||
storages.run_with_data(world::draw_world, &mut data);
|
storages.run_with_data(world::draw_world, &mut data);
|
||||||
|
storages.run_with_data(selection_box::draw_selection_box, &mut data);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer.queue().submit([encoder.finish()]);
|
renderer.queue().submit([encoder.finish()]);
|
||||||
|
|
|
@ -11,7 +11,6 @@ pub struct CameraUniformData {
|
||||||
pub view_proj: [f32; 4 * 4],
|
pub view_proj: [f32; 4 * 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//TODO if multiple cameras, buffer per camera
|
//TODO if multiple cameras, buffer per camera
|
||||||
#[derive(Unique)]
|
#[derive(Unique)]
|
||||||
pub struct CameraUniformBuffer {
|
pub struct CameraUniformBuffer {
|
|
@ -2,6 +2,7 @@ use bytemuck::{Pod, Zeroable};
|
||||||
use shipyard::{IntoWorkload, Workload};
|
use shipyard::{IntoWorkload, Workload};
|
||||||
|
|
||||||
mod cube;
|
mod cube;
|
||||||
|
pub use cube::CubePrimitive;
|
||||||
|
|
||||||
pub fn init_primitives() -> Workload {
|
pub fn init_primitives() -> Workload {
|
||||||
(
|
(
|
||||||
|
|
|
@ -1,9 +1,65 @@
|
||||||
|
use shipyard::{AllStoragesView, IntoIter, Unique, UniqueView, View};
|
||||||
|
use wgpu::RenderPassDescriptor;
|
||||||
|
use crate::{player::MainPlayer, world::raycast::LookingAtBlock};
|
||||||
|
use super::{camera_uniform::CameraUniformBuffer, depth::DepthTexture, primitives::CubePrimitive, RenderCtx};
|
||||||
|
|
||||||
mod pipeline;
|
mod pipeline;
|
||||||
|
mod uniform;
|
||||||
|
|
||||||
|
use uniform::SelectionBoxUniform;
|
||||||
|
|
||||||
|
#[derive(Unique)]
|
||||||
|
pub struct SboxRenderState {
|
||||||
|
pipeline: wgpu::RenderPipeline,
|
||||||
|
uniform: SelectionBoxUniform,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_selection_box_render_state(storages: AllStoragesView) {
|
||||||
|
let uniform = storages.run(uniform::init_selection_box_uniform);
|
||||||
|
let pipeline = storages.run_with_data(pipeline::init_selection_box_pipeline, &uniform);
|
||||||
|
storages.add_unique(SboxRenderState { pipeline, uniform });
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use uniform::update_selection_box_uniform
|
||||||
|
as update_selection_box_render_state;
|
||||||
|
|
||||||
pub fn draw_selection_box(
|
pub fn draw_selection_box(
|
||||||
ctx: &mut RenderCtx,
|
ctx: &mut RenderCtx,
|
||||||
|
state: UniqueView<SboxRenderState>,
|
||||||
|
depth: UniqueView<DepthTexture>,
|
||||||
|
cube: UniqueView<CubePrimitive>,
|
||||||
|
camera_ubo: UniqueView<CameraUniformBuffer>,
|
||||||
lookat: View<LookingAtBlock>,
|
lookat: View<LookingAtBlock>,
|
||||||
camera: View<Camera>,
|
player: View<MainPlayer>,
|
||||||
) {
|
) {
|
||||||
//TODO
|
let Some((LookingAtBlock(Some(_)), _)) = (&lookat, &player).iter().next() else {
|
||||||
|
return
|
||||||
|
};
|
||||||
|
let mut rpass = ctx.encoder.begin_render_pass(&RenderPassDescriptor {
|
||||||
|
label: Some("rpass_selection_box"),
|
||||||
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
|
view: ctx.surface_view,
|
||||||
|
resolve_target: None,
|
||||||
|
ops: wgpu::Operations {
|
||||||
|
load: wgpu::LoadOp::Load,
|
||||||
|
store: wgpu::StoreOp::Store,
|
||||||
|
},
|
||||||
|
})],
|
||||||
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
|
view: &depth.depth_view,
|
||||||
|
depth_ops: Some(wgpu::Operations {
|
||||||
|
load: wgpu::LoadOp::Load,
|
||||||
|
store: wgpu::StoreOp::Discard,
|
||||||
|
}),
|
||||||
|
stencil_ops: None,
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
|
||||||
|
rpass.set_pipeline(&state.pipeline);
|
||||||
|
rpass.set_bind_group(0, &camera_ubo.camera_bind_group, &[]);
|
||||||
|
rpass.set_bind_group(1, &state.uniform.bind_group, &[]);
|
||||||
|
rpass.set_index_buffer(cube.0.index.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
|
rpass.set_vertex_buffer(0, cube.0.vertex.slice(..));
|
||||||
|
rpass.draw_indexed(0..cube.0.index_len, 0, 0..1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,68 @@
|
||||||
|
use shipyard::UniqueView;
|
||||||
|
use crate::rendering::{
|
||||||
|
camera_uniform::CameraUniformBuffer,
|
||||||
|
depth::DepthTexture,
|
||||||
|
primitives::PrimitiveVertex,
|
||||||
|
Renderer
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn init_selection_box_pipeline(
|
||||||
|
uniform: &super::uniform::SelectionBoxUniform,
|
||||||
|
ren: UniqueView<Renderer>,
|
||||||
|
depth: UniqueView<DepthTexture>,
|
||||||
|
camera_ubo: UniqueView<CameraUniformBuffer>,
|
||||||
|
) -> wgpu::RenderPipeline {
|
||||||
|
let shader = ren.device().create_shader_module(
|
||||||
|
wgpu::include_wgsl!("../../../shaders/selection_box.wgsl")
|
||||||
|
);
|
||||||
|
|
||||||
|
let selection_box_pipeline_layout = ren.device().create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
||||||
|
label: Some("selection_box_pipeline_layout"),
|
||||||
|
bind_group_layouts: &[
|
||||||
|
&camera_ubo.camera_bind_group_layout,
|
||||||
|
&uniform.bind_group_layout,
|
||||||
|
],
|
||||||
|
push_constant_ranges: &[],
|
||||||
|
});
|
||||||
|
|
||||||
|
ren.device().create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
|
label: Some("selection_box_pipeline"),
|
||||||
|
layout: Some(&selection_box_pipeline_layout),
|
||||||
|
fragment: Some(wgpu::FragmentState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "fs_main",
|
||||||
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
|
format: ren.surface_config().format,
|
||||||
|
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
||||||
|
write_mask: wgpu::ColorWrites::COLOR,
|
||||||
|
})],
|
||||||
|
}),
|
||||||
|
vertex: wgpu::VertexState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: "vs_main",
|
||||||
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
|
buffers: &[
|
||||||
|
PrimitiveVertex::LAYOUT,
|
||||||
|
],
|
||||||
|
},
|
||||||
|
primitive: wgpu::PrimitiveState {
|
||||||
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||||
|
strip_index_format: None,
|
||||||
|
cull_mode: Some(wgpu::Face::Back),
|
||||||
|
front_face: wgpu::FrontFace::Ccw,
|
||||||
|
unclipped_depth: false,
|
||||||
|
polygon_mode: wgpu::PolygonMode::Fill,
|
||||||
|
conservative: false,
|
||||||
|
},
|
||||||
|
depth_stencil: Some(wgpu::DepthStencilState {
|
||||||
|
format: depth.depth_texture.format(),
|
||||||
|
depth_write_enabled: false,
|
||||||
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
||||||
|
stencil: wgpu::StencilState::default(),
|
||||||
|
bias: wgpu::DepthBiasState::default(),
|
||||||
|
}),
|
||||||
|
multisample: wgpu::MultisampleState::default(),
|
||||||
|
multiview: None,
|
||||||
|
})
|
||||||
|
}
|
79
kubi/src/rendering/selection_box/uniform.rs
Normal file
79
kubi/src/rendering/selection_box/uniform.rs
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
use glam::Vec3;
|
||||||
|
use shipyard::{IntoIter, UniqueView, View};
|
||||||
|
use bytemuck::{Pod, Zeroable};
|
||||||
|
use crate::{
|
||||||
|
player::MainPlayer,
|
||||||
|
rendering::Renderer,
|
||||||
|
world::raycast::LookingAtBlock,
|
||||||
|
};
|
||||||
|
use super::SboxRenderState;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Pod, Zeroable)]
|
||||||
|
#[repr(C, packed)]
|
||||||
|
pub struct SelectionBoxUniformData {
|
||||||
|
pub position: [f32; 3],
|
||||||
|
pub _padding: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SelectionBoxUniform {
|
||||||
|
pub buffer: wgpu::Buffer,
|
||||||
|
pub bind_group_layout: wgpu::BindGroupLayout,
|
||||||
|
pub bind_group: wgpu::BindGroup,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_selection_box_uniform(renderer: UniqueView<Renderer>) -> SelectionBoxUniform {
|
||||||
|
let buffer = renderer.device().create_buffer(&wgpu::BufferDescriptor {
|
||||||
|
label: Some("selection_box_uniform"),
|
||||||
|
size: std::mem::size_of::<SelectionBoxUniformData>() as u64,
|
||||||
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
|
mapped_at_creation: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
let bind_group_layout = renderer.device().create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
|
label: Some("selection_box_bind_group_layout"),
|
||||||
|
entries: &[wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 0,
|
||||||
|
visibility: wgpu::ShaderStages::VERTEX,
|
||||||
|
ty: wgpu::BindingType::Buffer {
|
||||||
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
|
has_dynamic_offset: false,
|
||||||
|
min_binding_size: None,
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
let bind_group = renderer.device().create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
|
label: Some("selection_box_bind_group"),
|
||||||
|
layout: &bind_group_layout,
|
||||||
|
entries: &[wgpu::BindGroupEntry {
|
||||||
|
binding: 0,
|
||||||
|
resource: buffer.as_entire_binding(),
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
|
||||||
|
SelectionBoxUniform {
|
||||||
|
buffer,
|
||||||
|
bind_group_layout,
|
||||||
|
bind_group,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_selection_box_uniform(
|
||||||
|
renderer: UniqueView<Renderer>,
|
||||||
|
state: UniqueView<SboxRenderState>,
|
||||||
|
lookat: View<LookingAtBlock>,
|
||||||
|
player: View<MainPlayer>,
|
||||||
|
) {
|
||||||
|
//TODO: only update if changed
|
||||||
|
if let Some((LookingAtBlock(Some(lookat)), _)) = (&lookat, &player).iter().next() {
|
||||||
|
renderer.queue().write_buffer(
|
||||||
|
&state.uniform.buffer,
|
||||||
|
0,
|
||||||
|
bytemuck::cast_slice(&[SelectionBoxUniformData {
|
||||||
|
position: (lookat.position.floor() + Vec3::splat(0.5)).to_array(),
|
||||||
|
_padding: 0,
|
||||||
|
}]),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
}
|
|
@ -6,7 +6,7 @@ use crate::{
|
||||||
prefabs::TexturePrefabs,
|
prefabs::TexturePrefabs,
|
||||||
world::{ChunkMeshStorage, ChunkStorage},
|
world::{ChunkMeshStorage, ChunkStorage},
|
||||||
};
|
};
|
||||||
use super::{camera::CameraUniformBuffer, depth::DepthTexture, RenderCtx};
|
use super::{camera_uniform::CameraUniformBuffer, depth::DepthTexture, RenderCtx};
|
||||||
|
|
||||||
mod pipeline;
|
mod pipeline;
|
||||||
mod vertex;
|
mod vertex;
|
||||||
|
@ -39,7 +39,7 @@ pub fn draw_world(
|
||||||
let camera = camera.iter().next().expect("No cameras in the scene");
|
let camera = camera.iter().next().expect("No cameras in the scene");
|
||||||
|
|
||||||
let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
let mut render_pass = ctx.encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
label: Some("draw_world"),
|
label: Some("rpass_draw_world"),
|
||||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
view: ctx.surface_view,
|
view: ctx.surface_view,
|
||||||
resolve_target: None,
|
resolve_target: None,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use shipyard::UniqueView;
|
use shipyard::UniqueView;
|
||||||
use crate::{
|
use crate::{
|
||||||
prefabs::TexturePrefabs,
|
prefabs::TexturePrefabs,
|
||||||
rendering::{camera::CameraUniformBuffer, depth::DepthTexture, world::ChunkVertex, Renderer}
|
rendering::{camera_uniform::CameraUniformBuffer, depth::DepthTexture, world::ChunkVertex, Renderer}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init_world_pipeline(
|
pub fn init_world_pipeline(
|
||||||
|
@ -33,7 +33,7 @@ pub fn init_world_pipeline(
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
format: ren.surface_config().format,
|
format: ren.surface_config().format,
|
||||||
blend: Some(wgpu::BlendState::REPLACE),
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
write_mask: wgpu::ColorWrites::COLOR,
|
||||||
})],
|
})],
|
||||||
}),
|
}),
|
||||||
vertex: wgpu::VertexState {
|
vertex: wgpu::VertexState {
|
||||||
|
|
Loading…
Reference in a new issue