use super::*;
#[wasm_bindgen]
pub struct WebCss2dRenderer {
pub(super) inner: crate::Css2dRenderer,
}
#[wasm_bindgen]
impl WebCss2dRenderer {
#[wasm_bindgen(constructor)]
pub fn new(w: u32, h: u32) -> WebCss2dRenderer {
WebCss2dRenderer {
inner: crate::Css2dRenderer::new(w, h),
}
}
}
#[wasm_bindgen]
pub struct WebSvgRenderer {
pub(super) inner: crate::SvgRenderer,
}
#[wasm_bindgen]
impl WebSvgRenderer {
#[wasm_bindgen(constructor)]
pub fn new(w: u32, h: u32) -> WebSvgRenderer {
WebSvgRenderer {
inner: crate::SvgRenderer::new(w, h),
}
}
#[wasm_bindgen(js_name = setShading)]
pub fn set_shading(&mut self, mode: u32) {
self.inner.options.shading = match mode {
1 => crate::SvgShading::Flat,
2 => crate::SvgShading::Wireframe,
_ => crate::SvgShading::Lit,
};
}
#[wasm_bindgen(js_name = setToneMapping)]
pub fn set_tone_mapping(&mut self, mode: u32, exposure: f32) {
let mapping = match mode {
0 => crate::ToneMapping::None,
4 => crate::ToneMapping::AcesFilmic,
6 => crate::ToneMapping::AgX,
7 => crate::ToneMapping::Neutral,
_ => crate::ToneMapping::Linear,
};
self.inner.set_tone_mapping(mapping, exposure);
}
#[wasm_bindgen(js_name = setBackground)]
pub fn set_background(&mut self, on: bool) {
self.inner.options.background = on;
}
#[wasm_bindgen(js_name = setCullBackfaces)]
pub fn set_cull_backfaces(&mut self, on: bool) {
self.inner.options.cull_backfaces = on;
}
#[wasm_bindgen(js_name = setSeamStroke)]
pub fn set_seam_stroke(&mut self, px: f32) {
self.inner.options.seam_stroke = px.max(0.0);
}
#[wasm_bindgen(js_name = setPrecision)]
pub fn set_precision(&mut self, places: u32) {
self.inner.options.precision = places.min(8) as usize;
}
#[wasm_bindgen(js_name = setCurveTolerance)]
pub fn set_curve_tolerance(&mut self, px: f32) {
self.inner.options.curve_tolerance = (px > 0.0).then_some(px);
}
#[wasm_bindgen(js_name = setDepthSplit)]
pub fn set_depth_split(&mut self, tolerance: f32) {
self.inner.options.depth_split = (tolerance > 0.0).then_some(tolerance);
}
#[wasm_bindgen(js_name = setSort)]
pub fn set_sort(&mut self, on: bool) {
self.inner.options.sort = on;
}
#[wasm_bindgen(js_name = setSize)]
pub fn set_size(&mut self, width: u32, height: u32) {
self.inner.set_size(width, height);
}
#[wasm_bindgen(getter)]
pub fn width(&self) -> u32 {
self.inner.width
}
#[wasm_bindgen(getter)]
pub fn height(&self) -> u32 {
self.inner.height
}
#[wasm_bindgen(js_name = setClearColor)]
pub fn set_clear_color(&mut self, hex: u32, alpha: f32) {
self.inner.set_clear_color(
Some(crate::Color::from_hex(hex)),
Some(alpha.clamp(0.0, 1.0)),
);
}
#[wasm_bindgen(js_name = clearClearColor)]
pub fn clear_clear_color(&mut self) {
self.inner.set_clear_color(None, None);
}
#[wasm_bindgen(js_name = renderToString)]
pub fn render_to_string(&self, scene: &mut WebScene, camera: &WebCamera) -> String {
match &camera.inner {
CameraInner::Perspective(c) => self.inner.render_to_string(&mut scene.inner, c),
CameraInner::Orthographic(c) => self.inner.render_to_string(&mut scene.inner, c),
}
}
}
#[wasm_bindgen(js_name = svgFromRgba)]
pub fn svg_from_rgba(width: u32, height: u32, rgba: &[u8]) -> Result<String, JsValue> {
let want = width as usize * height as usize * 4;
if rgba.len() != want {
return Err(JsValue::from_str(&format!(
"svgFromRgba: expected {want} bytes for {width}x{height}, got {}",
rgba.len()
)));
}
Ok(crate::svg_from_rgba(width, height, rgba))
}