Skip to content

Commit 0166db3

Browse files
authored
Deprecate shapes in bevy_render::mesh::shape (bevyengine#11773)
# Objective bevyengine#11431 and bevyengine#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()); ```
1 parent 76b6666 commit 0166db3

File tree

82 files changed

+294
-1112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+294
-1112
lines changed

crates/bevy_render/src/mesh/primitives/dim2.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,62 @@ impl From<Capsule2dMeshBuilder> for Mesh {
405405
capsule.build()
406406
}
407407
}
408+
409+
#[cfg(test)]
410+
mod tests {
411+
use bevy_math::primitives::RegularPolygon;
412+
413+
use crate::mesh::{Mesh, VertexAttributeValues};
414+
415+
/// Sin/cos and multiplication computations result in numbers like 0.4999999.
416+
/// Round these to numbers we expect like 0.5.
417+
fn fix_floats<const N: usize>(points: &mut [[f32; N]]) {
418+
for point in points.iter_mut() {
419+
for coord in point.iter_mut() {
420+
let round = (*coord * 2.).round() / 2.;
421+
if (*coord - round).abs() < 0.00001 {
422+
*coord = round;
423+
}
424+
}
425+
}
426+
}
427+
428+
#[test]
429+
fn test_regular_polygon() {
430+
let mut mesh = Mesh::from(RegularPolygon::new(7.0, 4));
431+
432+
let Some(VertexAttributeValues::Float32x3(mut positions)) =
433+
mesh.remove_attribute(Mesh::ATTRIBUTE_POSITION)
434+
else {
435+
panic!("Expected positions f32x3");
436+
};
437+
let Some(VertexAttributeValues::Float32x2(mut uvs)) =
438+
mesh.remove_attribute(Mesh::ATTRIBUTE_UV_0)
439+
else {
440+
panic!("Expected uvs f32x2");
441+
};
442+
let Some(VertexAttributeValues::Float32x3(normals)) =
443+
mesh.remove_attribute(Mesh::ATTRIBUTE_NORMAL)
444+
else {
445+
panic!("Expected normals f32x3");
446+
};
447+
448+
fix_floats(&mut positions);
449+
fix_floats(&mut uvs);
450+
451+
assert_eq!(
452+
[
453+
[0.0, 7.0, 0.0],
454+
[-7.0, 0.0, 0.0],
455+
[0.0, -7.0, 0.0],
456+
[7.0, 0.0, 0.0],
457+
],
458+
&positions[..]
459+
);
460+
461+
// Note V coordinate increases in the opposite direction to the Y coordinate.
462+
assert_eq!([[0.5, 0.0], [0.0, 0.5], [0.5, 1.0], [1.0, 0.5],], &uvs[..]);
463+
464+
assert_eq!(&[[0.0, 0.0, 1.0]; 4], &normals[..]);
465+
}
466+
}

0 commit comments

Comments
 (0)