Implement block coloring
This commit is contained in:
parent
5252da6ccb
commit
669d2552e8
10 changed files with 97 additions and 72 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
use cgmath::{ElementWise, Vector4};
|
||||||
use wgpu::{BufferUsage, RenderPass};
|
use wgpu::{BufferUsage, RenderPass};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -27,7 +28,7 @@ impl HotbarHud {
|
||||||
Some(BlockType::Cobblestone),
|
Some(BlockType::Cobblestone),
|
||||||
Some(BlockType::OakPlanks),
|
Some(BlockType::OakPlanks),
|
||||||
Some(BlockType::OakLog),
|
Some(BlockType::OakLog),
|
||||||
None,
|
Some(BlockType::OakLeaves),
|
||||||
];
|
];
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -73,6 +74,11 @@ impl HotbarHud {
|
||||||
if let Some(block) = self.blocks[slot as usize] {
|
if let Some(block) = self.blocks[slot as usize] {
|
||||||
let x = (-92 + 20 * slot as i32) as f32;
|
let x = (-92 + 20 * slot as i32) as f32;
|
||||||
let texture_indices = block.texture_indices();
|
let texture_indices = block.texture_indices();
|
||||||
|
let color = block.color();
|
||||||
|
|
||||||
|
let color_left = color.mul_element_wise(Vector4::new(0.5, 0.5, 0.5, 1.0)).into();
|
||||||
|
let color_front = color.mul_element_wise(Vector4::new(0.15, 0.15, 0.15, 1.0)).into();
|
||||||
|
let color_top = color.into();
|
||||||
|
|
||||||
vertices.extend(&[
|
vertices.extend(&[
|
||||||
// Left face
|
// Left face
|
||||||
|
@ -80,25 +86,25 @@ impl HotbarHud {
|
||||||
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 3.5],
|
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 3.5],
|
||||||
texture_coordinates: [1.0, 1.0],
|
texture_coordinates: [1.0, 1.0],
|
||||||
texture_index: texture_indices.0 as i32,
|
texture_index: texture_indices.0 as i32,
|
||||||
value: 0.5,
|
color: color_left,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 6.5],
|
position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 6.5],
|
||||||
texture_coordinates: [0.0, 1.0],
|
texture_coordinates: [0.0, 1.0],
|
||||||
texture_index: texture_indices.0 as i32,
|
texture_index: texture_indices.0 as i32,
|
||||||
value: 0.5,
|
color: color_left,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 15.5],
|
position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 15.5],
|
||||||
texture_coordinates: [0.0, 0.0],
|
texture_coordinates: [0.0, 0.0],
|
||||||
texture_index: texture_indices.0 as i32,
|
texture_index: texture_indices.0 as i32,
|
||||||
value: 0.5,
|
color: color_left,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5],
|
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5],
|
||||||
texture_coordinates: [1.0, 0.0],
|
texture_coordinates: [1.0, 0.0],
|
||||||
texture_index: texture_indices.0 as i32,
|
texture_index: texture_indices.0 as i32,
|
||||||
value: 0.5,
|
color: color_left,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Front face
|
// Front face
|
||||||
|
@ -106,25 +112,25 @@ impl HotbarHud {
|
||||||
position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 15.5],
|
position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 15.5],
|
||||||
texture_coordinates: [1.0, 0.0],
|
texture_coordinates: [1.0, 0.0],
|
||||||
texture_index: texture_indices.3 as i32,
|
texture_index: texture_indices.3 as i32,
|
||||||
value: 0.15,
|
color: color_front,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5],
|
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5],
|
||||||
texture_coordinates: [0.0, 0.0],
|
texture_coordinates: [0.0, 0.0],
|
||||||
texture_index: texture_indices.3 as i32,
|
texture_index: texture_indices.3 as i32,
|
||||||
value: 0.15,
|
color: color_front,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 3.5],
|
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 3.5],
|
||||||
texture_coordinates: [0.0, 1.0],
|
texture_coordinates: [0.0, 1.0],
|
||||||
texture_index: texture_indices.3 as i32,
|
texture_index: texture_indices.3 as i32,
|
||||||
value: 0.15,
|
color: color_front,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 6.5],
|
position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 6.5],
|
||||||
texture_coordinates: [1.0, 1.0],
|
texture_coordinates: [1.0, 1.0],
|
||||||
texture_index: texture_indices.3 as i32,
|
texture_index: texture_indices.3 as i32,
|
||||||
value: 0.15,
|
color: color_front,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Top face
|
// Top face
|
||||||
|
@ -132,25 +138,25 @@ impl HotbarHud {
|
||||||
position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 15.5],
|
position: [UI_SCALE_X * (x + 19.0), -1.0 + UI_SCALE_Y * 15.5],
|
||||||
texture_coordinates: [1.0, 0.0],
|
texture_coordinates: [1.0, 0.0],
|
||||||
texture_index: texture_indices.5 as i32,
|
texture_index: texture_indices.5 as i32,
|
||||||
value: 1.0,
|
color: color_top,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 18.5],
|
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 18.5],
|
||||||
texture_coordinates: [0.0, 0.0],
|
texture_coordinates: [0.0, 0.0],
|
||||||
texture_index: texture_indices.5 as i32,
|
texture_index: texture_indices.5 as i32,
|
||||||
value: 1.0,
|
color: color_top,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 15.5],
|
position: [UI_SCALE_X * (x + 5.0), -1.0 + UI_SCALE_Y * 15.5],
|
||||||
texture_coordinates: [0.0, 1.0],
|
texture_coordinates: [0.0, 1.0],
|
||||||
texture_index: texture_indices.5 as i32,
|
texture_index: texture_indices.5 as i32,
|
||||||
value: 1.0,
|
color: color_top,
|
||||||
},
|
},
|
||||||
HudVertex {
|
HudVertex {
|
||||||
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5],
|
position: [UI_SCALE_X * (x + 12.0), -1.0 + UI_SCALE_Y * 12.5],
|
||||||
texture_coordinates: [1.0, 1.0],
|
texture_coordinates: [1.0, 1.0],
|
||||||
texture_index: texture_indices.5 as i32,
|
texture_index: texture_indices.5 as i32,
|
||||||
value: 1.0,
|
color: color_top,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
@ -112,14 +112,14 @@ impl WidgetsHud {
|
||||||
pub fn redraw_hotbar_cursor(&self, render_context: &RenderContext) {
|
pub fn redraw_hotbar_cursor(&self, render_context: &RenderContext) {
|
||||||
let x = (-92 + 20 * self.hotbar_cursor_position as i32) as f32;
|
let x = (-92 + 20 * self.hotbar_cursor_position as i32) as f32;
|
||||||
let texture_index = 0;
|
let texture_index = 0;
|
||||||
let value = 1.0;
|
let color = [1.0; 4];
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
let vertices = [
|
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, value },
|
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, color },
|
||||||
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 * 23.0], texture_coordinates: [ 24.0 / 256.0, 22.0 / 256.0], texture_index, color },
|
||||||
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 + 24.0), -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 24.0 / 256.0, 46.0 / 256.0], texture_index, color },
|
||||||
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 },
|
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, color },
|
||||||
];
|
];
|
||||||
|
|
||||||
render_context.queue.write_buffer(
|
render_context.queue.write_buffer(
|
||||||
|
@ -143,22 +143,22 @@ impl WidgetsHud {
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub const VERTICES: [HudVertex; 12] = [
|
pub const VERTICES: [HudVertex; 12] = [
|
||||||
// Crosshair
|
// 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, value: 1.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, color: [1.0; 4] },
|
||||||
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, 0.0 / 256.0], texture_index: 0, color: [1.0; 4] },
|
||||||
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: [ 1.0, 16.0 / 256.0], texture_index: 0, color: [1.0; 4] },
|
||||||
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 },
|
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, color: [1.0; 4] },
|
||||||
|
|
||||||
// Hotbar
|
// 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, value: 1.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, color: [1.0; 4] },
|
||||||
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 + UI_SCALE_Y * 22.0], texture_coordinates: [182.0 / 256.0, 0.0 / 256.0], texture_index: 0, color: [1.0; 4] },
|
||||||
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: [182.0 / 256.0, 22.0 / 256.0], texture_index: 0, color: [1.0; 4] },
|
||||||
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 },
|
HudVertex { position: [UI_SCALE_X * -91.0, -1.0 ], texture_coordinates: [ 0.0 / 256.0, 22.0 / 256.0], texture_index: 0, color: [1.0; 4] },
|
||||||
|
|
||||||
// Hotbar cursor
|
// 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, value: 1.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, color: [1.0; 4] },
|
||||||
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 * 23.0], texture_coordinates: [ 24.0 / 256.0, 22.0 / 256.0], texture_index: 0, color: [1.0; 4] },
|
||||||
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 * -68.0, -1.0 + UI_SCALE_Y * -1.0], texture_coordinates: [ 24.0 / 256.0, 46.0 / 256.0], texture_index: 0, color: [1.0; 4] },
|
||||||
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 },
|
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, color: [1.0; 4] },
|
||||||
];
|
];
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
|
|
|
@ -2,14 +2,14 @@ struct VertexInput {
|
||||||
[[location(0)]] position: vec2<f32>;
|
[[location(0)]] position: vec2<f32>;
|
||||||
[[location(1)]] texture_coordinates: vec2<f32>;
|
[[location(1)]] texture_coordinates: vec2<f32>;
|
||||||
[[location(2)]] texture_index: i32;
|
[[location(2)]] texture_index: i32;
|
||||||
[[location(3)]] value: f32;
|
[[location(3)]] color: vec4<f32>;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VertexOutput {
|
struct VertexOutput {
|
||||||
[[builtin(position)]] clip_position: vec4<f32>;
|
[[builtin(position)]] clip_position: vec4<f32>;
|
||||||
[[location(0)]] texture_coordinates: vec2<f32>;
|
[[location(0)]] texture_coordinates: vec2<f32>;
|
||||||
[[location(1)]] texture_index: i32;
|
[[location(1)]] texture_index: i32;
|
||||||
[[location(2)]] value: f32;
|
[[location(2)]] color: vec4<f32>;
|
||||||
};
|
};
|
||||||
|
|
||||||
[[stage(vertex)]]
|
[[stage(vertex)]]
|
||||||
|
@ -18,7 +18,7 @@ fn main(model: VertexInput) -> VertexOutput {
|
||||||
out.texture_coordinates = model.texture_coordinates;
|
out.texture_coordinates = model.texture_coordinates;
|
||||||
out.clip_position = vec4<f32>(model.position, 0.0, 1.0);
|
out.clip_position = vec4<f32>(model.position, 0.0, 1.0);
|
||||||
out.texture_index = model.texture_index;
|
out.texture_index = model.texture_index;
|
||||||
out.value = model.value;
|
out.color = model.color;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,5 +28,5 @@ fn main(model: VertexInput) -> VertexOutput {
|
||||||
[[stage(fragment)]]
|
[[stage(fragment)]]
|
||||||
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||||
return textureSample(texture, sampler, in.texture_coordinates, in.texture_index)
|
return textureSample(texture, sampler, in.texture_coordinates, in.texture_index)
|
||||||
* vec4<f32>(vec3<f32>(in.value), 1.0);
|
* in.color;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ struct VertexInput {
|
||||||
[[location(2)]] normal: vec3<f32>;
|
[[location(2)]] normal: vec3<f32>;
|
||||||
[[location(3)]] highlighted: i32;
|
[[location(3)]] highlighted: i32;
|
||||||
[[location(4)]] texture_id: i32;
|
[[location(4)]] texture_id: i32;
|
||||||
|
[[location(5)]] color: vec4<f32>;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VertexOutput {
|
struct VertexOutput {
|
||||||
|
@ -30,6 +31,7 @@ struct VertexOutput {
|
||||||
[[location(2)]] world_position: vec3<f32>;
|
[[location(2)]] world_position: vec3<f32>;
|
||||||
[[location(3)]] highlighted: i32;
|
[[location(3)]] highlighted: i32;
|
||||||
[[location(4)]] texture_id: i32;
|
[[location(4)]] texture_id: i32;
|
||||||
|
[[location(5)]] color: vec4<f32>;
|
||||||
};
|
};
|
||||||
|
|
||||||
let pi: f32 = 3.14159265359;
|
let pi: f32 = 3.14159265359;
|
||||||
|
@ -53,6 +55,7 @@ fn main(model: VertexInput) -> VertexOutput {
|
||||||
|
|
||||||
out.clip_position = view.projection * vec4<f32>(out.world_position, 1.0);
|
out.clip_position = view.projection * vec4<f32>(out.world_position, 1.0);
|
||||||
out.highlighted = model.highlighted;
|
out.highlighted = model.highlighted;
|
||||||
|
out.color = model.color;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +69,7 @@ fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||||
texture_sampler,
|
texture_sampler,
|
||||||
in.texture_coordinates,
|
in.texture_coordinates,
|
||||||
in.texture_id
|
in.texture_id
|
||||||
);
|
) * in.color;
|
||||||
|
|
||||||
let light_position = vec3<f32>(-100.0, 500.0, -200.0);
|
let light_position = vec3<f32>(-100.0, 500.0, -200.0);
|
||||||
let light_color = vec3<f32>(1.0, 1.0, 1.0);
|
let light_color = vec3<f32>(1.0, 1.0, 1.0);
|
||||||
|
|
|
@ -107,10 +107,10 @@ impl TextRenderer {
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
let vertices = [
|
let vertices = [
|
||||||
HudVertex { position: [x, y ], texture_coordinates: [tx, ty ], texture_index: 0, value: 1.0 },
|
HudVertex { position: [x, y ], texture_coordinates: [tx, ty ], texture_index: 0, color: [1.0; 4]},
|
||||||
HudVertex { position: [x + DX, y ], texture_coordinates: [tx + s, ty ], texture_index: 0, value: 1.0 },
|
HudVertex { position: [x + DX, y ], texture_coordinates: [tx + s, ty ], texture_index: 0, color: [1.0; 4]},
|
||||||
HudVertex { position: [x + DX, y - DY], texture_coordinates: [tx + s, ty + s], texture_index: 0, value: 1.0 },
|
HudVertex { position: [x + DX, y - DY], texture_coordinates: [tx + s, ty + s], texture_index: 0, color: [1.0; 4]},
|
||||||
HudVertex { position: [x, y - DY], texture_coordinates: [tx, ty + s], texture_index: 0, value: 1.0 },
|
HudVertex { position: [x, y - DY], texture_coordinates: [tx, ty + s], texture_index: 0, color: [1.0; 4]},
|
||||||
];
|
];
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
|
|
|
@ -114,7 +114,7 @@ impl Texture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const TEXTURE_COUNT: usize = 43;
|
pub const TEXTURE_COUNT: usize = 44;
|
||||||
|
|
||||||
pub struct TextureManager {
|
pub struct TextureManager {
|
||||||
pub bind_group_layout: wgpu::BindGroupLayout,
|
pub bind_group_layout: wgpu::BindGroupLayout,
|
||||||
|
@ -189,6 +189,7 @@ impl TextureManager {
|
||||||
self.load(render_context, "assets/block/oak_log.png")?; // 40
|
self.load(render_context, "assets/block/oak_log.png")?; // 40
|
||||||
self.load(render_context, "assets/block/oak_log_top.png")?; // 41
|
self.load(render_context, "assets/block/oak_log_top.png")?; // 41
|
||||||
self.load(render_context, "assets/block/oak_planks.png")?; // 42
|
self.load(render_context, "assets/block/oak_planks.png")?; // 42
|
||||||
|
self.load(render_context, "assets/block/oak_leaves.png")?; // 43
|
||||||
assert_eq!(TEXTURE_COUNT, self.textures.len());
|
assert_eq!(TEXTURE_COUNT, self.textures.len());
|
||||||
|
|
||||||
let texture_array = render_context
|
let texture_array = render_context
|
||||||
|
|
|
@ -17,14 +17,14 @@ pub struct HudVertex {
|
||||||
pub position: [f32; 2],
|
pub position: [f32; 2],
|
||||||
pub texture_coordinates: [f32; 2],
|
pub texture_coordinates: [f32; 2],
|
||||||
pub texture_index: i32,
|
pub texture_index: i32,
|
||||||
pub value: f32,
|
pub color: [f32; 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
const HUD_VERTEX_ATTRIBUTES: &[VertexAttribute] = &wgpu::vertex_attr_array![
|
const HUD_VERTEX_ATTRIBUTES: &[VertexAttribute] = &wgpu::vertex_attr_array![
|
||||||
0 => Float32x2,
|
0 => Float32x2,
|
||||||
1 => Float32x2,
|
1 => Float32x2,
|
||||||
2 => Sint32,
|
2 => Sint32,
|
||||||
3 => Float32,
|
3 => Float32x4,
|
||||||
];
|
];
|
||||||
|
|
||||||
impl Vertex for HudVertex {
|
impl Vertex for HudVertex {
|
||||||
|
@ -40,8 +40,8 @@ impl Vertex for HudVertex {
|
||||||
/// Represents a vertex in world geometry.
|
/// Represents a vertex in world geometry.
|
||||||
///
|
///
|
||||||
/// Aside from the usual vertex position, texture coordinates and normal, this "vertex" also
|
/// 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
|
/// contains whether the block is highlighted (i.e. the player is pointing at the block), its
|
||||||
/// texture index (to address the texture arrays)
|
/// texture index (to address the texture arrays) and a color multiplier.
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Copy, Clone, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
#[derive(Copy, Clone, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
pub struct BlockVertex {
|
pub struct BlockVertex {
|
||||||
|
@ -50,6 +50,7 @@ pub struct BlockVertex {
|
||||||
pub normal: [f32; 3],
|
pub normal: [f32; 3],
|
||||||
pub highlighted: i32,
|
pub highlighted: i32,
|
||||||
pub texture_id: i32,
|
pub texture_id: i32,
|
||||||
|
pub color: [f32; 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
const BLOCK_VERTEX_ATTRIBUTES: &[VertexAttribute] = &wgpu::vertex_attr_array![
|
const BLOCK_VERTEX_ATTRIBUTES: &[VertexAttribute] = &wgpu::vertex_attr_array![
|
||||||
|
@ -58,6 +59,7 @@ const BLOCK_VERTEX_ATTRIBUTES: &[VertexAttribute] = &wgpu::vertex_attr_array![
|
||||||
2 => Float32x3,
|
2 => Float32x3,
|
||||||
3 => Sint32,
|
3 => Sint32,
|
||||||
4 => Sint32,
|
4 => Sint32,
|
||||||
|
5 => Float32x4,
|
||||||
];
|
];
|
||||||
|
|
||||||
impl Vertex for BlockVertex {
|
impl Vertex for BlockVertex {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use cgmath::Vector4;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ pub enum BlockType {
|
||||||
Water,
|
Water,
|
||||||
OakLog,
|
OakLog,
|
||||||
OakPlanks,
|
OakPlanks,
|
||||||
|
OakLeaves,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BlockType {
|
impl BlockType {
|
||||||
|
@ -31,6 +33,15 @@ impl BlockType {
|
||||||
BlockType::Water => ( 8, 8, 8, 8, 8, 8), // up to 39
|
BlockType::Water => ( 8, 8, 8, 8, 8, 8), // up to 39
|
||||||
BlockType::OakLog => (40, 40, 40, 40, 41, 41),
|
BlockType::OakLog => (40, 40, 40, 40, 41, 41),
|
||||||
BlockType::OakPlanks => (42, 42, 42, 42, 42, 42),
|
BlockType::OakPlanks => (42, 42, 42, 42, 42, 42),
|
||||||
|
BlockType::OakLeaves => (43, 43, 43, 43, 43, 43),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn color(self) -> Vector4<f32> {
|
||||||
|
match self {
|
||||||
|
Self::Water => Vector4::new(0.1540, 0.2885, 0.5575, 1.0),
|
||||||
|
Self::OakLeaves => Vector4::new(0.4784, 0.7294, 0.1255, 1.0),
|
||||||
|
_ => Vector4::new(1.0, 1.0, 1.0, 1.0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,7 @@ impl Npc {
|
||||||
normal,
|
normal,
|
||||||
highlighted: 0,
|
highlighted: 0,
|
||||||
texture_id: 0,
|
texture_id: 0,
|
||||||
|
color: [1.0, 1.0, 1.0, 1.0],
|
||||||
};
|
};
|
||||||
|
|
||||||
vertices.push(current_vert);
|
vertices.push(current_vert);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use cgmath::{Point3, Vector3, Zero};
|
use cgmath::{Point3, Vector3, Vector4, Zero};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
geometry::Geometry,
|
geometry::Geometry,
|
||||||
|
@ -58,10 +58,11 @@ impl Quad {
|
||||||
let y = self.position.y as f32;
|
let y = self.position.y as f32;
|
||||||
let z = self.position.z as f32;
|
let z = self.position.z as f32;
|
||||||
|
|
||||||
let t = match self.block_type {
|
let (t, color) = match self.block_type {
|
||||||
Some(block_type) => block_type.texture_indices(),
|
Some(block_type) => (block_type.texture_indices(), block_type.color()),
|
||||||
None => (0, 0, 0, 0, 0, 0),
|
None => ((0, 0, 0, 0, 0, 0), Vector4::new(1.0, 1.0, 1.0, 1.0)),
|
||||||
};
|
};
|
||||||
|
let color = color.into();
|
||||||
|
|
||||||
let mut current_index = start_index;
|
let mut current_index = start_index;
|
||||||
let mut vertices = Vec::new();
|
let mut vertices = Vec::new();
|
||||||
|
@ -72,10 +73,10 @@ impl Quad {
|
||||||
let highlighted = (self.highlighted_normal == normal) as i32;
|
let highlighted = (self.highlighted_normal == normal) as i32;
|
||||||
let normal = normal.cast().unwrap().into();
|
let normal = normal.cast().unwrap().into();
|
||||||
vertices.extend(&[
|
vertices.extend(&[
|
||||||
BlockVertex { position: [x, y, z ], texture_coordinates: [dz, 1.0], texture_id: t.0 as i32, normal, highlighted },
|
BlockVertex { position: [x, y, z ], texture_coordinates: [dz, 1.0], texture_id: t.0 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x, y, z + dz], texture_coordinates: [0.0, 1.0], texture_id: t.0 as i32, normal, highlighted },
|
BlockVertex { position: [x, y, z + dz], texture_coordinates: [0.0, 1.0], texture_id: t.0 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x, y + dy, z + dz], texture_coordinates: [0.0, 0.0], texture_id: t.0 as i32, normal, highlighted },
|
BlockVertex { position: [x, y + dy, z + dz], texture_coordinates: [0.0, 0.0], texture_id: t.0 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x, y + dy, z ], texture_coordinates: [dz, 0.0], texture_id: t.0 as i32, normal, highlighted },
|
BlockVertex { position: [x, y + dy, z ], texture_coordinates: [dz, 0.0], texture_id: t.0 as i32, normal, highlighted, color },
|
||||||
]);
|
]);
|
||||||
indices.extend(&[
|
indices.extend(&[
|
||||||
2 + current_index, current_index, 1 + current_index,
|
2 + current_index, current_index, 1 + current_index,
|
||||||
|
@ -89,10 +90,10 @@ impl Quad {
|
||||||
let highlighted = (self.highlighted_normal == normal) as i32;
|
let highlighted = (self.highlighted_normal == normal) as i32;
|
||||||
let normal = normal.cast().unwrap().into();
|
let normal = normal.cast().unwrap().into();
|
||||||
vertices.extend(&[
|
vertices.extend(&[
|
||||||
BlockVertex { position: [x + dx, y, z ], texture_coordinates: [0.0, 1.0], texture_id: t.1 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y, z ], texture_coordinates: [0.0, 1.0], texture_id: t.1 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y, z + dz], texture_coordinates: [dz, 1.0], texture_id: t.1 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y, z + dz], texture_coordinates: [dz, 1.0], texture_id: t.1 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y + dy, z + dz], texture_coordinates: [dz, 0.0], texture_id: t.1 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y + dy, z + dz], texture_coordinates: [dz, 0.0], texture_id: t.1 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y + dy, z ], texture_coordinates: [0.0, 0.0], texture_id: t.1 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y + dy, z ], texture_coordinates: [0.0, 0.0], texture_id: t.1 as i32, normal, highlighted, color },
|
||||||
]);
|
]);
|
||||||
indices.extend(&[
|
indices.extend(&[
|
||||||
1 + current_index, current_index, 2 + current_index,
|
1 + current_index, current_index, 2 + current_index,
|
||||||
|
@ -106,10 +107,10 @@ impl Quad {
|
||||||
let highlighted = (self.highlighted_normal == normal) as i32;
|
let highlighted = (self.highlighted_normal == normal) as i32;
|
||||||
let normal = normal.cast().unwrap().into();
|
let normal = normal.cast().unwrap().into();
|
||||||
vertices.extend(&[
|
vertices.extend(&[
|
||||||
BlockVertex { position: [x, y, z], texture_coordinates: [dx, 1.0], texture_id: t.2 as i32, normal, highlighted },
|
BlockVertex { position: [x, y, z], texture_coordinates: [dx, 1.0], texture_id: t.2 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x, y + dy, z], texture_coordinates: [dx, 0.0], texture_id: t.2 as i32, normal, highlighted },
|
BlockVertex { position: [x, y + dy, z], texture_coordinates: [dx, 0.0], texture_id: t.2 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y + dy, z], texture_coordinates: [0.0, 0.0], texture_id: t.2 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y + dy, z], texture_coordinates: [0.0, 0.0], texture_id: t.2 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y, z], texture_coordinates: [0.0, 1.0], texture_id: t.2 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y, z], texture_coordinates: [0.0, 1.0], texture_id: t.2 as i32, normal, highlighted, color },
|
||||||
]);
|
]);
|
||||||
indices.extend(&[
|
indices.extend(&[
|
||||||
2 + current_index, current_index, 1 + current_index,
|
2 + current_index, current_index, 1 + current_index,
|
||||||
|
@ -123,10 +124,10 @@ impl Quad {
|
||||||
let highlighted = (self.highlighted_normal == normal) as i32;
|
let highlighted = (self.highlighted_normal == normal) as i32;
|
||||||
let normal = normal.cast().unwrap().into();
|
let normal = normal.cast().unwrap().into();
|
||||||
vertices.extend(&[
|
vertices.extend(&[
|
||||||
BlockVertex { position: [x, y, z + dz], texture_coordinates: [0.0, 1.0], texture_id: t.3 as i32, normal, highlighted },
|
BlockVertex { position: [x, y, z + dz], texture_coordinates: [0.0, 1.0], texture_id: t.3 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x, y + dy, z + dz], texture_coordinates: [0.0, 0.0], texture_id: t.3 as i32, normal, highlighted },
|
BlockVertex { position: [x, y + dy, z + dz], texture_coordinates: [0.0, 0.0], texture_id: t.3 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y + dy, z + dz], texture_coordinates: [dx, 0.0], texture_id: t.3 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y + dy, z + dz], texture_coordinates: [dx, 0.0], texture_id: t.3 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y, z + dz], texture_coordinates: [dx, 1.0], texture_id: t.3 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y, z + dz], texture_coordinates: [dx, 1.0], texture_id: t.3 as i32, normal, highlighted, color },
|
||||||
]);
|
]);
|
||||||
indices.extend(&[
|
indices.extend(&[
|
||||||
1 + current_index, current_index, 2 + current_index,
|
1 + current_index, current_index, 2 + current_index,
|
||||||
|
@ -140,10 +141,10 @@ impl Quad {
|
||||||
let highlighted = (self.highlighted_normal == normal) as i32;
|
let highlighted = (self.highlighted_normal == normal) as i32;
|
||||||
let normal = normal.cast().unwrap().into();
|
let normal = normal.cast().unwrap().into();
|
||||||
vertices.extend(&[
|
vertices.extend(&[
|
||||||
BlockVertex { position: [x, y, z ], texture_coordinates: [dx, 0.0], texture_id: t.4 as i32, normal, highlighted },
|
BlockVertex { position: [x, y, z ], texture_coordinates: [dx, 0.0], texture_id: t.4 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x, y, z + dz], texture_coordinates: [dx, dz ], texture_id: t.4 as i32, normal, highlighted },
|
BlockVertex { position: [x, y, z + dz], texture_coordinates: [dx, dz ], texture_id: t.4 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y, z + dz], texture_coordinates: [0.0, dz ], texture_id: t.4 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y, z + dz], texture_coordinates: [0.0, dz ], texture_id: t.4 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y, z ], texture_coordinates: [0.0, 0.0], texture_id: t.4 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y, z ], texture_coordinates: [0.0, 0.0], texture_id: t.4 as i32, normal, highlighted, color },
|
||||||
]);
|
]);
|
||||||
indices.extend(&[
|
indices.extend(&[
|
||||||
current_index, 2 + current_index, 1 + current_index,
|
current_index, 2 + current_index, 1 + current_index,
|
||||||
|
@ -157,10 +158,10 @@ impl Quad {
|
||||||
let highlighted = (self.highlighted_normal == normal) as i32;
|
let highlighted = (self.highlighted_normal == normal) as i32;
|
||||||
let normal = normal.cast().unwrap().into();
|
let normal = normal.cast().unwrap().into();
|
||||||
vertices.extend(&[
|
vertices.extend(&[
|
||||||
BlockVertex { position: [x, y + dy, z ], texture_coordinates: [0.0, 0.0], texture_id: t.5 as i32, normal, highlighted },
|
BlockVertex { position: [x, y + dy, z ], texture_coordinates: [0.0, 0.0], texture_id: t.5 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x, y + dy, z + dz], texture_coordinates: [0.0, dz ], texture_id: t.5 as i32, normal, highlighted },
|
BlockVertex { position: [x, y + dy, z + dz], texture_coordinates: [0.0, dz ], texture_id: t.5 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y + dy, z + dz], texture_coordinates: [dx, dz ], texture_id: t.5 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y + dy, z + dz], texture_coordinates: [dx, dz ], texture_id: t.5 as i32, normal, highlighted, color },
|
||||||
BlockVertex { position: [x + dx, y + dy, z ], texture_coordinates: [dx, 0.0], texture_id: t.5 as i32, normal, highlighted },
|
BlockVertex { position: [x + dx, y + dy, z ], texture_coordinates: [dx, 0.0], texture_id: t.5 as i32, normal, highlighted, color },
|
||||||
]);
|
]);
|
||||||
indices.extend(&[
|
indices.extend(&[
|
||||||
current_index, 1 + current_index, 2 + current_index,
|
current_index, 1 + current_index, 2 + current_index,
|
||||||
|
|
Loading…
Reference in a new issue