Differentiate between Vertex and BlockVertex
This allows for BlockVertex to store additional information, like the texture ID, without resorting to ugly hacks like passing it as a float
This commit is contained in:
parent
fb23daa60c
commit
032d527af3
10 changed files with 143 additions and 141 deletions
|
@ -1,6 +1,6 @@
|
|||
use std::{collections::VecDeque, convert::TryInto, usize};
|
||||
|
||||
use crate::{cube, quad::Quad, vertex::Vertex};
|
||||
use crate::{cube, quad::Quad, vertex::BlockVertex};
|
||||
use ahash::{AHashMap, AHashSet};
|
||||
use cgmath::{Vector3, Zero};
|
||||
use noise::utils::{NoiseMapBuilder, PlaneMapBuilder};
|
||||
|
@ -321,7 +321,7 @@ impl Chunk {
|
|||
|
||||
fn quads_to_geometry(
|
||||
quads: Vec<(BlockType, i32, Vector3<i32>, Quad, Vector3<i32>, FaceFlags)>,
|
||||
) -> (Vec<Vertex>, Vec<u16>) {
|
||||
) -> (Vec<BlockVertex>, Vec<u16>) {
|
||||
let mut vertices = Vec::new();
|
||||
let mut indices = Vec::new();
|
||||
for (block_type, y, offset, quad, highlighted, visible_faces) in quads {
|
||||
|
@ -349,7 +349,7 @@ impl Chunk {
|
|||
&self,
|
||||
offset: Vector3<i32>,
|
||||
highlighted: Option<&(Vector3<usize>, Vector3<i32>)>,
|
||||
) -> (Vec<Vertex>, Vec<u16>) {
|
||||
) -> (Vec<BlockVertex>, Vec<u16>) {
|
||||
let mut quads: Vec<(BlockType, i32, Vector3<i32>, Quad, Vector3<i32>, FaceFlags)> =
|
||||
Vec::new();
|
||||
|
||||
|
|
52
src/cube.rs
52
src/cube.rs
|
@ -3,7 +3,7 @@ use cgmath::Vector3;
|
|||
use crate::{
|
||||
chunk::{FaceFlags, FACE_BACK, FACE_BOTTOM, FACE_FRONT, FACE_LEFT, FACE_RIGHT, FACE_TOP},
|
||||
quad::Quad,
|
||||
vertex::Vertex,
|
||||
vertex::BlockVertex,
|
||||
};
|
||||
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
|
@ -17,7 +17,7 @@ pub fn vertices(
|
|||
highlighted: Vector3<i32>,
|
||||
visible_faces: FaceFlags,
|
||||
start_index: u16,
|
||||
) -> (Vec<Vertex>, Vec<u16>) {
|
||||
) -> (Vec<BlockVertex>, Vec<u16>) {
|
||||
let w = quad.w as f32;
|
||||
let h = quad.h as f32;
|
||||
let zh = z_height;
|
||||
|
@ -37,10 +37,10 @@ pub fn vertices(
|
|||
let normal = [-1.0, 0.0, 0.0];
|
||||
let highlighted = (normal == highlighted) as i32;
|
||||
vertices.extend(&[
|
||||
Vertex { position: [x, y, z ], texture_coordinates: [h, 1.0, t.0 as f32], normal, highlighted },
|
||||
Vertex { position: [x, y, z + h], texture_coordinates: [0.0, 1.0, t.0 as f32], normal, highlighted },
|
||||
Vertex { position: [x, y + zh, z + h], texture_coordinates: [0.0, 0.0, t.0 as f32], normal, highlighted },
|
||||
Vertex { position: [x, y + zh, z ], texture_coordinates: [h, 0.0, t.0 as f32], normal, highlighted },
|
||||
BlockVertex { position: [x, y, z ], texture_coordinates: [h, 1.0], texture_id: t.0 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x, y, z + h], texture_coordinates: [0.0, 1.0], texture_id: t.0 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x, y + zh, z + h], texture_coordinates: [0.0, 0.0], texture_id: t.0 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x, y + zh, z ], texture_coordinates: [h, 0.0], texture_id: t.0 as i32, normal, highlighted },
|
||||
]);
|
||||
indices.extend(&[
|
||||
2 + current_index, current_index, 1 + current_index,
|
||||
|
@ -53,10 +53,10 @@ pub fn vertices(
|
|||
let normal = [1.0, 0.0, 0.0];
|
||||
let highlighted = (normal == highlighted) as i32;
|
||||
vertices.extend(&[
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [0.0, 1.0, t.1 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y, z + h], texture_coordinates: [h, 1.0, t.1 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y + zh, z + h], texture_coordinates: [h, 0.0, t.1 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y + zh, z ], texture_coordinates: [0.0, 0.0, t.1 as f32], normal, highlighted },
|
||||
BlockVertex { position: [x + w, y, z ], texture_coordinates: [0.0, 1.0], texture_id: t.1 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y, z + h], texture_coordinates: [h, 1.0], texture_id: t.1 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y + zh, z + h], texture_coordinates: [h, 0.0], texture_id: t.1 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y + zh, z ], texture_coordinates: [0.0, 0.0], texture_id: t.1 as i32, normal, highlighted },
|
||||
]);
|
||||
indices.extend(&[
|
||||
1 + current_index, current_index, 2 + current_index,
|
||||
|
@ -69,10 +69,10 @@ pub fn vertices(
|
|||
let normal = [0.0, 0.0, -1.0];
|
||||
let highlighted = (normal == highlighted) as i32;
|
||||
vertices.extend(&[
|
||||
Vertex { position: [x, y, z], texture_coordinates: [w, 1.0, t.2 as f32], normal, highlighted },
|
||||
Vertex { position: [x, y + zh, z], texture_coordinates: [w, 0.0, t.2 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y + zh, z], texture_coordinates: [0.0, 0.0, t.2 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y, z], texture_coordinates: [0.0, 1.0, t.2 as f32], normal, highlighted },
|
||||
BlockVertex { position: [x, y, z], texture_coordinates: [w, 1.0], texture_id: t.2 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x, y + zh, z], texture_coordinates: [w, 0.0], texture_id: t.2 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y + zh, z], texture_coordinates: [0.0, 0.0], texture_id: t.2 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y, z], texture_coordinates: [0.0, 1.0], texture_id: t.2 as i32, normal, highlighted },
|
||||
]);
|
||||
indices.extend(&[
|
||||
2 + current_index, current_index, 1 + current_index,
|
||||
|
@ -85,10 +85,10 @@ pub fn vertices(
|
|||
let normal = [0.0, 0.0, 1.0];
|
||||
let highlighted = (normal == highlighted) as i32;
|
||||
vertices.extend(&[
|
||||
Vertex { position: [x, y, z + h], texture_coordinates: [0.0, 1.0, t.3 as f32], normal, highlighted },
|
||||
Vertex { position: [x, y + zh, z + h], texture_coordinates: [0.0, 0.0, t.3 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y + zh, z + h], texture_coordinates: [w, 0.0, t.3 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y, z + h], texture_coordinates: [w, 1.0, t.3 as f32], normal, highlighted },
|
||||
BlockVertex { position: [x, y, z + h], texture_coordinates: [0.0, 1.0], texture_id: t.3 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x, y + zh, z + h], texture_coordinates: [0.0, 0.0], texture_id: t.3 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y + zh, z + h], texture_coordinates: [w, 0.0], texture_id: t.3 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y, z + h], texture_coordinates: [w, 1.0], texture_id: t.3 as i32, normal, highlighted },
|
||||
]);
|
||||
indices.extend(&[
|
||||
1 + current_index, current_index, 2 + current_index,
|
||||
|
@ -101,10 +101,10 @@ pub fn vertices(
|
|||
let normal = [0.0, -1.0, 0.0];
|
||||
let highlighted = (normal == highlighted) as i32;
|
||||
vertices.extend(&[
|
||||
Vertex { position: [x, y, z ], texture_coordinates: [w, 0.0, t.4 as f32], normal, highlighted },
|
||||
Vertex { position: [x, y, z + h], texture_coordinates: [w, h, t.4 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y, z + h], texture_coordinates: [0.0, h, t.4 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [0.0, 0.0, t.4 as f32], normal, highlighted },
|
||||
BlockVertex { position: [x, y, z ], texture_coordinates: [w, 0.0], texture_id: t.4 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x, y, z + h], texture_coordinates: [w, h ], texture_id: t.4 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y, z + h], texture_coordinates: [0.0, h ], texture_id: t.4 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y, z ], texture_coordinates: [0.0, 0.0], texture_id: t.4 as i32, normal, highlighted },
|
||||
]);
|
||||
indices.extend(&[
|
||||
current_index, 2 + current_index, 1 + current_index,
|
||||
|
@ -117,10 +117,10 @@ pub fn vertices(
|
|||
let normal = [0.0, 1.0, 0.0];
|
||||
let highlighted = (normal == highlighted) as i32;
|
||||
vertices.extend(&[
|
||||
Vertex { position: [x, y + zh, z ], texture_coordinates: [0.0, 0.0, t.5 as f32], normal, highlighted },
|
||||
Vertex { position: [x, y + zh, z + h], texture_coordinates: [0.0, h, t.5 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y + zh, z + h], texture_coordinates: [w, h, t.5 as f32], normal, highlighted },
|
||||
Vertex { position: [x + w, y + zh, z ], texture_coordinates: [w, 0.0, t.5 as f32], normal, highlighted },
|
||||
BlockVertex { position: [x, y + zh, z ], texture_coordinates: [0.0, 0.0], texture_id: t.5 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x, y + zh, z + h], texture_coordinates: [0.0, h ], texture_id: t.5 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y + zh, z + h], texture_coordinates: [w, h ], texture_id: t.5 as i32, normal, highlighted },
|
||||
BlockVertex { position: [x + w, y + zh, z ], texture_coordinates: [w, 0.0], texture_id: t.5 as i32, normal, highlighted },
|
||||
]);
|
||||
indices.extend(&[
|
||||
current_index, 1 + current_index, 2 + current_index,
|
||||
|
|
|
@ -8,7 +8,7 @@ mod state;
|
|||
mod text_renderer;
|
||||
mod texture;
|
||||
mod time;
|
||||
mod uniforms;
|
||||
mod view;
|
||||
mod vertex;
|
||||
mod world;
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[[block]]
|
||||
struct Uniforms {
|
||||
view_position: vec4<f32>;
|
||||
view_projection: mat4x4<f32>;
|
||||
struct View {
|
||||
position: vec4<f32>;
|
||||
projection: mat4x4<f32>;
|
||||
};
|
||||
|
||||
[[block]]
|
||||
|
@ -10,24 +10,26 @@ struct Time {
|
|||
};
|
||||
|
||||
[[group(1), binding(0)]]
|
||||
var<uniform> uniforms: Uniforms;
|
||||
var<uniform> view: View;
|
||||
|
||||
[[group(2), binding(0)]]
|
||||
var<uniform> time: Time;
|
||||
|
||||
struct VertexInput {
|
||||
[[location(0)]] position: vec3<f32>;
|
||||
[[location(1)]] texture_coordinates: vec3<f32>;
|
||||
[[location(1)]] texture_coordinates: vec2<f32>;
|
||||
[[location(2)]] normal: vec3<f32>;
|
||||
[[location(3)]] highlighted: i32;
|
||||
[[location(4)]] texture_id: i32;
|
||||
};
|
||||
|
||||
struct VertexOutput {
|
||||
[[builtin(position)]] clip_position: vec4<f32>;
|
||||
[[location(0)]] texture_coordinates: vec3<f32>;
|
||||
[[location(0)]] texture_coordinates: vec2<f32>;
|
||||
[[location(1)]] world_normal: vec3<f32>;
|
||||
[[location(2)]] world_position: vec3<f32>;
|
||||
[[location(3)]] highlighted: i32;
|
||||
[[location(4)]] texture_id: i32;
|
||||
};
|
||||
|
||||
let pi: f32 = 3.14159265359;
|
||||
|
@ -37,17 +39,19 @@ fn main(model: VertexInput) -> VertexOutput {
|
|||
var out: VertexOutput;
|
||||
|
||||
out.world_normal = model.normal;
|
||||
if (model.texture_coordinates.z == 8.0) {
|
||||
if (model.texture_id == 8) {
|
||||
// water
|
||||
let offset = (sin(time.time * 0.5 + model.position.x) * cos(time.time * 0.9 + model.position.y) + 2.5) / 10.0;
|
||||
out.world_position = vec3<f32>(model.position.x, model.position.y - offset, model.position.z);
|
||||
out.texture_coordinates = vec3<f32>(model.texture_coordinates.xy + (time.time / 10.0), 8.0 + (time.time * 10.0) % 32.0);
|
||||
out.texture_coordinates = model.texture_coordinates + (time.time / 10.0);
|
||||
out.texture_id = i32(8.0 + (time.time * 10.0) % 32.0);
|
||||
} else {
|
||||
out.world_position = model.position;
|
||||
out.texture_coordinates = model.texture_coordinates;
|
||||
out.texture_id = model.texture_id;
|
||||
}
|
||||
|
||||
out.clip_position = uniforms.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;
|
||||
return out;
|
||||
}
|
||||
|
@ -60,8 +64,8 @@ fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
|||
let object_color: vec4<f32> = textureSample(
|
||||
texture_array,
|
||||
texture_sampler,
|
||||
in.texture_coordinates.xy,
|
||||
i32(round(in.texture_coordinates.z))
|
||||
in.texture_coordinates,
|
||||
in.texture_id
|
||||
);
|
||||
|
||||
let light_position = vec3<f32>(-100.0, 500.0, -200.0);
|
||||
|
@ -71,7 +75,7 @@ fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
|||
let ambient_color = light_color * ambient_strength;
|
||||
|
||||
let light_direction = normalize(light_position - in.world_position);
|
||||
let view_direction = normalize(uniforms.view_position.xyz - in.world_position);
|
||||
let view_direction = normalize(view.position.xyz - in.world_position);
|
||||
let half_direction = normalize(view_direction + light_direction);
|
||||
|
||||
let diffuse_strength = max(dot(in.world_normal, light_direction), 0.0);
|
||||
|
|
|
@ -296,52 +296,44 @@ pub const CROSSHAIR_VERTICES: &[Vertex] = &[
|
|||
// Crosshair
|
||||
Vertex {
|
||||
position: [-UI_SCALE_X * 8.0, UI_SCALE_Y * 8.0, 0.0],
|
||||
texture_coordinates: [240.0 / 256.0, 0.0 / 256.0, 0.0],
|
||||
texture_coordinates: [240.0 / 256.0, 0.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
Vertex {
|
||||
position: [UI_SCALE_X * 8.0, UI_SCALE_Y * 8.0, 0.0],
|
||||
texture_coordinates: [1.0, 0.0 / 256.0, 0.0],
|
||||
texture_coordinates: [1.0, 0.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
Vertex {
|
||||
position: [UI_SCALE_X * 8.0, -UI_SCALE_Y * 8.0, 0.0],
|
||||
texture_coordinates: [1.0, 16.0 / 256.0, 0.0],
|
||||
texture_coordinates: [1.0, 16.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
Vertex {
|
||||
position: [-UI_SCALE_X * 8.0, -UI_SCALE_Y * 8.0, 0.0],
|
||||
texture_coordinates: [240.0 / 256.0, 16.0 / 256.0, 0.0],
|
||||
texture_coordinates: [240.0 / 256.0, 16.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
// Hotbar
|
||||
Vertex {
|
||||
position: [-UI_SCALE_X * 91.0, -1.0 + UI_SCALE_Y * 22.0, 0.0],
|
||||
texture_coordinates: [0.0 / 256.0, 0.0 / 256.0, 0.0],
|
||||
texture_coordinates: [0.0 / 256.0, 0.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
Vertex {
|
||||
position: [UI_SCALE_X * 91.0, -1.0 + UI_SCALE_Y * 22.0, 0.0],
|
||||
texture_coordinates: [182.0 / 256.0, 0.0 / 256.0, 0.0],
|
||||
texture_coordinates: [182.0 / 256.0, 0.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
Vertex {
|
||||
position: [UI_SCALE_X * 91.0, -1.0, 0.0],
|
||||
texture_coordinates: [182.0 / 256.0, 22.0 / 256.0, 0.0],
|
||||
texture_coordinates: [182.0 / 256.0, 22.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
Vertex {
|
||||
position: [-UI_SCALE_X * 91.0, -1.0, 0.0],
|
||||
texture_coordinates: [0.0 / 256.0, 22.0 / 256.0, 0.0],
|
||||
texture_coordinates: [0.0 / 256.0, 22.0 / 256.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
highlighted: 0,
|
||||
},
|
||||
];
|
||||
|
||||
|
|
|
@ -17,16 +17,16 @@ use crate::{
|
|||
render_context::RenderContext,
|
||||
texture::{Texture, TextureManager},
|
||||
time::Time,
|
||||
uniforms::Uniforms,
|
||||
vertex::Vertex,
|
||||
vertex::BlockVertex,
|
||||
view::View,
|
||||
world::World,
|
||||
};
|
||||
|
||||
pub struct WorldState {
|
||||
pub render_pipeline: wgpu::RenderPipeline,
|
||||
pub uniforms: Uniforms,
|
||||
pub uniform_buffer: wgpu::Buffer,
|
||||
pub uniform_bind_group: wgpu::BindGroup,
|
||||
pub view: View,
|
||||
pub view_buffer: wgpu::Buffer,
|
||||
pub view_bind_group: wgpu::BindGroup,
|
||||
pub texture_manager: TextureManager,
|
||||
pub camera: Camera,
|
||||
pub projection: Projection,
|
||||
|
@ -76,28 +76,23 @@ impl WorldState {
|
|||
(camera, projection)
|
||||
}
|
||||
|
||||
fn create_uniforms(
|
||||
fn create_view(
|
||||
camera: &Camera,
|
||||
projection: &Projection,
|
||||
render_context: &RenderContext,
|
||||
) -> (
|
||||
Uniforms,
|
||||
wgpu::Buffer,
|
||||
wgpu::BindGroupLayout,
|
||||
wgpu::BindGroup,
|
||||
) {
|
||||
let mut uniforms = Uniforms::new();
|
||||
uniforms.update_view_projection(camera, projection);
|
||||
) -> (View, wgpu::Buffer, wgpu::BindGroupLayout, wgpu::BindGroup) {
|
||||
let mut view = View::new();
|
||||
view.update_view_projection(camera, projection);
|
||||
|
||||
let uniform_buffer = render_context
|
||||
let view_buffer = render_context
|
||||
.device
|
||||
.create_buffer_init(&BufferInitDescriptor {
|
||||
label: Some("uniform_buffer"),
|
||||
contents: bytemuck::cast_slice(&[uniforms]),
|
||||
label: Some("view_buffer"),
|
||||
contents: bytemuck::cast_slice(&[view]),
|
||||
usage: wgpu::BufferUsage::UNIFORM | wgpu::BufferUsage::COPY_DST,
|
||||
});
|
||||
|
||||
let uniform_bind_group_layout =
|
||||
let view_bind_group_layout =
|
||||
render_context
|
||||
.device
|
||||
.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
|
@ -111,27 +106,21 @@ impl WorldState {
|
|||
},
|
||||
count: None,
|
||||
}],
|
||||
label: Some("uniform_bind_group_layout"),
|
||||
label: Some("view_bind_group_layout"),
|
||||
});
|
||||
|
||||
let uniform_bind_group =
|
||||
render_context
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &uniform_bind_group_layout,
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: uniform_buffer.as_entire_binding(),
|
||||
}],
|
||||
label: Some("uniform_bind_group"),
|
||||
});
|
||||
let view_bind_group = render_context
|
||||
.device
|
||||
.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
layout: &view_bind_group_layout,
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: view_buffer.as_entire_binding(),
|
||||
}],
|
||||
label: Some("view_bind_group"),
|
||||
});
|
||||
|
||||
(
|
||||
uniforms,
|
||||
uniform_buffer,
|
||||
uniform_bind_group_layout,
|
||||
uniform_bind_group,
|
||||
)
|
||||
(view, view_buffer, view_bind_group_layout, view_bind_group)
|
||||
}
|
||||
|
||||
fn create_time(
|
||||
|
@ -192,7 +181,7 @@ impl WorldState {
|
|||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "main",
|
||||
buffers: &[Vertex::desc()],
|
||||
buffers: &[BlockVertex::descriptor()],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
|
@ -309,8 +298,8 @@ impl WorldState {
|
|||
|
||||
let (camera, projection) = Self::create_camera(render_context);
|
||||
|
||||
let (uniforms, uniform_buffer, world_uniform_layout, uniform_bind_group) =
|
||||
Self::create_uniforms(&camera, &projection, render_context);
|
||||
let (view, view_buffer, view_bind_group_layout, view_bind_group) =
|
||||
Self::create_view(&camera, &projection, render_context);
|
||||
|
||||
let (time, time_buffer, time_layout, time_bind_group) = Self::create_time(render_context);
|
||||
|
||||
|
@ -330,7 +319,7 @@ impl WorldState {
|
|||
push_constant_ranges: &[],
|
||||
bind_group_layouts: &[
|
||||
&texture_manager.bind_group_layout,
|
||||
&world_uniform_layout,
|
||||
&view_bind_group_layout,
|
||||
&time_layout,
|
||||
],
|
||||
});
|
||||
|
@ -342,9 +331,9 @@ impl WorldState {
|
|||
|
||||
let mut world_state = Self {
|
||||
render_pipeline,
|
||||
uniforms,
|
||||
uniform_buffer,
|
||||
uniform_bind_group,
|
||||
view,
|
||||
view_buffer,
|
||||
view_bind_group,
|
||||
texture_manager,
|
||||
camera,
|
||||
projection,
|
||||
|
@ -406,7 +395,7 @@ impl WorldState {
|
|||
|
||||
let tm = &self.texture_manager;
|
||||
render_pass.set_bind_group(0, tm.bind_group.as_ref().unwrap(), &[]);
|
||||
render_pass.set_bind_group(1, &self.uniform_bind_group, &[]);
|
||||
render_pass.set_bind_group(1, &self.view_bind_group, &[]);
|
||||
render_pass.set_bind_group(2, &self.time_bind_group, &[]);
|
||||
|
||||
let camera_pos = self.camera.position.to_vec();
|
||||
|
@ -556,13 +545,11 @@ impl WorldState {
|
|||
self.update_position(dt);
|
||||
self.update_aim(render_context);
|
||||
|
||||
self.uniforms
|
||||
self.view
|
||||
.update_view_projection(&self.camera, &self.projection);
|
||||
render_context.queue.write_buffer(
|
||||
&self.uniform_buffer,
|
||||
0,
|
||||
bytemuck::cast_slice(&[self.uniforms]),
|
||||
);
|
||||
render_context
|
||||
.queue
|
||||
.write_buffer(&self.view_buffer, 0, bytemuck::cast_slice(&[self.view]));
|
||||
|
||||
self.time.time += dt.as_secs_f32();
|
||||
render_context.queue.write_buffer(
|
||||
|
|
|
@ -106,10 +106,10 @@ impl TextRenderer {
|
|||
|
||||
#[rustfmt::skip]
|
||||
let vertices = [
|
||||
Vertex { position: [x, y, 0.0], texture_coordinates: [tx, ty, 0.0], ..Default::default() },
|
||||
Vertex { position: [x + DX, y, 0.0], texture_coordinates: [tx + s, ty, 0.0], ..Default::default() },
|
||||
Vertex { position: [x + DX, y - DY, 0.0], texture_coordinates: [tx + s, ty + s, 0.0], ..Default::default() },
|
||||
Vertex { position: [x, y - DY, 0.0], texture_coordinates: [tx, ty + s, 0.0], ..Default::default() },
|
||||
Vertex { position: [x, y, 0.0], texture_coordinates: [tx, ty ], ..Default::default() },
|
||||
Vertex { position: [x + DX, y, 0.0], texture_coordinates: [tx + s, ty ], ..Default::default() },
|
||||
Vertex { position: [x + DX, y - DY, 0.0], texture_coordinates: [tx + s, ty + s], ..Default::default() },
|
||||
Vertex { position: [x, y - DY, 0.0], texture_coordinates: [tx, ty + s], ..Default::default() },
|
||||
];
|
||||
|
||||
#[rustfmt::skip]
|
||||
|
|
|
@ -1,41 +1,60 @@
|
|||
use std::mem::size_of;
|
||||
|
||||
use wgpu::VertexAttribute;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Vertex {
|
||||
pub position: [f32; 3],
|
||||
pub texture_coordinates: [f32; 3],
|
||||
pub texture_coordinates: [f32; 2],
|
||||
pub normal: [f32; 3],
|
||||
pub highlighted: i32,
|
||||
}
|
||||
|
||||
const VERTEX_DESC: &[VertexAttribute] = &wgpu::vertex_attr_array![
|
||||
0 => Float32x3,
|
||||
1 => Float32x2,
|
||||
2 => Float32x3,
|
||||
];
|
||||
|
||||
impl Vertex {
|
||||
pub fn desc<'a>() -> wgpu::VertexBufferLayout<'a> {
|
||||
wgpu::VertexBufferLayout {
|
||||
array_stride: size_of::<Vertex>() as wgpu::BufferAddress,
|
||||
array_stride: size_of::<Self>() as wgpu::BufferAddress,
|
||||
step_mode: wgpu::InputStepMode::Vertex,
|
||||
attributes: &[
|
||||
wgpu::VertexAttribute {
|
||||
offset: 0,
|
||||
shader_location: 0,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: 12,
|
||||
shader_location: 1,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: 24,
|
||||
shader_location: 2,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: 36,
|
||||
shader_location: 3,
|
||||
format: wgpu::VertexFormat::Sint32,
|
||||
},
|
||||
],
|
||||
attributes: VERTEX_DESC,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Vertex used to represent block vertices.
|
||||
///
|
||||
/// 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
|
||||
/// texture index (to address the texture arrays)
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct BlockVertex {
|
||||
pub position: [f32; 3],
|
||||
pub texture_coordinates: [f32; 2],
|
||||
pub normal: [f32; 3],
|
||||
pub highlighted: i32,
|
||||
pub texture_id: i32,
|
||||
}
|
||||
|
||||
const BLOCK_VERTEX_DESC: &[VertexAttribute] = &wgpu::vertex_attr_array![
|
||||
0 => Float32x3,
|
||||
1 => Float32x2,
|
||||
2 => Float32x3,
|
||||
3 => Sint32,
|
||||
4 => Sint32,
|
||||
];
|
||||
|
||||
impl BlockVertex {
|
||||
pub fn descriptor<'a>() -> wgpu::VertexBufferLayout<'a> {
|
||||
wgpu::VertexBufferLayout {
|
||||
array_stride: size_of::<Self>() as wgpu::BufferAddress,
|
||||
step_mode: wgpu::InputStepMode::Vertex,
|
||||
attributes: BLOCK_VERTEX_DESC,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,12 +4,12 @@ use crate::camera::{Camera, Projection};
|
|||
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Uniforms {
|
||||
pub struct View {
|
||||
view_position: [f32; 4],
|
||||
view_projection: [[f32; 4]; 4],
|
||||
}
|
||||
|
||||
impl Uniforms {
|
||||
impl View {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
view_position: [0.0; 4],
|
|
@ -1,6 +1,6 @@
|
|||
use crate::{
|
||||
chunk::{Block, Chunk, CHUNK_SIZE},
|
||||
vertex::Vertex,
|
||||
vertex::BlockVertex,
|
||||
};
|
||||
use cgmath::{InnerSpace, Vector3};
|
||||
use rayon::prelude::*;
|
||||
|
@ -63,7 +63,7 @@ impl World {
|
|||
pub fn to_geometry(
|
||||
&self,
|
||||
highlighted: Option<(Vector3<usize>, Vector3<i32>)>,
|
||||
) -> Vec<(Vector3<usize>, Vec<Vertex>, Vec<u16>)> {
|
||||
) -> Vec<(Vector3<usize>, Vec<BlockVertex>, Vec<u16>)> {
|
||||
let instant = std::time::Instant::now();
|
||||
|
||||
let chunks = &self.chunks;
|
||||
|
|
Loading…
Reference in a new issue