bevy_render/
extract_resource.rs

1use core::marker::PhantomData;
2
3use bevy_app::{App, Plugin};
4use bevy_ecs::prelude::*;
5pub use bevy_render_macros::ExtractResource;
6
7use crate::{Extract, ExtractSchedule, RenderApp};
8
9/// Describes how a resource gets extracted for rendering.
10///
11/// Therefore the resource is transferred from the "main world" into the "render world"
12/// in the [`ExtractSchedule`] step.
13pub trait ExtractResource: Resource {
14    type Source: Resource;
15
16    /// Defines how the resource is transferred into the "render world".
17    fn extract_resource(source: &Self::Source) -> Self;
18}
19
20/// This plugin extracts the resources into the "render world".
21///
22/// Therefore it sets up the[`ExtractSchedule`] step
23/// for the specified [`Resource`].
24pub struct ExtractResourcePlugin<R: ExtractResource>(PhantomData<R>);
25
26impl<R: ExtractResource> Default for ExtractResourcePlugin<R> {
27    fn default() -> Self {
28        Self(PhantomData)
29    }
30}
31
32impl<R: ExtractResource> Plugin for ExtractResourcePlugin<R> {
33    fn build(&self, app: &mut App) {
34        if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
35            render_app.add_systems(ExtractSchedule, extract_resource::<R>);
36        } else {
37            bevy_utils::error_once!(
38                "Render app did not exist when trying to add `extract_resource` for <{}>.",
39                core::any::type_name::<R>()
40            );
41        }
42    }
43}
44
45/// This system extracts the resource of the corresponding [`Resource`] type
46pub fn extract_resource<R: ExtractResource>(
47    mut commands: Commands,
48    main_resource: Extract<Option<Res<R::Source>>>,
49    target_resource: Option<ResMut<R>>,
50) {
51    if let Some(main_resource) = main_resource.as_ref() {
52        if let Some(mut target_resource) = target_resource {
53            if main_resource.is_changed() {
54                *target_resource = R::extract_resource(main_resource);
55            }
56        } else {
57            #[cfg(debug_assertions)]
58            if !main_resource.is_added() {
59                bevy_utils::warn_once!(
60                    "Removing resource {} from render world not expected, adding using `Commands`.
61                This may decrease performance",
62                    core::any::type_name::<R>()
63                );
64            }
65            commands.insert_resource(R::extract_resource(main_resource));
66        }
67    }
68}