Update packages
This commit is contained in:
parent
3cebdf3bfa
commit
ebef7fe1c5
12 changed files with 1126 additions and 792 deletions
|
@ -2,6 +2,7 @@ use std::time::Duration;
|
|||
|
||||
use cgmath::num_traits::clamp;
|
||||
use winit::{dpi::PhysicalPosition, event::*};
|
||||
use winit::keyboard::{PhysicalKey, KeyCode};
|
||||
|
||||
pub const NEAR_PLANE: f32 = 1.0;
|
||||
pub const FAR_PLANE: f32 = 3000.0;
|
||||
|
@ -175,38 +176,32 @@ impl CameraController {
|
|||
None => false,
|
||||
Some(event) => match event {
|
||||
WindowEvent::KeyboardInput {
|
||||
input:
|
||||
KeyboardInput {
|
||||
state,
|
||||
virtual_keycode: Some(keycode),
|
||||
..
|
||||
},
|
||||
event: key_event,
|
||||
..
|
||||
} => {
|
||||
let is_pressed = *state == ElementState::Pressed;
|
||||
let amount = if is_pressed { 1.0 } else { 0.0 };
|
||||
match keycode {
|
||||
VirtualKeyCode::W | VirtualKeyCode::Up => {
|
||||
let amount = if key_event.state == ElementState::Pressed { 1.0 } else { 0.0 };
|
||||
match key_event.physical_key {
|
||||
PhysicalKey::Code(KeyCode::KeyW) | PhysicalKey::Code(KeyCode::ArrowUp) => {
|
||||
self.move_forward = amount;
|
||||
true
|
||||
}
|
||||
VirtualKeyCode::A | VirtualKeyCode::Left => {
|
||||
PhysicalKey::Code(KeyCode::KeyA) | PhysicalKey::Code(KeyCode::ArrowLeft) => {
|
||||
self.move_left = amount;
|
||||
true
|
||||
}
|
||||
VirtualKeyCode::S | VirtualKeyCode::Down => {
|
||||
PhysicalKey::Code(KeyCode::KeyS) | PhysicalKey::Code(KeyCode::ArrowDown) => {
|
||||
self.move_backward = amount;
|
||||
true
|
||||
}
|
||||
VirtualKeyCode::D | VirtualKeyCode::Right => {
|
||||
PhysicalKey::Code(KeyCode::KeyD) | PhysicalKey::Code(KeyCode::ArrowRight) => {
|
||||
self.move_right = amount;
|
||||
true
|
||||
}
|
||||
VirtualKeyCode::Space => {
|
||||
PhysicalKey::Code(KeyCode::Space) => {
|
||||
self.move_up = amount;
|
||||
true
|
||||
}
|
||||
VirtualKeyCode::LControl => {
|
||||
PhysicalKey::Code(KeyCode::ControlLeft) => {
|
||||
self.move_down = amount;
|
||||
true
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ pub async fn load_model_gltf(
|
|||
// Average the tangents/bitangents
|
||||
for (i, n) in triangles_included.into_iter().enumerate() {
|
||||
let denom = 1.0 / n as f32;
|
||||
let mut v = &mut vertices[i];
|
||||
let v = &mut vertices[i];
|
||||
v.tangent = (cgmath::Vector3::from(v.tangent) * denom).into();
|
||||
v.bitangent = (cgmath::Vector3::from(v.bitangent) * denom).into();
|
||||
}
|
||||
|
@ -276,12 +276,12 @@ fn gltf_image_format_to_wgpu(format: gltf::image::Format, srgb: bool) -> wgpu::T
|
|||
gltf::image::Format::R8G8 => panic!(),
|
||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8UnormSrgb, // converted
|
||||
gltf::image::Format::R8G8B8A8 => wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
gltf::image::Format::B8G8R8 => wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
gltf::image::Format::B8G8R8A8 => wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
gltf::image::Format::R16 => panic!(),
|
||||
gltf::image::Format::R16G16 => panic!(),
|
||||
gltf::image::Format::R16G16B16 => panic!(), // converted
|
||||
gltf::image::Format::R16G16B16A16 => panic!(),
|
||||
gltf::image::Format::R32G32B32FLOAT => panic!(),
|
||||
gltf::image::Format::R32G32B32A32FLOAT => panic!(),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -290,12 +290,12 @@ fn gltf_image_format_to_wgpu(format: gltf::image::Format, srgb: bool) -> wgpu::T
|
|||
gltf::image::Format::R8G8 => wgpu::TextureFormat::Rg8Unorm,
|
||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8Unorm, // converted
|
||||
gltf::image::Format::R8G8B8A8 => wgpu::TextureFormat::Rgba8Unorm,
|
||||
gltf::image::Format::B8G8R8 => wgpu::TextureFormat::Bgra8Unorm,
|
||||
gltf::image::Format::B8G8R8A8 => wgpu::TextureFormat::Bgra8Unorm,
|
||||
gltf::image::Format::R16 => wgpu::TextureFormat::R16Unorm,
|
||||
gltf::image::Format::R16G16 => wgpu::TextureFormat::Rg16Unorm,
|
||||
gltf::image::Format::R16G16B16 => wgpu::TextureFormat::Rgba16Unorm, // converted
|
||||
gltf::image::Format::R16G16B16A16 => wgpu::TextureFormat::Rgba16Unorm,
|
||||
gltf::image::Format::R32G32B32FLOAT => wgpu::TextureFormat::Rgba32Float ,
|
||||
gltf::image::Format::R32G32B32A32FLOAT => wgpu::TextureFormat::Rgba32Float,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -305,12 +305,12 @@ fn gltf_image_format_stride(format: gltf::image::Format) -> u32 {
|
|||
gltf::image::Format::R8G8 => 2,
|
||||
gltf::image::Format::R8G8B8 => 4, // converted
|
||||
gltf::image::Format::R8G8B8A8 => 4,
|
||||
gltf::image::Format::B8G8R8 => 3,
|
||||
gltf::image::Format::B8G8R8A8 => 4,
|
||||
gltf::image::Format::R16 => 2,
|
||||
gltf::image::Format::R16G16 => 4,
|
||||
gltf::image::Format::R16G16B16 => 8, // converted
|
||||
gltf::image::Format::R16G16B16A16 => 8,
|
||||
gltf::image::Format::R32G32B32FLOAT => 12,
|
||||
gltf::image::Format::R32G32B32A32FLOAT => 16,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use cgmath::prelude::*;
|
||||
use wgpu::{InstanceDescriptor, Backends, TextureView, TextureViewDescriptor};
|
||||
use wgpu::{InstanceDescriptor, Backends, TextureView, TextureViewDescriptor, StoreOp};
|
||||
use std::default::Default;
|
||||
use std::num::NonZeroU32;
|
||||
use std::time::Duration;
|
||||
|
@ -52,7 +52,7 @@ impl State {
|
|||
pub async fn new(window: &Window) -> Self {
|
||||
let size = window.inner_size();
|
||||
let instance = wgpu::Instance::new(InstanceDescriptor { backends: Backends::all(), ..Default::default() });
|
||||
let surface = unsafe { instance.create_surface(window).unwrap() };
|
||||
let surface = unsafe { instance.create_surface(window) }.unwrap();
|
||||
|
||||
let adapter = instance
|
||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
|
@ -61,12 +61,13 @@ impl State {
|
|||
force_fallback_adapter: false,
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
.expect("failed to get adapter");
|
||||
|
||||
// TODO: some feature here doesn't work on WASM
|
||||
let (device, queue) = adapter
|
||||
.request_device(
|
||||
&wgpu::DeviceDescriptor {
|
||||
features: wgpu::Features::TEXTURE_BINDING_ARRAY
|
||||
features: wgpu::Features::TEXTURE_BINDING_ARRAY
|
||||
| wgpu::Features::SAMPLED_TEXTURE_AND_STORAGE_BUFFER_ARRAY_NON_UNIFORM_INDEXING,
|
||||
limits: if cfg!(target_arch = "wasm32") {
|
||||
wgpu::Limits::downlevel_webgl2_defaults()
|
||||
|
@ -78,7 +79,7 @@ impl State {
|
|||
None, // Trace path
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.expect("failed to get device");
|
||||
|
||||
let caps = surface.get_capabilities(&adapter);
|
||||
let config = wgpu::SurfaceConfiguration {
|
||||
|
@ -165,7 +166,7 @@ impl State {
|
|||
base_mip_level: 0,
|
||||
mip_level_count: None,
|
||||
base_array_layer: i as u32,
|
||||
array_layer_count: NonZeroU32::new(1),
|
||||
array_layer_count: Some(1),
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
|
@ -340,8 +341,8 @@ impl State {
|
|||
&queue,
|
||||
&texture_bind_group_layout,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let light_model = resources::load_model_gltf(
|
||||
"models/Cube.glb",
|
||||
|
@ -349,8 +350,8 @@ impl State {
|
|||
&queue,
|
||||
&texture_bind_group_layout,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let instances = vec![Instance {
|
||||
position: [0.0, 0.0, 0.0].into(),
|
||||
|
@ -509,18 +510,20 @@ impl State {
|
|||
|
||||
{
|
||||
let mut light_depth_render_pass =
|
||||
depth_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Light Depth Render Pass"),
|
||||
color_attachments: &[],
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &self.light_depth_texture_target_views[i],
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(1.0),
|
||||
store: true,
|
||||
depth_encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("Light Depth Render Pass"),
|
||||
color_attachments: &[],
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &self.light_depth_texture_target_views[i],
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(1.0),
|
||||
store: StoreOp::Store,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
}),
|
||||
});
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
});
|
||||
|
||||
light_depth_render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
|
||||
light_depth_render_pass.set_pipeline(&self.light_depth_pass.pipeline);
|
||||
|
@ -559,17 +562,19 @@ impl State {
|
|||
b: 0.0,
|
||||
a: 1.0,
|
||||
}),
|
||||
store: true,
|
||||
store: StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &self.depth_texture.view,
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Clear(1.0),
|
||||
store: true,
|
||||
store: StoreOp::Store,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
}),
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
});
|
||||
|
||||
geom_render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
|
||||
|
@ -592,17 +597,19 @@ impl State {
|
|||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: true,
|
||||
store: StoreOp::Store,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||
view: &self.depth_texture.view,
|
||||
depth_ops: Some(wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: true,
|
||||
store: StoreOp::Store,
|
||||
}),
|
||||
stencil_ops: None,
|
||||
}),
|
||||
timestamp_writes: None,
|
||||
occlusion_query_set: None,
|
||||
});
|
||||
|
||||
light_debug_render_pass.set_pipeline(&self.light_debug_pass.pipeline);
|
||||
|
|
|
@ -94,8 +94,8 @@ impl Texture {
|
|||
pixels,
|
||||
wgpu::ImageDataLayout {
|
||||
offset: 0,
|
||||
bytes_per_row: std::num::NonZeroU32::new(stride * dimensions.0),
|
||||
rows_per_image: std::num::NonZeroU32::new(dimensions.1),
|
||||
bytes_per_row: Some(stride * dimensions.0),
|
||||
rows_per_image: Some(dimensions.1),
|
||||
},
|
||||
size,
|
||||
);
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
use super::state::State;
|
||||
use winit::{
|
||||
event::*,
|
||||
event_loop::{ControlFlow, EventLoop},
|
||||
event_loop::{EventLoop},
|
||||
window::WindowBuilder,
|
||||
};
|
||||
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
fn create_window(event_loop: &EventLoop<()>) -> winit::window::Window {
|
||||
|
@ -25,23 +26,17 @@ fn create_window(event_loop: &EventLoop<()>) -> winit::window::Window {
|
|||
}
|
||||
|
||||
pub async fn run() {
|
||||
let event_loop = EventLoop::new();
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
let window = create_window(&event_loop);
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
// Winit prevents sizing with CSS, so we have to set
|
||||
// the size manually when on web.
|
||||
// https://github.com/rust-windowing/winit/pull/2074
|
||||
use winit::dpi::PhysicalSize;
|
||||
window.set_inner_size(PhysicalSize::new(1920, 1080));
|
||||
|
||||
use winit::platform::web::WindowExtWebSys;
|
||||
web_sys::window()
|
||||
.and_then(|win| win.document())
|
||||
.and_then(|doc| doc.body())
|
||||
.and_then(|body| {
|
||||
let canvas = web_sys::Element::from(window.canvas());
|
||||
let canvas = web_sys::Element::from(window.canvas().unwrap());
|
||||
body.append_child(&canvas).ok()
|
||||
})
|
||||
.expect("Couldn't append canvas to document body.");
|
||||
|
@ -52,11 +47,39 @@ pub async fn run() {
|
|||
let mut is_focused = true;
|
||||
|
||||
// Event loop
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
event_loop.run(move |event, window_target| {
|
||||
match event {
|
||||
Event::DeviceEvent { ref event, .. } => {
|
||||
state.input(None, Some(event));
|
||||
}
|
||||
// window render
|
||||
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested }
|
||||
if window_id == window.id() => {
|
||||
let now = instant::Instant::now();
|
||||
let dt = now - last_render;
|
||||
last_render = now;
|
||||
if is_focused {
|
||||
state.update(dt);
|
||||
match state.render() {
|
||||
Ok(_) => {
|
||||
window.request_redraw();
|
||||
}
|
||||
// Reconfigure the surface if lost
|
||||
Err(wgpu::SurfaceError::Lost) => {
|
||||
state.resize(state.size);
|
||||
window.request_redraw();
|
||||
}
|
||||
// The system is out of memory, we should probably quit
|
||||
Err(wgpu::SurfaceError::OutOfMemory) => window_target.exit(),
|
||||
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
||||
Err(e) => {
|
||||
eprintln!("{:?}", e);
|
||||
window.request_redraw();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// misc window input
|
||||
Event::WindowEvent {
|
||||
ref event,
|
||||
window_id,
|
||||
|
@ -65,69 +88,48 @@ pub async fn run() {
|
|||
match event {
|
||||
WindowEvent::CloseRequested
|
||||
| WindowEvent::KeyboardInput {
|
||||
input:
|
||||
KeyboardInput {
|
||||
state: ElementState::Pressed,
|
||||
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||
..
|
||||
},
|
||||
event: KeyEvent {
|
||||
state: ElementState::Pressed,
|
||||
physical_key: PhysicalKey::Code(KeyCode::Escape),
|
||||
..
|
||||
},
|
||||
..
|
||||
} => {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
*control_flow = ControlFlow::Exit;
|
||||
window_target.exit();
|
||||
}
|
||||
}
|
||||
WindowEvent::Resized(physical_size) => {
|
||||
state.resize(*physical_size);
|
||||
}
|
||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
||||
state.resize(**new_inner_size);
|
||||
window.request_redraw();
|
||||
}
|
||||
WindowEvent::Focused(focused) => {
|
||||
lock_cursor(&window, *focused);
|
||||
is_focused = *focused;
|
||||
window.request_redraw();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::RedrawRequested(window_id) if window_id == window.id() => {
|
||||
let now = instant::Instant::now();
|
||||
let dt = now - last_render;
|
||||
last_render = now;
|
||||
if is_focused {
|
||||
state.update(dt);
|
||||
match state.render() {
|
||||
Ok(_) => {}
|
||||
// Reconfigure the surface if lost
|
||||
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
|
||||
// The system is out of memory, we should probably quit
|
||||
Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit,
|
||||
// All other errors (Outdated, Timeout) should be resolved by the next frame
|
||||
Err(e) => eprintln!("{:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
Event::MainEventsCleared => {
|
||||
// RedrawRequested will only trigger once, unless we manually
|
||||
// request it.
|
||||
window.request_redraw();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
}).unwrap();
|
||||
}
|
||||
|
||||
fn lock_cursor(window: &winit::window::Window, lock: bool) {
|
||||
if lock {
|
||||
window
|
||||
match window
|
||||
.set_cursor_grab(if cfg!(target_arch = "wasm32") {
|
||||
winit::window::CursorGrabMode::Locked
|
||||
} else {
|
||||
winit::window::CursorGrabMode::Confined
|
||||
})
|
||||
.unwrap();
|
||||
{
|
||||
Err(e) => println!("Failed to grab cursor {e:?}"),
|
||||
_ => ()
|
||||
}
|
||||
window.set_cursor_visible(false);
|
||||
} else {
|
||||
window
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue