Skip to content

Commit 18b4898

Browse files
committed
Add support for unindexed meshes in Collider::from_bevy_mesh
fixes dimforge#490
1 parent 8d8d4ef commit 18b4898

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/geometry/collider_impl.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use na::DVector;
33
#[cfg(all(feature = "dim3", feature = "async-collider"))]
44
use {
55
bevy::prelude::*,
6-
bevy::render::mesh::{Indices, VertexAttributeValues},
6+
bevy::render::mesh::{Indices, PrimitiveTopology, VertexAttributeValues},
77
};
88

99
use rapier::prelude::{FeatureId, Point, Ray, SharedShape, Vector, DIM};
@@ -739,7 +739,7 @@ fn extract_mesh_vertices_indices(mesh: &Mesh) -> Option<(Vec<na::Point3<Real>>,
739739
use rapier::na::point;
740740

741741
let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION)?;
742-
let indices = mesh.indices()?;
742+
let indices = mesh.indices();
743743

744744
let vtx: Vec<_> = match vertices {
745745
VertexAttributeValues::Float32(vtx) => Some(
@@ -756,11 +756,40 @@ fn extract_mesh_vertices_indices(mesh: &Mesh) -> Option<(Vec<na::Point3<Real>>,
756756
}?;
757757

758758
let idx = match indices {
759-
Indices::U16(idx) => idx
759+
Some(Indices::U16(idx)) => idx
760760
.chunks_exact(3)
761761
.map(|i| [i[0] as u32, i[1] as u32, i[2] as u32])
762762
.collect(),
763-
Indices::U32(idx) => idx.chunks_exact(3).map(|i| [i[0], i[1], i[2]]).collect(),
763+
Some(Indices::U32(idx)) => idx.chunks_exact(3).map(|i| [i[0], i[1], i[2]]).collect(),
764+
None => {
765+
// Meshes loaded from glTF files may not necessarily have an index buffer
766+
// in order to save memory (e.g. files generated by osm2world), in which case
767+
// there's predefined algorithm to calculate indices for each topology.
768+
match mesh.primitive_topology() {
769+
PrimitiveTopology::TriangleList => {
770+
// [[0, 1, 2], [3, 4, 5], [6, 7, 8], ...]
771+
(0..vtx.len() as u32)
772+
.step_by(3)
773+
.map(|i| [i, i + 1, i + 2])
774+
.collect()
775+
}
776+
PrimitiveTopology::TriangleStrip => {
777+
// [[0, 1, 2], [2, 1, 3], [2, 3, 4], ...]
778+
(0..vtx.len() as u32 - 2)
779+
.map(|i| {
780+
if i % 2 == 0 {
781+
[i, i + 1, i + 2]
782+
} else {
783+
[i + 1, i, i + 2]
784+
}
785+
})
786+
.collect()
787+
}
788+
// ignore PointList, LineList, LineStrip:
789+
// they don't have meaningful colliders
790+
_ => return None,
791+
}
792+
}
764793
};
765794

766795
Some((vtx, idx))

0 commit comments

Comments
 (0)