pub struct Res<'w, T>{ /* private fields */ }
Expand description
Shared borrow of a Resource
.
See the Resource
documentation for usage.
If you need a unique mutable borrow, use ResMut
instead.
§Panics
Panics when used as a SystemParameter
if the resource does not exist.
Use Option<Res<T>>
instead if the resource might not always exist.
Implementations§
source§impl<'w, T> Res<'w, T>where
T: Resource,
impl<'w, T> Res<'w, T>where
T: Resource,
sourcepub fn clone(this: &Res<'w, T>) -> Res<'w, T>
pub fn clone(this: &Res<'w, T>) -> Res<'w, T>
Copies a reference to a resource.
Note that unless you actually need an instance of Res<T>
, you should
prefer to just convert it to &T
which can be freely copied.
sourcepub fn into_inner(self) -> &'w T
pub fn into_inner(self) -> &'w T
Due to lifetime limitations of the Deref
trait, this method can be used to obtain a
reference of the Resource
with a lifetime bound to 'w
instead of the lifetime of the
struct itself.
Examples found in repository?
examples/stress_tests/bevymark.rs (line 167)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
fn scheduled_spawner(
mut commands: Commands,
args: Res<Args>,
windows: Query<&Window>,
mut scheduled: ResMut<BirdScheduled>,
mut counter: ResMut<BevyCounter>,
bird_resources: ResMut<BirdResources>,
) {
let window = windows.single();
if scheduled.waves > 0 {
let bird_resources = bird_resources.into_inner();
spawn_birds(
&mut commands,
args.into_inner(),
&window.resolution,
&mut counter,
scheduled.per_wave,
bird_resources,
None,
scheduled.waves - 1,
);
scheduled.waves -= 1;
}
}
#[derive(Resource)]
struct BirdResources {
textures: Vec<Handle<Image>>,
materials: Vec<Handle<ColorMaterial>>,
quad: Mesh2dHandle,
color_rng: ChaCha8Rng,
material_rng: ChaCha8Rng,
velocity_rng: ChaCha8Rng,
transform_rng: ChaCha8Rng,
}
#[derive(Component)]
struct StatsText;
#[allow(clippy::too_many_arguments)]
fn setup(
mut commands: Commands,
args: Res<Args>,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
material_assets: ResMut<Assets<ColorMaterial>>,
images: ResMut<Assets<Image>>,
windows: Query<&Window>,
counter: ResMut<BevyCounter>,
) {
warn!(include_str!("warning_string.txt"));
let args = args.into_inner();
let images = images.into_inner();
let mut textures = Vec::with_capacity(args.material_texture_count.max(1));
if matches!(args.mode, Mode::Sprite) || args.material_texture_count > 0 {
textures.push(asset_server.load("branding/icon.png"));
}
init_textures(&mut textures, args, images);
let material_assets = material_assets.into_inner();
let materials = init_materials(args, &textures, material_assets);
let mut bird_resources = BirdResources {
textures,
materials,
quad: meshes
.add(Rectangle::from_size(Vec2::splat(BIRD_TEXTURE_SIZE as f32)))
.into(),
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
color_rng: ChaCha8Rng::seed_from_u64(42),
material_rng: ChaCha8Rng::seed_from_u64(42),
velocity_rng: ChaCha8Rng::seed_from_u64(42),
transform_rng: ChaCha8Rng::seed_from_u64(42),
};
let text_section = move |color: Srgba, value: &str| {
TextSection::new(
value,
TextStyle {
font_size: 40.0,
color: color.into(),
..default()
},
)
};
commands.spawn(Camera2dBundle::default());
commands
.spawn(NodeBundle {
style: Style {
position_type: PositionType::Absolute,
padding: UiRect::all(Val::Px(5.0)),
..default()
},
z_index: ZIndex::Global(i32::MAX),
background_color: Color::BLACK.with_alpha(0.75).into(),
..default()
})
.with_children(|c| {
c.spawn((
TextBundle::from_sections([
text_section(LIME, "Bird Count: "),
text_section(AQUA, ""),
text_section(LIME, "\nFPS (raw): "),
text_section(AQUA, ""),
text_section(LIME, "\nFPS (SMA): "),
text_section(AQUA, ""),
text_section(LIME, "\nFPS (EMA): "),
text_section(AQUA, ""),
]),
StatsText,
));
});
let mut scheduled = BirdScheduled {
per_wave: args.per_wave,
waves: args.waves,
};
if args.benchmark {
let counter = counter.into_inner();
for wave in (0..scheduled.waves).rev() {
spawn_birds(
&mut commands,
args,
&windows.single().resolution,
counter,
scheduled.per_wave,
&mut bird_resources,
Some(wave),
wave,
);
}
scheduled.waves = 0;
}
commands.insert_resource(bird_resources);
commands.insert_resource(scheduled);
}
#[allow(clippy::too_many_arguments)]
fn mouse_handler(
mut commands: Commands,
args: Res<Args>,
time: Res<Time>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
windows: Query<&Window>,
bird_resources: ResMut<BirdResources>,
mut counter: ResMut<BevyCounter>,
mut rng: Local<Option<ChaCha8Rng>>,
mut wave: Local<usize>,
) {
if rng.is_none() {
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
*rng = Some(ChaCha8Rng::seed_from_u64(42));
}
let rng = rng.as_mut().unwrap();
let window = windows.single();
if mouse_button_input.just_released(MouseButton::Left) {
counter.color = Color::linear_rgb(rng.gen(), rng.gen(), rng.gen());
}
if mouse_button_input.pressed(MouseButton::Left) {
let spawn_count = (BIRDS_PER_SECOND as f64 * time.delta_seconds_f64()) as usize;
spawn_birds(
&mut commands,
args.into_inner(),
&window.resolution,
&mut counter,
spawn_count,
bird_resources.into_inner(),
None,
*wave,
);
*wave += 1;
}
}
More examples
examples/shader/custom_phase_item.rs (line 80)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
fn render<'w>(
_: &P,
_: ROQueryItem<'w, Self::ViewQuery>,
_: Option<ROQueryItem<'w, Self::ItemQuery>>,
custom_phase_item_buffers: SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult {
// Borrow check workaround.
let custom_phase_item_buffers = custom_phase_item_buffers.into_inner();
// Tell the GPU where the vertices are.
pass.set_vertex_buffer(
0,
custom_phase_item_buffers
.vertices
.buffer()
.unwrap()
.slice(..),
);
// Tell the GPU where the indices are.
pass.set_index_buffer(
custom_phase_item_buffers
.indices
.buffer()
.unwrap()
.slice(..),
0,
IndexFormat::Uint32,
);
// Draw one triangle (3 vertices).
pass.draw_indexed(0..3, 0, 0..1);
RenderCommandResult::Success
}
examples/shader/shader_instancing.rs (line 257)
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
fn render<'w>(
item: &P,
_view: (),
instance_buffer: Option<&'w InstanceBuffer>,
(meshes, render_mesh_instances): SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult {
let Some(mesh_instance) = render_mesh_instances.render_mesh_queue_data(item.entity())
else {
return RenderCommandResult::Failure;
};
let Some(gpu_mesh) = meshes.into_inner().get(mesh_instance.mesh_asset_id) else {
return RenderCommandResult::Failure;
};
let Some(instance_buffer) = instance_buffer else {
return RenderCommandResult::Failure;
};
pass.set_vertex_buffer(0, gpu_mesh.vertex_buffer.slice(..));
pass.set_vertex_buffer(1, instance_buffer.buffer.slice(..));
match &gpu_mesh.buffer_info {
GpuBufferInfo::Indexed {
buffer,
index_format,
count,
} => {
pass.set_index_buffer(buffer.slice(..), 0, *index_format);
pass.draw_indexed(0..*count, 0, 0..instance_buffer.length as u32);
}
GpuBufferInfo::NonIndexed => {
pass.draw(0..gpu_mesh.vertex_count, 0..instance_buffer.length as u32);
}
}
RenderCommandResult::Success
}
examples/stress_tests/many_cubes.rs (line 140)
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
fn setup(
mut commands: Commands,
args: Res<Args>,
mesh_assets: ResMut<Assets<Mesh>>,
material_assets: ResMut<Assets<StandardMaterial>>,
images: ResMut<Assets<Image>>,
) {
warn!(include_str!("warning_string.txt"));
let args = args.into_inner();
let images = images.into_inner();
let material_assets = material_assets.into_inner();
let mesh_assets = mesh_assets.into_inner();
let meshes = init_meshes(args, mesh_assets);
let material_textures = init_textures(args, images);
let materials = init_materials(args, &material_textures, material_assets);
// We're seeding the PRNG here to make this example deterministic for testing purposes.
// This isn't strictly required in practical use unless you need your app to be deterministic.
let mut material_rng = ChaCha8Rng::seed_from_u64(42);
match args.layout {
Layout::Sphere => {
// NOTE: This pattern is good for testing performance of culling as it provides roughly
// the same number of visible meshes regardless of the viewing angle.
const N_POINTS: usize = WIDTH * HEIGHT * 4;
// NOTE: f64 is used to avoid precision issues that produce visual artifacts in the distribution
let radius = WIDTH as f64 * 2.5;
let golden_ratio = 0.5f64 * (1.0f64 + 5.0f64.sqrt());
for i in 0..N_POINTS {
let spherical_polar_theta_phi =
fibonacci_spiral_on_sphere(golden_ratio, i, N_POINTS);
let unit_sphere_p = spherical_polar_to_cartesian(spherical_polar_theta_phi);
let (mesh, transform) = meshes.choose(&mut material_rng).unwrap();
let mut cube = commands.spawn(PbrBundle {
mesh: mesh.clone(),
material: materials.choose(&mut material_rng).unwrap().clone(),
transform: Transform::from_translation((radius * unit_sphere_p).as_vec3())
.looking_at(Vec3::ZERO, Vec3::Y)
.mul_transform(*transform),
..default()
});
if args.no_frustum_culling {
cube.insert(NoFrustumCulling);
}
if args.no_automatic_batching {
cube.insert(NoAutomaticBatching);
}
}
// camera
let mut camera = commands.spawn(Camera3dBundle::default());
if args.gpu_culling {
camera.insert(GpuCulling);
}
if args.no_cpu_culling {
camera.insert(NoCpuCulling);
}
// Inside-out box around the meshes onto which shadows are cast (though you cannot see them...)
commands.spawn((
PbrBundle {
mesh: mesh_assets.add(Cuboid::from_size(Vec3::splat(radius as f32 * 2.2))),
material: material_assets.add(StandardMaterial::from(Color::WHITE)),
transform: Transform::from_scale(-Vec3::ONE),
..default()
},
NotShadowCaster,
));
}
_ => {
// NOTE: This pattern is good for demonstrating that frustum culling is working correctly
// as the number of visible meshes rises and falls depending on the viewing angle.
let scale = 2.5;
for x in 0..WIDTH {
for y in 0..HEIGHT {
// introduce spaces to break any kind of moiré pattern
if x % 10 == 0 || y % 10 == 0 {
continue;
}
// cube
commands.spawn(PbrBundle {
mesh: meshes.choose(&mut material_rng).unwrap().0.clone(),
material: materials.choose(&mut material_rng).unwrap().clone(),
transform: Transform::from_xyz((x as f32) * scale, (y as f32) * scale, 0.0),
..default()
});
commands.spawn(PbrBundle {
mesh: meshes.choose(&mut material_rng).unwrap().0.clone(),
material: materials.choose(&mut material_rng).unwrap().clone(),
transform: Transform::from_xyz(
(x as f32) * scale,
HEIGHT as f32 * scale,
(y as f32) * scale,
),
..default()
});
commands.spawn(PbrBundle {
mesh: meshes.choose(&mut material_rng).unwrap().0.clone(),
material: materials.choose(&mut material_rng).unwrap().clone(),
transform: Transform::from_xyz((x as f32) * scale, 0.0, (y as f32) * scale),
..default()
});
commands.spawn(PbrBundle {
mesh: meshes.choose(&mut material_rng).unwrap().0.clone(),
material: materials.choose(&mut material_rng).unwrap().clone(),
transform: Transform::from_xyz(0.0, (x as f32) * scale, (y as f32) * scale),
..default()
});
}
}
// camera
let center = 0.5 * scale * Vec3::new(WIDTH as f32, HEIGHT as f32, WIDTH as f32);
commands.spawn(Camera3dBundle {
transform: Transform::from_translation(center),
..default()
});
// Inside-out box around the meshes onto which shadows are cast (though you cannot see them...)
commands.spawn((
PbrBundle {
mesh: mesh_assets.add(Cuboid::from_size(2.0 * 1.1 * center)),
material: material_assets.add(StandardMaterial::from(Color::WHITE)),
transform: Transform::from_scale(-Vec3::ONE).with_translation(center),
..default()
},
NotShadowCaster,
));
}
}
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
shadows_enabled: args.shadows,
..default()
},
transform: Transform::IDENTITY.looking_at(Vec3::new(0.0, -1.0, -1.0), Vec3::Y),
..default()
});
}
Trait Implementations§
source§impl<'w, T> DetectChanges for Res<'w, T>
impl<'w, T> DetectChanges for Res<'w, T>
source§fn is_changed(&self) -> bool
fn is_changed(&self) -> bool
Returns
true
if this value was added or mutably dereferenced
either since the last time the system ran or, if the system never ran,
since the beginning of the program. Read moresource§fn last_changed(&self) -> Tick
fn last_changed(&self) -> Tick
Returns the change tick recording the time this data was most recently changed. Read more
source§impl<'w, 'a, T> IntoIterator for &'a Res<'w, T>
impl<'w, 'a, T> IntoIterator for &'a Res<'w, T>
source§impl<'a, T> SystemParam for Res<'a, T>where
T: Resource,
impl<'a, T> SystemParam for Res<'a, T>where
T: Resource,
source§type State = ComponentId
type State = ComponentId
Used to store data which persists across invocations of a system.
source§type Item<'w, 's> = Res<'w, T>
type Item<'w, 's> = Res<'w, T>
The item type returned when constructing this system param.
The value of this associated type should be
Self
, instantiated with new lifetimes. Read moresource§fn init_state(
world: &mut World,
system_meta: &mut SystemMeta,
) -> <Res<'a, T> as SystemParam>::State
fn init_state( world: &mut World, system_meta: &mut SystemMeta, ) -> <Res<'a, T> as SystemParam>::State
Registers any
World
access used by this SystemParam
and creates a new instance of this param’s State
.source§unsafe fn get_param<'w, 's>(
_: &'s mut <Res<'a, T> as SystemParam>::State,
system_meta: &SystemMeta,
world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> <Res<'a, T> as SystemParam>::Item<'w, 's>
unsafe fn get_param<'w, 's>( _: &'s mut <Res<'a, T> as SystemParam>::State, system_meta: &SystemMeta, world: UnsafeWorldCell<'w>, change_tick: Tick, ) -> <Res<'a, T> as SystemParam>::Item<'w, 's>
Creates a parameter to be passed into a
SystemParamFunction
. Read moresource§unsafe fn new_archetype(
state: &mut Self::State,
archetype: &Archetype,
system_meta: &mut SystemMeta,
)
unsafe fn new_archetype( state: &mut Self::State, archetype: &Archetype, system_meta: &mut SystemMeta, )
For the specified
Archetype
, registers the components accessed by this SystemParam
(if applicable).a Read moresource§fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World)
Applies any deferred mutations stored in this
SystemParam
’s state.
This is used to apply Commands
during apply_deferred
.source§fn queue(
state: &mut Self::State,
system_meta: &SystemMeta,
world: DeferredWorld<'_>,
)
fn queue( state: &mut Self::State, system_meta: &SystemMeta, world: DeferredWorld<'_>, )
Queues any deferred mutations to be applied at the next
apply_deferred
.impl<'a, T> ReadOnlySystemParam for Res<'a, T>where
T: Resource,
Auto Trait Implementations§
impl<'w, T> Freeze for Res<'w, T>where
T: ?Sized,
impl<'w, T> RefUnwindSafe for Res<'w, T>where
T: RefUnwindSafe + ?Sized,
impl<'w, T> Send for Res<'w, T>where
T: ?Sized,
impl<'w, T> Sync for Res<'w, T>where
T: ?Sized,
impl<'w, T> Unpin for Res<'w, T>where
T: ?Sized,
impl<'w, T> UnwindSafe for Res<'w, T>where
T: RefUnwindSafe + ?Sized,
Blanket Implementations§
source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
Return the
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more