From 58e2bea4f454928d905fb71026c192ee6b49c43f Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:47:56 +0100 Subject: [PATCH 1/5] warn and min for different vertex count --- crates/bevy_render/src/mesh/mesh/mod.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 49c87576915ec..636092a11964e 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -1,5 +1,6 @@ mod conversions; pub mod skinning; +use bevy_log::warn; pub use wgpu::PrimitiveTopology; use crate::{ @@ -330,10 +331,13 @@ impl Mesh { for (attribute_id, attribute_data) in &self.attributes { let attribute_len = attribute_data.values.len(); if let Some(previous_vertex_count) = vertex_count { - assert_eq!(previous_vertex_count, attribute_len, - "{attribute_id:?} has a different vertex count ({attribute_len}) than other attributes ({previous_vertex_count}) in this mesh."); + if previous_vertex_count != attribute_len { + warn!("{attribute_id:?} has a different vertex count ({attribute_len}) than other attributes ({previous_vertex_count}) in this mesh."); + 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) From 28581cafcfaea68be1ae76d8da555fbcbd7ef80b Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:57:18 +0100 Subject: [PATCH 2/5] limit buffer data to expected len --- crates/bevy_render/src/mesh/mesh/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 636092a11964e..957e711069401 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -360,7 +360,7 @@ impl Mesh { let mut attributes_interleaved_buffer = vec![0; vertex_count * vertex_size]; // bundle into interleaved buffers 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 attributes_bytes = attribute_data.values.get_bytes(); for (vertex_index, attribute_bytes) in From ca677d41fd4cfc4c30eadab22f64c2e1ad2e266d Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 5 Sep 2023 23:35:59 +0100 Subject: [PATCH 3/5] review comments --- crates/bevy_render/src/mesh/mesh/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 957e711069401..f77a94db66c48 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -324,15 +324,15 @@ impl Mesh { /// Counts all vertices of the mesh. /// - /// # Panics - /// Panics if the attributes have different vertex counts. + /// If the attributes have different vertex counts, the smallest is returned. pub fn count_vertices(&self) -> usize { let mut vertex_count: Option = None; for (attribute_id, attribute_data) in &self.attributes { let attribute_len = attribute_data.values.len(); if let Some(previous_vertex_count) = vertex_count { if previous_vertex_count != attribute_len { - warn!("{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 { From dc2fe8befab4d24a1d66b5dc1319353bd625f537 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 5 Sep 2023 23:55:12 +0100 Subject: [PATCH 4/5] remove another panic comment --- crates/bevy_render/src/mesh/mesh/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index f77a94db66c48..413f81abec798 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -346,9 +346,9 @@ impl Mesh { /// Computes and returns the vertex data of the mesh as bytes. /// 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. - /// - /// # Panics - /// Panics if the attributes have different vertex counts. + /// + /// If the vertex attributes have different lengths, they are all truncated to + /// the length of the smallest. pub fn get_vertex_buffer_data(&self) -> Vec { let mut vertex_size = 0; for attribute_data in self.attributes.values() { From b86d08ed646b726c97c49b42b8df029e112bbd9a Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Wed, 6 Sep 2023 00:07:05 +0100 Subject: [PATCH 5/5] fmt --- crates/bevy_render/src/mesh/mesh/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 413f81abec798..50e75c6893d24 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -346,7 +346,7 @@ impl Mesh { /// Computes and returns the vertex data of the mesh as bytes. /// 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. - /// + /// /// If the vertex attributes have different lengths, they are all truncated to /// the length of the smallest. pub fn get_vertex_buffer_data(&self) -> Vec {