diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index fde82287d1f56..7cd57f492fea6 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -13,7 +13,7 @@ use bevy_derive::EnumVariantMeta; use bevy_ecs::system::{lifetimeless::SRes, SystemParamItem}; use bevy_math::*; use bevy_reflect::TypeUuid; -use bevy_utils::Hashed; +use bevy_utils::{tracing::error, Hashed}; use std::{collections::BTreeMap, hash::Hash, iter::FusedIterator}; use thiserror::Error; use wgpu::{ @@ -101,19 +101,28 @@ impl Mesh { /// Sets the data for a vertex attribute (position, normal etc.). The name will /// often be one of the associated constants such as [`Mesh::ATTRIBUTE_POSITION`]. + /// + /// # Panics + /// Panics when the format of the values does not match the attribute's format. #[inline] pub fn insert_attribute( &mut self, attribute: MeshVertexAttribute, values: impl Into, ) { - self.attributes.insert( - attribute.id, - MeshAttributeData { - attribute, - values: values.into(), - }, - ); + let values: VertexAttributeValues = values.into(); + + let values_format = VertexFormat::from(&values); + if values_format != attribute.format { + error!( + "Invalid attribute format for {}. Given format is {:?} but expected {:?}", + attribute.name, values_format, attribute.format + ); + panic!("Failed to insert attribute"); + } + + self.attributes + .insert(attribute.id, MeshAttributeData { attribute, values }); } /// Removes the data for a vertex attribute @@ -994,3 +1003,16 @@ fn generate_tangents_for_mesh(mesh: &Mesh) -> Result, GenerateTang Ok(mikktspace_mesh.tangents) } + +#[cfg(test)] +mod tests { + use super::Mesh; + use wgpu::PrimitiveTopology; + + #[test] + #[should_panic] + fn panic_invalid_format() { + let mut mesh = Mesh::new(PrimitiveTopology::TriangleList); + mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, vec![[0.0, 0.0, 0.0]]); + } +}