mirror of
https://github.com/bevyengine/bevy
synced 2025-02-18 06:58:34 +00:00
warn and min for different vertex count (#9699)
# Objective Bevy currently crashes when meshes with different vertex counts for attributes are provided. ## Solution Instead of crashing we can warn and take the min length of all the given attributes.
This commit is contained in:
parent
cac844243f
commit
807d6465d2
1 changed files with 12 additions and 8 deletions
|
@ -1,5 +1,6 @@
|
||||||
mod conversions;
|
mod conversions;
|
||||||
pub mod skinning;
|
pub mod skinning;
|
||||||
|
use bevy_log::warn;
|
||||||
pub use wgpu::PrimitiveTopology;
|
pub use wgpu::PrimitiveTopology;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -323,17 +324,20 @@ impl Mesh {
|
||||||
|
|
||||||
/// Counts all vertices of the mesh.
|
/// Counts all vertices of the mesh.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// If the attributes have different vertex counts, the smallest is returned.
|
||||||
/// Panics if the attributes have different vertex counts.
|
|
||||||
pub fn count_vertices(&self) -> usize {
|
pub fn count_vertices(&self) -> usize {
|
||||||
let mut vertex_count: Option<usize> = None;
|
let mut vertex_count: Option<usize> = None;
|
||||||
for (attribute_id, attribute_data) in &self.attributes {
|
for (attribute_id, attribute_data) in &self.attributes {
|
||||||
let attribute_len = attribute_data.values.len();
|
let attribute_len = attribute_data.values.len();
|
||||||
if let Some(previous_vertex_count) = vertex_count {
|
if let Some(previous_vertex_count) = vertex_count {
|
||||||
assert_eq!(previous_vertex_count, attribute_len,
|
if previous_vertex_count != attribute_len {
|
||||||
"{attribute_id:?} has a different vertex count ({attribute_len}) than other attributes ({previous_vertex_count}) in this mesh.");
|
warn!("{attribute_id:?} has a different vertex count ({attribute_len}) than other attributes ({previous_vertex_count}) in this mesh, \
|
||||||
|
all attributes will be truncated to match the smallest.");
|
||||||
|
vertex_count = Some(std::cmp::min(previous_vertex_count, attribute_len));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
vertex_count = Some(attribute_len);
|
||||||
}
|
}
|
||||||
vertex_count = Some(attribute_len);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
vertex_count.unwrap_or(0)
|
vertex_count.unwrap_or(0)
|
||||||
|
@ -343,8 +347,8 @@ impl Mesh {
|
||||||
/// Therefore the attributes are located in the order of their [`MeshVertexAttribute::id`].
|
/// Therefore the attributes are located in the order of their [`MeshVertexAttribute::id`].
|
||||||
/// This is used to transform the vertex data into a GPU friendly format.
|
/// This is used to transform the vertex data into a GPU friendly format.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// If the vertex attributes have different lengths, they are all truncated to
|
||||||
/// Panics if the attributes have different vertex counts.
|
/// the length of the smallest.
|
||||||
pub fn get_vertex_buffer_data(&self) -> Vec<u8> {
|
pub fn get_vertex_buffer_data(&self) -> Vec<u8> {
|
||||||
let mut vertex_size = 0;
|
let mut vertex_size = 0;
|
||||||
for attribute_data in self.attributes.values() {
|
for attribute_data in self.attributes.values() {
|
||||||
|
@ -356,7 +360,7 @@ impl Mesh {
|
||||||
let mut attributes_interleaved_buffer = vec![0; vertex_count * vertex_size];
|
let mut attributes_interleaved_buffer = vec![0; vertex_count * vertex_size];
|
||||||
// bundle into interleaved buffers
|
// bundle into interleaved buffers
|
||||||
let mut attribute_offset = 0;
|
let mut attribute_offset = 0;
|
||||||
for attribute_data in self.attributes.values() {
|
for attribute_data in self.attributes.values().take(vertex_count) {
|
||||||
let attribute_size = attribute_data.attribute.format.get_size() as usize;
|
let attribute_size = attribute_data.attribute.format.get_size() as usize;
|
||||||
let attributes_bytes = attribute_data.values.get_bytes();
|
let attributes_bytes = attribute_data.values.get_bytes();
|
||||||
for (vertex_index, attribute_bytes) in
|
for (vertex_index, attribute_bytes) in
|
||||||
|
|
Loading…
Add table
Reference in a new issue