#![allow(unused_imports)]
use super::*;
use crate::core::{BufferAttribute, BufferGeometry, ObjectKind};
fn with_mesh<R>(
scene: &mut WebScene,
handle: &WebObjectHandle,
f: impl FnOnce(
&mut Arc<BufferGeometry>,
&mut Arc<crate::Material>,
&mut Vec<Arc<crate::Material>>,
&mut Vec<f32>,
&mut std::collections::HashMap<String, usize>,
) -> R,
) -> Option<R> {
match &mut scene.inner.get_mut(handle.id)?.kind {
ObjectKind::Mesh(m) => Some(f(
&mut m.geometry,
&mut m.material,
&mut m.materials,
&mut m.morph_influences,
&mut m.morph_target_dictionary,
)),
ObjectKind::SkinnedMesh(m) => Some(f(
&mut m.geometry,
&mut m.material,
&mut m.materials,
&mut m.morph_influences,
&mut m.morph_target_dictionary,
)),
_ => None,
}
}
fn edit_geometry(
scene: &mut WebScene,
handle: &WebObjectHandle,
f: impl FnOnce(&mut BufferGeometry),
) {
with_mesh(scene, handle, |g, _, _, _, _| f(Arc::make_mut(g)));
}
fn js_count(count: f64) -> u32 {
if !count.is_finite() || count >= u32::MAX as f64 {
u32::MAX
} else {
count.max(0.0) as u32
}
}
#[wasm_bindgen]
impl WebScene {
#[wasm_bindgen(js_name = setMeshGroups)]
pub fn set_mesh_groups(&mut self, handle: &WebObjectHandle, flat: Vec<u32>) {
edit_geometry(self, handle, |g| {
g.clear_groups();
for c in flat.chunks_exact(3) {
g.add_group(c[0], c[1], c[2]);
}
});
}
#[wasm_bindgen(js_name = setMeshDrawRange)]
pub fn set_mesh_draw_range(&mut self, handle: &WebObjectHandle, start: f64, count: f64) {
edit_geometry(self, handle, |g| {
g.set_draw_range(start.max(0.0) as u32, js_count(count));
});
}
#[wasm_bindgen(js_name = clearMeshMaterials)]
pub fn clear_mesh_materials(&mut self, handle: &WebObjectHandle) {
with_mesh(self, handle, |_, _, mats, _, _| mats.clear());
}
#[wasm_bindgen(js_name = pushMeshMaterial)]
pub fn push_mesh_material(&mut self, handle: &WebObjectHandle, mat: &WebMaterial) {
with_mesh(self, handle, |_, single, mats, _, _| {
if mats.is_empty() {
*single = mat.inner.clone();
}
mats.push(mat.inner.clone());
});
}
#[wasm_bindgen(js_name = setMeshMaterialAt)]
pub fn set_mesh_material_at(
&mut self,
handle: &WebObjectHandle,
index: usize,
mat: &WebMaterial,
) {
with_mesh(self, handle, |_, single, mats, _, _| {
if let Some(slot) = mats.get_mut(index) {
*slot = mat.inner.clone();
if index == 0 {
*single = mat.inner.clone();
}
}
});
}
#[wasm_bindgen(js_name = setMorphAttribute)]
pub fn set_morph_attribute(
&mut self,
handle: &WebObjectHandle,
name: &str,
target: usize,
array: Vec<f32>,
item_size: usize,
) {
edit_geometry(self, handle, |g| {
let mut list = g.morph_attributes.remove(name).unwrap_or_default();
if list.len() <= target {
list.resize(
target + 1,
BufferAttribute::new(Vec::new(), item_size.max(1)),
);
}
list[target] = BufferAttribute::new(array, item_size.max(1));
g.set_morph_attribute(name.to_string(), list);
});
}
#[wasm_bindgen(js_name = clearMorphAttributes)]
pub fn clear_morph_attributes(&mut self, handle: &WebObjectHandle) {
edit_geometry(self, handle, |g| {
g.morph_attributes.clear();
g.morph_target_names.clear();
g.bump_version();
});
}
#[wasm_bindgen(js_name = setMorphTargetsRelative)]
pub fn set_morph_targets_relative(&mut self, handle: &WebObjectHandle, relative: bool) {
edit_geometry(self, handle, |g| g.morph_targets_relative = relative);
}
#[wasm_bindgen(js_name = setMorphTargetNames)]
pub fn set_morph_target_names(&mut self, handle: &WebObjectHandle, names: Vec<String>) {
edit_geometry(self, handle, |g| g.morph_target_names = names);
}
#[wasm_bindgen(js_name = updateMorphTargets)]
pub fn update_morph_targets(&mut self, handle: &WebObjectHandle) {
if let Some(obj) = self.inner.get_mut(handle.id) {
match &mut obj.kind {
ObjectKind::Mesh(m) => m.update_morph_targets(),
ObjectKind::SkinnedMesh(m) => m.update_morph_targets(),
_ => {}
}
}
}
#[wasm_bindgen(js_name = setMorphTargetInfluences)]
pub fn set_morph_target_influences(&mut self, handle: &WebObjectHandle, influences: Vec<f32>) {
with_mesh(self, handle, |_, _, _, w, _| *w = influences);
}
#[wasm_bindgen(js_name = getMorphTargetInfluences)]
pub fn get_morph_target_influences(&mut self, handle: &WebObjectHandle) -> Vec<f32> {
with_mesh(self, handle, |_, _, _, w, _| w.clone()).unwrap_or_default()
}
#[wasm_bindgen(js_name = getMorphTargetDictionary)]
pub fn get_morph_target_dictionary(&mut self, handle: &WebObjectHandle) -> JsValue {
let o = js_sys::Object::new();
if let Some(dict) = with_mesh(self, handle, |_, _, _, _, d| d.clone()) {
for (k, v) in dict {
let _ = js_sys::Reflect::set(&o, &k.into(), &(v as f64).into());
}
}
o.into()
}
#[wasm_bindgen(js_name = setMatrixAutoUpdate)]
pub fn set_matrix_auto_update(&mut self, handle: &WebObjectHandle, on: bool) {
if let Some(o) = self.inner.get_mut(handle.id) {
o.matrix_auto_update = on;
}
}
#[wasm_bindgen(js_name = setMatrixWorldAutoUpdate)]
pub fn set_matrix_world_auto_update(&mut self, handle: &WebObjectHandle, on: bool) {
if let Some(o) = self.inner.get_mut(handle.id) {
o.matrix_world_auto_update = on;
}
}
#[wasm_bindgen(js_name = setLocalMatrix)]
pub fn set_local_matrix(&mut self, handle: &WebObjectHandle, elements: Vec<f32>) {
if let (Some(o), Ok(e)) = (
self.inner.get_mut(handle.id),
<[f32; 16]>::try_from(elements),
) {
o.set_matrix(crate::math::Matrix4 { elements: e });
}
}
#[wasm_bindgen(js_name = applyMatrix4)]
pub fn apply_matrix4(&mut self, handle: &WebObjectHandle, elements: Vec<f32>) {
if let (Some(o), Ok(e)) = (
self.inner.get_mut(handle.id),
<[f32; 16]>::try_from(elements),
) {
o.apply_matrix4(&crate::math::Matrix4 { elements: e });
}
}
#[wasm_bindgen(js_name = setRotationEuler)]
pub fn set_rotation_euler(
&mut self,
handle: &WebObjectHandle,
x: f32,
y: f32,
z: f32,
order: &str,
) {
let order = crate::math::EulerOrder::parse(order).unwrap_or_default();
if let Some(o) = self.inner.get_mut(handle.id) {
o.set_rotation(crate::math::Euler::with_order(x, y, z, order));
}
}
#[wasm_bindgen(js_name = getRotationEuler)]
pub fn get_rotation_euler(&self, handle: &WebObjectHandle) -> Vec<f32> {
self.inner
.get(handle.id)
.map(|o| {
let e = o.rotation();
vec![e.x, e.y, e.z]
})
.unwrap_or_default()
}
#[wasm_bindgen(js_name = getMatrices)]
pub fn get_matrices(&mut self, handle: &WebObjectHandle) -> Vec<f32> {
self.inner.update_world();
self.inner
.get(handle.id)
.map(|o| {
o.matrix
.elements
.iter()
.chain(&o.matrix_world.elements)
.copied()
.collect()
})
.unwrap_or_default()
}
}
#[wasm_bindgen]
impl WebRaycaster {
#[wasm_bindgen(js_name = setLayersMask)]
pub fn set_layers_mask(&mut self, mask: u32) {
self.inner.layers.mask = mask;
}
#[wasm_bindgen(js_name = layersMask)]
pub fn layers_mask(&self) -> u32 {
self.inner.layers.mask
}
}