1
- use crate :: blocks:: block:: FaceDirections ;
1
+ use crate :: blocks;
2
+ use crate :: blocks:: block:: { FaceDirections , TexturedBlock } ;
2
3
use crate :: material:: Texture ;
3
4
use crate :: pipeline:: Uniforms ;
4
5
use crate :: player:: Player ;
@@ -14,9 +15,6 @@ use super::Pipeline;
14
15
15
16
pub struct UIPipeline {
16
17
pub pipeline : wgpu:: RenderPipeline ,
17
- pub ui_bindgroup : wgpu:: BindGroup ,
18
- pub selected_blockid_buffer : wgpu:: Buffer ,
19
- pub resolution_buffer : wgpu:: Buffer ,
20
18
pub screenspace_buffer : wgpu:: Buffer ,
21
19
}
22
20
@@ -58,7 +56,6 @@ impl Pipeline for UIPipeline {
58
56
} ) ;
59
57
rpass. set_pipeline ( & self . pipeline ) ;
60
58
rpass. set_bind_group ( 0 , & main_pipeline_ref. bind_group_0 , & [ ] ) ;
61
- rpass. set_bind_group ( 1 , & self . ui_bindgroup , & [ ] ) ;
62
59
rpass. set_vertex_buffer ( 0 , self . screenspace_buffer . slice ( ..) ) ;
63
60
rpass. draw ( 0 ..6 , 0 ..1 ) ;
64
61
}
@@ -74,96 +71,34 @@ impl Pipeline for UIPipeline {
74
71
source : wgpu:: ShaderSource :: Wgsl ( shader_source. into ( ) ) ,
75
72
} ) ;
76
73
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 ) ;
83
80
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
- ] ;
87
81
let screenspace_buffer =
88
82
state
89
83
. device
90
84
. create_buffer_init ( & wgpu:: util:: BufferInitDescriptor {
91
85
contents : bytemuck:: cast_slice ( & screen_quad) ,
92
86
label : Some ( "Screenspace rectangle" ) ,
93
- usage : BufferUsages :: VERTEX ,
87
+ usage : BufferUsages :: VERTEX | BufferUsages :: COPY_DST ,
94
88
} ) ;
95
89
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
- } ) ;
152
90
// Pipeline layouts
153
91
let pipeline_layout =
154
92
state
155
93
. device
156
94
. create_pipeline_layout ( & wgpu:: PipelineLayoutDescriptor {
157
95
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 ] ,
167
102
push_constant_ranges : & [ ] ,
168
103
} ) ;
169
104
@@ -207,19 +142,34 @@ impl Pipeline for UIPipeline {
207
142
Self {
208
143
screenspace_buffer,
209
144
pipeline : render_pipeline,
210
- resolution_buffer,
211
- selected_blockid_buffer,
212
- ui_bindgroup,
213
145
}
214
146
}
215
147
fn update (
216
148
& mut self ,
217
149
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,
221
154
) -> 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);
223
173
// let player = state.player.read().unwrap();
224
174
// if let Some(block_ptr) = player.facing_block.as_ref() {
225
175
// let block = block_ptr.read().unwrap();
@@ -261,15 +211,53 @@ impl Pipeline for UIPipeline {
261
211
}
262
212
}
263
213
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
+ }
264
243
fn get_vertex_data_layout ( ) -> wgpu:: VertexBufferLayout < ' static > {
265
244
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 ,
267
246
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
+ ] ,
273
261
}
274
262
}
275
263
}
0 commit comments