From df694989c9953e3b288ddd9d44b731ba2f33bfd4 Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Mon, 5 Apr 2021 10:32:51 +0200 Subject: [PATCH] cargo fmt --- build.rs | 2 +- src/camera.rs | 6 +++- src/main.rs | 46 ++++++++++++++++--------- src/model.rs | 33 +++++++++--------- src/terrain/mod.rs | 80 +++++++++++++++++++++++--------------------- src/terrain/table.rs | 53 ++++++++++++++++------------- 6 files changed, 122 insertions(+), 98 deletions(-) diff --git a/build.rs b/build.rs index 82b06d3..5c19f4d 100644 --- a/build.rs +++ b/build.rs @@ -1,12 +1,12 @@ use anyhow::*; use fs_extra::{copy_items, dir::CopyOptions}; use glob::glob; +use rayon::prelude::*; use std::path::PathBuf; use std::{ env, fs::{read_to_string, write}, }; -use rayon::prelude::*; struct ShaderData { src: String, diff --git a/src/camera.rs b/src/camera.rs index 73b8d23..84ac1e9 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -73,7 +73,11 @@ impl CameraController { } pub fn process_keyboard(&mut self, key: VirtualKeyCode, state: ElementState) -> bool { - let amount = if state == ElementState::Pressed { 1.0 } else { 0.0 }; + let amount = if state == ElementState::Pressed { + 1.0 + } else { + 0.0 + }; match key { VirtualKeyCode::Space => { self.amount_up = amount; diff --git a/src/main.rs b/src/main.rs index c1d34f5..7632fb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,20 @@ use imgui::im_str; use model::{DrawLight, DrawModel, Model, Vertex}; use wgpu::util::DeviceExt; -use winit::{dpi::PhysicalPosition, event::*, event_loop::{ControlFlow, EventLoop}, window::{Window, WindowBuilder}}; +use winit::{ + dpi::PhysicalPosition, + event::*, + event_loop::{ControlFlow, EventLoop}, + window::{Window, WindowBuilder}, +}; use cgmath::prelude::*; mod camera; mod light; mod model; -mod texture; mod terrain; +mod texture; type Vec3 = cgmath::Vector3; @@ -202,7 +207,7 @@ struct State { rotation_speed: f32, last_mouse_pos: PhysicalPosition, - mouse_pressed: bool + mouse_pressed: bool, } impl State { @@ -260,7 +265,10 @@ impl State { wgpu::BindGroupLayoutEntry { binding: 1, visibility: wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::Sampler { filtering: false, comparison: false }, + ty: wgpu::BindingType::Sampler { + filtering: false, + comparison: false, + }, count: None, }, // Normal map texture @@ -277,7 +285,10 @@ impl State { wgpu::BindGroupLayoutEntry { binding: 3, visibility: wgpu::ShaderStage::FRAGMENT, - ty: wgpu::BindingType::Sampler { filtering: false, comparison: false }, + ty: wgpu::BindingType::Sampler { + filtering: false, + comparison: false, + }, count: None, }, ], @@ -317,8 +328,8 @@ impl State { usage: wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST, }); - let projection = camera::Projection::new(sc_desc.width, sc_desc.height, cgmath::Deg(45.0), 0.1, 100.0); - + let projection = + camera::Projection::new(sc_desc.width, sc_desc.height, cgmath::Deg(45.0), 0.1, 100.0); let camera = camera::Camera::new((0.0, 5.0, -10.0), cgmath::Deg(90.0), cgmath::Deg(-20.0)); @@ -513,14 +524,16 @@ impl State { } fn input(&mut self, event: &WindowEvent) -> bool { - - match event { - WindowEvent::KeyboardInput { input: winit::event::KeyboardInput { - virtual_keycode: Some(key), - state, + WindowEvent::KeyboardInput { + input: + winit::event::KeyboardInput { + virtual_keycode: Some(key), + state, + .. + }, .. - }, .. } => { + } => { if self.imgui.io().want_capture_keyboard { true } else { @@ -550,7 +563,8 @@ impl State { fn update(&mut self, dt: f32) { self.camera_controller.update_camera(&mut self.camera, dt); - self.uniforms.update_view_proj(&self.camera, &self.projection); + self.uniforms + .update_view_proj(&self.camera, &self.projection); let mut encoder = self .device @@ -732,7 +746,7 @@ fn create_render_pipeline( strip_index_format: None, front_face: wgpu::FrontFace::Ccw, cull_mode: wgpu::CullMode::Back, - polygon_mode: wgpu::PolygonMode::Fill + polygon_mode: wgpu::PolygonMode::Fill, }, depth_stencil: depth_format.map(|format| wgpu::DepthStencilState { format, @@ -746,6 +760,6 @@ fn create_render_pipeline( count: 1, mask: !0, alpha_to_coverage_enabled: false, - } + }, }) } diff --git a/src/model.rs b/src/model.rs index bf52686..d6aeed5 100644 --- a/src/model.rs +++ b/src/model.rs @@ -159,23 +159,22 @@ impl Model { .map(|m| { let mut vertices: Vec<_> = (0..m.mesh.positions.len() / 3) .into_par_iter() - .map(|i| { - ModelVertex { - position: [ - m.mesh.positions[i * 3], - m.mesh.positions[i * 3 + 1], - m.mesh.positions[i * 3 + 2], - ], - tex_coords: [m.mesh.texcoords[i * 2], m.mesh.texcoords[i * 2 + 1]], - normal: [ - m.mesh.normals[i * 3], - m.mesh.normals[i * 3 + 1], - m.mesh.normals[i * 3 + 2], - ], - tangent: [0.0; 3], - bitangent: [0.0; 3], - } - }).collect(); + .map(|i| ModelVertex { + position: [ + m.mesh.positions[i * 3], + m.mesh.positions[i * 3 + 1], + m.mesh.positions[i * 3 + 2], + ], + tex_coords: [m.mesh.texcoords[i * 2], m.mesh.texcoords[i * 2 + 1]], + normal: [ + m.mesh.normals[i * 3], + m.mesh.normals[i * 3 + 1], + m.mesh.normals[i * 3 + 2], + ], + tangent: [0.0; 3], + bitangent: [0.0; 3], + }) + .collect(); let indices = &m.mesh.indices; diff --git a/src/terrain/mod.rs b/src/terrain/mod.rs index 749ec85..459e1d3 100644 --- a/src/terrain/mod.rs +++ b/src/terrain/mod.rs @@ -60,7 +60,7 @@ pub fn generate_terrain_marching_cube(seed: u64, chunk_coords: Vector3) -> let mut noise = FastNoise::new(); let chunk_coords = chunk_coords * CHUNK_SIZE as i32; noise.set_seed(seed); - noise.set_frequency(0.03); + noise.set_frequency(0.1); (0..CHUNK_ARRAY_SIZE) .into_par_iter() @@ -102,42 +102,43 @@ pub fn generate_terrain_marching_cube(seed: u64, chunk_coords: Vector3) -> let edge = table::EDGES[cube_index]; let points = calculate_points(edge, corners, values); + table::TRIANGLES[cube_index] + .into_par_iter() + .chunks(3) + .take(5) + .filter_map(move |i| { + if *i[0] < 0 { + return None; + } - let mut vertices = vec![]; - for i in (0..5).map(|i| i * 3) { - if table::TRIANGLES[cube_index][i] < 0 { - break; - } + let (a, b, c) = ( + points[*i[0] as usize], + points[*i[1] as usize], + points[*i[2] as usize], + ); - let (a, b, c) = ( - points[table::TRIANGLES[cube_index][i] as usize], - points[table::TRIANGLES[cube_index][i + 1] as usize], - points[table::TRIANGLES[cube_index][i + 2] as usize], - ); + let u = a - c; + let v = b - c; + let normal = u.cross(v).normalize(); - let u = b - a; - let v = c - a; - let normal = Vector3::new( - u.y * v.z - u.z * v.y, - u.z * v.x - u.x * v.z, - u.x * v.y - u.y * v.x, - ) - .normalize(); + //assert!(!normal.x.is_nan() && !normal.y.is_nan() && !normal.z.is_nan(), "normal: {:?}, u: {:?}, v: {:?}", normal, u, v); - vertices.push(TerrainVertex { - position: a.into(), - normal: normal.into(), - }); - vertices.push(TerrainVertex { - position: b.into(), - normal: normal.into(), - }); - vertices.push(TerrainVertex { - position: c.into(), - normal: normal.into(), - }); - } - vertices + Some(vec![ + TerrainVertex { + position: a.into(), + normal: normal.into(), + }, + TerrainVertex { + position: b.into(), + normal: normal.into(), + }, + TerrainVertex { + position: c.into(), + normal: normal.into(), + }, + ]) + }) + .flatten() }) .flatten() .collect() @@ -205,16 +206,17 @@ mod tests { use super::*; #[test] fn test_gen() { - let mut vert = generate_terrain_marching_cube(0, Vector3 { x: 0, y: 0, z: 0 }); - vert.extend(generate_terrain_marching_cube(0, Vector3 { x: 1, y: 0, z: 0 })); - vert.extend(generate_terrain_marching_cube(0, Vector3 { x: 0, y: 0, z: 1 })); - vert.extend(generate_terrain_marching_cube(0, Vector3 { x: 1, y: 0, z: 1 })); + let vert = generate_terrain_marching_cube(0, Vector3 { x: 0, y: 0, z: 0 }); let count = vert.len(); - for v in vert { + for v in &vert { let [x, y, z] = v.position; println!("v {} {} {}", x, y, z); } - for i in (0..count / 3).map(|i| i * 3 + 1) { + for v in &vert { + let [x, y, z] = v.normal; + println!("vn {} {} {}", x, y, z); + } + for i in (1..=count).step_by(3) { println!("f {} {} {}", i, i + 1, i + 2); } } diff --git a/src/terrain/table.rs b/src/terrain/table.rs index 73058ec..a80d3f1 100644 --- a/src/terrain/table.rs +++ b/src/terrain/table.rs @@ -1,27 +1,30 @@ - -pub const EDGES: [i32; 256] = -[ - 0x000, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00, - 0x190, 0x099, 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90, - 0x230, 0x339, 0x033, 0x13a, 0x636, 0x73f, 0x435, 0x53c, 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, - 0x3a0, 0x2a9, 0x1a3, 0x0aa, 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0, - 0x460, 0x569, 0x663, 0x76a, 0x066, 0x16f, 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60, - 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0x0ff, 0x3f5, 0x2fc, 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, - 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x055, 0x15c, 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950, - 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0x0cc, 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, - 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0x0cc, 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0, - 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 0x15c, 0x055, 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650, - 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, 0x3f5, 0x0ff, 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, - 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x066, 0x76a, 0x663, 0x569, 0x460, - 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 0x4ac, 0x5a5, 0x6af, 0x7a6, 0x0aa, 0x1a3, 0x2a9, 0x3a0, - 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x033, 0x339, 0x230, - 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x099, 0x190, - 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x000 +pub const EDGES: [i32; 256] = [ + 0x000, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c, 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, + 0xd03, 0xe09, 0xf00, 0x190, 0x099, 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c, 0x99c, 0x895, + 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90, 0x230, 0x339, 0x033, 0x13a, 0x636, 0x73f, 0x435, + 0x53c, 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30, 0x3a0, 0x2a9, 0x1a3, 0x0aa, + 0x7a6, 0x6af, 0x5a5, 0x4ac, 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0, 0x460, + 0x569, 0x663, 0x76a, 0x066, 0x16f, 0x265, 0x36c, 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, + 0xa69, 0xb60, 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0x0ff, 0x3f5, 0x2fc, 0xdfc, 0xcf5, 0xfff, + 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0, 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x055, 0x15c, + 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950, 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, + 0x2cf, 0x1c5, 0x0cc, 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0, 0x8c0, 0x9c9, + 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc, 0x0cc, 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, + 0x7c0, 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c, 0x15c, 0x055, 0x35f, 0x256, + 0x55a, 0x453, 0x759, 0x650, 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc, 0x2fc, + 0x3f5, 0x0ff, 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0, 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, + 0xd65, 0xc6c, 0x36c, 0x265, 0x16f, 0x066, 0x76a, 0x663, 0x569, 0x460, 0xca0, 0xda9, 0xea3, + 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac, 0x4ac, 0x5a5, 0x6af, 0x7a6, 0x0aa, 0x1a3, 0x2a9, 0x3a0, + 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c, 0x53c, 0x435, 0x73f, 0x636, 0x13a, + 0x033, 0x339, 0x230, 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c, 0x69c, 0x795, + 0x49f, 0x596, 0x29a, 0x393, 0x099, 0x190, 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, + 0x80c, 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x000, ]; -pub const TRIANGLES: [[i32; 16]; 256] = -[ - [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], +pub const TRIANGLES: [[i32; 16]; 256] = [ + [ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + ], [0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], [0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], [1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], @@ -276,5 +279,7 @@ pub const TRIANGLES: [[i32; 16]; 256] = [1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], [0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], [0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1] -]; \ No newline at end of file + [ + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + ], +];