Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ mint = ["glam/mint"]
libm = ["dep:libm", "glam/libm"]
# Enable assertions to check the validity of parameters passed to glam
glam_assert = ["glam/glam-assert"]
glam_bytemuck = ["glam/bytemuck"]
# Enable assertions in debug builds to check the validity of parameters passed to glam
debug_glam_assert = ["glam/debug-glam-assert"]
# Enable the rand dependency for shape_sampling
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_mesh/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ keywords = ["bevy"]
bevy_app = { path = "../bevy_app", version = "0.18.0-dev" }
bevy_asset = { path = "../bevy_asset", version = "0.18.0-dev" }
bevy_image = { path = "../bevy_image", version = "0.18.0-dev", optional = true }
bevy_math = { path = "../bevy_math", version = "0.18.0-dev" }
bevy_math = { path = "../bevy_math", version = "0.18.0-dev", features = [
"glam_bytemuck",
] }
bevy_reflect = { path = "../bevy_reflect", version = "0.18.0-dev" }
bevy_ecs = { path = "../bevy_ecs", version = "0.18.0-dev" }
bevy_transform = { path = "../bevy_transform", version = "0.18.0-dev" }
Expand Down
33 changes: 33 additions & 0 deletions crates/bevy_mesh/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ pub enum Indices {
}

impl Indices {
/// Create a empty indices with the given capacity and vertex count. It will be [`Indices::U16`] if the vertex count <= 65536, otherwise it will be [`Indices::U32`].
pub fn with_capacity(capacity: usize, vertex_count: u32) -> Self {
if vertex_count <= u16::MAX as u32 + 1 {
Indices::U16(Vec::with_capacity(capacity))
} else {
Indices::U32(Vec::with_capacity(capacity))
}
}

/// Returns an iterator over the indices.
pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
match self {
Expand Down Expand Up @@ -109,6 +118,30 @@ impl Indices {
pub fn push(&mut self, index: u32) {
self.extend([index]);
}

/// Resize the indices in place so that `len` is equal to `new_len`.
pub fn resize(&mut self, new_len: usize, value: u32) {
match self {
Indices::U16(indices) => {
indices.resize(new_len, value as u16);
}
Indices::U32(indices) => {
indices.resize(new_len, value);
}
}
}

/// Set a value of indices at the given index.
pub fn set(&mut self, index: usize, value: u32) {
match self {
Indices::U16(indices) => {
indices[index] = value as u16;
}
Indices::U32(indices) => {
indices[index] = value;
}
}
}
}

/// Extend the indices with indices from an iterator.
Expand Down
63 changes: 31 additions & 32 deletions crates/bevy_mesh/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl CircularSectorMeshBuilder {
impl MeshBuilder for CircularSectorMeshBuilder {
fn build(&self) -> Mesh {
let resolution = self.resolution as usize;
let mut indices = Vec::with_capacity((resolution - 1) * 3);
let mut indices = Indices::with_capacity((resolution - 1) * 3, (resolution + 1) as u32);
let mut positions = Vec::with_capacity(resolution + 1);
let normals = vec![[0.0, 0.0, 1.0]; resolution + 1];
let mut uvs = Vec::with_capacity(resolution + 1);
Expand Down Expand Up @@ -207,7 +207,7 @@ impl MeshBuilder for CircularSectorMeshBuilder {

for i in 1..self.resolution {
// Index 0 is the center.
indices.extend_from_slice(&[0, i, i + 1]);
indices.extend([0, i, i + 1]);
}

Mesh::new(
Expand All @@ -217,7 +217,7 @@ impl MeshBuilder for CircularSectorMeshBuilder {
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_indices(Indices::U32(indices))
.with_inserted_indices(indices)
}
}

Expand Down Expand Up @@ -315,7 +315,7 @@ impl CircularSegmentMeshBuilder {
impl MeshBuilder for CircularSegmentMeshBuilder {
fn build(&self) -> Mesh {
let resolution = self.resolution as usize;
let mut indices = Vec::with_capacity((resolution - 1) * 3);
let mut indices = Indices::with_capacity((resolution - 1) * 3, (resolution + 1) as u32);
let mut positions = Vec::with_capacity(resolution + 1);
let normals = vec![[0.0, 0.0, 1.0]; resolution + 1];
let mut uvs = Vec::with_capacity(resolution + 1);
Expand Down Expand Up @@ -354,7 +354,7 @@ impl MeshBuilder for CircularSegmentMeshBuilder {

for i in 1..self.resolution {
// Index 0 is the midpoint of the chord.
indices.extend_from_slice(&[0, i, i + 1]);
indices.extend([0, i, i + 1]);
}

Mesh::new(
Expand All @@ -364,7 +364,7 @@ impl MeshBuilder for CircularSegmentMeshBuilder {
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_indices(Indices::U32(indices))
.with_inserted_indices(indices)
}
}

Expand Down Expand Up @@ -429,21 +429,21 @@ impl Meshable for ConvexPolygon {
impl MeshBuilder for ConvexPolygonMeshBuilder {
fn build(&self) -> Mesh {
let len = self.vertices.len();
let mut indices = Vec::with_capacity((len - 2) * 3);
let mut indices = Indices::with_capacity((len - 2) * 3, len as u32);
let mut positions = Vec::with_capacity(len);

for vertex in &self.vertices {
positions.push([vertex.x, vertex.y, 0.0]);
}
for i in 2..len as u32 {
indices.extend_from_slice(&[0, i - 1, i]);
indices.extend([0, i - 1, i]);
}
Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_indices(Indices::U32(indices))
.with_inserted_indices(indices)
}
}

Expand Down Expand Up @@ -578,7 +578,7 @@ impl EllipseMeshBuilder {
impl MeshBuilder for EllipseMeshBuilder {
fn build(&self) -> Mesh {
let resolution = self.resolution as usize;
let mut indices = Vec::with_capacity((resolution - 2) * 3);
let mut indices = Indices::with_capacity((resolution - 2) * 3, resolution as u32);
let mut positions = Vec::with_capacity(resolution);
let normals = vec![[0.0, 0.0, 1.0]; resolution];
let mut uvs = Vec::with_capacity(resolution);
Expand All @@ -599,7 +599,7 @@ impl MeshBuilder for EllipseMeshBuilder {
}

for i in 1..(self.resolution - 1) {
indices.extend_from_slice(&[0, i, i + 1]);
indices.extend([0, i, i + 1]);
}

Mesh::new(
Expand All @@ -609,7 +609,7 @@ impl MeshBuilder for EllipseMeshBuilder {
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_indices(Indices::U32(indices))
.with_inserted_indices(indices)
}
}

Expand Down Expand Up @@ -657,7 +657,7 @@ impl Segment2dMeshBuilder {
impl MeshBuilder for Segment2dMeshBuilder {
fn build(&self) -> Mesh {
let positions = self.segment.vertices.map(|v| v.extend(0.0)).to_vec();
let indices = Indices::U32(vec![0, 1]);
let indices = Indices::U16(vec![0, 1]);

Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::default())
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
Expand Down Expand Up @@ -696,11 +696,9 @@ impl MeshBuilder for Polyline2dMeshBuilder {
.map(|v| v.extend(0.0))
.collect();

let indices = Indices::U32(
(0..self.polyline.vertices.len() as u32 - 1)
.flat_map(|i| [i, i + 1])
.collect(),
);
let mut indices =
Indices::with_capacity(self.polyline.vertices.len() - 1, positions.len() as u32);
indices.extend((0..self.polyline.vertices.len() as u32 - 1).flat_map(|i| [i, i + 1]));

Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::default())
.with_inserted_indices(indices)
Expand Down Expand Up @@ -769,7 +767,7 @@ impl MeshBuilder for AnnulusMeshBuilder {
let outer_radius = self.annulus.outer_circle.radius;

let num_vertices = (self.resolution as usize + 1) * 2;
let mut indices = Vec::with_capacity(self.resolution as usize * 6);
let mut indices = Indices::with_capacity(self.resolution as usize * 6, num_vertices as u32);
let mut positions = Vec::with_capacity(num_vertices);
let mut uvs = Vec::with_capacity(num_vertices);
let normals = vec![[0.0, 0.0, 1.0]; num_vertices];
Expand Down Expand Up @@ -807,8 +805,8 @@ impl MeshBuilder for AnnulusMeshBuilder {
let outer_vertex = 2 * i + 1;
let next_inner = inner_vertex + 2;
let next_outer = outer_vertex + 2;
indices.extend_from_slice(&[inner_vertex, outer_vertex, next_outer]);
indices.extend_from_slice(&[next_outer, next_inner, inner_vertex]);
indices.extend([inner_vertex, outer_vertex, next_outer]);
indices.extend([next_outer, next_inner, inner_vertex]);
}

Mesh::new(
Expand All @@ -818,7 +816,7 @@ impl MeshBuilder for AnnulusMeshBuilder {
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_indices(Indices::U32(indices))
.with_inserted_indices(indices)
}
}

Expand Down Expand Up @@ -906,7 +904,7 @@ impl MeshBuilder for RhombusMeshBuilder {
];
let normals = vec![[0.0, 0.0, 1.0]; 4];
let uvs = vec![[1.0, 0.5], [0.5, 0.0], [0.0, 0.5], [0.5, 1.0]];
let indices = Indices::U32(vec![0, 1, 2, 2, 3, 0]);
let indices = Indices::U16(vec![0, 1, 2, 2, 3, 0]);

Mesh::new(
PrimitiveTopology::TriangleList,
Expand Down Expand Up @@ -983,9 +981,9 @@ impl MeshBuilder for Triangle2dMeshBuilder {

let is_ccw = self.triangle.winding_order() == WindingOrder::CounterClockwise;
let indices = if is_ccw {
Indices::U32(vec![0, 1, 2])
Indices::U16(vec![0, 1, 2])
} else {
Indices::U32(vec![2, 1, 0])
Indices::U16(vec![2, 1, 0])
};

Mesh::new(
Expand Down Expand Up @@ -1063,7 +1061,7 @@ impl MeshBuilder for RectangleMeshBuilder {
];
let normals = vec![[0.0, 0.0, 1.0]; 4];
let uvs = vec![[1.0, 0.0], [0.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
let indices = Indices::U32(vec![0, 1, 2, 0, 2, 3]);
let indices = Indices::U16(vec![0, 1, 2, 0, 2, 3]);

Mesh::new(
PrimitiveTopology::TriangleList,
Expand Down Expand Up @@ -1149,7 +1147,8 @@ impl MeshBuilder for Capsule2dMeshBuilder {
let vertex_count = 2 * resolution;

// Six extra indices for the two triangles between the semicircles
let mut indices = Vec::with_capacity((resolution as usize - 2) * 2 * 3 + 6);
let mut indices =
Indices::with_capacity((resolution as usize - 2) * 2 * 3 + 6, vertex_count);
let mut positions = Vec::with_capacity(vertex_count as usize);
let normals = vec![[0.0, 0.0, 1.0]; vertex_count as usize];
let mut uvs = Vec::with_capacity(vertex_count as usize);
Expand Down Expand Up @@ -1182,11 +1181,11 @@ impl MeshBuilder for Capsule2dMeshBuilder {

// Add top semicircle indices
for i in 1..resolution - 1 {
indices.extend_from_slice(&[0, i, i + 1]);
indices.extend([0, i, i + 1]);
}

// Add indices for top left triangle of the part between the semicircles
indices.extend_from_slice(&[0, resolution - 1, resolution]);
indices.extend([0, resolution - 1, resolution]);

// Create bottom semicircle
for i in resolution..vertex_count {
Expand All @@ -1201,11 +1200,11 @@ impl MeshBuilder for Capsule2dMeshBuilder {

// Add bottom semicircle indices
for i in 1..resolution - 1 {
indices.extend_from_slice(&[resolution, resolution + i, resolution + i + 1]);
indices.extend([resolution, resolution + i, resolution + i + 1]);
}

// Add indices for bottom right triangle of the part between the semicircles
indices.extend_from_slice(&[resolution, vertex_count - 1, 0]);
indices.extend([resolution, vertex_count - 1, 0]);

Mesh::new(
PrimitiveTopology::TriangleList,
Expand All @@ -1214,7 +1213,7 @@ impl MeshBuilder for Capsule2dMeshBuilder {
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_indices(Indices::U32(indices))
.with_inserted_indices(indices)
}
}

Expand Down
Loading