mirror of
https://github.com/guilhermewerner/wgpu-renderer
synced 2025-06-16 13:54:21 +00:00
Basic window and wasm support
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,2 +1,4 @@
|
|||||||
|
package/
|
||||||
target/
|
target/
|
||||||
|
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
15
Cargo.toml
15
Cargo.toml
@ -9,7 +9,7 @@ publish = false
|
|||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
name = "renderer"
|
name = "renderer"
|
||||||
crate-type = ["rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
@ -17,3 +17,16 @@ name = "renderer"
|
|||||||
path = "src/main.rs"
|
path = "src/main.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
cfg-if = "1"
|
||||||
|
env_logger = "0.10"
|
||||||
|
log = "0.4"
|
||||||
|
wgpu = "0.18"
|
||||||
|
winit = "0.28"
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
console_error_panic_hook = "0.1.6"
|
||||||
|
console_log = "1.0"
|
||||||
|
wasm-bindgen = "0.2"
|
||||||
|
wasm-bindgen-futures = "0.4.30"
|
||||||
|
web-sys = { version = "0.3", features = ["Document", "Window", "Element"] }
|
||||||
|
wgpu = { version = "0.18", features = ["webgl"] }
|
||||||
|
26
examples/index.html
Normal file
26
examples/index.html
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Renderer</title>
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<style>
|
||||||
|
canvas {
|
||||||
|
background-color: black;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body id="wasm-example">
|
||||||
|
<script type="module">
|
||||||
|
import init from "../package/renderer.js";
|
||||||
|
|
||||||
|
init().then(() => {
|
||||||
|
console.log("WASM Loaded");
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
3
scripts/wasm.ps1
Normal file
3
scripts/wasm.ps1
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env sh
|
||||||
|
|
||||||
|
wasm-pack build --target web -d package
|
62
src/lib.rs
62
src/lib.rs
@ -0,0 +1,62 @@
|
|||||||
|
use winit::{
|
||||||
|
dpi::LogicalSize,
|
||||||
|
event::*,
|
||||||
|
event_loop::{ControlFlow, EventLoop},
|
||||||
|
window::WindowBuilder,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
|
||||||
|
pub fn run() {
|
||||||
|
cfg_if::cfg_if! {
|
||||||
|
if #[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");
|
||||||
|
} else {
|
||||||
|
env_logger::init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let event_loop = EventLoop::new();
|
||||||
|
let window = WindowBuilder::new()
|
||||||
|
.with_title("Renderer")
|
||||||
|
.with_inner_size(LogicalSize::new(1280, 720))
|
||||||
|
.build(&event_loop)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
{
|
||||||
|
use winit::platform::web::WindowExtWebSys;
|
||||||
|
web_sys::window()
|
||||||
|
.and_then(|win| win.document())
|
||||||
|
.and_then(|doc| {
|
||||||
|
let dst = doc.get_element_by_id("wasm-example")?;
|
||||||
|
let canvas = web_sys::Element::from(window.canvas());
|
||||||
|
dst.append_child(&canvas).ok()?;
|
||||||
|
Some(())
|
||||||
|
})
|
||||||
|
.expect("Couldn't append canvas to document body.");
|
||||||
|
}
|
||||||
|
|
||||||
|
event_loop.run(move |event, _, control_flow| match event {
|
||||||
|
Event::WindowEvent {
|
||||||
|
ref event,
|
||||||
|
window_id,
|
||||||
|
} if window_id == window.id() => match event {
|
||||||
|
WindowEvent::CloseRequested
|
||||||
|
| WindowEvent::KeyboardInput {
|
||||||
|
input:
|
||||||
|
KeyboardInput {
|
||||||
|
state: ElementState::Pressed,
|
||||||
|
virtual_keycode: Some(VirtualKeyCode::Escape),
|
||||||
|
..
|
||||||
|
},
|
||||||
|
..
|
||||||
|
} => *control_flow = ControlFlow::Exit,
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
_ => {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -1 +1,5 @@
|
|||||||
fn main() {}
|
use renderer::run;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
run();
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user