diff --git a/crates/bevy_render/src/mesh/shape/regular_polygon.rs b/crates/bevy_render/src/mesh/shape/regular_polygon.rs index 481dc5c8ce..5b57a61c46 100644 --- a/crates/bevy_render/src/mesh/shape/regular_polygon.rs +++ b/crates/bevy_render/src/mesh/shape/regular_polygon.rs @@ -111,3 +111,64 @@ impl From for Mesh { Mesh::from(RegularPolygon::from(circle)) } } + +#[cfg(test)] +mod tests { + use crate::mesh::shape::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(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 { + radius: 7., + sides: 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], [1.0, 0.5], [0.5, 1.0], [0.0, 0.5]], &uvs[..]); + + assert_eq!(&[[0.0, 0.0, 1.0]; 4], &normals[..]); + } +}