#![allow(unused_imports)]
use super::*;
use crate::exporters::{ExportKind, ExportObject, NumberFormat, ObjOptions, PlyOptions};
use crate::loaders::{GltfExportOptions, GltfMaterialInfo, GltfMaterialKind};
struct Entry {
parent: Option<usize>,
name: String,
kind: Option<ExportKind>,
position: [f64; 3],
quaternion: [f64; 4],
scale: [f64; 3],
matrix_world: [f64; 16],
geometry: Option<Arc<crate::BufferGeometry>>,
material: Option<usize>,
}
#[wasm_bindgen]
pub struct WebSceneExporter {
entries: Vec<Entry>,
materials: Vec<(Arc<crate::Material>, GltfMaterialInfo)>,
}
fn fixed<const N: usize>(v: &[f64], what: &str) -> Result<[f64; N], JsValue> {
v.try_into()
.map_err(|_| JsValue::from_str(&format!("{what}: expected {N} numbers, got {}", v.len())))
}
#[wasm_bindgen]
impl WebSceneExporter {
#[wasm_bindgen(constructor)]
pub fn new() -> WebSceneExporter {
WebSceneExporter {
entries: Vec::new(),
materials: Vec::new(),
}
}
#[wasm_bindgen(js_name = addObject)]
pub fn add_object(
&mut self,
parent: i32,
name: String,
kind: &str,
position: &[f64],
quaternion: &[f64],
scale: &[f64],
matrix_world: &[f64],
) -> Result<u32, JsValue> {
let parent = usize::try_from(parent).ok();
if let Some(p) = parent
&& p >= self.entries.len()
{
return Err(JsValue::from_str(&format!("addObject: no parent {p}")));
}
let kind = match kind {
"mesh" => Some(ExportKind::Mesh),
"line" => Some(ExportKind::Line),
"lineSegments" => Some(ExportKind::LineSegments),
"lineLoop" => Some(ExportKind::LineLoop),
"points" => Some(ExportKind::Points),
_ => None,
};
self.entries.push(Entry {
parent,
name,
kind,
position: fixed(position, "position")?,
quaternion: fixed(quaternion, "quaternion")?,
scale: fixed(scale, "scale")?,
matrix_world: fixed(matrix_world, "matrixWorld")?,
geometry: None,
material: None,
});
Ok((self.entries.len() - 1) as u32)
}
#[wasm_bindgen(js_name = setBufferGeometry)]
pub fn set_buffer_geometry(
&mut self,
index: u32,
geometry: &WebBufferGeometry,
) -> Result<(), JsValue> {
self.entry(index)?.geometry = Some(geometry.inner.clone());
Ok(())
}
#[wasm_bindgen(js_name = setGeometry)]
pub fn set_geometry(&mut self, index: u32, geometry: &WebGeometry) -> Result<(), JsValue> {
self.entry(index)?.geometry = Some(geometry.inner.clone());
Ok(())
}
#[wasm_bindgen(js_name = addMaterial)]
pub fn add_material(
&mut self,
kind: &str,
name: String,
color: &[f32],
opacity: f32,
transparent: bool,
metalness: f32,
roughness: f32,
emissive: &[f32],
side: u32,
alpha_test: f32,
) -> u32 {
let rgb = |v: &[f32], d: f32| [0, 1, 2].map(|k| v.get(k).copied().unwrap_or(d));
let color = rgb(color, 1.0);
let emissive = rgb(emissive, 0.0);
let colour = crate::Color::new(color[0], color[1], color[2]);
let kind = match kind {
"standard" => GltfMaterialKind::Standard,
"basic" => GltfMaterialKind::Basic,
_ => GltfMaterialKind::Other,
};
let material: crate::Material = match kind {
GltfMaterialKind::Basic => crate::materials::BasicMaterial {
transparent,
opacity,
side,
alpha_test,
..crate::materials::BasicMaterial::new(colour)
}
.into(),
_ => crate::materials::StandardMaterial {
metalness,
roughness,
opacity,
side,
emissive: crate::Color::new(emissive[0], emissive[1], emissive[2]),
..crate::materials::StandardMaterial::new(colour)
}
.into(),
};
let info = GltfMaterialInfo {
name,
kind,
color,
opacity,
transparent,
metalness,
roughness,
emissive,
alpha_test,
double_sided: side == 2,
};
self.materials.push((Arc::new(material), info));
(self.materials.len() - 1) as u32
}
#[wasm_bindgen(js_name = setMaterial)]
pub fn set_material(&mut self, index: u32, material: u32) -> Result<(), JsValue> {
if material as usize >= self.materials.len() {
return Err(JsValue::from_str(&format!(
"setMaterial: no material {material}"
)));
}
self.entry(index)?.material = Some(material as usize);
Ok(())
}
pub fn obj(&self, polygons: bool) -> String {
crate::exporters::write_obj(
&self.flat(),
&ObjOptions {
polygons,
numbers: NumberFormat::JavaScript,
},
)
}
#[wasm_bindgen(js_name = stlAscii)]
pub fn stl_ascii(&self) -> String {
crate::exporters::write_stl_ascii(&self.flat(), NumberFormat::JavaScript)
}
#[wasm_bindgen(js_name = stlBinary)]
pub fn stl_binary(&self) -> Vec<u8> {
crate::exporters::write_stl_binary(&self.flat())
}
pub fn ply(
&self,
binary: bool,
little_endian: bool,
exclude_attributes: Vec<String>,
polygons: bool,
) -> Option<Vec<u8>> {
crate::exporters::write_ply(
&self.flat(),
&PlyOptions {
binary,
little_endian,
exclude_attributes,
polygons,
numbers: NumberFormat::JavaScript,
comment: None,
},
)
}
pub fn gltf(&self, trs: bool, scene_name: Option<String>) -> String {
let (arena, roots, options) = self.arena(trs, scene_name);
crate::loaders::scene_to_gltf_with(
&arena,
&roots,
&GltfExportOptions {
embed_buffer: true,
..options
},
)
.0
}
pub fn glb(&self, trs: bool, scene_name: Option<String>) -> Vec<u8> {
let (arena, roots, options) = self.arena(trs, scene_name);
crate::loaders::scene_to_glb_with(&arena, &roots, &options)
}
}
impl WebSceneExporter {
fn entry(&mut self, index: u32) -> Result<&mut Entry, JsValue> {
self.entries
.get_mut(index as usize)
.ok_or_else(|| JsValue::from_str(&format!("no object {index}")))
}
fn flat(&self) -> Vec<ExportObject> {
self.entries
.iter()
.filter_map(|e| {
Some(ExportObject {
name: e.name.clone(),
kind: e.kind?,
geometry: e.geometry.clone()?,
matrix_world: e.matrix_world,
material_name: e
.material
.map(|m| self.materials[m].1.name.clone())
.unwrap_or_default(),
})
})
.collect()
}
fn arena(
&self,
trs: bool,
scene_name: Option<String>,
) -> (
crate::core::ObjectArena,
Vec<crate::core::ObjectId>,
GltfExportOptions,
) {
let mut arena = crate::core::ObjectArena::new();
let mut ids = Vec::with_capacity(self.entries.len());
let mut roots = Vec::new();
let default_material = Arc::new(crate::Material::from(
crate::materials::StandardMaterial::new(crate::Color::WHITE),
));
for e in &self.entries {
let mut object = match (e.kind, &e.geometry) {
(Some(ExportKind::Mesh), Some(g)) => {
let material = e
.material
.map(|m| self.materials[m].0.clone())
.unwrap_or_else(|| default_material.clone());
crate::core::Object3D::mesh(crate::core::Mesh::from_arc(g.clone(), material))
}
_ => crate::core::Object3D::group(),
};
object.name = e.name.clone();
object.position = crate::Vector3::new(
e.position[0] as f32,
e.position[1] as f32,
e.position[2] as f32,
);
object.quaternion = crate::Quaternion::new(
e.quaternion[0] as f32,
e.quaternion[1] as f32,
e.quaternion[2] as f32,
e.quaternion[3] as f32,
);
object.scale =
crate::Vector3::new(e.scale[0] as f32, e.scale[1] as f32, e.scale[2] as f32);
let id = arena.insert(object);
match e.parent {
Some(p) => arena.add_child(ids[p], id),
None => roots.push(id),
}
ids.push(id);
}
let material_info = self
.materials
.iter()
.map(|(m, info)| (Arc::as_ptr(m) as usize, info.clone()))
.collect();
let options = GltfExportOptions {
generator: "THREE.GLTFExporter r165".into(),
trs,
three_js: true,
scene_name,
material_info,
..GltfExportOptions::default()
};
(arena, roots, options)
}
}
#[wasm_bindgen(js_name = writeGcode)]
pub fn write_gcode(
paths: &[f64],
counts: &[u32],
feed_rate: f64,
safe_z: f64,
tool_on: Vec<String>,
tool_off: Vec<String>,
) -> Result<String, JsValue> {
let total: usize = counts.iter().map(|&c| c as usize).sum();
if total * 3 != paths.len() {
return Err(JsValue::from_str(&format!(
"writeGcode: counts sum to {total} points, paths holds {} numbers",
paths.len()
)));
}
let mut at = 0;
let polylines: Vec<_> = counts
.iter()
.map(|&c| {
let pts = paths[at..at + c as usize * 3]
.chunks(3)
.map(|p| [p[0], p[1], p[2]])
.collect();
at += c as usize * 3;
crate::exporters::GcodePath::new(pts)
})
.collect();
let options = crate::exporters::GcodeOptions {
feed_rate,
safe_z: (!safe_z.is_nan()).then_some(safe_z),
tool_on,
tool_off,
..Default::default()
};
Ok(crate::exporters::write_gcode(&polylines, &options))
}