Out-of-range panic when generating a basic procedural mesh #5257
-
Hey folks! I'm thinking about using Bevy for doing generative art, which means procedurally generating meshes. As a test, I'm trying to procedurally generate a circle mesh. I've written some code, but I'm getting an out-of-range panic (panicked at 'range end index 320 out of range for slice of length 288') and I'm not sure what's causing it. Here's my mesh generation code: fn get_circle_mesh(center: [f32; 3], radius: f32, num_sides: usize) -> Mesh {
let [center_x, center_y, center_z] = center;
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut positions = Vec::with_capacity(num_sides);
let mut indexes: Vec<u16> = Vec::with_capacity(num_sides * 3);
let mut normals = Vec::with_capacity(num_sides);
let mut uvs = Vec::with_capacity(num_sides);
positions.push(center);
normals.push([0.0, 0.0, 1.0]);
uvs.push([0.0, 0.0, 1.0]);
for i in 1..=num_sides {
let progress = (i - 1) as f32 / num_sides as f32;
// Generate circle vertices.
let rotation = progress * TAU;
let x = center_x + radius * rotation.cos();
let y = center_y + radius * rotation.sin();
positions.push([x, y, center_z]);
// Get indexes.
let mut next_index = i + 1;
if next_index > num_sides {
next_index = 1;
}
indexes.push(0);
indexes.push(i as u16);
indexes.push(next_index as u16);
normals.push([0.0, 0.0, 1.0]);
uvs.push([0.0, 0.0, 1.0]);
}
// Set mesh vertex positions
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
// Set mesh vertex normals
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
// Set mesh vertex UVs. Although the mesh doesn't have any texture applied,
// UVs are still required by the render pipeline. So these UVs are zeroed out.
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
// Tell bevy to construct triangles from a list of vertex indices,
// where each 3 vertex indices form an triangle.
mesh.set_indices(Some(Indices::U16(indexes)));
mesh
} And my setup code: fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(AmbientLight {
brightness: 1.0,
..Default::default()
})
.add_startup_system(setup)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Create a camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
let circle_mesh = get_circle_mesh([0.0, 0.0, 0.0], 1.0, 8);
let circle_mesh_handle = meshes.add(circle_mesh);
commands.spawn_bundle(PbrBundle {
mesh: circle_mesh_handle,
material: materials.add(Color::rgb(0.0, 0.0, 1.0).into()),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..Default::default()
});
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Here is the problem:
Just set to:
And it should work fine. Edit: This question was already answered on Discord by IceSentry, which already sent an PR to solve this issue. |
Beta Was this translation helpful? Give feedback.
Mesh::ATTRIBUTE_UV_0
has the typeVertexFormat::Float32x2
so you should use a[f32; 2]
.Here is the problem:
Just set to:
And it should work fine.
Edit: This question was already answered on Discord by IceSentry, which already sent an PR to solve this issue.