Skip to content

Commit

Permalink
Deprecate shapes in bevy_render::mesh::shape (#11773)
Browse files Browse the repository at this point in the history
# Objective

#11431 and #11688 implemented meshing support for Bevy's new geometric
primitives. The next step is to deprecate the shapes in
`bevy_render::mesh::shape` and to later remove them completely for 0.14.

## Solution

Deprecate the shapes and reduce code duplication by utilizing the
primitive meshing API for the old shapes where possible.

Note that some shapes have behavior that can't be exactly reproduced
with the new primitives yet:

- `Box` is more of an AABB with min/max extents
- `Plane` supports a subdivision count
- `Quad` has a `flipped` property

These types have not been changed to utilize the new primitives yet.

---

## Changelog

- Deprecated all shapes in `bevy_render::mesh::shape`
- Changed all examples to use new primitives for meshing

## Migration Guide

Bevy has previously used rendering-specific types like `UVSphere` and
`Quad` for primitive mesh shapes. These have now been deprecated to use
the geometric primitives newly introduced in version 0.13.

Some examples:

```rust
let before = meshes.add(shape::Box::new(5.0, 0.15, 5.0));
let after = meshes.add(Cuboid::new(5.0, 0.15, 5.0));

let before = meshes.add(shape::Quad::default());
let after = meshes.add(Rectangle::default());

let before = meshes.add(shape::Plane::from_size(5.0));
// The surface normal can now also be specified when using `new`
let after = meshes.add(Plane3d::default().mesh().size(5.0, 5.0));

let before = meshes.add(
    Mesh::try_from(shape::Icosphere {
        radius: 0.5,
        subdivisions: 5,
    })
    .unwrap(),
);
let after = meshes.add(Sphere::new(0.5).mesh().ico(5).unwrap());
```
  • Loading branch information
Jondolf authored Feb 8, 2024
1 parent 76b6666 commit 0166db3
Show file tree
Hide file tree
Showing 82 changed files with 294 additions and 1,112 deletions.
59 changes: 59 additions & 0 deletions crates/bevy_render/src/mesh/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,62 @@ impl From<Capsule2dMeshBuilder> for Mesh {
capsule.build()
}
}

#[cfg(test)]
mod tests {
use bevy_math::primitives::RegularPolygon;

use crate::mesh::{Mesh, VertexAttributeValues};

/// Sin/cos and multiplication computations result in numbers like 0.4999999.
/// Round these to numbers we expect like 0.5.
fn fix_floats<const N: usize>(points: &mut [[f32; N]]) {
for point in points.iter_mut() {
for coord in point.iter_mut() {
let round = (*coord * 2.).round() / 2.;
if (*coord - round).abs() < 0.00001 {
*coord = round;
}
}
}
}

#[test]
fn test_regular_polygon() {
let mut mesh = Mesh::from(RegularPolygon::new(7.0, 4));

let Some(VertexAttributeValues::Float32x3(mut positions)) =
mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION)
else {
panic!("Expected positions f32x3");
};
let Some(VertexAttributeValues::Float32x2(mut uvs)) =
mesh.remove_attribute(Mesh::ATTRIBUTE_UV_0)
else {
panic!("Expected uvs f32x2");
};
let Some(VertexAttributeValues::Float32x3(normals)) =
mesh.remove_attribute(Mesh::ATTRIBUTE_NORMAL)
else {
panic!("Expected normals f32x3");
};

fix_floats(&mut positions);
fix_floats(&mut uvs);

assert_eq!(
[
[0.0, 7.0, 0.0],
[-7.0, 0.0, 0.0],
[0.0, -7.0, 0.0],
[7.0, 0.0, 0.0],
],
&positions[..]
);

// Note V coordinate increases in the opposite direction to the Y coordinate.
assert_eq!([[0.5, 0.0], [0.0, 0.5], [0.5, 1.0], [1.0, 0.5],], &uvs[..]);

assert_eq!(&[[0.0, 0.0, 1.0]; 4], &normals[..]);
}
}
Loading

0 comments on commit 0166db3

Please sign in to comment.