This commit is contained in:
Lauri Räsänen 2023-01-27 23:16:57 +02:00
parent a7ea7ee65b
commit dc3bd2a433
6 changed files with 119 additions and 8 deletions

26
Cargo.lock generated
View file

@ -268,6 +268,26 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "console_error_panic_hook"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc"
dependencies = [
"cfg-if",
"wasm-bindgen",
]
[[package]]
name = "console_log"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501a375961cef1a0d44767200e66e4a559283097e91d0730b1d75dfb2f8a1494"
dependencies = [
"log",
"web-sys",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
@ -2241,7 +2261,10 @@ version = "0.1.0"
dependencies = [
"anyhow",
"bytemuck",
"cfg-if",
"cgmath",
"console_error_panic_hook",
"console_log",
"env_logger",
"fs_extra",
"glob",
@ -2252,6 +2275,9 @@ dependencies = [
"regex",
"rust-embed",
"tobj",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"wgpu",
"wgpu-types",
"winit",

View file

@ -3,7 +3,12 @@ name = "wgpu-renderer"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]
[[bin]]
name = "wgpu-renderer"
path = "src/main.rs"
[dependencies]
winit = "0.27.5"
@ -20,6 +25,19 @@ gltf = "1.0.0"
wgpu-types = "0.14.1"
regex = "1.7.1"
rust-embed = "6.4.2"
cfg-if = "1"
[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6"
console_log = "0.2.0"
wgpu = { version = "0.14", features = ["webgl"]}
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.30"
web-sys = { version = "0.3", features = [
"Document",
"Window",
"Element",
]}
[build-dependencies]
anyhow = "1.0.68"

25
index.html Normal file
View file

@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>wgpu-renderer</title>
<style>
canvas {
background-color: black;
}
</style>
</head>
<body id="wasm-example">
<script type="module">
import init from "./pkg/wgpu_renderer.js";
init().then(() => {
console.log("WASM Loaded");
});
</script>
</body>
</html>

View file

@ -17,9 +17,33 @@ pub async fn run() {
let mut state = State::new(&window).await;
let mut last_render = Instant::now();
#[cfg(target_arch = "wasm32")]
{
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init().expect("could not initialize logger");
// Winit prevents sizing with CSS, so we have to set
// the size manually when on web.
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());
body.append_child(&canvas).ok()
})
.expect("Couldn't append canvas to document body.");
}
#[cfg(not(target_arch = "wasm"))]
{
window
.set_cursor_grab(winit::window::CursorGrabMode::Confined)
.unwrap();
}
window.set_cursor_visible(false);
// Event loop

22
src/lib.rs Normal file
View file

@ -0,0 +1,22 @@
#![allow(clippy::needless_return)]
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
mod core;
mod shaders;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn run() {
#[cfg(target_arch = "wasm32")]
{
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
console_log::init_with_level(log::Level::Warn).expect("Couldn't initialize logger");
wasm_bindgen_futures::spawn_local(core::window::run());
}
#[cfg(not(target_arch = "wasm32"))]
{
env_logger::init();
pollster::block_on(core::window::run());
}
}

View file

@ -1,9 +1,5 @@
#![allow(clippy::needless_return)]
mod core;
mod shaders;
use wgpu_renderer::run;
fn main() {
env_logger::init();
pollster::block_on(core::window::run());
run();
}