diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 950d32d6fba18..d2d42778453dd 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -149,6 +149,7 @@ pub fn extract_meshes( entity, ( handle.clone_weak(), + DynamicUniformIndex::::default(), MeshUniform { flags: if not_receiver.is_some() { MeshFlags::empty().bits @@ -174,6 +175,7 @@ pub fn extract_meshes( entity, ( mesh.clone_weak(), + DynamicUniformIndex::::default(), MeshUniform { flags: if not_receiver.is_some() { MeshFlags::empty().bits diff --git a/crates/bevy_render/src/extract_component.rs b/crates/bevy_render/src/extract_component.rs index 673b23c30770e..f4f78b1429c8e 100644 --- a/crates/bevy_render/src/extract_component.rs +++ b/crates/bevy_render/src/extract_component.rs @@ -15,7 +15,7 @@ use bevy_ecs::{ use std::{marker::PhantomData, ops::Deref}; /// Stores the index of a uniform inside of [`ComponentUniforms`]. -#[derive(Component)] +#[derive(Component, Copy)] pub struct DynamicUniformIndex { index: u32, marker: PhantomData, @@ -28,6 +28,24 @@ impl DynamicUniformIndex { } } +impl Default for DynamicUniformIndex { + fn default() -> Self { + Self { + index: 0, + marker: PhantomData, + } + } +} + +impl Clone for DynamicUniformIndex { + fn clone(&self) -> Self { + Self { + index: self.index, + marker: PhantomData, + } + } +} + /// Describes how a component gets extracted for rendering. /// /// Therefore the component is transferred from the "app world" into the "render world" @@ -100,28 +118,17 @@ impl Default for ComponentUniforms { /// This system prepares all components of the corresponding component type. /// They are transformed into uniforms and stored in the [`ComponentUniforms`] resource. fn prepare_uniform_components( - mut commands: Commands, render_device: Res, render_queue: Res, mut component_uniforms: ResMut>, - components: Query<(Entity, &C)>, + mut components: Query<(&C, &mut DynamicUniformIndex)>, ) where C: ShaderType + WriteInto + Clone, { component_uniforms.uniforms.clear(); - let entities = components - .iter() - .map(|(entity, component)| { - ( - entity, - (DynamicUniformIndex:: { - index: component_uniforms.uniforms.push(component.clone()), - marker: PhantomData, - },), - ) - }) - .collect::>(); - commands.insert_or_spawn_batch(entities); + for (component, mut dynamic) in components.iter_mut() { + dynamic.index = component_uniforms.uniforms.push(component.clone()); + } component_uniforms .uniforms diff --git a/crates/bevy_sprite/src/mesh2d/mesh.rs b/crates/bevy_sprite/src/mesh2d/mesh.rs index 7a6ebfeadff93..a1f70a5bc6479 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh.rs +++ b/crates/bevy_sprite/src/mesh2d/mesh.rs @@ -128,6 +128,7 @@ pub fn extract_mesh2d( entity, ( Mesh2dHandle(handle.0.clone_weak()), + DynamicUniformIndex::::default(), Mesh2dUniform { flags: MeshFlags::empty().bits, transform,