Move all the initialization logic to a system for manual_material example.

This commit is contained in:
andriyDev 2025-07-13 14:04:39 -07:00
parent ab5a35a677
commit a3d12cfcec

View File

@ -29,7 +29,7 @@ use bevy::{
sync_world::MainEntity, sync_world::MainEntity,
texture::GpuImage, texture::GpuImage,
view::ExtractedView, view::ExtractedView,
Extract, RenderApp, Extract, RenderApp, RenderStartup,
}, },
utils::Parallel, utils::Parallel,
}; };
@ -55,24 +55,28 @@ impl Plugin for ImageMaterialPlugin {
check_entities_needing_specialization.after(AssetEventSystems), check_entities_needing_specialization.after(AssetEventSystems),
) )
.init_resource::<EntitiesNeedingSpecialization<ImageMaterial>>(); .init_resource::<EntitiesNeedingSpecialization<ImageMaterial>>();
}
fn finish(&self, app: &mut App) {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else { let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return; return;
}; };
render_app.add_systems( render_app
.add_systems(RenderStartup, init_image_material_resources)
.add_systems(
ExtractSchedule, ExtractSchedule,
( (
extract_image_materials, extract_image_materials,
extract_image_materials_needing_specialization, extract_image_materials_needing_specialization,
), ),
); );
}
}
render_app.world_mut().resource_scope( fn init_image_material_resources(
|world: &mut World, mut bind_group_allocators: Mut<MaterialBindGroupAllocators>| { mut commands: Commands,
world.resource_scope(|world: &mut World, render_device: Mut<RenderDevice>| { render_device: Res<RenderDevice>,
mut bind_group_allocators: ResMut<MaterialBindGroupAllocators>,
) {
let bind_group_layout = render_device.create_bind_group_layout( let bind_group_layout = render_device.create_bind_group_layout(
"image_material_layout", "image_material_layout",
&BindGroupLayoutEntries::sequential( &BindGroupLayoutEntries::sequential(
@ -84,23 +88,13 @@ impl Plugin for ImageMaterialPlugin {
), ),
); );
let sampler = render_device.create_sampler(&SamplerDescriptor::default()); let sampler = render_device.create_sampler(&SamplerDescriptor::default());
world.insert_resource(ImageMaterialBindGroupLayout(bind_group_layout.clone())); commands.insert_resource(ImageMaterialBindGroupLayout(bind_group_layout.clone()));
world.insert_resource(ImageMaterialBindGroupSampler(sampler)); commands.insert_resource(ImageMaterialBindGroupSampler(sampler));
bind_group_allocators.insert( bind_group_allocators.insert(
TypeId::of::<ImageMaterial>(), TypeId::of::<ImageMaterial>(),
MaterialBindGroupAllocator::new( MaterialBindGroupAllocator::new(&render_device, None, None, bind_group_layout, None),
&render_device,
None,
None,
bind_group_layout,
None,
),
); );
});
},
);
}
} }
#[derive(Resource)] #[derive(Resource)]