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

Deprecate shapes in bevy_render::mesh::shape #11773

Merged
merged 5 commits into from
Feb 8, 2024
Merged
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
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the purpose of this test please. I'm not sure I could fix this if I broke it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not my test, it was added by #11017. Personally, I find it to be a bit redundant and would remove it, but it was on the old RegularPolygon type so I kept it for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Document RegularPolygon by writing a test" and then proceeds to write no documentation for the test.

Fair enough.

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
Loading