clippy suggestions

This commit is contained in:
Lauri Räsänen 2023-01-28 14:11:54 +02:00
parent c1148ea8bc
commit 8c51f91d00
9 changed files with 33 additions and 77 deletions

View file

@ -23,7 +23,7 @@ impl Projection {
} }
pub fn get_matrix(&self) -> cgmath::Matrix4<f32> { pub fn get_matrix(&self) -> cgmath::Matrix4<f32> {
return cgmath::perspective(cgmath::Deg(self.fovy), self.aspect, self.znear, self.zfar); cgmath::perspective(cgmath::Deg(self.fovy), self.aspect, self.znear, self.zfar)
} }
} }
@ -50,7 +50,7 @@ impl Camera {
pub fn get_view_matrix(&self) -> cgmath::Matrix4<f32> { pub fn get_view_matrix(&self) -> cgmath::Matrix4<f32> {
let (_right, up, forward) = self.get_vecs(); let (_right, up, forward) = self.get_vecs();
return cgmath::Matrix4::look_to_rh(self.position, forward, up); cgmath::Matrix4::look_to_rh(self.position, forward, up)
} }
pub fn get_vecs( pub fn get_vecs(
@ -67,7 +67,7 @@ impl Camera {
cgmath::Vector3::new(pitch_cos * yaw_cos, pitch_sin, pitch_cos * yaw_sin).normalize(); cgmath::Vector3::new(pitch_cos * yaw_cos, pitch_sin, pitch_cos * yaw_sin).normalize();
let right = cgmath::Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize(); let right = cgmath::Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
let up = right.cross(forward); let up = right.cross(forward);
return (right, up, forward); (right, up, forward)
} }
pub fn update(&mut self, dt: Duration, controller: &CameraController) { pub fn update(&mut self, dt: Duration, controller: &CameraController) {
@ -168,7 +168,7 @@ impl CameraController {
window_event: Option<&WindowEvent>, window_event: Option<&WindowEvent>,
device_event: Option<&DeviceEvent>, device_event: Option<&DeviceEvent>,
) -> bool { ) -> bool {
let mut handled = match window_event { let handled = match window_event {
None => false, None => false,
Some(event) => match event { Some(event) => match event {
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
@ -185,27 +185,27 @@ impl CameraController {
match keycode { match keycode {
VirtualKeyCode::W | VirtualKeyCode::Up => { VirtualKeyCode::W | VirtualKeyCode::Up => {
self.move_forward = amount; self.move_forward = amount;
return true; true
} }
VirtualKeyCode::A | VirtualKeyCode::Left => { VirtualKeyCode::A | VirtualKeyCode::Left => {
self.move_left = amount; self.move_left = amount;
return true; true
} }
VirtualKeyCode::S | VirtualKeyCode::Down => { VirtualKeyCode::S | VirtualKeyCode::Down => {
self.move_backward = amount; self.move_backward = amount;
return true; true
} }
VirtualKeyCode::D | VirtualKeyCode::Right => { VirtualKeyCode::D | VirtualKeyCode::Right => {
self.move_right = amount; self.move_right = amount;
return true; true
} }
VirtualKeyCode::Space => { VirtualKeyCode::Space => {
self.move_up = amount; self.move_up = amount;
return true; true
} }
VirtualKeyCode::LControl => { VirtualKeyCode::LControl => {
self.move_down = amount; self.move_down = amount;
return true; true
} }
_ => false, _ => false,
} }
@ -217,7 +217,7 @@ impl CameraController {
} else { } else {
self.speed /= 2.0; self.speed /= 2.0;
} }
return true; true
} }
MouseScrollDelta::PixelDelta(PhysicalPosition { y: scroll, .. }) => { MouseScrollDelta::PixelDelta(PhysicalPosition { y: scroll, .. }) => {
if *scroll > 0.0 { if *scroll > 0.0 {
@ -225,7 +225,7 @@ impl CameraController {
} else { } else {
self.speed /= 2.0; self.speed /= 2.0;
} }
return true; true
} }
}, },
_ => false, _ => false,
@ -236,15 +236,13 @@ impl CameraController {
return true; return true;
} }
handled = match device_event { match device_event {
Some(DeviceEvent::MouseMotion { delta }) => { Some(DeviceEvent::MouseMotion { delta }) => {
self.deltax += delta.0 as f32; self.deltax += delta.0 as f32;
self.deltay += delta.1 as f32; self.deltay += delta.1 as f32;
return true; true
} }
_ => false, _ => false,
}; }
return handled;
} }
} }

View file

@ -14,12 +14,12 @@ pub struct InstanceRaw {
impl Instance { impl Instance {
pub fn to_raw(&self) -> InstanceRaw { pub fn to_raw(&self) -> InstanceRaw {
return InstanceRaw { InstanceRaw {
model: (cgmath::Matrix4::from_translation(self.position) model: (cgmath::Matrix4::from_translation(self.position)
* cgmath::Matrix4::from(self.rotation)) * cgmath::Matrix4::from(self.rotation))
.into(), .into(),
normal: cgmath::Matrix3::from(self.rotation).into(), normal: cgmath::Matrix3::from(self.rotation).into(),
}; }
} }
} }

View file

@ -12,11 +12,11 @@ pub struct LightUniform {
impl LightUniform { impl LightUniform {
pub fn new(position: [f32; 3], color: [f32; 4]) -> Self { pub fn new(position: [f32; 3], color: [f32; 4]) -> Self {
return LightUniform { Self {
position, position,
_padding: 0, _padding: 0,
color, color,
}; }
} }
} }

View file

@ -57,7 +57,7 @@ impl Material {
label: None, label: None,
}); });
return Self { Self {
name: String::from(name), name: String::from(name),
diffuse_texture, diffuse_texture,
normal_texture, normal_texture,
@ -65,7 +65,7 @@ impl Material {
metallic_factor, metallic_factor,
roughness_factor, roughness_factor,
bind_group, bind_group,
}; }
} }
} }

View file

@ -8,10 +8,6 @@ use crate::core::texture::Texture;
#[folder = "res"] #[folder = "res"]
struct Asset; struct Asset;
pub fn load_binary(file_name: &str) -> Vec<u8> {
Asset::get(file_name).unwrap().data.into_owned()
}
pub fn load_string(file_name: &str) -> String { pub fn load_string(file_name: &str) -> String {
let binary = Asset::get(file_name).unwrap(); let binary = Asset::get(file_name).unwrap();
std::str::from_utf8(binary.data.as_ref()).unwrap().to_owned() std::str::from_utf8(binary.data.as_ref()).unwrap().to_owned()
@ -258,7 +254,7 @@ pub async fn load_model_gltf(
materials.push(Material::new( materials.push(Material::new(
device, device,
&material.name().unwrap_or("Default Material"), material.name().unwrap_or("Default Material"),
diffuse_texture, diffuse_texture,
normal_texture, normal_texture,
rm_texture, rm_texture,
@ -336,5 +332,5 @@ fn gltf_pixels_to_wgpu(mut bytes: Vec<u8>, format: gltf::image::Format) -> Vec<u
.collect(); .collect();
} }
return bytes; bytes
} }

View file

@ -280,7 +280,7 @@ impl State {
) )
}; };
return Self { Self {
size, size,
surface, surface,
device, device,
@ -300,7 +300,7 @@ impl State {
light_buffer, light_buffer,
light_render_pipeline, light_render_pipeline,
light_bind_group, light_bind_group,
}; }
} }
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) { pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
@ -322,9 +322,9 @@ impl State {
window_event: Option<&WindowEvent>, window_event: Option<&WindowEvent>,
device_event: Option<&DeviceEvent>, device_event: Option<&DeviceEvent>,
) -> bool { ) -> bool {
return self self
.camera_controller .camera_controller
.process_events(window_event, device_event); .process_events(window_event, device_event)
} }
pub fn update(&mut self, dt: Duration) { pub fn update(&mut self, dt: Duration) {
@ -411,7 +411,7 @@ impl State {
self.queue.submit(std::iter::once(encoder.finish())); self.queue.submit(std::iter::once(encoder.finish()));
output.present(); output.present();
return Ok(()); Ok(())
} }
} }
@ -425,7 +425,7 @@ fn create_render_pipeline(
) -> wgpu::RenderPipeline { ) -> wgpu::RenderPipeline {
let shader = device.create_shader_module(shader); let shader = device.create_shader_module(shader);
return device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"), label: Some("Render Pipeline"),
layout: Some(layout), layout: Some(layout),
vertex: wgpu::VertexState { vertex: wgpu::VertexState {
@ -470,5 +470,5 @@ fn create_render_pipeline(
alpha_to_coverage_enabled: false, alpha_to_coverage_enabled: false,
}, },
multiview: None, multiview: None,
}); })
} }

View file

@ -1,5 +1,4 @@
use anyhow::*; use anyhow::*;
use image::GenericImageView;
pub struct Texture { pub struct Texture {
pub texture: wgpu::Texture, pub texture: wgpu::Texture,
@ -52,41 +51,6 @@ impl Texture {
} }
} }
pub fn from_bytes(
device: &wgpu::Device,
queue: &wgpu::Queue,
bytes: &[u8],
label: &str,
is_normal_map: bool,
) -> Result<Self> {
let img = image::load_from_memory(bytes)?;
return Self::from_image(device, queue, &img, Some(label), is_normal_map);
}
pub fn from_image(
device: &wgpu::Device,
queue: &wgpu::Queue,
img: &image::DynamicImage,
label: Option<&str>,
is_normal_map: bool,
) -> Result<Self> {
let rgba = img.to_rgba8();
let dimensions = img.dimensions();
return Self::from_pixels(
device,
queue,
&rgba,
dimensions,
4,
if is_normal_map {
wgpu::TextureFormat::Rgba8UnormSrgb
} else {
wgpu::TextureFormat::Rgba8Unorm
},
label,
);
}
pub fn from_pixels( pub fn from_pixels(
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue, queue: &wgpu::Queue,
@ -140,10 +104,10 @@ impl Texture {
..Default::default() ..Default::default()
}); });
return Ok(Self { Ok(Self {
texture, texture,
view, view,
sampler, sampler,
}); })
} }
} }

View file

@ -1,5 +1,3 @@
#![allow(clippy::needless_return)]
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;

View file

@ -19,5 +19,5 @@ pub fn preprocess_wgsl(filename: &str) -> wgpu::ShaderSource {
source = source.replace(whole_match, &nested_source); source = source.replace(whole_match, &nested_source);
} }
return wgpu::ShaderSource::Wgsl(source.into()); wgpu::ShaderSource::Wgsl(source.into())
} }