This commit is contained in:
Lucas Franca 2025-07-15 22:34:41 +02:00 committed by GitHub
commit 83a5420d7c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -263,6 +263,35 @@ impl Mesh {
.insert(attribute.id, MeshAttributeData { attribute, values }); .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<VertexAttributeValues>,
) {
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.). /// 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`]. /// The name will often be one of the associated constants such as [`Mesh::ATTRIBUTE_POSITION`].
/// ///
@ -283,6 +312,27 @@ impl Mesh {
self 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<VertexAttributeValues>,
) -> Self {
self.insert_attribute_if_not_empty(attribute, values);
self
}
/// Removes the data for a vertex attribute /// Removes the data for a vertex attribute
pub fn remove_attribute( pub fn remove_attribute(
&mut self, &mut self,