diff --git a/crates/bevy_mesh/src/mesh.rs b/crates/bevy_mesh/src/mesh.rs index 3492788c4b..b7f0058c19 100644 --- a/crates/bevy_mesh/src/mesh.rs +++ b/crates/bevy_mesh/src/mesh.rs @@ -263,6 +263,35 @@ impl Mesh { .insert(attribute.id, MeshAttributeData { attribute, values }); } + /// Sets the data for a vertex attribute (position, normal, etc.) if not empty. + /// The name will often be one of the associated constants such + /// as [`Mesh::ATTRIBUTE_POSITION`]. + /// + /// `Aabb` of entities with modified mesh are not updated automatically. + /// + /// # Panics + /// Panics when the format of the values does not match the attribute's format. + #[inline] + pub fn insert_attribute_if_not_empty( + &mut self, + attribute: MeshVertexAttribute, + values: impl Into, + ) { + let values = values.into(); + if !values.is_empty() { + let values_format = VertexFormat::from(&values); + if values_format != attribute.format { + panic!( + "Failed to insert attribute. Invalid attribute format for {}. Given format is {values_format:?} but expected {:?}", + attribute.name, attribute.format + ); + } + + self.attributes + .insert(attribute.id, MeshAttributeData { attribute, values }); + } + } + /// Consumes the mesh and returns a mesh with data set for a vertex attribute (position, normal, etc.). /// The name will often be one of the associated constants such as [`Mesh::ATTRIBUTE_POSITION`]. /// @@ -283,6 +312,27 @@ impl Mesh { self } + /// Consumes the mesh and returns a mesh with data set for a vertex attribute (position, normal, etc.) + /// if it is not empty. + /// The name will often be one of the associated constants such as [`Mesh::ATTRIBUTE_POSITION`]. + /// + /// (Alternatively, you can use [`Mesh::insert_attribute`] to mutate an existing mesh in-place) + /// + /// `Aabb` of entities with modified mesh are not updated automatically. + /// + /// # Panics + /// Panics when the format of the values does not match the attribute's format. + #[must_use] + #[inline] + pub fn with_inserted_attribute_if_not_empty( + mut self, + attribute: MeshVertexAttribute, + values: impl Into, + ) -> Self { + self.insert_attribute_if_not_empty(attribute, values); + self + } + /// Removes the data for a vertex attribute pub fn remove_attribute( &mut self,