Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Immutable Component Support #16372

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,17 @@ description = "Creates a hierarchy of parents and children entities"
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "immutable_components"
bushrat011899 marked this conversation as resolved.
Show resolved Hide resolved
path = "examples/ecs/immutable_components.rs"
doc-scrape-examples = true

[package.metadata.example.immutable_components]
name = "Immutable Components"
description = "Demonstrates the creation and utility of immutable components"
category = "ECS (Entity Component System)"
wasm = false

[[example]]
name = "iter_combinations"
path = "examples/ecs/iter_combinations.rs"
Expand Down
12 changes: 6 additions & 6 deletions benches/benches/bevy_ecs/change_detection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy_ecs::{
component::Component,
component::{Component, ComponentMut},
entity::Entity,
prelude::{Added, Changed, EntityWorldMut, QueryState},
query::QueryFilter,
Expand Down Expand Up @@ -124,7 +124,7 @@ fn all_added_detection(criterion: &mut Criterion) {
}
}

fn all_changed_detection_generic<T: Component + Default + BenchModify>(
fn all_changed_detection_generic<T: ComponentMut + Default + BenchModify>(
group: &mut BenchGroup,
entity_count: u32,
) {
Expand Down Expand Up @@ -172,7 +172,7 @@ fn all_changed_detection(criterion: &mut Criterion) {
}
}

fn few_changed_detection_generic<T: Component + Default + BenchModify>(
fn few_changed_detection_generic<T: ComponentMut + Default + BenchModify>(
group: &mut BenchGroup,
entity_count: u32,
) {
Expand Down Expand Up @@ -222,7 +222,7 @@ fn few_changed_detection(criterion: &mut Criterion) {
}
}

fn none_changed_detection_generic<T: Component + Default>(
fn none_changed_detection_generic<T: ComponentMut + Default>(
group: &mut BenchGroup,
entity_count: u32,
) {
Expand Down Expand Up @@ -271,7 +271,7 @@ fn insert_if_bit_enabled<const B: u16>(entity: &mut EntityWorldMut, i: u16) {
}
}

fn add_archetypes_entities<T: Component + Default>(
fn add_archetypes_entities<T: ComponentMut + Default>(
world: &mut World,
archetype_count: u16,
entity_count: u32,
Expand All @@ -298,7 +298,7 @@ fn add_archetypes_entities<T: Component + Default>(
}
}
}
fn multiple_archetype_none_changed_detection_generic<T: Component + Default + BenchModify>(
fn multiple_archetype_none_changed_detection_generic<T: ComponentMut + Default + BenchModify>(
group: &mut BenchGroup,
archetype_count: u16,
entity_count: u32,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_animation/src/animation_curves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use core::{
marker::PhantomData,
};

use bevy_ecs::{component::Component, world::Mut};
use bevy_ecs::{component::ComponentMut, world::Mut};
use bevy_math::{
curve::{
cores::{UnevenCore, UnevenCoreError},
Expand Down Expand Up @@ -162,7 +162,7 @@ use crate::{
/// [`AnimationClip`]: crate::AnimationClip
pub trait AnimatableProperty: Reflect + TypePath {
/// The type of the component that the property lives on.
type Component: Component;
type Component: ComponentMut;

/// The type of the property to be animated.
type Property: Animatable + FromReflect + Reflectable + Clone + Sync + Debug;
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_animation/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy_derive::{Deref, DerefMut};
use bevy_ecs::{
component::Component,
event::EventReader,
reflect::ReflectComponent,
reflect::{ReflectComponent, ReflectComponentMut},
system::{Res, ResMut, Resource},
};
use bevy_reflect::{prelude::ReflectDefault, Reflect, ReflectSerialize};
Expand Down Expand Up @@ -127,7 +127,7 @@ pub struct AnimationGraph {

/// A [`Handle`] to the [`AnimationGraph`] to be used by the [`AnimationPlayer`](crate::AnimationPlayer) on the same entity.
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect, PartialEq, Eq, From)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct AnimationGraphHandle(pub Handle<AnimationGraph>);

impl From<AnimationGraphHandle> for AssetId<AnimationGraph> {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl Hash for AnimationTargetId {
/// time. However, you can change [`AnimationTarget`]'s `player` property at
/// runtime to change which player is responsible for animating the entity.
#[derive(Clone, Copy, Component, Reflect, VisitEntities, VisitEntitiesMut)]
#[reflect(Component, MapEntities, VisitEntities, VisitEntitiesMut)]
#[reflect(ComponentMut, Component, MapEntities, VisitEntities, VisitEntitiesMut)]
pub struct AnimationTarget {
/// The ID of this animation target.
///
Expand Down Expand Up @@ -768,7 +768,7 @@ impl ActiveAnimation {
/// Automatically added to any root animations of a scene when it is
/// spawned.
#[derive(Component, Default, Reflect)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct AnimationPlayer {
active_animations: HashMap<AnimationNodeIndex, ActiveAnimation>,
blend_weights: HashMap<AnimationNodeIndex, f32>,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_animation/src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use bevy_ecs::{
component::Component,
reflect::ReflectComponent,
reflect::{ReflectComponent, ReflectComponentMut},
system::{Query, Res},
};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
Expand All @@ -29,7 +29,7 @@ use crate::{graph::AnimationNodeIndex, ActiveAnimation, AnimationPlayer};
/// component to get confused about which animation is the "main" animation, and
/// transitions will usually be incorrect as a result.
#[derive(Component, Default, Reflect)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct AnimationTransitions {
main_animation: Option<AnimationNodeIndex>,
transitions: Vec<AnimationTransition>,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl App {
/// adding reflect data as specified in the [`Reflect`](bevy_reflect::Reflect) derive:
/// ```ignore (No serde "derive" feature)
/// #[derive(Component, Serialize, Deserialize, Reflect)]
/// #[reflect(Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
/// #[reflect(ComponentMut, Component, Serialize, Deserialize)] // will register ReflectComponent, ReflectSerialize, ReflectDeserialize
/// ```
///
/// See [`bevy_reflect::TypeRegistry::register`] for more information.
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_audio/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum PlaybackMode {
/// [`AudioSink`][crate::AudioSink] or [`SpatialAudioSink`][crate::SpatialAudioSink]
/// components. Changes to this component will *not* be applied to already-playing audio.
#[derive(Component, Clone, Copy, Debug, Reflect)]
#[reflect(Default, Component, Debug)]
#[reflect(Default, ComponentMut, Component, Debug)]
bushrat011899 marked this conversation as resolved.
Show resolved Hide resolved
pub struct PlaybackSettings {
/// The desired playback behavior.
pub mode: PlaybackMode,
Expand Down Expand Up @@ -147,7 +147,7 @@ impl PlaybackSettings {
/// This must be accompanied by `Transform` and `GlobalTransform`.
/// Only one entity with a `SpatialListener` should be present at any given time.
#[derive(Component, Clone, Debug, Reflect)]
#[reflect(Default, Component, Debug)]
#[reflect(Default, ComponentMut, Component, Debug)]
pub struct SpatialListener {
/// Left ear position relative to the `GlobalTransform`.
pub left_ear_offset: Vec3,
Expand Down Expand Up @@ -249,7 +249,7 @@ pub type AudioBundle = AudioSourceBundle<AudioSource>;
/// Playback can be configured using the [`PlaybackSettings`] component. Note that changes to the
/// `PlaybackSettings` component will *not* affect already-playing audio.
#[derive(Component, Reflect)]
#[reflect(Component)]
#[reflect(ComponentMut, Component)]
#[require(PlaybackSettings)]
pub struct AudioPlayer<Source = AudioSource>(pub Handle<Source>)
where
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "bevy_reflect")]
use bevy_ecs::reflect::ReflectComponent;
use bevy_ecs::reflect::{ReflectComponent, ReflectComponentMut};
use bevy_ecs::{component::Component, entity::Entity, query::QueryData};

use alloc::borrow::Cow;
Expand Down Expand Up @@ -27,7 +27,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
#[cfg_attr(
feature = "bevy_reflect",
derive(Reflect),
reflect(Component, Default, Debug)
reflect(ComponentMut, Component, Default, Debug)
)]
#[cfg_attr(
all(feature = "serialize", feature = "bevy_reflect"),
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_core_pipeline/src/auto_exposure/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use core::ops::RangeInclusive;

use super::compensation_curve::AutoExposureCompensationCurve;
use bevy_asset::Handle;
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_ecs::{
prelude::Component,
reflect::{ReflectComponent, ReflectComponentMut},
};
use bevy_image::Image;
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::extract_component::ExtractComponent;
Expand All @@ -24,7 +27,7 @@ use bevy_utils::default;
///
/// **Auto Exposure requires compute shaders and is not compatible with WebGL2.**
#[derive(Component, Clone, Reflect, ExtractComponent)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct AutoExposure {
/// The range of exposure values for the histogram.
///
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_core_pipeline/src/bloom/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use super::downsampling_pipeline::BloomUniforms;
use bevy_ecs::{prelude::Component, query::QueryItem, reflect::ReflectComponent};
use bevy_ecs::{
prelude::Component,
query::QueryItem,
reflect::{ReflectComponent, ReflectComponentMut},
};
use bevy_math::{AspectRatio, URect, UVec4, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{extract_component::ExtractComponent, prelude::Camera};
Expand All @@ -25,7 +29,7 @@ use bevy_render::{extract_component::ExtractComponent, prelude::Camera};
/// See <https://starlederer.github.io/bloom/> for a visualization of the parametric curve
/// used in Bevy as well as a visualization of the curve's respective scattering profile.
#[derive(Component, Reflect, Clone)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct Bloom {
/// Controls the baseline of how much the image is scattered (default: 0.15).
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use node::CasNode;
///
/// To use this, add the [`ContrastAdaptiveSharpening`] component to a 2D or 3D camera.
#[derive(Component, Reflect, Clone)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct ContrastAdaptiveSharpening {
/// Enable or disable sharpening.
pub enabled: bool,
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Default for ContrastAdaptiveSharpening {
}

#[derive(Component, Default, Reflect, Clone)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct DenoiseCas(bool);

/// The uniform struct extracted from [`ContrastAdaptiveSharpening`] attached to a [`Camera`].
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use bevy_transform::prelude::{GlobalTransform, Transform};
/// A 2D camera component. Enables the 2D render graph for a [`Camera`].
#[derive(Component, Default, Reflect, Clone, ExtractComponent)]
#[extract_component_filter(With<Camera>)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
#[require(
Camera,
DebandDither,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/core_3d/camera_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use serde::{Deserialize, Serialize};
/// This means "forward" is -Z.
#[derive(Component, Reflect, Clone, ExtractComponent)]
#[extract_component_filter(With<Camera>)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
#[require(
Camera,
DebandDither(|| DebandDither::Enabled),
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/dof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use bevy_ecs::{
component::Component,
entity::Entity,
query::{QueryItem, With},
reflect::ReflectComponent,
reflect::{ReflectComponent, ReflectComponentMut},
schedule::IntoSystemConfigs as _,
system::{lifetimeless::Read, Commands, Query, Res, ResMut, Resource},
world::{FromWorld, World},
Expand Down Expand Up @@ -77,7 +77,7 @@ pub struct DepthOfFieldPlugin;
///
/// [depth of field]: https://en.wikipedia.org/wiki/Depth_of_field
#[derive(Component, Clone, Copy, Reflect)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct DepthOfField {
/// The appearance of the effect.
pub mode: DepthOfFieldMode,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/fxaa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Sensitivity {
/// A component for enabling Fast Approximate Anti-Aliasing (FXAA)
/// for a [`bevy_render::camera::Camera`].
#[derive(Reflect, Component, Clone, ExtractComponent)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
#[extract_component_filter(With<Camera>)]
#[doc(alias = "FastApproximateAntiAliasing")]
pub struct Fxaa {
Expand Down
7 changes: 5 additions & 2 deletions crates/bevy_core_pipeline/src/motion_blur/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use crate::{
use bevy_app::{App, Plugin};
use bevy_asset::{load_internal_asset, Handle};
use bevy_ecs::{
bundle::Bundle, component::Component, query::With, reflect::ReflectComponent,
bundle::Bundle,
component::Component,
query::With,
reflect::{ReflectComponent, ReflectComponentMut},
schedule::IntoSystemConfigs,
};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
Expand Down Expand Up @@ -69,7 +72,7 @@ pub struct MotionBlurBundle {
/// # }
/// ````
#[derive(Reflect, Component, Clone, ExtractComponent, ShaderType)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
#[extract_component_filter(With<Camera>)]
#[require(DepthPrepass, MotionVectorPrepass)]
pub struct MotionBlur {
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_core_pipeline/src/oit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ impl Component for OrderIndependentTransparencySettings {
}
}

impl ComponentMut for OrderIndependentTransparencySettings {}

/// A plugin that adds support for Order Independent Transparency (OIT).
/// This can correctly render some scenes that would otherwise have artifacts due to alpha blending, but uses more memory.
///
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/post_process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy_ecs::{
component::Component,
entity::Entity,
query::{QueryItem, With},
reflect::ReflectComponent,
reflect::{ReflectComponent, ReflectComponentMut},
schedule::IntoSystemConfigs as _,
system::{lifetimeless::Read, Commands, Query, Res, ResMut, Resource},
world::{FromWorld, World},
Expand Down Expand Up @@ -96,7 +96,7 @@ pub struct PostProcessingPlugin;
///
/// [Gjøl & Svendsen 2016]: https://github.com/playdeadgames/publications/blob/master/INSIDE/rendering_inside_gdc2016.pdf
#[derive(Reflect, Component, Clone)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct ChromaticAberration {
/// The lookup texture that determines the color gradient.
///
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_core_pipeline/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ pub const MOTION_VECTOR_PREPASS_FORMAT: TextureFormat = TextureFormat::Rg16Float

/// If added to a [`crate::prelude::Camera3d`] then depth values will be copied to a separate texture available to the main pass.
#[derive(Component, Default, Reflect, Clone)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct DepthPrepass;

/// If added to a [`crate::prelude::Camera3d`] then vertex world normals will be copied to a separate texture available to the main pass.
/// Normals will have normal map textures already applied.
#[derive(Component, Default, Reflect, Clone)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct NormalPrepass;

/// If added to a [`crate::prelude::Camera3d`] then screen space motion vectors will be copied to a separate texture available to the main pass.
#[derive(Component, Default, Reflect, Clone)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct MotionVectorPrepass;

/// If added to a [`crate::prelude::Camera3d`] then deferred materials will be rendered to the deferred gbuffer texture and will be available to subsequent passes.
/// Note the default deferred lighting plugin also requires `DepthPrepass` to work correctly.
#[derive(Component, Default, Reflect)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
pub struct DeferredPrepass;

#[derive(Component, ShaderType, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core_pipeline/src/smaa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use bevy_ecs::{
component::Component,
entity::Entity,
query::{QueryItem, With},
reflect::ReflectComponent,
reflect::{ReflectComponent, ReflectComponentMut},
schedule::IntoSystemConfigs as _,
system::{lifetimeless::Read, Commands, Query, Res, ResMut, Resource},
world::{FromWorld, World},
Expand Down Expand Up @@ -92,7 +92,7 @@ pub struct SmaaPlugin;
/// A component for enabling Subpixel Morphological Anti-Aliasing (SMAA)
/// for a [`bevy_render::camera::Camera`].
#[derive(Clone, Copy, Default, Component, Reflect, ExtractComponent)]
#[reflect(Component, Default)]
#[reflect(ComponentMut, Component, Default)]
#[doc(alias = "SubpixelMorphologicalAntiAliasing")]
pub struct Smaa {
/// A predefined set of SMAA parameters: i.e. a quality level.
Expand Down
Loading
Loading