Skip to content

Commit 63ba5d0

Browse files
committed
feat: show which block you're about to place
1 parent 868c717 commit 63ba5d0

File tree

11 files changed

+154
-148
lines changed

11 files changed

+154
-148
lines changed

src/blocks/block_type.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ pub enum BlockType {
7272
Sand,
7373
}
7474
impl BlockType {
75+
pub const MAX_ID: u32 = 6;
76+
7577
pub fn get_config(&self) -> BlockTypeConfigs {
7678
BlockTypeConfigs::get(*self)
7779
}

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ async fn run(event_loop: EventLoop<()>, window: Window) {
143143
};
144144
} else if let Event::DeviceEvent { event, .. } = event {
145145
match event {
146+
DeviceEvent::MouseWheel {
147+
delta: winit::event::MouseScrollDelta::LineDelta(_, deltay),
148+
} => {
149+
state.handle_wheel(deltay);
150+
}
146151
DeviceEvent::MouseMotion { delta } => {
147152
state.handle_mouse(&glam::vec2(delta.0 as f32, delta.1 as f32))
148153
}

src/pipelines/highlight_selected.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,9 @@ impl Pipeline for HighlightSelectedPipeline {
5858
fn update(
5959
&mut self,
6060
_pipeline_manager: &PipelineManager,
61-
player: Arc<RwLock<Player>>,
62-
queue: Arc<wgpu::Queue>,
63-
_device: Arc<wgpu::Device>,
61+
state: &State,
6462
) -> Result<(), Box<dyn std::error::Error>> {
65-
let player = player.read().unwrap();
63+
let player = state.player.read().unwrap();
6664
if let Some(block_ptr) = player.facing_block.as_ref() {
6765
let mut face_data = FaceDirections::all()
6866
.iter()
@@ -83,12 +81,12 @@ impl Pipeline for HighlightSelectedPipeline {
8381
})
8482
.collect::<Vec<_>>();
8583

86-
queue.write_buffer(
84+
state.queue.write_buffer(
8785
&self.selected_block_vertex_buffer,
8886
0,
8987
bytemuck::cast_slice(&block_positions),
9088
);
91-
queue.write_buffer(
89+
state.queue.write_buffer(
9290
&self.selected_block_index_buffer,
9391
0,
9492
bytemuck::cast_slice(&face_data.1),

src/pipelines/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ impl Pipeline for MainPipeline {
8585
fn update(
8686
&mut self,
8787
pipeline_manager: &PipelineManager,
88-
player: Arc<RwLock<Player>>,
89-
queue: Arc<wgpu::Queue>,
90-
device: Arc<wgpu::Device>,
88+
// player: Arc<RwLock<Player>>,
89+
state: &State, // queue: Arc<wgpu::Queue>,
90+
// device: Arc<wgpu::Device>,
91+
// surface_configuration: &wgpu::SurfaceConfiguration,
9192
) -> Result<(), Box<dyn std::error::Error>> {
9293
Ok(())
9394
}

src/pipelines/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ pub trait Pipeline {
1111
fn update(
1212
&mut self,
1313
pipeline_manager: &PipelineManager,
14-
player: Arc<RwLock<Player>>,
15-
queue: Arc<wgpu::Queue>,
16-
device: Arc<wgpu::Device>,
14+
state: &State,
15+
// player: Arc<RwLock<Player>>,
16+
// queue: Arc<wgpu::Queue>,
17+
// device: Arc<wgpu::Device>,
18+
// surface_config: &wgpu::SurfaceConfiguration,
1719
) -> Result<(), Box<dyn Error>>;
1820
fn render(
1921
&self,

src/pipelines/pipeline_manager.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,28 @@ impl PipelineManager {
4343
pipeline.ui_pipeline = Some(RefCell::new(UIPipeline::init(state, &pipeline)));
4444
return pipeline;
4545
}
46-
pub fn update(
47-
&mut self,
48-
player: Arc<RwLock<Player>>,
49-
queue: Arc<wgpu::Queue>,
50-
device: Arc<wgpu::Device>,
51-
) -> Result<(), Box<dyn std::error::Error>> {
52-
self.main_pipeline.as_ref().unwrap().borrow_mut().update(
53-
&self,
54-
player.clone(),
55-
queue.clone(),
56-
device.clone(),
57-
)?;
46+
47+
pub fn update(&self, state: &State) -> Result<(), Box<dyn std::error::Error>> {
48+
self.main_pipeline
49+
.as_ref()
50+
.unwrap()
51+
.borrow_mut()
52+
.update(&self, state)?;
5853
self.translucent_pipeline
5954
.as_ref()
6055
.unwrap()
6156
.borrow_mut()
62-
.update(&self, player.clone(), queue.clone(), device.clone())?;
57+
.update(&self, state)?;
6358
self.highlight_selected_pipeline
6459
.as_ref()
6560
.unwrap()
6661
.borrow_mut()
67-
.update(&self, player.clone(), queue.clone(), device.clone())?;
62+
.update(&self, state)?;
63+
self.ui_pipeline
64+
.as_ref()
65+
.unwrap()
66+
.borrow_mut()
67+
.update(&self, state)?;
6868

6969
return Ok(());
7070
}

src/pipelines/translucent.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ impl Pipeline for TranslucentPipeline {
2929
fn update(
3030
&mut self,
3131
_pipeline_manager: &PipelineManager,
32-
player: Arc<RwLock<Player>>,
33-
queue: Arc<wgpu::Queue>,
34-
_device: Arc<wgpu::Device>,
32+
state: &State,
33+
// player: Arc<RwLock<Player>>,
34+
// queue: Arc<wgpu::Queue>,
35+
// _device: Arc<wgpu::Device>,
36+
// _surface_config: &wgpu::SurfaceConfiguration,
3537
) -> Result<(), Box<dyn std::error::Error>> {
3638
Ok(())
3739
}

src/pipelines/ui.rs

Lines changed: 81 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::blocks::block::FaceDirections;
1+
use crate::blocks;
2+
use crate::blocks::block::{FaceDirections, TexturedBlock};
23
use crate::material::Texture;
34
use crate::pipeline::Uniforms;
45
use crate::player::Player;
@@ -14,9 +15,6 @@ use super::Pipeline;
1415

1516
pub struct UIPipeline {
1617
pub pipeline: wgpu::RenderPipeline,
17-
pub ui_bindgroup: wgpu::BindGroup,
18-
pub selected_blockid_buffer: wgpu::Buffer,
19-
pub resolution_buffer: wgpu::Buffer,
2018
pub screenspace_buffer: wgpu::Buffer,
2119
}
2220

@@ -58,7 +56,6 @@ impl Pipeline for UIPipeline {
5856
});
5957
rpass.set_pipeline(&self.pipeline);
6058
rpass.set_bind_group(0, &main_pipeline_ref.bind_group_0, &[]);
61-
rpass.set_bind_group(1, &self.ui_bindgroup, &[]);
6259
rpass.set_vertex_buffer(0, self.screenspace_buffer.slice(..));
6360
rpass.draw(0..6, 0..1);
6461
}
@@ -74,96 +71,34 @@ impl Pipeline for UIPipeline {
7471
source: wgpu::ShaderSource::Wgsl(shader_source.into()),
7572
});
7673

77-
let selected_blockid_buffer = state.device.create_buffer(&wgpu::BufferDescriptor {
78-
label: None,
79-
mapped_at_creation: false,
80-
size: std::mem::size_of::<u32>() as u64,
81-
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
82-
});
74+
let aspect_ratio = state.surface_config.height as f32 / state.surface_config.width as f32;
75+
76+
let player = state.player.read().unwrap();
77+
let block_type = player.placing_block;
78+
let tex_coords = block_type.get_texcoords(FaceDirections::Front);
79+
let screen_quad = Self::create_screen_quad(aspect_ratio, tex_coords);
8380

84-
let screen_quad: Vec<f32> = vec![
85-
-1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, -1.0,
86-
];
8781
let screenspace_buffer =
8882
state
8983
.device
9084
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
9185
contents: bytemuck::cast_slice(&screen_quad),
9286
label: Some("Screenspace rectangle"),
93-
usage: BufferUsages::VERTEX,
87+
usage: BufferUsages::VERTEX | BufferUsages::COPY_DST,
9488
});
9589

96-
let resolution = [
97-
state.surface_config.width as f32,
98-
state.surface_config.height as f32,
99-
];
100-
let resolution_buffer =
101-
state
102-
.device
103-
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
104-
contents: bytemuck::cast_slice(&[resolution]),
105-
label: None,
106-
usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST,
107-
});
108-
109-
let ui_bindgroup_layout =
110-
state
111-
.device
112-
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
113-
label: None,
114-
entries: &[
115-
wgpu::BindGroupLayoutEntry {
116-
binding: 0,
117-
visibility: wgpu::ShaderStages::FRAGMENT,
118-
ty: wgpu::BindingType::Buffer {
119-
ty: wgpu::BufferBindingType::Uniform,
120-
has_dynamic_offset: false,
121-
min_binding_size: None,
122-
},
123-
count: None,
124-
},
125-
wgpu::BindGroupLayoutEntry {
126-
binding: 1,
127-
visibility: wgpu::ShaderStages::FRAGMENT,
128-
ty: wgpu::BindingType::Buffer {
129-
ty: wgpu::BufferBindingType::Uniform,
130-
has_dynamic_offset: false,
131-
min_binding_size: None,
132-
},
133-
count: None,
134-
},
135-
],
136-
});
137-
138-
let ui_bindgroup = state.device.create_bind_group(&wgpu::BindGroupDescriptor {
139-
layout: &ui_bindgroup_layout,
140-
label: None,
141-
entries: &[
142-
wgpu::BindGroupEntry {
143-
binding: 0,
144-
resource: resolution_buffer.as_entire_binding(),
145-
},
146-
wgpu::BindGroupEntry {
147-
binding: 1,
148-
resource: selected_blockid_buffer.as_entire_binding(),
149-
},
150-
],
151-
});
15290
// Pipeline layouts
15391
let pipeline_layout =
15492
state
15593
.device
15694
.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
15795
label: None,
158-
bind_group_layouts: &[
159-
&pipeline_manager
160-
.main_pipeline
161-
.as_ref()
162-
.unwrap()
163-
.borrow()
164-
.bind_group_0_layout,
165-
&ui_bindgroup_layout,
166-
],
96+
bind_group_layouts: &[&pipeline_manager
97+
.main_pipeline
98+
.as_ref()
99+
.unwrap()
100+
.borrow()
101+
.bind_group_0_layout],
167102
push_constant_ranges: &[],
168103
});
169104

@@ -207,19 +142,34 @@ impl Pipeline for UIPipeline {
207142
Self {
208143
screenspace_buffer,
209144
pipeline: render_pipeline,
210-
resolution_buffer,
211-
selected_blockid_buffer,
212-
ui_bindgroup,
213145
}
214146
}
215147
fn update(
216148
&mut self,
217149
pipeline_manager: &PipelineManager,
218-
player: Arc<RwLock<Player>>,
219-
queue: Arc<wgpu::Queue>,
220-
device: Arc<wgpu::Device>,
150+
state: &State, // player: Arc<RwLock<Player>>,
151+
// queue: Arc<wgpu::Queue>,
152+
// device: Arc<wgpu::Device>,
153+
// surface_config: &wgpu::SurfaceConfiguration,
221154
) -> Result<(), Box<dyn std::error::Error>> {
222-
todo!();
155+
let aspect_ratio = state.surface_config.height as f32 / state.surface_config.width as f32;
156+
let player = state.player.read().unwrap();
157+
let block_type = player.placing_block;
158+
let tex_coords = block_type.get_texcoords(FaceDirections::Front);
159+
let screen_quad = Self::create_screen_quad(aspect_ratio, tex_coords);
160+
state.queue.write_buffer(
161+
&self.screenspace_buffer,
162+
0,
163+
bytemuck::cast_slice(&screen_quad),
164+
);
165+
Ok(())
166+
167+
// let aspect_ratio = state.surface_config.height as f32 / state.surface_config.width as f32;
168+
169+
// let player = state.player.read().unwrap();
170+
// let block_type = player.placing_block;
171+
// let tex_coords = block_type.get_texcoords(FaceDirections::Front);
172+
// let screen_quad = Self::create_screen_quad(aspect_ratio, tex_coords);
223173
// let player = state.player.read().unwrap();
224174
// if let Some(block_ptr) = player.facing_block.as_ref() {
225175
// let block = block_ptr.read().unwrap();
@@ -261,15 +211,53 @@ impl Pipeline for UIPipeline {
261211
}
262212
}
263213
impl UIPipeline {
214+
// Creates the rectangle coords for displaying the block that would be placed if something is placed.
215+
fn create_screen_quad(aspect_ratio: f32, tex_coords: [[f32; 2]; 4]) -> Vec<f32> {
216+
vec![
217+
-0.9 * aspect_ratio,
218+
-0.9,
219+
tex_coords[0][0],
220+
tex_coords[0][1],
221+
-0.9 * aspect_ratio,
222+
-0.6,
223+
tex_coords[1][0],
224+
tex_coords[1][1],
225+
-0.6 * aspect_ratio,
226+
-0.6,
227+
tex_coords[2][0],
228+
tex_coords[2][1],
229+
-0.9 * aspect_ratio,
230+
-0.9,
231+
tex_coords[0][0],
232+
tex_coords[0][1],
233+
-0.6 * aspect_ratio,
234+
-0.6,
235+
tex_coords[2][0],
236+
tex_coords[2][1],
237+
-0.6 * aspect_ratio,
238+
-0.9,
239+
tex_coords[3][0],
240+
tex_coords[3][1],
241+
]
242+
}
264243
fn get_vertex_data_layout() -> wgpu::VertexBufferLayout<'static> {
265244
wgpu::VertexBufferLayout {
266-
array_stride: std::mem::size_of::<[f32; 2]>() as wgpu::BufferAddress,
245+
array_stride: std::mem::size_of::<[f32; 4]>() as wgpu::BufferAddress,
267246
step_mode: wgpu::VertexStepMode::Vertex,
268-
attributes: &[wgpu::VertexAttribute {
269-
format: wgpu::VertexFormat::Float32x2,
270-
offset: 0,
271-
shader_location: 0,
272-
}],
247+
attributes: &[
248+
// Position
249+
wgpu::VertexAttribute {
250+
format: wgpu::VertexFormat::Float32x2,
251+
offset: 0,
252+
shader_location: 0,
253+
},
254+
// Uv
255+
wgpu::VertexAttribute {
256+
format: wgpu::VertexFormat::Float32x2,
257+
offset: std::mem::size_of::<[f32; 2]>() as u64,
258+
shader_location: 1,
259+
},
260+
],
273261
}
274262
}
275263
}

0 commit comments

Comments
 (0)