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,
|
Split WorldQuery into WorldQueryData and WorldQueryFilter (#9918)
# Objective
- Fixes #7680
- This is an updated for https://github.com/bevyengine/bevy/pull/8899
which had the same objective but fell a long way behind the latest
changes
## Solution
The traits `WorldQueryData : WorldQuery` and `WorldQueryFilter :
WorldQuery` have been added and some of the types and functions from
`WorldQuery` has been moved into them.
`ReadOnlyWorldQuery` has been replaced with `ReadOnlyWorldQueryData`.
`WorldQueryFilter` is safe (as long as `WorldQuery` is implemented
safely).
`WorldQueryData` is unsafe - safely implementing it requires that
`Self::ReadOnly` is a readonly version of `Self` (this used to be a
safety requirement of `WorldQuery`)
The type parameters `Q` and `F` of `Query` must now implement
`WorldQueryData` and `WorldQueryFilter` respectively.
This makes it impossible to accidentally use a filter in the data
position or vice versa which was something that could lead to bugs.
~~Compile failure tests have been added to check this.~~
It was previously sometimes useful to use `Option<With<T>>` in the data
position. Use `Has<T>` instead in these cases.
The `WorldQuery` derive macro has been split into separate derive macros
for `WorldQueryData` and `WorldQueryFilter`.
Previously it was possible to derive both `WorldQuery` for a struct that
had a mixture of data and filter items. This would not work correctly in
some cases but could be a useful pattern in others. *This is no longer
possible.*
---
## Notes
- The changes outside of `bevy_ecs` are all changing type parameters to
the new types, updating the macro use, or replacing `Option<With<T>>`
with `Has<T>`.
- All `WorldQueryData` types always returned `true` for `IS_ARCHETYPAL`
so I moved it to `WorldQueryFilter` and
replaced all calls to it with `true`. That should be the only logic
change outside of the macro generation code.
- `Changed<T>` and `Added<T>` were being generated by a macro that I
have expanded. Happy to revert that if desired.
- The two derive macros share some functions for implementing
`WorldQuery` but the tidiest way I could find to implement them was to
give them a ton of arguments and ask clippy to ignore that.
## Changelog
### Changed
- Split `WorldQuery` into `WorldQueryData` and `WorldQueryFilter` which
now have separate derive macros. It is not possible to derive both for
the same type.
- `Query` now requires that the first type argument implements
`WorldQueryData` and the second implements `WorldQueryFilter`
## Migration Guide
- Update derives
```rust
// old
#[derive(WorldQuery)]
#[world_query(mutable, derive(Debug))]
struct CustomQuery {
entity: Entity,
a: &'static mut ComponentA
}
#[derive(WorldQuery)]
struct QueryFilter {
_c: With<ComponentC>
}
// new
#[derive(WorldQueryData)]
#[world_query_data(mutable, derive(Debug))]
struct CustomQuery {
entity: Entity,
a: &'static mut ComponentA,
}
#[derive(WorldQueryFilter)]
struct QueryFilter {
_c: With<ComponentC>
}
```
- Replace `Option<With<T>>` with `Has<T>`
```rust
/// old
fn my_system(query: Query<(Entity, Option<With<ComponentA>>)>)
{
for (entity, has_a_option) in query.iter(){
let has_a:bool = has_a_option.is_some();
//todo!()
}
}
/// new
fn my_system(query: Query<(Entity, Has<ComponentA>)>)
{
for (entity, has_a) in query.iter(){
//todo!()
}
}
```
- Fix queries which had filters in the data position or vice versa.
```rust
// old
fn my_system(query: Query<(Entity, With<ComponentA>)>)
{
for (entity, _) in query.iter(){
//todo!()
}
}
// new
fn my_system(query: Query<Entity, With<ComponentA>>)
{
for entity in query.iter(){
//todo!()
}
}
// old
fn my_system(query: Query<AnyOf<(&ComponentA, With<ComponentB>)>>)
{
for (entity, _) in query.iter(){
//todo!()
}
}
// new
fn my_system(query: Query<Option<&ComponentA>, Or<(With<ComponentA>, With<ComponentB>)>>)
{
for entity in query.iter(){
//todo!()
}
}
```
---------
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2023-11-28 03:56:07 +00:00
|
|
|
query::{QueryItem, ReadOnlyWorldQueryData, WorldQueryFilter},
|
2023-10-08 10:34:44 +00:00
|
|
|
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 {
|
Split WorldQuery into WorldQueryData and WorldQueryFilter (#9918)
# Objective
- Fixes #7680
- This is an updated for https://github.com/bevyengine/bevy/pull/8899
which had the same objective but fell a long way behind the latest
changes
## Solution
The traits `WorldQueryData : WorldQuery` and `WorldQueryFilter :
WorldQuery` have been added and some of the types and functions from
`WorldQuery` has been moved into them.
`ReadOnlyWorldQuery` has been replaced with `ReadOnlyWorldQueryData`.
`WorldQueryFilter` is safe (as long as `WorldQuery` is implemented
safely).
`WorldQueryData` is unsafe - safely implementing it requires that
`Self::ReadOnly` is a readonly version of `Self` (this used to be a
safety requirement of `WorldQuery`)
The type parameters `Q` and `F` of `Query` must now implement
`WorldQueryData` and `WorldQueryFilter` respectively.
This makes it impossible to accidentally use a filter in the data
position or vice versa which was something that could lead to bugs.
~~Compile failure tests have been added to check this.~~
It was previously sometimes useful to use `Option<With<T>>` in the data
position. Use `Has<T>` instead in these cases.
The `WorldQuery` derive macro has been split into separate derive macros
for `WorldQueryData` and `WorldQueryFilter`.
Previously it was possible to derive both `WorldQuery` for a struct that
had a mixture of data and filter items. This would not work correctly in
some cases but could be a useful pattern in others. *This is no longer
possible.*
---
## Notes
- The changes outside of `bevy_ecs` are all changing type parameters to
the new types, updating the macro use, or replacing `Option<With<T>>`
with `Has<T>`.
- All `WorldQueryData` types always returned `true` for `IS_ARCHETYPAL`
so I moved it to `WorldQueryFilter` and
replaced all calls to it with `true`. That should be the only logic
change outside of the macro generation code.
- `Changed<T>` and `Added<T>` were being generated by a macro that I
have expanded. Happy to revert that if desired.
- The two derive macros share some functions for implementing
`WorldQuery` but the tidiest way I could find to implement them was to
give them a ton of arguments and ask clippy to ignore that.
## Changelog
### Changed
- Split `WorldQuery` into `WorldQueryData` and `WorldQueryFilter` which
now have separate derive macros. It is not possible to derive both for
the same type.
- `Query` now requires that the first type argument implements
`WorldQueryData` and the second implements `WorldQueryFilter`
## Migration Guide
- Update derives
```rust
// old
#[derive(WorldQuery)]
#[world_query(mutable, derive(Debug))]
struct CustomQuery {
entity: Entity,
a: &'static mut ComponentA
}
#[derive(WorldQuery)]
struct QueryFilter {
_c: With<ComponentC>
}
// new
#[derive(WorldQueryData)]
#[world_query_data(mutable, derive(Debug))]
struct CustomQuery {
entity: Entity,
a: &'static mut ComponentA,
}
#[derive(WorldQueryFilter)]
struct QueryFilter {
_c: With<ComponentC>
}
```
- Replace `Option<With<T>>` with `Has<T>`
```rust
/// old
fn my_system(query: Query<(Entity, Option<With<ComponentA>>)>)
{
for (entity, has_a_option) in query.iter(){
let has_a:bool = has_a_option.is_some();
//todo!()
}
}
/// new
fn my_system(query: Query<(Entity, Has<ComponentA>)>)
{
for (entity, has_a) in query.iter(){
//todo!()
}
}
```
- Fix queries which had filters in the data position or vice versa.
```rust
// old
fn my_system(query: Query<(Entity, With<ComponentA>)>)
{
for (entity, _) in query.iter(){
//todo!()
}
}
// new
fn my_system(query: Query<Entity, With<ComponentA>>)
{
for entity in query.iter(){
//todo!()
}
}
// old
fn my_system(query: Query<AnyOf<(&ComponentA, With<ComponentB>)>>)
{
for (entity, _) in query.iter(){
//todo!()
}
}
// new
fn my_system(query: Query<Option<&ComponentA>, Or<(With<ComponentA>, With<ComponentB>)>>)
{
for entity in query.iter(){
//todo!()
}
}
```
---------
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2023-11-28 03:56:07 +00:00
|
|
|
/// ECS [`ReadOnlyWorldQueryData`] to fetch the components to extract.
|
|
|
|
type Query: ReadOnlyWorldQueryData;
|
2023-10-08 10:34:44 +00:00
|
|
|
/// Filters the entities with additional constraints.
|
Split WorldQuery into WorldQueryData and WorldQueryFilter (#9918)
# Objective
- Fixes #7680
- This is an updated for https://github.com/bevyengine/bevy/pull/8899
which had the same objective but fell a long way behind the latest
changes
## Solution
The traits `WorldQueryData : WorldQuery` and `WorldQueryFilter :
WorldQuery` have been added and some of the types and functions from
`WorldQuery` has been moved into them.
`ReadOnlyWorldQuery` has been replaced with `ReadOnlyWorldQueryData`.
`WorldQueryFilter` is safe (as long as `WorldQuery` is implemented
safely).
`WorldQueryData` is unsafe - safely implementing it requires that
`Self::ReadOnly` is a readonly version of `Self` (this used to be a
safety requirement of `WorldQuery`)
The type parameters `Q` and `F` of `Query` must now implement
`WorldQueryData` and `WorldQueryFilter` respectively.
This makes it impossible to accidentally use a filter in the data
position or vice versa which was something that could lead to bugs.
~~Compile failure tests have been added to check this.~~
It was previously sometimes useful to use `Option<With<T>>` in the data
position. Use `Has<T>` instead in these cases.
The `WorldQuery` derive macro has been split into separate derive macros
for `WorldQueryData` and `WorldQueryFilter`.
Previously it was possible to derive both `WorldQuery` for a struct that
had a mixture of data and filter items. This would not work correctly in
some cases but could be a useful pattern in others. *This is no longer
possible.*
---
## Notes
- The changes outside of `bevy_ecs` are all changing type parameters to
the new types, updating the macro use, or replacing `Option<With<T>>`
with `Has<T>`.
- All `WorldQueryData` types always returned `true` for `IS_ARCHETYPAL`
so I moved it to `WorldQueryFilter` and
replaced all calls to it with `true`. That should be the only logic
change outside of the macro generation code.
- `Changed<T>` and `Added<T>` were being generated by a macro that I
have expanded. Happy to revert that if desired.
- The two derive macros share some functions for implementing
`WorldQuery` but the tidiest way I could find to implement them was to
give them a ton of arguments and ask clippy to ignore that.
## Changelog
### Changed
- Split `WorldQuery` into `WorldQueryData` and `WorldQueryFilter` which
now have separate derive macros. It is not possible to derive both for
the same type.
- `Query` now requires that the first type argument implements
`WorldQueryData` and the second implements `WorldQueryFilter`
## Migration Guide
- Update derives
```rust
// old
#[derive(WorldQuery)]
#[world_query(mutable, derive(Debug))]
struct CustomQuery {
entity: Entity,
a: &'static mut ComponentA
}
#[derive(WorldQuery)]
struct QueryFilter {
_c: With<ComponentC>
}
// new
#[derive(WorldQueryData)]
#[world_query_data(mutable, derive(Debug))]
struct CustomQuery {
entity: Entity,
a: &'static mut ComponentA,
}
#[derive(WorldQueryFilter)]
struct QueryFilter {
_c: With<ComponentC>
}
```
- Replace `Option<With<T>>` with `Has<T>`
```rust
/// old
fn my_system(query: Query<(Entity, Option<With<ComponentA>>)>)
{
for (entity, has_a_option) in query.iter(){
let has_a:bool = has_a_option.is_some();
//todo!()
}
}
/// new
fn my_system(query: Query<(Entity, Has<ComponentA>)>)
{
for (entity, has_a) in query.iter(){
//todo!()
}
}
```
- Fix queries which had filters in the data position or vice versa.
```rust
// old
fn my_system(query: Query<(Entity, With<ComponentA>)>)
{
for (entity, _) in query.iter(){
//todo!()
}
}
// new
fn my_system(query: Query<Entity, With<ComponentA>>)
{
for entity in query.iter(){
//todo!()
}
}
// old
fn my_system(query: Query<AnyOf<(&ComponentA, With<ComponentB>)>>)
{
for (entity, _) in query.iter(){
//todo!()
}
}
// new
fn my_system(query: Query<Option<&ComponentA>, Or<(With<ComponentA>, With<ComponentB>)>>)
{
for entity in query.iter(){
//todo!()
}
}
```
---------
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2023-11-28 03:56:07 +00:00
|
|
|
type Filter: WorldQueryFilter;
|
2023-10-08 10:34:44 +00:00
|
|
|
|
|
|
|
/// 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())
|
|
|
|
}
|
|
|
|
}
|