diff --git a/src/hud/hotbar_hud.rs b/src/hud/hotbar_hud.rs index 6a1f337..b959ff6 100644 --- a/src/hud/hotbar_hud.rs +++ b/src/hud/hotbar_hud.rs @@ -70,26 +70,108 @@ impl HotbarHud { let mut vertices = Vec::new(); let mut indices = Vec::new(); - let mut index = 0; - for cursor_index in 0..9 { - if let Some(block) = self.blocks[cursor_index as usize] { - let x = (-92 + 20 * cursor_index as i32) as f32; - let texture_index = block.texture_indices().2.try_into().unwrap(); + let mut index_offset = 0; + for slot in 0..9 { + if let Some(block) = self.blocks[slot as usize] { + let x = (-92 + 20 * slot as i32) as f32; + let texture_indices = block.texture_indices(); - #[rustfmt::skip] vertices.extend(&[ - HudVertex { position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 18.0], texture_coordinates: [0.0, 0.0], texture_index }, - HudVertex { position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 18.0], texture_coordinates: [1.0, 0.0], texture_index }, - HudVertex { position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 4.0], texture_coordinates: [1.0, 1.0], texture_index }, - HudVertex { position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 4.0], texture_coordinates: [0.0, 1.0], texture_index }, + // Left face + HudVertex { + position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 3.5], + texture_coordinates: [1.0, 1.0], + texture_index: texture_indices.0 as i32, + value: 0.5, + }, + HudVertex { + position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 6.5], + texture_coordinates: [0.0, 1.0], + texture_index: texture_indices.0 as i32, + value: 0.5, + }, + HudVertex { + position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 15.5], + texture_coordinates: [0.0, 0.0], + texture_index: texture_indices.0 as i32, + value: 0.5, + }, + HudVertex { + position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5], + texture_coordinates: [1.0, 0.0], + texture_index: texture_indices.0 as i32, + value: 0.5, + }, + + // Front face + HudVertex { + position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 15.5], + texture_coordinates: [1.0, 0.0], + texture_index: texture_indices.3 as i32, + value: 0.15, + }, + HudVertex { + position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5], + texture_coordinates: [0.0, 0.0], + texture_index: texture_indices.3 as i32, + value: 0.15, + }, + HudVertex { + position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 3.5], + texture_coordinates: [0.0, 1.0], + texture_index: texture_indices.3 as i32, + value: 0.15, + }, + HudVertex { + position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 6.5], + texture_coordinates: [1.0, 1.0], + texture_index: texture_indices.3 as i32, + value: 0.15, + }, + + // Top face + HudVertex { + position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 15.5], + texture_coordinates: [1.0, 0.0], + texture_index: texture_indices.5 as i32, + value: 1.0, + }, + HudVertex { + position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 18.5], + texture_coordinates: [0.0, 0.0], + texture_index: texture_indices.5 as i32, + value: 1.0, + }, + HudVertex { + position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 15.5], + texture_coordinates: [0.0, 1.0], + texture_index: texture_indices.5 as i32, + value: 1.0, + }, + HudVertex { + position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5], + texture_coordinates: [1.0, 1.0], + texture_index: texture_indices.5 as i32, + value: 1.0, + }, ]); #[rustfmt::skip] indices.extend(&[ - index, 2 + index, 1 + index, - index, 3 + index, 2 + index, + // Left face + 2 + index_offset, index_offset, 1 + index_offset, + 3 + index_offset, index_offset, 2 + index_offset, + + // Right face + 6 + index_offset, 4 + index_offset, 5 + index_offset, + 7 + index_offset, 4 + index_offset, 6 + index_offset, + + // Top face + 10 + index_offset, 8 + index_offset, 9 + index_offset, + 11 + index_offset, 8 + index_offset, 10 + index_offset, ]); - index += 4; + + index_offset += 12; } } diff --git a/src/hud/widgets_hud.rs b/src/hud/widgets_hud.rs index 834b92c..39e9c44 100644 --- a/src/hud/widgets_hud.rs +++ b/src/hud/widgets_hud.rs @@ -112,13 +112,14 @@ impl WidgetsHud { pub fn redraw_hotbar_cursor(&self, render_context: &RenderContext) { let x = (-92 + 20 * self.hotbar_cursor_position as i32) as f32; let texture_index = 0; + let value = 1.0; #[rustfmt::skip] let vertices = [ - HudVertex { position: [UI_SCALE_X * (x ), -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 0.0 / 256.0, 22.0 / 256.0], texture_index }, - HudVertex { position: [UI_SCALE_X * (x + 24.0), -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 24.0 / 256.0, 22.0 / 256.0], texture_index }, - HudVertex { position: [UI_SCALE_X * (x + 24.0), -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 24.0 / 256.0, 46.0 / 256.0], texture_index }, - HudVertex { position: [UI_SCALE_X * (x ), -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 0.0 / 256.0, 46.0 / 256.0], texture_index }, + HudVertex { position: [UI_SCALE_X * (x ), -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 0.0 / 256.0, 22.0 / 256.0], texture_index, value }, + HudVertex { position: [UI_SCALE_X * (x + 24.0), -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 24.0 / 256.0, 22.0 / 256.0], texture_index, value }, + HudVertex { position: [UI_SCALE_X * (x + 24.0), -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 24.0 / 256.0, 46.0 / 256.0], texture_index, value }, + HudVertex { position: [UI_SCALE_X * (x ), -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 0.0 / 256.0, 46.0 / 256.0], texture_index, value }, ]; render_context.queue.write_buffer( @@ -142,22 +143,22 @@ impl WidgetsHud { #[rustfmt::skip] pub const VERTICES: [HudVertex; 12] = [ // Crosshair - HudVertex { position: [UI_SCALE_X * -8.0, UI_SCALE_Y * 8.0], texture_coordinates: [240.0 / 256.0, 0.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * 8.0, UI_SCALE_Y * 8.0], texture_coordinates: [ 1.0, 0.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * 8.0, UI_SCALE_Y * -8.0], texture_coordinates: [ 1.0, 16.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * -8.0, UI_SCALE_Y * -8.0], texture_coordinates: [240.0 / 256.0, 16.0 / 256.0], texture_index: 0 }, + HudVertex { position: [UI_SCALE_X * -8.0, UI_SCALE_Y * 8.0], texture_coordinates: [240.0 / 256.0, 0.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * 8.0, UI_SCALE_Y * 8.0], texture_coordinates: [ 1.0, 0.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * 8.0, UI_SCALE_Y * -8.0], texture_coordinates: [ 1.0, 16.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * -8.0, UI_SCALE_Y * -8.0], texture_coordinates: [240.0 / 256.0, 16.0 / 256.0], texture_index: 0, value: 1.0 }, // Hotbar - HudVertex { position: [UI_SCALE_X * -91.0, -1.0 + UI_SCALE_Y * 22.0], texture_coordinates: [ 0.0 / 256.0, 0.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * 91.0, -1.0 + UI_SCALE_Y * 22.0], texture_coordinates: [182.0 / 256.0, 0.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * 91.0, -1.0 ], texture_coordinates: [182.0 / 256.0, 22.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * -91.0, -1.0 ], texture_coordinates: [ 0.0 / 256.0, 22.0 / 256.0], texture_index: 0 }, + HudVertex { position: [UI_SCALE_X * -91.0, -1.0 + UI_SCALE_Y * 22.0], texture_coordinates: [ 0.0 / 256.0, 0.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * 91.0, -1.0 + UI_SCALE_Y * 22.0], texture_coordinates: [182.0 / 256.0, 0.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * 91.0, -1.0 ], texture_coordinates: [182.0 / 256.0, 22.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * -91.0, -1.0 ], texture_coordinates: [ 0.0 / 256.0, 22.0 / 256.0], texture_index: 0, value: 1.0 }, // Hotbar cursor - HudVertex { position: [UI_SCALE_X * -92.0, -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 0.0 / 256.0, 22.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * -68.0, -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 24.0 / 256.0, 22.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * -68.0, -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 24.0 / 256.0, 46.0 / 256.0], texture_index: 0 }, - HudVertex { position: [UI_SCALE_X * -92.0, -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 0.0 / 256.0, 46.0 / 256.0], texture_index: 0 }, + HudVertex { position: [UI_SCALE_X * -92.0, -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 0.0 / 256.0, 22.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * -68.0, -1.0 + UI_SCALE_Y * 23.0], texture_coordinates: [ 24.0 / 256.0, 22.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * -68.0, -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 24.0 / 256.0, 46.0 / 256.0], texture_index: 0, value: 1.0 }, + HudVertex { position: [UI_SCALE_X * -92.0, -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 0.0 / 256.0, 46.0 / 256.0], texture_index: 0, value: 1.0 }, ]; #[rustfmt::skip] diff --git a/src/shaders/ui.wgsl b/src/shaders/ui.wgsl index 40f2e9f..df3da54 100644 --- a/src/shaders/ui.wgsl +++ b/src/shaders/ui.wgsl @@ -2,12 +2,14 @@ struct VertexInput { [[location(0)]] position: vec2; [[location(1)]] texture_coordinates: vec2; [[location(2)]] texture_index: i32; + [[location(3)]] value: f32; }; struct VertexOutput { [[builtin(position)]] clip_position: vec4; [[location(0)]] texture_coordinates: vec2; [[location(1)]] texture_index: i32; + [[location(2)]] value: f32; }; [[stage(vertex)]] @@ -16,6 +18,7 @@ fn main(model: VertexInput) -> VertexOutput { out.texture_coordinates = model.texture_coordinates; out.clip_position = vec4(model.position, 0.0, 1.0); out.texture_index = model.texture_index; + out.value = model.value; return out; } @@ -24,5 +27,6 @@ fn main(model: VertexInput) -> VertexOutput { [[stage(fragment)]] fn main(in: VertexOutput) -> [[location(0)]] vec4 { - return textureSample(texture, sampler, in.texture_coordinates, in.texture_index); + return textureSample(texture, sampler, in.texture_coordinates, in.texture_index) + * vec4(vec3(in.value), 1.0); } diff --git a/src/text_renderer.rs b/src/text_renderer.rs index 86893b0..3f6d07e 100644 --- a/src/text_renderer.rs +++ b/src/text_renderer.rs @@ -107,10 +107,10 @@ impl TextRenderer { #[rustfmt::skip] let vertices = [ - HudVertex { position: [x, y ], texture_coordinates: [tx, ty ], texture_index: 0 }, - HudVertex { position: [x + DX, y ], texture_coordinates: [tx + s, ty ], texture_index: 0 }, - HudVertex { position: [x + DX, y - DY], texture_coordinates: [tx + s, ty + s], texture_index: 0 }, - HudVertex { position: [x, y - DY], texture_coordinates: [tx, ty + s], texture_index: 0 }, + HudVertex { position: [x, y ], texture_coordinates: [tx, ty ], texture_index: 0, value: 1.0 }, + HudVertex { position: [x + DX, y ], texture_coordinates: [tx + s, ty ], texture_index: 0, value: 1.0 }, + HudVertex { position: [x + DX, y - DY], texture_coordinates: [tx + s, ty + s], texture_index: 0, value: 1.0 }, + HudVertex { position: [x, y - DY], texture_coordinates: [tx, ty + s], texture_index: 0, value: 1.0 }, ]; #[rustfmt::skip] diff --git a/src/vertex.rs b/src/vertex.rs index 5bb7992..bf09592 100644 --- a/src/vertex.rs +++ b/src/vertex.rs @@ -30,21 +30,25 @@ impl Vertex for PlainVertex { } } -/// Vertex used to represent HUD vertices. +/// Represents a vertex in HUD geometry. /// -/// A vertex with a 2D position and no normal, for representing UI elements. +/// Used to bind vertex information to shaders with a 2D position, texture +/// coordinates and index (for texture arrays) and a value (for dimming e.g. +/// the sides on blocks in inventories) #[repr(C)] #[derive(Copy, Clone, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)] pub struct HudVertex { pub position: [f32; 2], pub texture_coordinates: [f32; 2], pub texture_index: i32, + pub value: f32, } const HUD_VERTEX_ATTRIBUTES: &[VertexAttribute] = &wgpu::vertex_attr_array![ 0 => Float32x2, 1 => Float32x2, 2 => Sint32, + 3 => Float32, ]; impl Vertex for HudVertex { @@ -57,7 +61,7 @@ impl Vertex for HudVertex { } } -/// Vertex used to represent block vertices. +/// Represents a vertex in world geometry. /// /// Aside from the usual vertex position, texture coordinates and normal, this "vertex" also /// contains whether the block is highlighted (i.e. the player is pointing at the block) and its