From ba3574479dac4cbce8aa6d6ad3ac43e4c1485c94 Mon Sep 17 00:00:00 2001 From: Ben Reeves Date: Mon, 30 May 2022 15:11:10 +0000 Subject: [PATCH] bevy_render: Add `attributes` and `attributes_mut` methods to `Mesh`. (#3927) # Use Case Seems generally useful, but specifically motivated by my work on the [`bevy_datasize`](https://github.com/BGR360/bevy_datasize) crate. For that project, I'm implementing "heap size estimators" for all of the Bevy internal types. To do this accurately for `Mesh`, I need to get the lengths of all of the mesh's attribute vectors. Currently, in order to accomplish this, I am doing the following: * Checking all of the attributes that are mentioned in the `Mesh` class ([see here](https://github.com/BGR360/bevy_datasize/blob/0531ec2d026085a31e937b12d5ecf4109005e737/src/builtins/render/mesh.rs#L46-L54)) * Providing the user with an option to configure additional attributes to check ([see here](https://github.com/BGR360/bevy_datasize/blob/0531ec2d026085a31e937b12d5ecf4109005e737/src/config.rs#L7-L21)) This is both overly complicated and a bit wasteful (since I have to check every attribute name that I know about in case there are attributes set for it). --- crates/bevy_render/src/mesh/mesh/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/bevy_render/src/mesh/mesh/mod.rs b/crates/bevy_render/src/mesh/mesh/mod.rs index 12623a27be54a4..c14498aefc02cf 100644 --- a/crates/bevy_render/src/mesh/mesh/mod.rs +++ b/crates/bevy_render/src/mesh/mesh/mod.rs @@ -141,6 +141,20 @@ impl Mesh { .map(|data| &mut data.values) } + /// Returns an iterator that yields references to the data of each vertex attribute. + pub fn attributes(&self) -> impl Iterator { + self.attributes + .iter() + .map(|(name, values)| (name.as_ref(), values)) + } + + /// Returns an iterator that yields mutable references to the data of each vertex attribute. + pub fn attributes_mut(&mut self) -> impl Iterator { + self.attributes + .iter_mut() + .map(|(name, values)| (name.as_ref(), values)) + } + /// Sets the vertex indices of the mesh. They describe how triangles are constructed out of the /// vertex attributes and are therefore only useful for the [`PrimitiveTopology`] variants /// that use triangles.