Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for unindexed meshes in Collider::from_bevy_mesh #491

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/geometry/collider_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use na::DVector;
#[cfg(all(feature = "dim3", feature = "async-collider"))]
use {
bevy::prelude::*,
bevy::render::mesh::{Indices, VertexAttributeValues},
bevy::render::mesh::{Indices, PrimitiveTopology, VertexAttributeValues},
};

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

let vertices = mesh.attribute(Mesh::ATTRIBUTE_POSITION)?;
let indices = mesh.indices()?;
let indices = mesh.indices();

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

let idx = match indices {
Indices::U16(idx) => idx
Some(Indices::U16(idx)) => idx
.chunks_exact(3)
.map(|i| [i[0] as u32, i[1] as u32, i[2] as u32])
.collect(),
Indices::U32(idx) => idx.chunks_exact(3).map(|i| [i[0], i[1], i[2]]).collect(),
Some(Indices::U32(idx)) => idx.chunks_exact(3).map(|i| [i[0], i[1], i[2]]).collect(),
None => {
// Meshes loaded from glTF files may not necessarily have an index buffer
// in order to save memory (e.g. files generated by osm2world), in which case
// there's predefined algorithm to calculate indices for each topology.
match mesh.primitive_topology() {
PrimitiveTopology::TriangleList => {
// [[0, 1, 2], [3, 4, 5], [6, 7, 8], ...]
(0..vtx.len() as u32)
.step_by(3)
.map(|i| [i, i + 1, i + 2])
.collect()
}
PrimitiveTopology::TriangleStrip => {
// [[0, 1, 2], [2, 1, 3], [2, 3, 4], ...]
(0..vtx.len() as u32 - 2)
.map(|i| {
if i % 2 == 0 {
[i, i + 1, i + 2]
} else {
[i + 1, i, i + 2]
}
})
.collect()
}
// ignore PointList, LineList, LineStrip:
// they don't have meaningful colliders
_ => return None,
}
}
};

Some((vtx, idx))
Expand Down