2023-10-13 17:06:53 +00:00
|
|
|
//! Convenience logic for turning components from the main world into extracted
|
2023-10-08 10:34:44 +00:00
|
|
|
//! instances in the render world.
|
|
|
|
//!
|
|
|
|
//! This is essentially the same as the `extract_component` module, but
|
|
|
|
//! higher-performance because it avoids the ECS overhead.
|
|
|
|
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use bevy_app::{App, Plugin};
|
|
|
|
use bevy_asset::{Asset, AssetId, Handle};
|
|
|
|
use bevy_derive::{Deref, DerefMut};
|
|
|
|
use bevy_ecs::{
|
|
|
|
prelude::Entity,
|
|
|
|
query::{QueryItem, ReadOnlyWorldQuery, WorldQuery},
|
|
|
|
system::{lifetimeless::Read, Query, ResMut, Resource},
|
|
|
|
};
|
|
|
|
use bevy_utils::EntityHashMap;
|
|
|
|
|
|
|
|
use crate::{prelude::ViewVisibility, Extract, ExtractSchedule, RenderApp};
|
|
|
|
|
|
|
|
/// Describes how to extract data needed for rendering from a component or
|
|
|
|
/// components.
|
|
|
|
///
|
|
|
|
/// Before rendering, any applicable components will be transferred from the
|
|
|
|
/// main world to the render world in the [`ExtractSchedule`] step.
|
|
|
|
///
|
|
|
|
/// This is essentially the same as
|
|
|
|
/// [`ExtractComponent`](crate::extract_component::ExtractComponent), but
|
|
|
|
/// higher-performance because it avoids the ECS overhead.
|
2023-10-13 17:06:53 +00:00
|
|
|
pub trait ExtractInstance: Send + Sync + Sized + 'static {
|
2023-10-08 10:34:44 +00:00
|
|
|
/// ECS [`WorldQuery`] to fetch the components to extract.
|
|
|
|
type Query: WorldQuery + ReadOnlyWorldQuery;
|
|
|
|
/// Filters the entities with additional constraints.
|
|
|
|
type Filter: WorldQuery + ReadOnlyWorldQuery;
|
|
|
|
|
|
|
|
/// Defines how the component is transferred into the "render world".
|
2023-10-13 17:06:53 +00:00
|
|
|
fn extract(item: QueryItem<'_, Self::Query>) -> Option<Self>;
|
2023-10-08 10:34:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This plugin extracts one or more components into the "render world" as
|
2023-10-13 17:06:53 +00:00
|
|
|
/// extracted instances.
|
2023-10-08 10:34:44 +00:00
|
|
|
///
|
|
|
|
/// Therefore it sets up the [`ExtractSchedule`] step for the specified
|
2023-10-13 17:06:53 +00:00
|
|
|
/// [`ExtractedInstances`].
|
2023-10-08 10:34:44 +00:00
|
|
|
#[derive(Default)]
|
2023-10-13 17:06:53 +00:00
|
|
|
pub struct ExtractInstancesPlugin<EI>
|
2023-10-08 10:34:44 +00:00
|
|
|
where
|
2023-10-13 17:06:53 +00:00
|
|
|
EI: ExtractInstance,
|
2023-10-08 10:34:44 +00:00
|
|
|
{
|
|
|
|
only_extract_visible: bool,
|
2023-10-13 17:06:53 +00:00
|
|
|
marker: PhantomData<fn() -> EI>,
|
2023-10-08 10:34:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
/// Stores all extract instances of a type in the render world.
|
2023-10-08 10:34:44 +00:00
|
|
|
#[derive(Resource, Deref, DerefMut)]
|
2023-10-13 17:06:53 +00:00
|
|
|
pub struct ExtractedInstances<EI>(EntityHashMap<Entity, EI>)
|
2023-10-08 10:34:44 +00:00
|
|
|
where
|
2023-10-13 17:06:53 +00:00
|
|
|
EI: ExtractInstance;
|
2023-10-08 10:34:44 +00:00
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
impl<EI> Default for ExtractedInstances<EI>
|
2023-10-08 10:34:44 +00:00
|
|
|
where
|
2023-10-13 17:06:53 +00:00
|
|
|
EI: ExtractInstance,
|
2023-10-08 10:34:44 +00:00
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
impl<EI> ExtractInstancesPlugin<EI>
|
2023-10-08 10:34:44 +00:00
|
|
|
where
|
2023-10-13 17:06:53 +00:00
|
|
|
EI: ExtractInstance,
|
2023-10-08 10:34:44 +00:00
|
|
|
{
|
2023-10-13 17:06:53 +00:00
|
|
|
/// Creates a new [`ExtractInstancesPlugin`] that unconditionally extracts to
|
2023-10-08 10:34:44 +00:00
|
|
|
/// the render world, whether the entity is visible or not.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
only_extract_visible: false,
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
/// Creates a new [`ExtractInstancesPlugin`] that extracts to the render world
|
2023-10-08 10:34:44 +00:00
|
|
|
/// if and only if the entity it's attached to is visible.
|
|
|
|
pub fn extract_visible() -> Self {
|
|
|
|
Self {
|
|
|
|
only_extract_visible: true,
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
impl<EI> Plugin for ExtractInstancesPlugin<EI>
|
2023-10-08 10:34:44 +00:00
|
|
|
where
|
2023-10-13 17:06:53 +00:00
|
|
|
EI: ExtractInstance,
|
2023-10-08 10:34:44 +00:00
|
|
|
{
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
|
2023-10-13 17:06:53 +00:00
|
|
|
render_app.init_resource::<ExtractedInstances<EI>>();
|
2023-10-08 10:34:44 +00:00
|
|
|
if self.only_extract_visible {
|
2023-10-13 17:06:53 +00:00
|
|
|
render_app.add_systems(ExtractSchedule, extract_visible::<EI>);
|
2023-10-08 10:34:44 +00:00
|
|
|
} else {
|
2023-10-13 17:06:53 +00:00
|
|
|
render_app.add_systems(ExtractSchedule, extract_all::<EI>);
|
2023-10-08 10:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
fn extract_all<EI>(
|
|
|
|
mut extracted_instances: ResMut<ExtractedInstances<EI>>,
|
|
|
|
query: Extract<Query<(Entity, EI::Query), EI::Filter>>,
|
2023-10-08 10:34:44 +00:00
|
|
|
) where
|
2023-10-13 17:06:53 +00:00
|
|
|
EI: ExtractInstance,
|
2023-10-08 10:34:44 +00:00
|
|
|
{
|
2023-10-13 17:06:53 +00:00
|
|
|
extracted_instances.clear();
|
2023-10-08 10:34:44 +00:00
|
|
|
for (entity, other) in &query {
|
2023-10-13 17:06:53 +00:00
|
|
|
if let Some(extract_instance) = EI::extract(other) {
|
|
|
|
extracted_instances.insert(entity, extract_instance);
|
2023-10-08 10:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
fn extract_visible<EI>(
|
|
|
|
mut extracted_instances: ResMut<ExtractedInstances<EI>>,
|
|
|
|
query: Extract<Query<(Entity, &ViewVisibility, EI::Query), EI::Filter>>,
|
2023-10-08 10:34:44 +00:00
|
|
|
) where
|
2023-10-13 17:06:53 +00:00
|
|
|
EI: ExtractInstance,
|
2023-10-08 10:34:44 +00:00
|
|
|
{
|
2023-10-13 17:06:53 +00:00
|
|
|
extracted_instances.clear();
|
2023-10-08 10:34:44 +00:00
|
|
|
for (entity, view_visibility, other) in &query {
|
|
|
|
if view_visibility.get() {
|
2023-10-13 17:06:53 +00:00
|
|
|
if let Some(extract_instance) = EI::extract(other) {
|
|
|
|
extracted_instances.insert(entity, extract_instance);
|
2023-10-08 10:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
impl<A> ExtractInstance for AssetId<A>
|
2023-10-08 10:34:44 +00:00
|
|
|
where
|
|
|
|
A: Asset,
|
|
|
|
{
|
|
|
|
type Query = Read<Handle<A>>;
|
|
|
|
type Filter = ();
|
|
|
|
|
2023-10-13 17:06:53 +00:00
|
|
|
fn extract(item: QueryItem<'_, Self::Query>) -> Option<Self> {
|
2023-10-08 10:34:44 +00:00
|
|
|
Some(item.id())
|
|
|
|
}
|
|
|
|
}
|