Skip to content

Commit

Permalink
implement set material
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Oct 11, 2024
1 parent a6f0962 commit cfda827
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 45 deletions.
14 changes: 14 additions & 0 deletions crates/unavi-scripting/src/api/wired/scene/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ mod tests {
node: &Resource<NodeRes>,
primitive: &Resource<PrimitiveRes>,
) {
let default_material = data
.api
.wired_scene
.as_ref()
.unwrap()
.default_material
.clone();

let world = app.world_mut();
data.push_commands(&mut world.commands());
world.flush_commands();
Expand Down Expand Up @@ -240,6 +248,12 @@ mod tests {
node_entity
);
assert_eq!(world.get::<Children>(node_entity).unwrap().len(), 1);
assert_eq!(
world
.get::<Handle<StandardMaterial>>(inner.node_primitives[0].entity)
.unwrap(),
&default_material
);
}

fn test_remove_node_primitives(
Expand Down
135 changes: 90 additions & 45 deletions crates/unavi-scripting/src/api/wired/scene/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,50 +84,28 @@ impl HostPrimitive for ScriptData {
None => None,
};

let primitive = self.table.get(&self_)?;
primitive.write().material = material;

// let mesh = Resource::<MeshRes>::new_own(primitive.mesh);
// let mesh = self.table.get(&mesh)?;
//
// let mesh_nodes = mesh.nodes.iter().map(|r| r.rep()).collect::<Vec<_>>();
//
// let default_material = self
// .api
// .wired_scene
// .as_ref()
// .unwrap()
// .default_material
// .clone();
// let nodes = self
// .api
// .wired_scene
// .as_ref()
// .unwrap()
// .entities
// .nodes
// .clone();
//
// self.c.unwrap()ommand_send.try_send(Box::new(move |world: &mut World| {
// let nodes = nodes.read().unwrap();
//
// // let material = material_rep.map(|r| materials.get(&r).unwrap().handle.clone());
//
// for nid in mesh_nodes {
// let node_ent = nodes.get(&nid).unwrap(); // TODO: is this unwrap safe?
// let node_mesh = world.get::<NodeMesh>(*node_ent).unwrap();
//
// for (pid, p_ent) in node_mesh.node_primitives.clone() {
// if pid == rep {
// // if let Some(material) = &material {
// // world.entity_mut(p_ent).insert(material.clone());
// // } else {
// // world.entity_mut(p_ent).insert(default_material.clone());
// // }
// }
// }
// }
// });
let primitive = self.table.get(&self_)?.clone();
primitive.write().material = material.clone();

let default_material = self
.api
.wired_scene
.as_ref()
.unwrap()
.default_material
.clone();

self.command_send
.try_send(Box::new(move |world: &mut World| {
let handle = material
.map(|m| m.read().handle.get().unwrap().clone())
.unwrap_or_else(|| default_material);

for np in primitive.read().node_primitives.iter() {
world.entity_mut(np.entity).insert(handle.clone());
}
}))
.unwrap();

Ok(())
}
Expand Down Expand Up @@ -231,7 +209,10 @@ impl HostPrimitive for ScriptData {

#[cfg(test)]
mod tests {
use crate::api::tests::init_test_data;
use crate::api::{
tests::init_test_data,
wired::scene::bindings::{material::HostMaterial, node::HostNode},
};

use super::*;

Expand All @@ -250,4 +231,68 @@ mod tests {
HostPrimitive::drop(&mut data, res).unwrap();
assert!(data.table.get(&res_weak).is_err());
}

#[test]
fn test_set_material() {
let (mut app, mut data) = init_test_data();
let default_material = data
.api
.wired_scene
.as_ref()
.unwrap()
.default_material
.clone();

let material = HostMaterial::new(&mut data).unwrap();
let mesh = HostMesh::new(&mut data).unwrap();
let primitive =
HostMesh::create_primitive(&mut data, Resource::new_own(mesh.rep())).unwrap();

let node = HostNode::new(&mut data).unwrap();
HostNode::set_mesh(&mut data, node, Some(mesh)).unwrap();

let world = app.world_mut();
data.push_commands(&mut world.commands());
world.flush_commands();

let entity = data.table.get(&primitive).unwrap().read().node_primitives[0].entity;

// Set new material.
HostPrimitive::set_material(
&mut data,
Resource::new_own(primitive.rep()),
Some(Resource::new_own(material.rep())),
)
.unwrap();

let world = app.world_mut();
data.push_commands(&mut world.commands());
world.flush_commands();

let material_handle = data
.table
.get(&material)
.unwrap()
.read()
.handle
.get()
.unwrap()
.clone();
assert_eq!(
*app.world().get::<Handle<StandardMaterial>>(entity).unwrap(),
material_handle
);

// Remove material.
HostPrimitive::set_material(&mut data, Resource::new_own(primitive.rep()), None).unwrap();

let world = app.world_mut();
data.push_commands(&mut world.commands());
world.flush_commands();

assert_eq!(
*app.world().get::<Handle<StandardMaterial>>(entity).unwrap(),
default_material
);
}
}

0 comments on commit cfda827

Please sign in to comment.