Update packages
This commit is contained in:
parent
3cebdf3bfa
commit
ebef7fe1c5
12 changed files with 1126 additions and 792 deletions
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/wgpu-renderer.iml" filepath="$PROJECT_DIR$/.idea/wgpu-renderer.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
11
.idea/wgpu-renderer.iml
generated
Normal file
11
.idea/wgpu-renderer.iml
generated
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="EMPTY_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
1655
Cargo.lock
generated
1655
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
34
Cargo.toml
34
Cargo.toml
|
@ -14,27 +14,27 @@ path = "src/main.rs"
|
||||||
opt-level = 1 # loading gltf is super slow without this
|
opt-level = 1 # loading gltf is super slow without this
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
winit = "0.28.3"
|
winit = { version = "0.29.3", features = ["rwh_05"] }
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
log = "0.4.17"
|
log = "0.4.17"
|
||||||
wgpu = "0.15.1"
|
wgpu = "0.18.0"
|
||||||
pollster = "0.2.5"
|
pollster = "0.3.0"
|
||||||
bytemuck = { version = "1.13.0", features = ["derive"] }
|
bytemuck = { version = "1.14.0", features = ["derive"] }
|
||||||
image = { version = "0.24.5", features = ["png", "tga"] }
|
image = { version = "0.24.7", features = ["png", "tga"] }
|
||||||
anyhow = "1.0.68"
|
anyhow = "1.0.75"
|
||||||
cgmath = "0.18"
|
cgmath = "0.18"
|
||||||
tobj = { version = "3.2.1", features = ["async"] }
|
tobj = { version = "4.0.0", features = ["async"] }
|
||||||
gltf = "1.0.0"
|
gltf = "1.3.0"
|
||||||
wgpu-types = "0.15.2"
|
wgpu-types = "0.18.0"
|
||||||
regex = "1.7.1"
|
regex = "1.10.2"
|
||||||
rust-embed = { version = "6.4.2", features = ["debug-embed"]}
|
rust-embed = { version = "8.0.0", features = ["debug-embed"] }
|
||||||
cfg-if = "1"
|
cfg-if = "1.0.0"
|
||||||
instant = "0.1.12"
|
instant = { version = "0.1.12", features = ["wasm-bindgen"] }
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
console_error_panic_hook = "0.1.6"
|
console_error_panic_hook = "0.1.6"
|
||||||
console_log = "0.2.0"
|
console_log = "1.0.0"
|
||||||
wgpu = { version = "0.15.1", features = ["webgl"]}
|
wgpu = { version = "0.18.0", features = ["webgl"] }
|
||||||
wasm-bindgen = "0.2"
|
wasm-bindgen = "0.2"
|
||||||
wasm-bindgen-futures = "0.4.30"
|
wasm-bindgen-futures = "0.4.30"
|
||||||
web-sys = { version = "0.3", features = [
|
web-sys = { version = "0.3", features = [
|
||||||
|
@ -44,6 +44,6 @@ web-sys = { version = "0.3", features = [
|
||||||
] }
|
] }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
anyhow = "1.0.68"
|
anyhow = "1.0.75"
|
||||||
fs_extra = "1.2.0"
|
fs_extra = "1.3.0"
|
||||||
glob = "0.3.1"
|
glob = "0.3.1"
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#!/bin/bash -e
|
#!/bin/bash -e
|
||||||
|
|
||||||
wasm-pack build --target web
|
wasm-pack build --debug --target web
|
||||||
sed 's/.\/pkg/./g' index.html > pkg/index.html
|
sed 's/.\/pkg/./g' index.html > pkg/index.html
|
||||||
miniserve pkg --index index.html
|
miniserve pkg --index index.html
|
||||||
|
|
|
@ -2,6 +2,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
use cgmath::num_traits::clamp;
|
use cgmath::num_traits::clamp;
|
||||||
use winit::{dpi::PhysicalPosition, event::*};
|
use winit::{dpi::PhysicalPosition, event::*};
|
||||||
|
use winit::keyboard::{PhysicalKey, KeyCode};
|
||||||
|
|
||||||
pub const NEAR_PLANE: f32 = 1.0;
|
pub const NEAR_PLANE: f32 = 1.0;
|
||||||
pub const FAR_PLANE: f32 = 3000.0;
|
pub const FAR_PLANE: f32 = 3000.0;
|
||||||
|
@ -175,38 +176,32 @@ impl CameraController {
|
||||||
None => false,
|
None => false,
|
||||||
Some(event) => match event {
|
Some(event) => match event {
|
||||||
WindowEvent::KeyboardInput {
|
WindowEvent::KeyboardInput {
|
||||||
input:
|
event: key_event,
|
||||||
KeyboardInput {
|
|
||||||
state,
|
|
||||||
virtual_keycode: Some(keycode),
|
|
||||||
..
|
|
||||||
},
|
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
let is_pressed = *state == ElementState::Pressed;
|
let amount = if key_event.state == ElementState::Pressed { 1.0 } else { 0.0 };
|
||||||
let amount = if is_pressed { 1.0 } else { 0.0 };
|
match key_event.physical_key {
|
||||||
match keycode {
|
PhysicalKey::Code(KeyCode::KeyW) | PhysicalKey::Code(KeyCode::ArrowUp) => {
|
||||||
VirtualKeyCode::W | VirtualKeyCode::Up => {
|
|
||||||
self.move_forward = amount;
|
self.move_forward = amount;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
VirtualKeyCode::A | VirtualKeyCode::Left => {
|
PhysicalKey::Code(KeyCode::KeyA) | PhysicalKey::Code(KeyCode::ArrowLeft) => {
|
||||||
self.move_left = amount;
|
self.move_left = amount;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
VirtualKeyCode::S | VirtualKeyCode::Down => {
|
PhysicalKey::Code(KeyCode::KeyS) | PhysicalKey::Code(KeyCode::ArrowDown) => {
|
||||||
self.move_backward = amount;
|
self.move_backward = amount;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
VirtualKeyCode::D | VirtualKeyCode::Right => {
|
PhysicalKey::Code(KeyCode::KeyD) | PhysicalKey::Code(KeyCode::ArrowRight) => {
|
||||||
self.move_right = amount;
|
self.move_right = amount;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
VirtualKeyCode::Space => {
|
PhysicalKey::Code(KeyCode::Space) => {
|
||||||
self.move_up = amount;
|
self.move_up = amount;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
VirtualKeyCode::LControl => {
|
PhysicalKey::Code(KeyCode::ControlLeft) => {
|
||||||
self.move_down = amount;
|
self.move_down = amount;
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,7 +114,7 @@ pub async fn load_model_gltf(
|
||||||
// Average the tangents/bitangents
|
// Average the tangents/bitangents
|
||||||
for (i, n) in triangles_included.into_iter().enumerate() {
|
for (i, n) in triangles_included.into_iter().enumerate() {
|
||||||
let denom = 1.0 / n as f32;
|
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.tangent = (cgmath::Vector3::from(v.tangent) * denom).into();
|
||||||
v.bitangent = (cgmath::Vector3::from(v.bitangent) * 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::R8G8 => panic!(),
|
||||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8UnormSrgb, // converted
|
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8UnormSrgb, // converted
|
||||||
gltf::image::Format::R8G8B8A8 => wgpu::TextureFormat::Rgba8UnormSrgb,
|
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::R16 => panic!(),
|
||||||
gltf::image::Format::R16G16 => panic!(),
|
gltf::image::Format::R16G16 => panic!(),
|
||||||
gltf::image::Format::R16G16B16 => panic!(), // converted
|
gltf::image::Format::R16G16B16 => panic!(), // converted
|
||||||
gltf::image::Format::R16G16B16A16 => panic!(),
|
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::R8G8 => wgpu::TextureFormat::Rg8Unorm,
|
||||||
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8Unorm, // converted
|
gltf::image::Format::R8G8B8 => wgpu::TextureFormat::Rgba8Unorm, // converted
|
||||||
gltf::image::Format::R8G8B8A8 => wgpu::TextureFormat::Rgba8Unorm,
|
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::R16 => wgpu::TextureFormat::R16Unorm,
|
||||||
gltf::image::Format::R16G16 => wgpu::TextureFormat::Rg16Unorm,
|
gltf::image::Format::R16G16 => wgpu::TextureFormat::Rg16Unorm,
|
||||||
gltf::image::Format::R16G16B16 => wgpu::TextureFormat::Rgba16Unorm, // converted
|
gltf::image::Format::R16G16B16 => wgpu::TextureFormat::Rgba16Unorm, // converted
|
||||||
gltf::image::Format::R16G16B16A16 => wgpu::TextureFormat::Rgba16Unorm,
|
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::R8G8 => 2,
|
||||||
gltf::image::Format::R8G8B8 => 4, // converted
|
gltf::image::Format::R8G8B8 => 4, // converted
|
||||||
gltf::image::Format::R8G8B8A8 => 4,
|
gltf::image::Format::R8G8B8A8 => 4,
|
||||||
gltf::image::Format::B8G8R8 => 3,
|
|
||||||
gltf::image::Format::B8G8R8A8 => 4,
|
|
||||||
gltf::image::Format::R16 => 2,
|
gltf::image::Format::R16 => 2,
|
||||||
gltf::image::Format::R16G16 => 4,
|
gltf::image::Format::R16G16 => 4,
|
||||||
gltf::image::Format::R16G16B16 => 8, // converted
|
gltf::image::Format::R16G16B16 => 8, // converted
|
||||||
gltf::image::Format::R16G16B16A16 => 8,
|
gltf::image::Format::R16G16B16A16 => 8,
|
||||||
|
gltf::image::Format::R32G32B32FLOAT => 12,
|
||||||
|
gltf::image::Format::R32G32B32A32FLOAT => 16,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use cgmath::prelude::*;
|
use cgmath::prelude::*;
|
||||||
use wgpu::{InstanceDescriptor, Backends, TextureView, TextureViewDescriptor};
|
use wgpu::{InstanceDescriptor, Backends, TextureView, TextureViewDescriptor, StoreOp};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -52,7 +52,7 @@ impl State {
|
||||||
pub async fn new(window: &Window) -> Self {
|
pub async fn new(window: &Window) -> Self {
|
||||||
let size = window.inner_size();
|
let size = window.inner_size();
|
||||||
let instance = wgpu::Instance::new(InstanceDescriptor { backends: Backends::all(), ..Default::default() });
|
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
|
let adapter = instance
|
||||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||||
|
@ -61,8 +61,9 @@ impl State {
|
||||||
force_fallback_adapter: false,
|
force_fallback_adapter: false,
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.expect("failed to get adapter");
|
||||||
|
|
||||||
|
// TODO: some feature here doesn't work on WASM
|
||||||
let (device, queue) = adapter
|
let (device, queue) = adapter
|
||||||
.request_device(
|
.request_device(
|
||||||
&wgpu::DeviceDescriptor {
|
&wgpu::DeviceDescriptor {
|
||||||
|
@ -78,7 +79,7 @@ impl State {
|
||||||
None, // Trace path
|
None, // Trace path
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.expect("failed to get device");
|
||||||
|
|
||||||
let caps = surface.get_capabilities(&adapter);
|
let caps = surface.get_capabilities(&adapter);
|
||||||
let config = wgpu::SurfaceConfiguration {
|
let config = wgpu::SurfaceConfiguration {
|
||||||
|
@ -165,7 +166,7 @@ impl State {
|
||||||
base_mip_level: 0,
|
base_mip_level: 0,
|
||||||
mip_level_count: None,
|
mip_level_count: None,
|
||||||
base_array_layer: i as u32,
|
base_array_layer: i as u32,
|
||||||
array_layer_count: NonZeroU32::new(1),
|
array_layer_count: Some(1),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
|
@ -516,10 +517,12 @@ impl State {
|
||||||
view: &self.light_depth_texture_target_views[i],
|
view: &self.light_depth_texture_target_views[i],
|
||||||
depth_ops: Some(wgpu::Operations {
|
depth_ops: Some(wgpu::Operations {
|
||||||
load: wgpu::LoadOp::Clear(1.0),
|
load: wgpu::LoadOp::Clear(1.0),
|
||||||
store: true,
|
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_vertex_buffer(1, self.instance_buffer.slice(..));
|
||||||
|
@ -559,17 +562,19 @@ impl State {
|
||||||
b: 0.0,
|
b: 0.0,
|
||||||
a: 1.0,
|
a: 1.0,
|
||||||
}),
|
}),
|
||||||
store: true,
|
store: StoreOp::Store,
|
||||||
},
|
},
|
||||||
})],
|
})],
|
||||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
view: &self.depth_texture.view,
|
view: &self.depth_texture.view,
|
||||||
depth_ops: Some(wgpu::Operations {
|
depth_ops: Some(wgpu::Operations {
|
||||||
load: wgpu::LoadOp::Clear(1.0),
|
load: wgpu::LoadOp::Clear(1.0),
|
||||||
store: true,
|
store: StoreOp::Store,
|
||||||
}),
|
}),
|
||||||
stencil_ops: None,
|
stencil_ops: None,
|
||||||
}),
|
}),
|
||||||
|
timestamp_writes: None,
|
||||||
|
occlusion_query_set: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
geom_render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
|
geom_render_pass.set_vertex_buffer(1, self.instance_buffer.slice(..));
|
||||||
|
@ -592,17 +597,19 @@ impl State {
|
||||||
resolve_target: None,
|
resolve_target: None,
|
||||||
ops: wgpu::Operations {
|
ops: wgpu::Operations {
|
||||||
load: wgpu::LoadOp::Load,
|
load: wgpu::LoadOp::Load,
|
||||||
store: true,
|
store: StoreOp::Store,
|
||||||
},
|
},
|
||||||
})],
|
})],
|
||||||
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
view: &self.depth_texture.view,
|
view: &self.depth_texture.view,
|
||||||
depth_ops: Some(wgpu::Operations {
|
depth_ops: Some(wgpu::Operations {
|
||||||
load: wgpu::LoadOp::Load,
|
load: wgpu::LoadOp::Load,
|
||||||
store: true,
|
store: StoreOp::Store,
|
||||||
}),
|
}),
|
||||||
stencil_ops: None,
|
stencil_ops: None,
|
||||||
}),
|
}),
|
||||||
|
timestamp_writes: None,
|
||||||
|
occlusion_query_set: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
light_debug_render_pass.set_pipeline(&self.light_debug_pass.pipeline);
|
light_debug_render_pass.set_pipeline(&self.light_debug_pass.pipeline);
|
||||||
|
|
|
@ -94,8 +94,8 @@ impl Texture {
|
||||||
pixels,
|
pixels,
|
||||||
wgpu::ImageDataLayout {
|
wgpu::ImageDataLayout {
|
||||||
offset: 0,
|
offset: 0,
|
||||||
bytes_per_row: std::num::NonZeroU32::new(stride * dimensions.0),
|
bytes_per_row: Some(stride * dimensions.0),
|
||||||
rows_per_image: std::num::NonZeroU32::new(dimensions.1),
|
rows_per_image: Some(dimensions.1),
|
||||||
},
|
},
|
||||||
size,
|
size,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use super::state::State;
|
use super::state::State;
|
||||||
use winit::{
|
use winit::{
|
||||||
event::*,
|
event::*,
|
||||||
event_loop::{ControlFlow, EventLoop},
|
event_loop::{EventLoop},
|
||||||
window::WindowBuilder,
|
window::WindowBuilder,
|
||||||
};
|
};
|
||||||
|
use winit::keyboard::{KeyCode, PhysicalKey};
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
fn create_window(event_loop: &EventLoop<()>) -> winit::window::Window {
|
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() {
|
pub async fn run() {
|
||||||
let event_loop = EventLoop::new();
|
let event_loop = EventLoop::new().unwrap();
|
||||||
let window = create_window(&event_loop);
|
let window = create_window(&event_loop);
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
#[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;
|
use winit::platform::web::WindowExtWebSys;
|
||||||
web_sys::window()
|
web_sys::window()
|
||||||
.and_then(|win| win.document())
|
.and_then(|win| win.document())
|
||||||
.and_then(|doc| doc.body())
|
.and_then(|doc| doc.body())
|
||||||
.and_then(|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()
|
body.append_child(&canvas).ok()
|
||||||
})
|
})
|
||||||
.expect("Couldn't append canvas to document body.");
|
.expect("Couldn't append canvas to document body.");
|
||||||
|
@ -52,11 +47,39 @@ pub async fn run() {
|
||||||
let mut is_focused = true;
|
let mut is_focused = true;
|
||||||
|
|
||||||
// Event loop
|
// Event loop
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, window_target| {
|
||||||
match event {
|
match event {
|
||||||
Event::DeviceEvent { ref event, .. } => {
|
Event::DeviceEvent { ref event, .. } => {
|
||||||
state.input(None, Some(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 {
|
Event::WindowEvent {
|
||||||
ref event,
|
ref event,
|
||||||
window_id,
|
window_id,
|
||||||
|
@ -65,69 +88,48 @@ pub async fn run() {
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested
|
WindowEvent::CloseRequested
|
||||||
| WindowEvent::KeyboardInput {
|
| WindowEvent::KeyboardInput {
|
||||||
input:
|
event: KeyEvent {
|
||||||
KeyboardInput {
|
|
||||||
state: ElementState::Pressed,
|
state: ElementState::Pressed,
|
||||||
virtual_keycode: Some(VirtualKeyCode::Escape),
|
physical_key: PhysicalKey::Code(KeyCode::Escape),
|
||||||
..
|
..
|
||||||
},
|
},
|
||||||
..
|
..
|
||||||
} => {
|
} => {
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
{
|
{
|
||||||
*control_flow = ControlFlow::Exit;
|
window_target.exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WindowEvent::Resized(physical_size) => {
|
WindowEvent::Resized(physical_size) => {
|
||||||
state.resize(*physical_size);
|
state.resize(*physical_size);
|
||||||
}
|
window.request_redraw();
|
||||||
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
|
|
||||||
state.resize(**new_inner_size);
|
|
||||||
}
|
}
|
||||||
WindowEvent::Focused(focused) => {
|
WindowEvent::Focused(focused) => {
|
||||||
lock_cursor(&window, *focused);
|
lock_cursor(&window, *focused);
|
||||||
is_focused = *focused;
|
is_focused = *focused;
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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();
|
window.request_redraw();
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn lock_cursor(window: &winit::window::Window, lock: bool) {
|
fn lock_cursor(window: &winit::window::Window, lock: bool) {
|
||||||
if lock {
|
if lock {
|
||||||
window
|
match window
|
||||||
.set_cursor_grab(if cfg!(target_arch = "wasm32") {
|
.set_cursor_grab(if cfg!(target_arch = "wasm32") {
|
||||||
winit::window::CursorGrabMode::Locked
|
winit::window::CursorGrabMode::Locked
|
||||||
} else {
|
} else {
|
||||||
winit::window::CursorGrabMode::Confined
|
winit::window::CursorGrabMode::Confined
|
||||||
})
|
})
|
||||||
.unwrap();
|
{
|
||||||
|
Err(e) => println!("Failed to grab cursor {e:?}"),
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
window.set_cursor_visible(false);
|
window.set_cursor_visible(false);
|
||||||
} else {
|
} else {
|
||||||
window
|
window
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue