Skip to content

Commit

Permalink
Fix #402 Updating instanced transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
asny committed Oct 20, 2023
1 parent 9a7ace0 commit 101ddac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
41 changes: 20 additions & 21 deletions examples/wireframe/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,34 +118,33 @@ fn edge_transformations(cpu_mesh: &CpuMesh) -> Instances {
let indices = cpu_mesh.indices.to_u32().unwrap();
let positions = cpu_mesh.positions.to_f32();
let mut transformations = Vec::new();
let mut keys = Vec::new();
for f in 0..indices.len() / 3 {
let mut fun = |i1, i2| {
let key = if i1 < i2 { (i1, i2) } else { (i2, i1) };
if !keys.contains(&key) {
keys.push(key);
let p1: Vec3 = positions[i1];
let p2: Vec3 = positions[i2];
transformations.push(
Mat4::from_translation(p1)
* Into::<Mat4>::into(Quat::from_arc(
vec3(1.0, 0.0, 0.0),
(p2 - p1).normalize(),
None,
))
* Mat4::from_nonuniform_scale((p1 - p2).magnitude(), 1.0, 1.0),
);
}
};
let i1 = indices[3 * f] as usize;
let i2 = indices[3 * f + 1] as usize;
let i3 = indices[3 * f + 2] as usize;
fun(i1, i2);
fun(i2, i3);
fun(i3, i1);

if i1 < i2 {
transformations.push(edge_transform(positions[i1], positions[i2]));
}
if i2 < i3 {
transformations.push(edge_transform(positions[i2], positions[i3]));
}
if i3 < i1 {
transformations.push(edge_transform(positions[i3], positions[i1]));
}
}
Instances {
transformations,
..Default::default()
}
}

fn edge_transform(p1: Vec3, p2: Vec3) -> Mat4 {
Mat4::from_translation(p1)
* Into::<Mat4>::into(Quat::from_arc(
vec3(1.0, 0.0, 0.0),
(p2 - p1).normalize(),
None,
))
* Mat4::from_nonuniform_scale((p1 - p2).magnitude(), 1.0, 1.0)
}
1 change: 1 addition & 0 deletions src/renderer/geometry/instanced_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl InstancedMesh {

// Next, we can compute the instance buffers with that ordering.
let instance_buffers = &mut s.0;
instance_buffers.clear();

if indices
.iter()
Expand Down

0 comments on commit 101ddac

Please sign in to comment.