Meshify chunks and switch to texture arrays from the atlas
This commit is contained in:
parent
e23c4549a2
commit
503288733c
10 changed files with 278 additions and 149 deletions
BIN
assets/grass_block_side_plains.png
(Stored with Git LFS)
Normal file
BIN
assets/grass_block_side_plains.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
assets/grass_block_top_plains.png
(Stored with Git LFS)
Normal file
BIN
assets/grass_block_top_plains.png
(Stored with Git LFS)
Normal file
Binary file not shown.
40
src/chunk.rs
40
src/chunk.rs
|
@ -16,6 +16,22 @@ pub enum BlockType {
|
|||
Gravel,
|
||||
}
|
||||
|
||||
impl BlockType {
|
||||
pub const fn texture_indices(self) -> (usize, usize, usize, usize, usize, usize) {
|
||||
#[rustfmt::skip]
|
||||
let indices = match self {
|
||||
BlockType::Cobblestone => ( 0, 0, 0, 0, 0, 0),
|
||||
BlockType::Dirt => ( 1, 1, 1, 1, 1, 1),
|
||||
BlockType::Stone => ( 2, 2, 2, 2, 2, 2),
|
||||
BlockType::Grass => ( 4, 4, 4, 4, 2, 3),
|
||||
BlockType::Bedrock => ( 5, 5, 5, 5, 5, 5),
|
||||
BlockType::Sand => ( 6, 6, 6, 6, 6, 6),
|
||||
BlockType::Gravel => ( 7, 7, 7, 7, 7, 7),
|
||||
};
|
||||
indices
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Block {
|
||||
pub block_type: BlockType,
|
||||
|
@ -116,7 +132,10 @@ impl Chunk {
|
|||
&& self.get_block(x + 1, y + 1, z + 1).is_some()
|
||||
}
|
||||
|
||||
pub fn to_instances(&self, offset: Vector3<i32>) -> (Vec<Vertex>, Vec<u16>) {
|
||||
pub fn to_instances(
|
||||
&self,
|
||||
offset: Vector3<i32>,
|
||||
) -> (Vec<Vertex>, Vec<u16>, Vec<Vec<(usize, usize, usize)>>) {
|
||||
let mut quads: Vec<(BlockType, i32, Vector3<i32>, Quad)> = Vec::new();
|
||||
|
||||
for (y, y_blocks) in self.blocks.iter().enumerate() {
|
||||
|
@ -180,17 +199,30 @@ impl Chunk {
|
|||
|
||||
let mut vertices = Vec::new();
|
||||
let mut indices = Vec::new();
|
||||
for (quad_index, (_, y, offset, quad)) in quads.iter().enumerate() {
|
||||
let mut index_indices: Vec<Vec<(usize, usize, usize)>> = Vec::new();
|
||||
for (quad_index, (block_type, y, offset, quad)) in quads.iter().enumerate() {
|
||||
#[rustfmt::skip]
|
||||
let v = cube::vertices(quad, *y, *offset, (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1));
|
||||
let v = cube::vertices(quad, *y, *offset, block_type.texture_indices());
|
||||
vertices.extend(&v);
|
||||
|
||||
let o = indices.len();
|
||||
#[rustfmt::skip]
|
||||
index_indices.push(match block_type {
|
||||
BlockType::Cobblestone => vec![(o + 0, o + 36, 0)],
|
||||
BlockType::Dirt => vec![(o + 0, o + 36, 1)],
|
||||
BlockType::Stone => vec![(o + 0, o + 36, 2)],
|
||||
BlockType::Grass => vec![(o + 0, o + 24, 4), (24, 30, 1), (30, 36, 3)],
|
||||
BlockType::Bedrock => vec![(o + 0, o + 36, 5)],
|
||||
BlockType::Sand => vec![(o + 0, o + 36, 5)],
|
||||
BlockType::Gravel => vec![(o + 0, o + 36, 6)],
|
||||
});
|
||||
|
||||
for index in cube::INDICES {
|
||||
indices.push(index + quad_index as u16 * 24);
|
||||
}
|
||||
}
|
||||
|
||||
(vertices, indices)
|
||||
(vertices, indices, index_indices)
|
||||
}
|
||||
|
||||
pub fn get_block(&self, x: usize, y: usize, z: usize) -> Option<&Block> {
|
||||
|
|
59
src/cube.rs
59
src/cube.rs
|
@ -2,18 +2,11 @@ use cgmath::Vector3;
|
|||
|
||||
use crate::{quad::Quad, vertex::Vertex};
|
||||
|
||||
const S: f32 = 512.0 / 4096.0;
|
||||
|
||||
pub fn vertices(
|
||||
quad: &Quad,
|
||||
y: i32,
|
||||
offset: Vector3<i32>,
|
||||
left: (i32, i32),
|
||||
right: (i32, i32),
|
||||
back: (i32, i32),
|
||||
front: (i32, i32),
|
||||
bottom: (i32, i32),
|
||||
top: (i32, i32),
|
||||
texture_indices: (usize, usize, usize, usize, usize, usize),
|
||||
) -> [Vertex; 24] {
|
||||
let w = quad.w as f32;
|
||||
let h = quad.h as f32;
|
||||
|
@ -22,43 +15,45 @@ pub fn vertices(
|
|||
let y = (y + offset.y) as f32;
|
||||
let z = (quad.y + offset.z) as f32;
|
||||
|
||||
let t = texture_indices;
|
||||
|
||||
#[rustfmt::skip]
|
||||
let vertices = [
|
||||
// Left
|
||||
Vertex { position: [x, y, z ], texture_coordinates: [left.0 as f32 * S + S, left.1 as f32 * S + S], normal: [-1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x, y, z + h ], texture_coordinates: [left.0 as f32 * S, left.1 as f32 * S + S], normal: [-1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x, y + 1.0, z + h ], texture_coordinates: [left.0 as f32 * S, left.1 as f32 * S ], normal: [-1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x, y + 1.0, z ], texture_coordinates: [left.0 as f32 * S + S, left.1 as f32 * S ], normal: [-1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x, y, z ], texture_coordinates: [h, 1.0, t.0 as f32], normal: [-1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x, y, z + h ], texture_coordinates: [0.0, 1.0, t.0 as f32], normal: [-1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x, y + 1.0, z + h ], texture_coordinates: [0.0, 0.0, t.0 as f32], normal: [-1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x, y + 1.0, z ], texture_coordinates: [h, 0.0, t.0 as f32], normal: [-1.0, 0.0, 0.0] },
|
||||
|
||||
// Right
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [right.0 as f32 * S, right.1 as f32 * S + S], normal: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x + w, y, z + h ], texture_coordinates: [right.0 as f32 * S + S, right.1 as f32 * S + S], normal: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z + h ], texture_coordinates: [right.0 as f32 * S + S, right.1 as f32 * S ], normal: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z ], texture_coordinates: [right.0 as f32 * S, right.1 as f32 * S ], normal: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [0.0, 1.0, t.1 as f32], normal: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x + w, y, z + h ], texture_coordinates: [h, 1.0, t.1 as f32], normal: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z + h ], texture_coordinates: [h, 0.0, t.1 as f32], normal: [1.0, 0.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z ], texture_coordinates: [0.0, 0.0, t.1 as f32], normal: [1.0, 0.0, 0.0] },
|
||||
|
||||
// Back
|
||||
Vertex { position: [x, y, z ], texture_coordinates: [back.0 as f32 * S + S, back.1 as f32 * S + S], normal: [0.0, 0.0, -1.0] },
|
||||
Vertex { position: [x, y + 1.0, z ], texture_coordinates: [back.0 as f32 * S + S, back.1 as f32 * S ], normal: [0.0, 0.0, -1.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z ], texture_coordinates: [back.0 as f32 * S, back.1 as f32 * S ], normal: [0.0, 0.0, -1.0] },
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [back.0 as f32 * S, back.1 as f32 * S + S], normal: [0.0, 0.0, -1.0] },
|
||||
Vertex { position: [x, y, z ], texture_coordinates: [w, 1.0, t.2 as f32], normal: [0.0, 0.0, -1.0] },
|
||||
Vertex { position: [x, y + 1.0, z ], texture_coordinates: [w, 0.0, t.2 as f32], normal: [0.0, 0.0, -1.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z ], texture_coordinates: [0.0, 0.0, t.2 as f32], normal: [0.0, 0.0, -1.0] },
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [0.0, 1.0, t.2 as f32], normal: [0.0, 0.0, -1.0] },
|
||||
|
||||
// Front
|
||||
Vertex { position: [x, y, z + h], texture_coordinates: [front.0 as f32 * S, front.1 as f32 * S + S], normal: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [x, y + 1.0, z + h], texture_coordinates: [front.0 as f32 * S, front.1 as f32 * S ], normal: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z + h], texture_coordinates: [front.0 as f32 * S + S, front.1 as f32 * S ], normal: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [x + w, y, z + h], texture_coordinates: [front.0 as f32 * S + S, front.1 as f32 * S + S], normal: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [x, y, z + h], texture_coordinates: [0.0, 1.0, t.3 as f32], normal: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [x, y + 1.0, z + h], texture_coordinates: [0.0, 0.0, t.3 as f32], normal: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z + h], texture_coordinates: [w, 0.0, t.3 as f32], normal: [0.0, 0.0, 1.0] },
|
||||
Vertex { position: [x + w, y, z + h], texture_coordinates: [w, 1.0, t.3 as f32], normal: [0.0, 0.0, 1.0] },
|
||||
|
||||
// Bottom
|
||||
Vertex { position: [x, y, z + 0.0], texture_coordinates: [bottom.0 as f32 * S + S, bottom.1 as f32 * S ], normal: [0.0, -1.0, 0.0] },
|
||||
Vertex { position: [x, y, z + h ], texture_coordinates: [bottom.0 as f32 * S + S, bottom.1 as f32 * S + S], normal: [0.0, -1.0, 0.0] },
|
||||
Vertex { position: [x + w, y, z + h ], texture_coordinates: [bottom.0 as f32 * S, bottom.1 as f32 * S + S], normal: [0.0, -1.0, 0.0] },
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [bottom.0 as f32 * S, bottom.1 as f32 * S ], normal: [0.0, -1.0, 0.0] },
|
||||
Vertex { position: [x, y, z + 0.0], texture_coordinates: [w, 0.0, t.4 as f32], normal: [0.0, -1.0, 0.0] },
|
||||
Vertex { position: [x, y, z + h ], texture_coordinates: [w, h, t.4 as f32], normal: [0.0, -1.0, 0.0] },
|
||||
Vertex { position: [x + w, y, z + h ], texture_coordinates: [0.0, h, t.4 as f32], normal: [0.0, -1.0, 0.0] },
|
||||
Vertex { position: [x + w, y, z ], texture_coordinates: [0.0, 0.0, t.4 as f32], normal: [0.0, -1.0, 0.0] },
|
||||
|
||||
// Top
|
||||
Vertex { position: [x, y + 1.0, z ], texture_coordinates: [top.0 as f32 * S, top.1 as f32 * S ], normal: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [x, y + 1.0, z + h], texture_coordinates: [top.0 as f32 * S, top.1 as f32 * S + S], normal: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z + h], texture_coordinates: [top.0 as f32 * S + S, top.1 as f32 * S + S], normal: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z ], texture_coordinates: [top.0 as f32 * S + S, top.1 as f32 * S ], normal: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [x, y + 1.0, z ], texture_coordinates: [0.0, 0.0, t.5 as f32], normal: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [x, y + 1.0, z + h], texture_coordinates: [0.0, h, t.5 as f32], normal: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z + h], texture_coordinates: [w, h, t.5 as f32], normal: [0.0, 1.0, 0.0] },
|
||||
Vertex { position: [x + w, y + 1.0, z ], texture_coordinates: [w, 0.0, t.5 as f32], normal: [0.0, 1.0, 0.0] },
|
||||
];
|
||||
vertices
|
||||
}
|
||||
|
|
|
@ -18,13 +18,13 @@ var<uniform> light: Light;
|
|||
|
||||
struct VertexInput {
|
||||
[[location(0)]] position: vec3<f32>;
|
||||
[[location(1)]] texture_coordinates: vec2<f32>;
|
||||
[[location(1)]] texture_coordinates: vec3<f32>;
|
||||
[[location(2)]] normal: vec3<f32>;
|
||||
};
|
||||
|
||||
struct VertexOutput {
|
||||
[[builtin(position)]] clip_position: vec4<f32>;
|
||||
[[location(0)]] texture_coordinates: vec2<f32>;
|
||||
[[location(0)]] texture_coordinates: vec3<f32>;
|
||||
[[location(1)]] world_normal: vec3<f32>;
|
||||
[[location(2)]] world_position: vec3<f32>;
|
||||
};
|
||||
|
@ -39,13 +39,17 @@ fn main(model: VertexInput) -> VertexOutput {
|
|||
return out;
|
||||
}
|
||||
|
||||
[[group(0), binding(0)]] var sampler_diffuse: sampler;
|
||||
[[group(0), binding(1)]] var texture: texture_2d<f32>;
|
||||
[[group(0), binding(0)]] var texture_sampler: sampler;
|
||||
[[group(0), binding(1)]] var texture_array: texture_2d_array<f32>;
|
||||
|
||||
[[stage(fragment)]]
|
||||
fn main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
|
||||
let object_color: vec4<f32> =
|
||||
textureSample(texture, sampler_diffuse, in.texture_coordinates);
|
||||
let object_color: vec4<f32> = textureSample(
|
||||
texture_array,
|
||||
texture_sampler,
|
||||
in.texture_coordinates.xy,
|
||||
i32(in.texture_coordinates.z)
|
||||
);
|
||||
|
||||
let ambient_strength = 0.1;
|
||||
let ambient_color = light.color * ambient_strength;
|
||||
|
|
29
src/state.rs
29
src/state.rs
|
@ -16,43 +16,43 @@ 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],
|
||||
texture_coordinates: [240.0 / 256.0, 0.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [UI_SCALE_X * 8.0, UI_SCALE_Y * 8.0, 0.0],
|
||||
texture_coordinates: [1.0, 0.0 / 256.0],
|
||||
texture_coordinates: [1.0, 0.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [UI_SCALE_X * 8.0, -UI_SCALE_Y * 8.0, 0.0],
|
||||
texture_coordinates: [1.0, 16.0 / 256.0],
|
||||
texture_coordinates: [1.0, 16.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.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],
|
||||
texture_coordinates: [240.0 / 256.0, 16.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.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],
|
||||
texture_coordinates: [0.0 / 256.0, 0.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.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],
|
||||
texture_coordinates: [182.0 / 256.0, 0.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [UI_SCALE_X * 91.0, -1.0, 0.0],
|
||||
texture_coordinates: [182.0 / 256.0, 22.0 / 256.0],
|
||||
texture_coordinates: [182.0 / 256.0, 22.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
},
|
||||
Vertex {
|
||||
position: [-UI_SCALE_X * 91.0, -1.0, 0.0],
|
||||
texture_coordinates: [0.0 / 256.0, 22.0 / 256.0],
|
||||
texture_coordinates: [0.0 / 256.0, 22.0 / 256.0, 0.0],
|
||||
normal: [0.0, 0.0, 0.0],
|
||||
},
|
||||
];
|
||||
|
@ -258,7 +258,7 @@ impl State {
|
|||
(swap_chain_descriptor, swap_chain)
|
||||
}
|
||||
|
||||
pub async fn new(window: &Window) -> Self {
|
||||
pub async fn new(window: &Window) -> State {
|
||||
let window_size = window.inner_size();
|
||||
|
||||
let (render_surface, render_adapter, render_device, render_queue) =
|
||||
|
@ -279,7 +279,7 @@ impl State {
|
|||
|
||||
let ui_crosshair_vertex_buffer = render_device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: Some("GUI crosshair vertex buffer"),
|
||||
contents: bytemuck::cast_slice(CROSSHAIR_VERTICES),
|
||||
contents: bytemuck::cast_slice(&CROSSHAIR_VERTICES),
|
||||
usage: wgpu::BufferUsage::VERTEX,
|
||||
});
|
||||
let ui_crosshair_index_buffer = render_device.create_buffer_init(&BufferInitDescriptor {
|
||||
|
@ -476,17 +476,14 @@ impl State {
|
|||
|
||||
render_pass.set_pipeline(&self.world_state.render_pipeline);
|
||||
|
||||
let tm = &self.world_state.texture_manager;
|
||||
render_pass.set_bind_group(0, tm.bind_group.as_ref().unwrap(), &[]);
|
||||
render_pass.set_bind_group(1, &self.world_state.uniform_bind_group, &[]);
|
||||
render_pass.set_bind_group(2, &self.world_state.light_bind_group, &[]);
|
||||
|
||||
// Set the texture
|
||||
let texture_bind_group = &self.world_state.texture_bind_group;
|
||||
render_pass.set_bind_group(0, texture_bind_group, &[]);
|
||||
|
||||
for (chunk_vertices, chunk_indices, index_count) in &self.world_state.chunk_buffers {
|
||||
for (chunk_vertices, chunk_indices, _, index_count) in &self.world_state.chunk_buffers {
|
||||
render_pass.set_vertex_buffer(0, chunk_vertices.slice(..));
|
||||
render_pass.set_index_buffer(chunk_indices.slice(..), wgpu::IndexFormat::Uint16);
|
||||
|
||||
render_pass.draw_indexed(0..*index_count as u32, 0, 0..1);
|
||||
triangle_count += index_count / 3;
|
||||
}
|
||||
|
|
166
src/texture.rs
166
src/texture.rs
|
@ -1,6 +1,9 @@
|
|||
use std::num::NonZeroU32;
|
||||
|
||||
use image::EncodableLayout;
|
||||
use wgpu::Origin3d;
|
||||
|
||||
use crate::texture;
|
||||
|
||||
pub struct Texture {
|
||||
pub texture: wgpu::Texture,
|
||||
|
@ -40,7 +43,7 @@ impl Texture {
|
|||
mag_filter: wgpu::FilterMode::Linear,
|
||||
min_filter: wgpu::FilterMode::Linear,
|
||||
mipmap_filter: wgpu::FilterMode::Nearest,
|
||||
compare: Some(wgpu::CompareFunction::LessEqual), // 5.
|
||||
compare: Some(wgpu::CompareFunction::LessEqual),
|
||||
lod_min_clamp: -100.0,
|
||||
lod_max_clamp: 100.0,
|
||||
..Default::default()
|
||||
|
@ -59,7 +62,6 @@ impl Texture {
|
|||
bytes: &[u8],
|
||||
label: &str,
|
||||
) -> anyhow::Result<Self> {
|
||||
println!("decoding image");
|
||||
let image = image::load_from_memory(bytes)?;
|
||||
let rgba = image.into_rgba8();
|
||||
let (width, height) = rgba.dimensions();
|
||||
|
@ -70,7 +72,6 @@ impl Texture {
|
|||
depth_or_array_layers: 1,
|
||||
};
|
||||
|
||||
println!("creating texture");
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some(label),
|
||||
size: texture_size,
|
||||
|
@ -78,10 +79,11 @@ impl Texture {
|
|||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
||||
usage: wgpu::TextureUsage::SAMPLED
|
||||
| wgpu::TextureUsage::COPY_DST
|
||||
| wgpu::TextureUsage::COPY_SRC,
|
||||
});
|
||||
|
||||
println!("writing texture");
|
||||
queue.write_texture(
|
||||
wgpu::ImageCopyTexture {
|
||||
texture: &texture,
|
||||
|
@ -97,15 +99,12 @@ impl Texture {
|
|||
texture_size,
|
||||
);
|
||||
|
||||
println!("creating texture view");
|
||||
let view = texture.create_view(&wgpu::TextureViewDescriptor {
|
||||
label: Some(&format!("texture_view_{}", label)),
|
||||
dimension: Some(wgpu::TextureViewDimension::D2),
|
||||
..wgpu::TextureViewDescriptor::default()
|
||||
});
|
||||
|
||||
println!("done");
|
||||
|
||||
Ok(Self {
|
||||
texture,
|
||||
sampler: None,
|
||||
|
@ -113,3 +112,154 @@ impl Texture {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub const TEXTURE_COUNT: usize = 8;
|
||||
|
||||
pub struct TextureManager {
|
||||
pub bind_group_layout: wgpu::BindGroupLayout,
|
||||
pub sampler: wgpu::Sampler,
|
||||
|
||||
pub textures: Vec<Texture>,
|
||||
pub bind_group: Option<wgpu::BindGroup>,
|
||||
}
|
||||
|
||||
impl TextureManager {
|
||||
pub fn new(render_device: &wgpu::Device) -> Self {
|
||||
let bind_group_layout =
|
||||
render_device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("texture_bind_group_layout"),
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler {
|
||||
comparison: false,
|
||||
filtering: true,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
||||
view_dimension: wgpu::TextureViewDimension::D2Array,
|
||||
multisampled: false,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let sampler = render_device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
address_mode_u: wgpu::AddressMode::Repeat,
|
||||
address_mode_v: wgpu::AddressMode::Repeat,
|
||||
address_mode_w: wgpu::AddressMode::Repeat,
|
||||
..wgpu::SamplerDescriptor::default()
|
||||
});
|
||||
|
||||
Self {
|
||||
bind_group_layout,
|
||||
sampler,
|
||||
|
||||
textures: Vec::new(),
|
||||
bind_group: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_all(&mut self, device: &wgpu::Device, queue: &wgpu::Queue) -> anyhow::Result<()> {
|
||||
self.load(device, queue, "assets/block/cobblestone.png")?; // 0
|
||||
self.load(device, queue, "assets/block/dirt.png")?; // 1
|
||||
self.load(device, queue, "assets/block/stone.png")?; // 2
|
||||
self.load(device, queue, "assets/grass_block_top_plains.png")?; // 3
|
||||
self.load(device, queue, "assets/grass_block_side_plains.png")?; // 4
|
||||
self.load(device, queue, "assets/block/bedrock.png")?; // 5
|
||||
self.load(device, queue, "assets/block/sand.png")?; // 6
|
||||
self.load(device, queue, "assets/block/gravel.png")?; // 7
|
||||
assert_eq!(TEXTURE_COUNT, self.textures.len());
|
||||
|
||||
let texture_array = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: None,
|
||||
size: wgpu::Extent3d {
|
||||
width: 512,
|
||||
height: 512,
|
||||
depth_or_array_layers: TEXTURE_COUNT as u32,
|
||||
},
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
|
||||
});
|
||||
|
||||
let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
|
||||
label: Some("texture copy encoder"),
|
||||
});
|
||||
|
||||
for (i, texture) in self.textures.iter().enumerate() {
|
||||
encoder.copy_texture_to_texture(
|
||||
wgpu::ImageCopyTexture {
|
||||
texture: &texture.texture,
|
||||
mip_level: 0,
|
||||
origin: Origin3d::ZERO,
|
||||
},
|
||||
wgpu::ImageCopyTexture {
|
||||
texture: &texture_array,
|
||||
mip_level: 0,
|
||||
origin: Origin3d {
|
||||
x: 0,
|
||||
y: 0,
|
||||
z: i as u32,
|
||||
},
|
||||
},
|
||||
wgpu::Extent3d {
|
||||
width: 512,
|
||||
height: 512,
|
||||
depth_or_array_layers: 1,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
queue.submit(std::iter::once(encoder.finish()));
|
||||
|
||||
let view = texture_array.create_view(&wgpu::TextureViewDescriptor {
|
||||
label: None,
|
||||
dimension: Some(wgpu::TextureViewDimension::D2Array),
|
||||
array_layer_count: NonZeroU32::new(TEXTURE_COUNT as u32),
|
||||
..wgpu::TextureViewDescriptor::default()
|
||||
});
|
||||
|
||||
self.bind_group = Some(device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some(&("Block texture bind group")),
|
||||
layout: &self.bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::Sampler(&self.sampler),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::TextureView(&view),
|
||||
},
|
||||
],
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
path: &str,
|
||||
) -> anyhow::Result<usize> {
|
||||
let bytes = std::fs::read(path)?;
|
||||
let texture = Texture::from_bytes(device, queue, &bytes, path)?;
|
||||
|
||||
let id = self.textures.len();
|
||||
self.textures.push(texture);
|
||||
|
||||
println!("loaded {} to {}", path, id);
|
||||
Ok(id)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use std::mem::size_of;
|
|||
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
pub struct Vertex {
|
||||
pub position: [f32; 3],
|
||||
pub texture_coordinates: [f32; 2],
|
||||
pub texture_coordinates: [f32; 3],
|
||||
pub normal: [f32; 3],
|
||||
}
|
||||
|
||||
|
@ -20,12 +20,12 @@ impl Vertex {
|
|||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: size_of::<[f32; 3]>() as wgpu::BufferAddress,
|
||||
offset: 12,
|
||||
shader_location: 1,
|
||||
format: wgpu::VertexFormat::Float32x2,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
wgpu::VertexAttribute {
|
||||
offset: size_of::<[f32; 3 + 2]>() as wgpu::BufferAddress,
|
||||
offset: 20,
|
||||
shader_location: 2,
|
||||
format: wgpu::VertexFormat::Float32x3,
|
||||
},
|
||||
|
|
|
@ -26,7 +26,7 @@ impl World {
|
|||
Self { chunks }
|
||||
}
|
||||
|
||||
pub fn to_instances(&self) -> Vec<(Vec<Vertex>, Vec<u16>)> {
|
||||
pub fn to_instances(&self) -> Vec<(Vec<Vertex>, Vec<u16>, Vec<Vec<(usize, usize, usize)>>)> {
|
||||
let instant = std::time::Instant::now();
|
||||
let mut instances = Vec::new();
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ use winit::dpi::PhysicalSize;
|
|||
use crate::{
|
||||
camera::{Camera, Projection},
|
||||
light::Light,
|
||||
texture::Texture,
|
||||
texture::{Texture, TextureManager},
|
||||
uniforms::Uniforms,
|
||||
vertex::Vertex,
|
||||
world::World,
|
||||
|
@ -18,75 +18,28 @@ pub struct WorldState {
|
|||
pub uniforms: Uniforms,
|
||||
pub uniform_buffer: wgpu::Buffer,
|
||||
pub uniform_bind_group: wgpu::BindGroup,
|
||||
pub texture_bind_group: wgpu::BindGroup,
|
||||
pub texture_manager: TextureManager,
|
||||
pub camera: Camera,
|
||||
pub projection: Projection,
|
||||
pub depth_texture: Texture,
|
||||
pub light_bind_group: wgpu::BindGroup,
|
||||
pub world: World,
|
||||
|
||||
pub chunk_buffers: Vec<(wgpu::Buffer, wgpu::Buffer, usize)>,
|
||||
pub chunk_buffers: Vec<(
|
||||
wgpu::Buffer,
|
||||
wgpu::Buffer,
|
||||
Vec<Vec<(usize, usize, usize)>>,
|
||||
usize,
|
||||
)>,
|
||||
}
|
||||
|
||||
impl WorldState {
|
||||
fn create_textures(
|
||||
render_device: &wgpu::Device,
|
||||
render_queue: &wgpu::Queue,
|
||||
) -> (wgpu::BindGroupLayout, wgpu::BindGroup) {
|
||||
let sampler = render_device.create_sampler(&wgpu::SamplerDescriptor::default());
|
||||
|
||||
let bind_group_layout =
|
||||
render_device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("texture_bind_group_layout"),
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler {
|
||||
comparison: false,
|
||||
filtering: true,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStage::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Float { filterable: true },
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
multisampled: false,
|
||||
},
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let instant = std::time::Instant::now();
|
||||
let texture = Texture::from_bytes(
|
||||
render_device,
|
||||
render_queue,
|
||||
include_bytes!("../assets/atlas.png"),
|
||||
"Block texture atlas",
|
||||
)
|
||||
.unwrap();
|
||||
println!("loading block texture atlas took {:?}", instant.elapsed());
|
||||
|
||||
let bind_group = render_device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("Block texture atlas bind group"),
|
||||
layout: &bind_group_layout,
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::TextureView(&texture.view),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
(bind_group_layout, bind_group)
|
||||
fn create_textures(render_device: &wgpu::Device, render_queue: &wgpu::Queue) -> TextureManager {
|
||||
let mut texture_manager = TextureManager::new(render_device);
|
||||
texture_manager
|
||||
.load_all(render_device, render_queue)
|
||||
.unwrap();
|
||||
texture_manager
|
||||
}
|
||||
|
||||
fn create_camera(swap_chain_descriptor: &wgpu::SwapChainDescriptor) -> (Camera, Projection) {
|
||||
|
@ -247,17 +200,13 @@ impl WorldState {
|
|||
}],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
strip_index_format: None,
|
||||
front_face: wgpu::FrontFace::Ccw,
|
||||
cull_mode: Some(wgpu::Face::Back),
|
||||
polygon_mode: if wireframe {
|
||||
wgpu::PolygonMode::Line
|
||||
} else {
|
||||
wgpu::PolygonMode::Fill
|
||||
},
|
||||
clamp_depth: false,
|
||||
conservative: false,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: Some(wgpu::DepthStencilState {
|
||||
format: Texture::DEPTH_FORMAT,
|
||||
|
@ -266,11 +215,7 @@ impl WorldState {
|
|||
stencil: wgpu::StencilState::default(),
|
||||
bias: wgpu::DepthBiasState::default(),
|
||||
}),
|
||||
multisample: wgpu::MultisampleState {
|
||||
count: 1,
|
||||
mask: !0,
|
||||
alpha_to_coverage_enabled: false,
|
||||
},
|
||||
multisample: wgpu::MultisampleState::default(),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -279,7 +224,7 @@ impl WorldState {
|
|||
|
||||
let world_geometry = self.world.to_instances();
|
||||
self.chunk_buffers.clear();
|
||||
for (chunk_vertices, chunk_indices) in world_geometry {
|
||||
for (chunk_vertices, chunk_indices, index_textures) in world_geometry {
|
||||
self.chunk_buffers.push((
|
||||
render_device.create_buffer_init(&BufferInitDescriptor {
|
||||
label: None,
|
||||
|
@ -291,6 +236,7 @@ impl WorldState {
|
|||
contents: &bytemuck::cast_slice(&chunk_indices),
|
||||
usage: wgpu::BufferUsage::INDEX,
|
||||
}),
|
||||
index_textures,
|
||||
chunk_indices.len(),
|
||||
));
|
||||
}
|
||||
|
@ -306,13 +252,12 @@ impl WorldState {
|
|||
) -> WorldState {
|
||||
let world = World::generate();
|
||||
|
||||
let (world_texture_layout, texture_bind_group) =
|
||||
Self::create_textures(&render_device, &render_queue);
|
||||
let texture_manager = Self::create_textures(render_device, render_queue);
|
||||
|
||||
let (camera, projection) = Self::create_camera(&swap_chain_descriptor);
|
||||
let (camera, projection) = Self::create_camera(swap_chain_descriptor);
|
||||
|
||||
let (uniforms, uniform_buffer, world_uniform_layout, uniform_bind_group) =
|
||||
Self::create_uniforms(&camera, &projection, &render_device);
|
||||
Self::create_uniforms(&camera, &projection, render_device);
|
||||
|
||||
let (_, _, world_light_layout, light_bind_group) = Self::create_light(&render_device);
|
||||
|
||||
|
@ -320,7 +265,7 @@ impl WorldState {
|
|||
&render_device,
|
||||
&swap_chain_descriptor,
|
||||
&[
|
||||
&world_texture_layout,
|
||||
&texture_manager.bind_group_layout,
|
||||
&world_uniform_layout,
|
||||
&world_light_layout,
|
||||
],
|
||||
|
@ -334,7 +279,7 @@ impl WorldState {
|
|||
uniforms,
|
||||
uniform_buffer,
|
||||
uniform_bind_group,
|
||||
texture_bind_group,
|
||||
texture_manager,
|
||||
camera,
|
||||
projection,
|
||||
depth_texture,
|
||||
|
|
Loading…
Reference in a new issue