src/workshop_web.rs
Repository source snapshot · Plain text · GitHub
1//! Thin browser host. JavaScript owns fetch, the clock and input events.2use crate::{workshop::Frame, workshop_surface::WorkshopSurface};3use wasm_bindgen::prelude::*;45#[wasm_bindgen]6pub struct WorkshopRenderer {7 inner: WorkshopSurface,8}910#[wasm_bindgen]11impl WorkshopRenderer {12 // ANCHOR: web-create13 #[wasm_bindgen(js_name = create)]14 pub async fn create(canvas: web_sys::HtmlCanvasElement) -> Result<WorkshopRenderer, JsValue> {15 console_error_panic_hook::set_once();16 let width = canvas.width();17 let height = canvas.height();18 let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());19 let surface = instance20 .create_surface(wgpu::SurfaceTarget::Canvas(canvas))21 .map_err(|e| JsValue::from_str(&e.to_string()))?;22 let inner = WorkshopSurface::new(instance, surface, width, height)23 .await24 .map_err(|e| JsValue::from_str(&e))?;25 Ok(Self { inner })26 }27 // END: web-create2829 pub fn resize(&mut self, width: u32, height: u32) {30 self.inner.resize(width, height);31 }32 pub fn adapter(&self) -> String {33 self.inner.adapter_label.clone()34 }3536 #[wasm_bindgen(js_name = renderRelight)]37 pub fn render_relight(38 &mut self,39 azimuth: f32,40 elevation: f32,41 intensity: f32,42 roughness: f32,43 metallic: f32,44 time: f32,45 yaw: f32,46 mode: u32,47 wrong_normals: bool,48 ) -> Result<bool, JsValue> {49 self.inner50 .render_relight(crate::relight::RelightFrame {51 azimuth,52 elevation,53 intensity,54 roughness,55 metallic,56 time,57 yaw,58 mode,59 wrong_normals,60 })61 .map_err(|e| JsValue::from_str(&e))62 }6364 #[wasm_bindgen(js_name = loadModel)]65 pub fn load_model(&mut self, bytes: &[u8]) -> Result<(), JsValue> {66 self.inner67 .pass68 .load_model(&self.inner.device, bytes)69 .map_err(|e| JsValue::from_str(&e))70 }7172 pub fn render(73 &mut self,74 stage: u32,75 time: f32,76 yaw: f32,77 pitch: f32,78 distance: f32,79 reverse_order: bool,80 ) -> Result<i32, JsValue> {81 self.inner82 .render(Frame {83 stage,84 time,85 yaw,86 pitch,87 distance,88 reverse_order,89 })90 .map(|count| count.map_or(-1, |n| n as i32))91 .map_err(|e| JsValue::from_str(&e))92 }93}94