pub struct Image {
pub data: Option<Vec<u8>>,
pub data_order: TextureDataOrder,
pub texture_descriptor: TextureDescriptor<Option<&'static str>, &'static [TextureFormat]>,
pub sampler: ImageSampler,
pub texture_view_descriptor: Option<TextureViewDescriptor<Option<&'static str>>>,
pub asset_usage: RenderAssetUsages,
pub copy_on_resize: bool,
}Expand description
An image, optimized for usage in rendering.
§Remote Inspection
To transmit an Image between two running Bevy apps, e.g. through BRP, use SerializedImage.
This type is only meant for short-term transmission between same versions and should not be stored anywhere.
Fields§
§data: Option<Vec<u8>>Raw pixel data.
If the image is being used as a storage texture which doesn’t need to be initialized by the
CPU, then this should be None.
Otherwise, it should always be Some.
data_order: TextureDataOrderFor texture data with layers and mips, this field controls how wgpu interprets the buffer layout.
Use TextureDataOrder::default() for all other cases.
texture_descriptor: TextureDescriptor<Option<&'static str>, &'static [TextureFormat]>Describes the data layout of the GPU texture.
For example, whether a texture contains 1D/2D/3D data, and what the format of the texture data is.
§Field Usage Notes
TextureDescriptor::labelis used for caching purposes when not usingAsset<Image>.
If you use assets, the label is purely a debugging aid.TextureDescriptor::view_formatsis currently unused by Bevy.
sampler: ImageSamplerThe ImageSampler to use during rendering.
texture_view_descriptor: Option<TextureViewDescriptor<Option<&'static str>>>Describes how the GPU texture should be interpreted.
For example, 2D image data could be read as plain 2D, an array texture of layers of 2D with the same dimensions (and the number of layers in that case),
a cube map, an array of cube maps, etc.
§Field Usage Notes
TextureViewDescriptor::labelis used for caching purposes when not usingAsset<Image>.
If you use assets, the label is purely a debugging aid.
asset_usage: RenderAssetUsagesWhere this image asset will be used. See RenderAssetUsages for more.
copy_on_resize: boolWhether this image should be copied on the GPU when resized.
Implementations§
Source§impl Image
impl Image
Sourcepub fn new(
size: Extent3d,
dimension: TextureDimension,
data: Vec<u8>,
format: TextureFormat,
asset_usage: RenderAssetUsages,
) -> Image
pub fn new( size: Extent3d, dimension: TextureDimension, data: Vec<u8>, format: TextureFormat, asset_usage: RenderAssetUsages, ) -> Image
Creates a new image from raw binary data and the corresponding metadata.
§Panics
Panics if the length of the data, volume of the size and the size of the format
do not match.
Examples found in repository?
517 fn create_white_cubemap(size: u32) -> Image {
518 // f16 bytes for 1.0 (white): [0, 60] in little-endian
519 const WHITE_F16: [u8; 2] = [0, 60];
520 const WHITE_PIXEL: [u8; 8] = [
521 WHITE_F16[0],
522 WHITE_F16[1], // R
523 WHITE_F16[0],
524 WHITE_F16[1], // G
525 WHITE_F16[0],
526 WHITE_F16[1], // B
527 WHITE_F16[0],
528 WHITE_F16[1], // A
529 ];
530
531 let pixel_data: Vec<u8> = (0..6 * size * size).flat_map(|_| WHITE_PIXEL).collect();
532
533 Image {
534 texture_view_descriptor: Some(TextureViewDescriptor {
535 dimension: Some(TextureViewDimension::Cube),
536 ..Default::default()
537 }),
538 ..Image::new(
539 Extent3d {
540 width: size,
541 height: size,
542 depth_or_array_layers: 6,
543 },
544 TextureDimension::D2,
545 pixel_data,
546 TextureFormat::Rgba16Float,
547 RenderAssetUsages::RENDER_WORLD,
548 )
549 }
550 }
551
552 pub fn setup(
553 mut commands: Commands,
554 mut meshes: ResMut<Assets<Mesh>>,
555 mut materials: ResMut<Assets<StandardMaterial>>,
556 mut images: ResMut<Assets<Image>>,
557 ) {
558 let sphere_mesh = meshes.add(Sphere::new(0.45));
559
560 // Light should come from the environment map only
561 commands.insert_resource(GlobalAmbientLight::NONE);
562
563 // add entities to the world
564 for y in -2..=2 {
565 for x in -5..=5 {
566 let x01 = (x + 5) as f32 / 10.0;
567 let y01 = (y + 2) as f32 / 4.0;
568 // sphere
569 commands.spawn((
570 Mesh3d(sphere_mesh.clone()),
571 MeshMaterial3d(materials.add(StandardMaterial {
572 base_color: LinearRgba::WHITE.into(),
573 // vary key PBR parameters on a grid of spheres to show the effect
574 metallic: y01,
575 perceptual_roughness: x01,
576 ..default()
577 })),
578 Transform::from_xyz(x as f32, y as f32 + 0.5, 0.0),
579 DespawnOnExit(CURRENT_SCENE),
580 ));
581 }
582 }
583
584 // Create a pure white cubemap
585 let white_cubemap = create_white_cubemap(256);
586 let white_cubemap_handle = images.add(white_cubemap);
587
588 let mut solid_color_light = EnvironmentMapLight::solid_color(&mut images, Color::WHITE);
589 solid_color_light.intensity = 500.0;
590
591 // camera
592 commands.spawn((
593 Camera3d::default(),
594 Hdr,
595 Tonemapping::None,
596 Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::default(), Vec3::Y),
597 Projection::from(OrthographicProjection {
598 scale: 0.01,
599 scaling_mode: ScalingMode::WindowSize,
600 ..OrthographicProjection::default_3d()
601 }),
602 Skybox {
603 image: Some(white_cubemap_handle),
604 // middle gray
605 brightness: 500.0,
606 ..default()
607 },
608 solid_color_light,
609 DespawnOnExit(CURRENT_SCENE),
610 ));
611 }
612}
613
614mod white_furnace_environment_map_light {
615 use bevy::{
616 asset::RenderAssetUsages,
617 camera::{Hdr, ScalingMode},
618 core_pipeline::tonemapping::Tonemapping,
619 light::Skybox,
620 prelude::*,
621 render::render_resource::{
622 Extent3d, TextureDimension, TextureFormat, TextureViewDescriptor, TextureViewDimension,
623 },
624 };
625
626 const CURRENT_SCENE: super::Scene = super::Scene::WhiteFurnaceEnvironmentMapLight;
627
628 /// Creates a pure white cubemap
629 fn create_white_cubemap(size: u32) -> Image {
630 // f16 bytes for 1.0 (white): [0, 60] in little-endian
631 const WHITE_F16: [u8; 2] = [0, 60];
632 const WHITE_PIXEL: [u8; 8] = [
633 WHITE_F16[0],
634 WHITE_F16[1], // R
635 WHITE_F16[0],
636 WHITE_F16[1], // G
637 WHITE_F16[0],
638 WHITE_F16[1], // B
639 WHITE_F16[0],
640 WHITE_F16[1], // A
641 ];
642
643 let pixel_data: Vec<u8> = (0..6 * size * size).flat_map(|_| WHITE_PIXEL).collect();
644
645 Image {
646 texture_view_descriptor: Some(TextureViewDescriptor {
647 dimension: Some(TextureViewDimension::Cube),
648 ..Default::default()
649 }),
650 ..Image::new(
651 Extent3d {
652 width: size,
653 height: size,
654 depth_or_array_layers: 6,
655 },
656 TextureDimension::D2,
657 pixel_data,
658 TextureFormat::Rgba16Float,
659 RenderAssetUsages::RENDER_WORLD,
660 )
661 }
662 }Sourcepub fn new_uninit(
size: Extent3d,
dimension: TextureDimension,
format: TextureFormat,
asset_usage: RenderAssetUsages,
) -> Image
pub fn new_uninit( size: Extent3d, dimension: TextureDimension, format: TextureFormat, asset_usage: RenderAssetUsages, ) -> Image
Exactly the same as Image::new, but doesn’t initialize the image
Examples found in repository?
423fn create_mirror_texture_image(images: &mut Assets<Image>, window_size: UVec2) -> Handle<Image> {
424 let mirror_image_extent = Extent3d {
425 width: window_size.x,
426 height: window_size.y,
427 depth_or_array_layers: 1,
428 };
429
430 let mut image = Image::new_uninit(
431 mirror_image_extent,
432 TextureDimension::D2,
433 TextureFormat::Bgra8UnormSrgb,
434 RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
435 );
436 image.texture_descriptor.usage |=
437 TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
438
439 images.add(image)
440}More examples
75 fn new_render_target(&mut self, width: u32, height: u32) -> RenderTarget {
76 let mut target = Image::new_uninit(
77 Extent3d {
78 width,
79 height,
80 depth_or_array_layers: 1,
81 },
82 TextureDimension::D2,
83 TextureFormat::Rgba8UnormSrgb,
84 RenderAssetUsages::RENDER_WORLD,
85 );
86 // We're going to render to this image, mark it as such
87 target.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
88 self.0
89 .main
90 .world_mut()
91 .resource_mut::<Assets<Image>>()
92 .add(target)
93 .into()
94 }766 fn regenerate_mipmap_source_image(
767 &mut self,
768 commands: &mut Commands,
769 images: &mut Assets<Image>,
770 ) -> Handle<Image> {
771 let image_data = self.generate_image_data();
772
773 let mut image = Image::new_uninit(
774 Extent3d {
775 width: self.image_width as u32,
776 height: self.image_height as u32,
777 depth_or_array_layers: 1,
778 },
779 TextureDimension::D2,
780 TextureFormat::Rgba8Unorm,
781 RenderAssetUsages::all(),
782 );
783 image.texture_descriptor.mip_level_count = self.image_mip_level_count();
784 image.texture_descriptor.usage |= TextureUsages::STORAGE_BINDING;
785 image.data = Some(image_data);
786
787 let image_handle = images.add(image);
788 commands.insert_resource(MipmapSourceImage(image_handle.clone()));
789
790 image_handle
791 }307 fn from_world(world: &mut World) -> Self {
308 let mut images = world.resource_mut::<Assets<Image>>();
309
310 // Create a new 32-bit floating point depth texture.
311 let mut depth_image = Image::new_uninit(
312 Extent3d {
313 width: DEPTH_TEXTURE_SIZE,
314 height: DEPTH_TEXTURE_SIZE,
315 depth_or_array_layers: 1,
316 },
317 TextureDimension::D2,
318 TextureFormat::Depth32Float,
319 RenderAssetUsages::default(),
320 );
321
322 // Create a sampler. Note that this needs to specify a `compare`
323 // function in order to be compatible with depth textures.
324 depth_image.sampler = ImageSampler::Descriptor(ImageSamplerDescriptor {
325 label: Some("custom depth image sampler".to_owned()),
326 compare: Some(ImageCompareFunction::Always),
327 ..ImageSamplerDescriptor::default()
328 });
329
330 let depth_image_handle = images.add(depth_image);
331 DemoDepthTexture(depth_image_handle)
332 }25fn test(
26 mut commands: Commands,
27 mut images: ResMut<Assets<Image>>,
28 mut meshes: ResMut<Assets<Mesh>>,
29 mut materials: ResMut<Assets<StandardMaterial>>,
30) {
31 // Spawn a UI camera
32 commands.spawn(Camera3d::default());
33
34 // Set up an texture for the 3D camera to render to.
35 // The size of the texture will be based on the viewport's ui size.
36 let mut image = Image::new_uninit(
37 default(),
38 TextureDimension::D2,
39 TextureFormat::Bgra8UnormSrgb,
40 RenderAssetUsages::all(),
41 );
42 image.texture_descriptor.usage =
43 TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
44 let image_handle = images.add(image);
45
46 // Spawn the 3D camera
47 let camera = commands
48 .spawn((
49 Camera3d::default(),
50 Camera {
51 // Render this camera before our UI camera
52 order: -1,
53 ..default()
54 },
55 RenderTarget::Image(image_handle.clone().into()),
56 ))
57 .id();
58
59 // Spawn something for the 3D camera to look at
60 commands
61 .spawn((
62 Mesh3d(meshes.add(Cuboid::new(5.0, 5.0, 5.0))),
63 MeshMaterial3d(materials.add(Color::WHITE)),
64 Transform::from_xyz(0.0, 0.0, -10.0),
65 Shape,
66 ))
67 // We can observe pointer events on our objects as normal, the
68 // `bevy::ui::widget::viewport_picking` system will take care of ensuring our viewport
69 // clicks pass through
70 .observe(on_drag_cuboid);
71
72 // Spawn our viewport widget
73 commands
74 .spawn((
75 Node {
76 position_type: PositionType::Absolute,
77 top: px(50),
78 left: px(50),
79 width: px(200),
80 height: px(200),
81 border: UiRect::all(px(5)),
82 ..default()
83 },
84 BorderColor::all(Color::WHITE),
85 ViewportNode::new(camera),
86 ))
87 .observe(on_drag_viewport);
88}67fn setup(
68 mut commands: Commands,
69 mut images: ResMut<Assets<Image>>,
70 mut buffers: ResMut<Assets<ShaderBuffer>>,
71) {
72 // Create a storage buffer with some data
73 let buffer: Vec<u32> = (0..BUFFER_LEN as u32).collect();
74 let mut buffer = ShaderBuffer::from(buffer);
75 // We need to enable the COPY_SRC usage so we can copy the buffer to the cpu
76 buffer.buffer_description.usage |= BufferUsages::COPY_SRC;
77 let buffer = buffers.add(buffer);
78
79 // Create a storage texture with some data
80 let size = Extent3d {
81 width: BUFFER_LEN as u32,
82 height: 1,
83 ..default()
84 };
85 // We create an uninitialized image since this texture will only be used for getting data out
86 // of the compute shader, not getting data in, so there's no reason for it to exist on the CPU
87 let mut image = Image::new_uninit(
88 size,
89 TextureDimension::D2,
90 TextureFormat::R32Uint,
91 RenderAssetUsages::RENDER_WORLD,
92 );
93 // We also need to enable the COPY_SRC, as well as STORAGE_BINDING so we can use it in the
94 // compute shader
95 image.texture_descriptor.usage |= TextureUsages::COPY_SRC | TextureUsages::STORAGE_BINDING;
96 let image = images.add(image);
97
98 // Spawn the readback components. For each frame, the data will be read back from the GPU
99 // asynchronously and trigger the `ReadbackComplete` event on this entity. Despawn the entity
100 // to stop reading back the data.
101 commands
102 .spawn(Readback::buffer(buffer.clone()))
103 .observe(|event: On<ReadbackComplete>| {
104 // This matches the type which was used to create the `ShaderBuffer` above,
105 // and is a convenient way to interpret the data.
106 let data: Vec<u32> = event.to_shader_type();
107 info!("Buffer {:?}", data);
108 });
109
110 // It is also possible to read only a range of the buffer.
111 commands
112 .spawn(Readback::buffer_range(
113 buffer.clone(),
114 4 * u32::SHADER_SIZE.get(), // skip the first four elements
115 8 * u32::SHADER_SIZE.get(), // read eight elements
116 ))
117 .observe(|event: On<ReadbackComplete>| {
118 let data: Vec<u32> = event.to_shader_type();
119 info!("Buffer range {:?}", data);
120 });
121
122 // This is just a simple way to pass the buffer handle to the render app for our compute node
123 commands.insert_resource(ReadbackBuffer(buffer));
124
125 // Textures can also be read back from the GPU. Pay careful attention to the format of the
126 // texture, as it will affect how the data is interpreted.
127 commands
128 .spawn(Readback::texture(image.clone()))
129 .observe(|event: On<ReadbackComplete>| {
130 // You probably want to interpret the data as a color rather than a `ShaderType`,
131 // but in this case we know the data is a single channel storage texture, so we can
132 // interpret it as a `Vec<u32>`
133 let data: Vec<u32> = event.to_shader_type();
134 info!("Image {:?}", data);
135 });
136 commands.insert_resource(ReadbackImage(image));
137}Sourcepub fn transparent() -> Image
pub fn transparent() -> Image
A transparent white 1x1x1 image.
Contrast to Image::default, which is opaque.
Sourcepub fn default_uninit() -> Image
pub fn default_uninit() -> Image
Creates a new uninitialized 1x1x1 image
Sourcepub fn new_fill(
size: Extent3d,
dimension: TextureDimension,
pixel: &[u8],
format: TextureFormat,
asset_usage: RenderAssetUsages,
) -> Image
pub fn new_fill( size: Extent3d, dimension: TextureDimension, pixel: &[u8], format: TextureFormat, asset_usage: RenderAssetUsages, ) -> Image
Creates a new image from raw binary data and the corresponding metadata, by filling
the image data with the pixel data repeated multiple times.
§Panics
Panics if the size of the format is not a multiple of the length of the pixel data.
Examples found in repository?
481fn init_textures(textures: &mut Vec<Handle<Image>>, args: &Args, images: &mut Assets<Image>) {
482 let mut color_rng = ChaCha8Rng::seed_from_u64(42);
483 while textures.len() < args.material_texture_count {
484 let pixel = [
485 color_rng.random(),
486 color_rng.random(),
487 color_rng.random(),
488 255,
489 ];
490 textures.push(images.add(Image::new_fill(
491 Extent3d {
492 width: CUBE_TEXTURE_SIZE as u32,
493 height: CUBE_TEXTURE_SIZE as u32,
494 depth_or_array_layers: 1,
495 },
496 TextureDimension::D2,
497 &pixel,
498 TextureFormat::Rgba8UnormSrgb,
499 RenderAssetUsages::RENDER_WORLD,
500 )));
501 }
502}More examples
232fn uv_debug_texture() -> Image {
233 const TEXTURE_SIZE: usize = 8;
234
235 let mut palette: [u8; 32] = [
236 255, 102, 159, 255, 255, 159, 102, 255, 236, 255, 102, 255, 121, 255, 102, 255, 102, 255,
237 198, 255, 102, 198, 255, 255, 121, 102, 255, 255, 236, 102, 255, 255,
238 ];
239
240 let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4];
241 for y in 0..TEXTURE_SIZE {
242 let offset = TEXTURE_SIZE * y * 4;
243 texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette);
244 palette.rotate_right(4);
245 }
246
247 Image::new_fill(
248 Extent3d {
249 width: TEXTURE_SIZE as u32,
250 height: TEXTURE_SIZE as u32,
251 depth_or_array_layers: 1,
252 },
253 TextureDimension::D2,
254 &texture_data,
255 TextureFormat::Rgba8UnormSrgb,
256 RenderAssetUsages::RENDER_WORLD,
257 )
258}359fn init_textures(args: &Args, images: &mut Assets<Image>) -> Vec<Handle<Image>> {
360 // We're seeding the PRNG here to make this example deterministic for testing purposes.
361 // This isn't strictly required in practical use unless you need your app to be deterministic.
362 let mut color_rng = ChaCha8Rng::seed_from_u64(42);
363 let color_bytes: Vec<u8> = (0..(args.material_texture_count * 4))
364 .map(|i| {
365 if (i % 4) == 3 {
366 255
367 } else {
368 color_rng.random()
369 }
370 })
371 .collect();
372 color_bytes
373 .chunks(4)
374 .map(|pixel| {
375 images.add(Image::new_fill(
376 Extent3d::default(),
377 TextureDimension::D2,
378 pixel,
379 TextureFormat::Rgba8UnormSrgb,
380 RenderAssetUsages::RENDER_WORLD,
381 ))
382 })
383 .collect()
384}621fn init_textures(textures: &mut Vec<Handle<Image>>, args: &Args, images: &mut Assets<Image>) {
622 // We're seeding the PRNG here to make this example deterministic for testing purposes.
623 // This isn't strictly required in practical use unless you need your app to be deterministic.
624 let mut color_rng = ChaCha8Rng::seed_from_u64(42);
625 while textures.len() < args.material_texture_count {
626 let pixel = [
627 color_rng.random(),
628 color_rng.random(),
629 color_rng.random(),
630 255,
631 ];
632 textures.push(images.add(Image::new_fill(
633 Extent3d {
634 width: BIRD_TEXTURE_SIZE as u32,
635 height: BIRD_TEXTURE_SIZE as u32,
636 depth_or_array_layers: 1,
637 },
638 TextureDimension::D2,
639 &pixel,
640 TextureFormat::Rgba8UnormSrgb,
641 RenderAssetUsages::RENDER_WORLD,
642 )));
643 }
644}504fn uv_debug_texture() -> Image {
505 const TEXTURE_SIZE: usize = 8;
506
507 let mut palette: [u8; 32] = [
508 255, 102, 159, 255, 255, 159, 102, 255, 236, 255, 102, 255, 121, 255, 102, 255, 102, 255,
509 198, 255, 102, 198, 255, 255, 121, 102, 255, 255, 236, 102, 255, 255,
510 ];
511
512 let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4];
513 for y in 0..TEXTURE_SIZE {
514 let offset = TEXTURE_SIZE * y * 4;
515 texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette);
516 palette.rotate_right(4);
517 }
518
519 let mut img = Image::new_fill(
520 Extent3d {
521 width: TEXTURE_SIZE as u32,
522 height: TEXTURE_SIZE as u32,
523 depth_or_array_layers: 1,
524 },
525 TextureDimension::D2,
526 &texture_data,
527 TextureFormat::Rgba8UnormSrgb,
528 RenderAssetUsages::RENDER_WORLD,
529 );
530 img.sampler = ImageSampler::Descriptor(ImageSamplerDescriptor::default());
531 img
532}355fn uv_debug_texture() -> Image {
356 use bevy::{asset::RenderAssetUsages, render::render_resource::*};
357 const TEXTURE_SIZE: usize = 7;
358
359 let mut palette = [
360 164, 164, 164, 255, 168, 168, 168, 255, 153, 153, 153, 255, 139, 139, 139, 255, 153, 153,
361 153, 255, 177, 177, 177, 255, 159, 159, 159, 255,
362 ];
363
364 let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4];
365 for y in 0..TEXTURE_SIZE {
366 let offset = TEXTURE_SIZE * y * 4;
367 texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette);
368 palette.rotate_right(12);
369 }
370
371 let mut img = Image::new_fill(
372 Extent3d {
373 width: TEXTURE_SIZE as u32,
374 height: TEXTURE_SIZE as u32,
375 depth_or_array_layers: 1,
376 },
377 TextureDimension::D2,
378 &texture_data,
379 TextureFormat::Rgba8UnormSrgb,
380 RenderAssetUsages::RENDER_WORLD,
381 );
382 img.sampler = ImageSampler::Descriptor(ImageSamplerDescriptor {
383 address_mode_u: ImageAddressMode::Repeat,
384 address_mode_v: ImageAddressMode::MirrorRepeat,
385 mag_filter: ImageFilterMode::Nearest,
386 ..ImageSamplerDescriptor::linear()
387 });
388 img
389}Sourcepub fn new_target_texture(
width: u32,
height: u32,
format: TextureFormat,
view_format: Option<TextureFormat>,
) -> Image
pub fn new_target_texture( width: u32, height: u32, format: TextureFormat, view_format: Option<TextureFormat>, ) -> Image
Create a new zero-filled image with a given size, which can be rendered to.
Useful for mirrors, UI, or exporting images for example.
This is primarily for use as a render target for a Camera.
See RenderTarget::Image.
For Standard Dynamic Range (SDR) images you can use TextureFormat::Rgba8UnormSrgb.
For High Dynamic Range (HDR) images you can use TextureFormat::Rgba16Float.
The default TextureUsages are
TEXTURE_BINDING,
COPY_DST,
RENDER_ATTACHMENT.
The default RenderAssetUsages is MAIN_WORLD | RENDER_WORLD
so that it is accessible from the CPU and GPU.
You can customize this by changing the asset_usage field.
Examples found in repository?
54fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
55 let mut image = Image::new_target_texture(SIZE.x, SIZE.y, TextureFormat::Rgba32Float, None);
56 image.asset_usage = RenderAssetUsages::RENDER_WORLD;
57 image.texture_descriptor.usage =
58 TextureUsages::COPY_DST | TextureUsages::STORAGE_BINDING | TextureUsages::TEXTURE_BINDING;
59 let image0 = images.add(image.clone());
60 let image1 = images.add(image);
61
62 commands.spawn((
63 Sprite {
64 image: image0.clone(),
65 custom_size: Some(SIZE.as_vec2()),
66 ..default()
67 },
68 Transform::from_scale(Vec3::splat(DISPLAY_FACTOR as f32)),
69 ));
70 commands.spawn(Camera2d);
71
72 commands.insert_resource(GameOfLifeImages {
73 texture_a: image0,
74 texture_b: image1,
75 });
76
77 commands.insert_resource(GameOfLifeUniforms {
78 alive_color: LinearRgba::RED,
79 });
80}More examples
227fn setup_render_target(
228 commands: &mut Commands,
229 images: &mut ResMut<Assets<Image>>,
230 render_device: &Res<RenderDevice>,
231 scene_controller: &mut ResMut<SceneController>,
232 pre_roll_frames: u32,
233 scene_name: String,
234) -> RenderTarget {
235 let size = Extent3d {
236 width: scene_controller.width,
237 height: scene_controller.height,
238 ..Default::default()
239 };
240
241 // This is the texture that will be rendered to.
242 let mut render_target_image =
243 Image::new_target_texture(size.width, size.height, TextureFormat::Rgba8UnormSrgb, None);
244 render_target_image.texture_descriptor.usage |= TextureUsages::COPY_SRC;
245 let render_target_image_handle = images.add(render_target_image);
246
247 // This is the texture that will be copied to.
248 let cpu_image =
249 Image::new_target_texture(size.width, size.height, TextureFormat::Rgba8UnormSrgb, None);
250 let cpu_image_handle = images.add(cpu_image);
251
252 commands.spawn(ImageCopier::new(
253 render_target_image_handle.clone(),
254 size,
255 render_device,
256 ));
257
258 commands.spawn(ImageToSave(cpu_image_handle));
259
260 scene_controller.state = SceneState::Render(pre_roll_frames);
261 scene_controller.name = scene_name;
262 RenderTarget::Image(render_target_image_handle.into())
263}24fn setup(
25 mut commands: Commands,
26 mut meshes: ResMut<Assets<Mesh>>,
27 mut materials: ResMut<Assets<StandardMaterial>>,
28 mut images: ResMut<Assets<Image>>,
29) {
30 // This is the texture that will be rendered to.
31 let image = Image::new_target_texture(
32 512,
33 512,
34 TextureFormat::Rgba8Unorm,
35 Some(TextureFormat::Rgba8UnormSrgb),
36 );
37
38 let image_handle = images.add(image);
39
40 let cube_handle = meshes.add(Cuboid::new(4.0, 4.0, 4.0));
41 let cube_material_handle = materials.add(StandardMaterial {
42 base_color: Color::srgb(0.8, 0.7, 0.6),
43 reflectance: 0.02,
44 unlit: false,
45 ..default()
46 });
47
48 // This specifies the layer used for the first pass, which will be attached to the first pass camera and cube.
49 let first_pass_layer = RenderLayers::layer(1);
50
51 // The cube that will be rendered to the texture.
52 commands.spawn((
53 Mesh3d(cube_handle),
54 MeshMaterial3d(cube_material_handle),
55 Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
56 FirstPassCube,
57 first_pass_layer.clone(),
58 ));
59
60 // Light
61 // NOTE: we add the light to both layers so it affects both the rendered-to-texture cube, and the cube on which we display the texture
62 // Setting the layer to RenderLayers::layer(0) would cause the main view to be lit, but the rendered-to-texture cube to be unlit.
63 // Setting the layer to RenderLayers::layer(1) would cause the rendered-to-texture cube to be lit, but the main view to be unlit.
64 commands.spawn((
65 PointLight::default(),
66 Transform::from_translation(Vec3::new(0.0, 0.0, 10.0)),
67 RenderLayers::layer(0).with(1),
68 ));
69
70 commands.spawn((
71 Camera3d::default(),
72 Camera {
73 // render before the "main pass" camera
74 order: -1,
75 clear_color: Color::WHITE.into(),
76 ..default()
77 },
78 RenderTarget::Image(image_handle.clone().into()),
79 Transform::from_translation(Vec3::new(0.0, 0.0, 15.0)).looking_at(Vec3::ZERO, Vec3::Y),
80 first_pass_layer,
81 ));
82
83 let cube_size = 4.0;
84 let cube_handle = meshes.add(Cuboid::new(cube_size, cube_size, cube_size));
85
86 // This material has the texture that has been rendered.
87 let material_handle = materials.add(StandardMaterial {
88 base_color_texture: Some(image_handle),
89 reflectance: 0.02,
90 unlit: false,
91 ..default()
92 });
93
94 // Main pass cube, with material containing the rendered first pass texture.
95 commands.spawn((
96 Mesh3d(cube_handle),
97 MeshMaterial3d(material_handle),
98 Transform::from_xyz(0.0, 0.0, 1.5).with_rotation(Quat::from_rotation_x(-PI / 5.0)),
99 MainPassCube,
100 ));
101
102 // The main pass camera.
103 commands.spawn((
104 Camera3d::default(),
105 Transform::from_xyz(0.0, 0.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y),
106 ));
107}Sourcepub fn width(&self) -> u32
pub fn width(&self) -> u32
Returns the width of a 2D image.
Examples found in repository?
39fn atlas_render_system(
40 mut commands: Commands,
41 mut state: ResMut<State>,
42 font_atlas_set: Res<FontAtlasSet>,
43 images: Res<Assets<Image>>,
44) {
45 if let Some(font_atlases) = font_atlas_set.values().next() {
46 let x_offset = state.atlas_count as f32;
47 if state.atlas_count == font_atlases.len() as u32 {
48 return;
49 }
50 let font_atlas = &font_atlases[state.atlas_count as usize];
51 let image = images.get(&font_atlas.texture).unwrap();
52 state.atlas_count += 1;
53 commands.spawn((
54 ImageNode::new(font_atlas.texture.clone()),
55 Node {
56 position_type: PositionType::Absolute,
57 top: Val::ZERO,
58 left: px(image.width() as f32 * x_offset),
59 ..default()
60 },
61 ));
62 }
63}More examples
148fn asset_loaded(
149 asset_server: Res<AssetServer>,
150 mut images: ResMut<Assets<Image>>,
151 mut cubemap: ResMut<Cubemap>,
152 mut skyboxes: Query<&mut Skybox>,
153) {
154 if !cubemap.is_loaded && asset_server.load_state(&cubemap.image_handle).is_loaded() {
155 info!("Swapping to {}...", CUBEMAPS[cubemap.index].0);
156 let mut image = images.get_mut(&cubemap.image_handle).unwrap();
157 // NOTE: PNGs do not have any metadata that could indicate they contain a cubemap texture,
158 // so they appear as one texture. The following code reconfigures the texture as necessary.
159 if image.texture_descriptor.array_layer_count() == 1 {
160 let layers = image.height() / image.width();
161 image
162 .reinterpret_stacked_2d_as_array(layers)
163 .expect("asset should be 2d texture and height will always be evenly divisible with the given layers");
164 image.texture_view_descriptor = Some(TextureViewDescriptor {
165 dimension: Some(TextureViewDimension::Cube),
166 ..default()
167 });
168 }
169
170 for mut skybox in &mut skyboxes {
171 skybox.image = Some(cubemap.image_handle.clone());
172 }
173
174 cubemap.is_loaded = true;
175 }
176}450fn update(
451 images_to_save: Query<&ImageToSave>,
452 receiver: Res<MainWorldReceiver>,
453 mut images: ResMut<Assets<Image>>,
454 mut scene_controller: ResMut<SceneController>,
455 mut app_exit_writer: MessageWriter<AppExit>,
456 mut file_number: Local<u32>,
457) {
458 if let SceneState::Render(n) = scene_controller.state {
459 if n < 1 {
460 // We don't want to block the main world on this,
461 // so we use try_recv which attempts to receive without blocking
462 let mut image_data = Vec::new();
463 while let Ok(data) = receiver.try_recv() {
464 // image generation could be faster than saving to fs,
465 // that's why use only last of them
466 image_data = data;
467 }
468 if !image_data.is_empty() {
469 for image in images_to_save.iter() {
470 // Fill correct data from channel to image
471 let mut img_bytes = images.get_mut(image.id()).unwrap();
472
473 // We need to ensure that this works regardless of the image dimensions
474 // If the image became wider when copying from the texture to the buffer,
475 // then the data is reduced to its original size when copying from the buffer to the image.
476 let row_bytes = img_bytes.width() as usize
477 * img_bytes.texture_descriptor.format.pixel_size().unwrap();
478 let aligned_row_bytes = RenderDevice::align_copy_bytes_per_row(row_bytes);
479 if row_bytes == aligned_row_bytes {
480 img_bytes.data.as_mut().unwrap().clone_from(&image_data);
481 } else {
482 // shrink data to original image size
483 img_bytes.data = Some(
484 image_data
485 .chunks(aligned_row_bytes)
486 .take(img_bytes.height() as usize)
487 .flat_map(|row| &row[..row_bytes.min(row.len())])
488 .cloned()
489 .collect(),
490 );
491 }
492
493 // Create RGBA Image Buffer
494 let img = match img_bytes.clone().try_into_dynamic() {
495 Ok(img) => img.to_rgba8(),
496 Err(e) => panic!("Failed to create image buffer {e:?}"),
497 };
498
499 // Prepare directory for images, test_images in bevy folder is used here for example
500 // You should choose the path depending on your needs
501 let images_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_images");
502 info!("Saving image to: {images_dir:?}");
503 std::fs::create_dir_all(&images_dir).unwrap();
504
505 // Choose filename starting from 000.png
506 let image_path = images_dir.join(format!("{:03}.png", file_number.deref()));
507 *file_number.deref_mut() += 1;
508
509 // Finally saving image to file, this heavy blocking operation is kept here
510 // for example simplicity, but in real app you should move it to a separate task
511 if let Err(e) = img.save(image_path) {
512 panic!("Failed to save image: {e}");
513 };
514 }
515 if scene_controller.single_image {
516 app_exit_writer.write(AppExit::Success);
517 }
518 }
519 } else {
520 // clears channel for skipped frames
521 while receiver.try_recv().is_ok() {}
522 scene_controller.state = SceneState::Render(n - 1);
523 }
524 }
525}Sourcepub fn height(&self) -> u32
pub fn height(&self) -> u32
Returns the height of a 2D image.
Examples found in repository?
148fn asset_loaded(
149 asset_server: Res<AssetServer>,
150 mut images: ResMut<Assets<Image>>,
151 mut cubemap: ResMut<Cubemap>,
152 mut skyboxes: Query<&mut Skybox>,
153) {
154 if !cubemap.is_loaded && asset_server.load_state(&cubemap.image_handle).is_loaded() {
155 info!("Swapping to {}...", CUBEMAPS[cubemap.index].0);
156 let mut image = images.get_mut(&cubemap.image_handle).unwrap();
157 // NOTE: PNGs do not have any metadata that could indicate they contain a cubemap texture,
158 // so they appear as one texture. The following code reconfigures the texture as necessary.
159 if image.texture_descriptor.array_layer_count() == 1 {
160 let layers = image.height() / image.width();
161 image
162 .reinterpret_stacked_2d_as_array(layers)
163 .expect("asset should be 2d texture and height will always be evenly divisible with the given layers");
164 image.texture_view_descriptor = Some(TextureViewDescriptor {
165 dimension: Some(TextureViewDimension::Cube),
166 ..default()
167 });
168 }
169
170 for mut skybox in &mut skyboxes {
171 skybox.image = Some(cubemap.image_handle.clone());
172 }
173
174 cubemap.is_loaded = true;
175 }
176}More examples
450fn update(
451 images_to_save: Query<&ImageToSave>,
452 receiver: Res<MainWorldReceiver>,
453 mut images: ResMut<Assets<Image>>,
454 mut scene_controller: ResMut<SceneController>,
455 mut app_exit_writer: MessageWriter<AppExit>,
456 mut file_number: Local<u32>,
457) {
458 if let SceneState::Render(n) = scene_controller.state {
459 if n < 1 {
460 // We don't want to block the main world on this,
461 // so we use try_recv which attempts to receive without blocking
462 let mut image_data = Vec::new();
463 while let Ok(data) = receiver.try_recv() {
464 // image generation could be faster than saving to fs,
465 // that's why use only last of them
466 image_data = data;
467 }
468 if !image_data.is_empty() {
469 for image in images_to_save.iter() {
470 // Fill correct data from channel to image
471 let mut img_bytes = images.get_mut(image.id()).unwrap();
472
473 // We need to ensure that this works regardless of the image dimensions
474 // If the image became wider when copying from the texture to the buffer,
475 // then the data is reduced to its original size when copying from the buffer to the image.
476 let row_bytes = img_bytes.width() as usize
477 * img_bytes.texture_descriptor.format.pixel_size().unwrap();
478 let aligned_row_bytes = RenderDevice::align_copy_bytes_per_row(row_bytes);
479 if row_bytes == aligned_row_bytes {
480 img_bytes.data.as_mut().unwrap().clone_from(&image_data);
481 } else {
482 // shrink data to original image size
483 img_bytes.data = Some(
484 image_data
485 .chunks(aligned_row_bytes)
486 .take(img_bytes.height() as usize)
487 .flat_map(|row| &row[..row_bytes.min(row.len())])
488 .cloned()
489 .collect(),
490 );
491 }
492
493 // Create RGBA Image Buffer
494 let img = match img_bytes.clone().try_into_dynamic() {
495 Ok(img) => img.to_rgba8(),
496 Err(e) => panic!("Failed to create image buffer {e:?}"),
497 };
498
499 // Prepare directory for images, test_images in bevy folder is used here for example
500 // You should choose the path depending on your needs
501 let images_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_images");
502 info!("Saving image to: {images_dir:?}");
503 std::fs::create_dir_all(&images_dir).unwrap();
504
505 // Choose filename starting from 000.png
506 let image_path = images_dir.join(format!("{:03}.png", file_number.deref()));
507 *file_number.deref_mut() += 1;
508
509 // Finally saving image to file, this heavy blocking operation is kept here
510 // for example simplicity, but in real app you should move it to a separate task
511 if let Err(e) = img.save(image_path) {
512 panic!("Failed to save image: {e}");
513 };
514 }
515 if scene_controller.single_image {
516 app_exit_writer.write(AppExit::Success);
517 }
518 }
519 } else {
520 // clears channel for skipped frames
521 while receiver.try_recv().is_ok() {}
522 scene_controller.state = SceneState::Render(n - 1);
523 }
524 }
525}Sourcepub fn aspect_ratio(&self) -> AspectRatio
pub fn aspect_ratio(&self) -> AspectRatio
Returns the aspect ratio (width / height) of a 2D image.
Sourcepub fn size_f32(&self) -> Vec2
pub fn size_f32(&self) -> Vec2
Returns the size of a 2D image as f32.
Examples found in repository?
227fn resize_image(
228 image_mesh: Query<(&MeshMaterial3d<StandardMaterial>, &Mesh3d), With<HDRViewer>>,
229 materials: Res<Assets<StandardMaterial>>,
230 mut meshes: ResMut<Assets<Mesh>>,
231 images: Res<Assets<Image>>,
232 mut image_event_reader: MessageReader<AssetEvent<Image>>,
233) {
234 for event in image_event_reader.read() {
235 let (AssetEvent::Added { id } | AssetEvent::Modified { id }) = event else {
236 continue;
237 };
238
239 for (mat_h, mesh_h) in &image_mesh {
240 let Some(mat) = materials.get(mat_h) else {
241 continue;
242 };
243
244 let Some(ref base_color_texture) = mat.base_color_texture else {
245 continue;
246 };
247
248 if *id != base_color_texture.id() {
249 continue;
250 };
251
252 let Some(image_changed) = images.get(*id) else {
253 continue;
254 };
255
256 let size = image_changed.size_f32().normalize_or_zero() * 1.4;
257 // Resize Mesh
258 let quad = Mesh::from(Rectangle::from_size(size));
259 meshes.insert(mesh_h, quad).unwrap();
260 }
261 }
262}Sourcepub fn resize(&mut self, size: Extent3d)
pub fn resize(&mut self, size: Extent3d)
Resizes the image to the new size, by removing information or appending 0 to the data.
Does not properly scale the contents of the image.
If you need to keep pixel data intact, use Image::resize_in_place.
Examples found in repository?
84fn setup_camera(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
85 let canvas_size = Extent3d {
86 width: RES_WIDTH,
87 height: RES_HEIGHT,
88 ..default()
89 };
90
91 // This Image serves as a canvas representing the low-resolution game screen
92 let mut canvas = Image {
93 texture_descriptor: TextureDescriptor {
94 label: None,
95 size: canvas_size,
96 dimension: TextureDimension::D2,
97 format: TextureFormat::Bgra8UnormSrgb,
98 mip_level_count: 1,
99 sample_count: 1,
100 usage: TextureUsages::TEXTURE_BINDING
101 | TextureUsages::COPY_DST
102 | TextureUsages::RENDER_ATTACHMENT,
103 view_formats: &[],
104 },
105 ..default()
106 };
107
108 // Fill image.data with zeroes
109 canvas.resize(canvas_size);
110
111 let image_handle = images.add(canvas);
112
113 // This camera renders whatever is on `PIXEL_PERFECT_LAYERS` to the canvas
114 commands.spawn((
115 Camera2d,
116 Camera {
117 // Render before the "main pass" camera
118 order: -1,
119 clear_color: ClearColorConfig::Custom(GRAY.into()),
120 ..default()
121 },
122 RenderTarget::Image(image_handle.clone().into()),
123 Msaa::Off,
124 InGameCamera,
125 PIXEL_PERFECT_LAYERS,
126 ));
127
128 // Spawn the canvas
129 commands.spawn((Sprite::from_image(image_handle), Canvas, HIGH_RES_LAYERS));
130
131 // The "outer" camera renders whatever is on `HIGH_RES_LAYERS` to the screen.
132 // here, the canvas and one of the sample sprites will be rendered by this camera
133 commands.spawn((Camera2d, Msaa::Off, OuterCamera, HIGH_RES_LAYERS));
134}Sourcepub fn reinterpret_size(
&mut self,
new_size: Extent3d,
) -> Result<(), TextureReinterpretationError>
pub fn reinterpret_size( &mut self, new_size: Extent3d, ) -> Result<(), TextureReinterpretationError>
Changes the size if the total number of data elements (pixels) remains the same.
If not, returns TextureReinterpretationError::IncompatibleSizes.
Sourcepub fn resize_in_place(&mut self, new_size: Extent3d)
pub fn resize_in_place(&mut self, new_size: Extent3d)
Resizes the image to the new size, keeping the pixel data intact, anchored at the top-left. When growing, the new space is filled with 0. When shrinking, the image is clipped.
For faster resizing when keeping pixel data intact is not important, use Image::resize.
Sourcepub fn reinterpret_stacked_2d_as_array(
&mut self,
layers: u32,
) -> Result<(), TextureReinterpretationError>
pub fn reinterpret_stacked_2d_as_array( &mut self, layers: u32, ) -> Result<(), TextureReinterpretationError>
Takes a 2D image containing vertically stacked images of the same size, and reinterprets
it as a 2D array texture, where each of the stacked images becomes one layer of the
array. This is primarily for use with the texture2DArray shader uniform type.
§Errors
Returns TextureReinterpretationError if the texture is not 2D, has more than one layers
or is not evenly dividable into the layers.
Examples found in repository?
148fn asset_loaded(
149 asset_server: Res<AssetServer>,
150 mut images: ResMut<Assets<Image>>,
151 mut cubemap: ResMut<Cubemap>,
152 mut skyboxes: Query<&mut Skybox>,
153) {
154 if !cubemap.is_loaded && asset_server.load_state(&cubemap.image_handle).is_loaded() {
155 info!("Swapping to {}...", CUBEMAPS[cubemap.index].0);
156 let mut image = images.get_mut(&cubemap.image_handle).unwrap();
157 // NOTE: PNGs do not have any metadata that could indicate they contain a cubemap texture,
158 // so they appear as one texture. The following code reconfigures the texture as necessary.
159 if image.texture_descriptor.array_layer_count() == 1 {
160 let layers = image.height() / image.width();
161 image
162 .reinterpret_stacked_2d_as_array(layers)
163 .expect("asset should be 2d texture and height will always be evenly divisible with the given layers");
164 image.texture_view_descriptor = Some(TextureViewDescriptor {
165 dimension: Some(TextureViewDimension::Cube),
166 ..default()
167 });
168 }
169
170 for mut skybox in &mut skyboxes {
171 skybox.image = Some(cubemap.image_handle.clone());
172 }
173
174 cubemap.is_loaded = true;
175 }
176}Sourcepub fn create_stacked_array_from_2d_grid(
&self,
rows: u32,
columns: u32,
) -> Result<Image, TextureReinterpretationError>
pub fn create_stacked_array_from_2d_grid( &self, rows: u32, columns: u32, ) -> Result<Image, TextureReinterpretationError>
Returns a newly constructed 2D image using the same properties as &self from a grid of tiles of the specified size, The new image is constructed in a vertical stack of tiles to be used as a 2D array texture.
This is primarily for preparing grid based tilesets.
§Errors
Returns TextureReinterpretationError if the texture is not 2D, has more than one layers
or is not evenly dividable by size_in_tiles.
Sourcepub fn convert(&self, new_format: TextureFormat) -> Option<Image>
pub fn convert(&self, new_format: TextureFormat) -> Option<Image>
Convert a texture from a format to another. Only a few formats are supported as input and output:
TextureFormat::R8UnormTextureFormat::Rg8UnormTextureFormat::Rgba8UnormSrgb
To get Image as a image::DynamicImage see:
Image::try_into_dynamic.
Sourcepub fn from_buffer(
buffer: &[u8],
image_type: ImageType<'_>,
supported_compressed_formats: CompressedImageFormats,
is_srgb: bool,
image_sampler: ImageSampler,
asset_usage: RenderAssetUsages,
) -> Result<Image, TextureError>
pub fn from_buffer( buffer: &[u8], image_type: ImageType<'_>, supported_compressed_formats: CompressedImageFormats, is_srgb: bool, image_sampler: ImageSampler, asset_usage: RenderAssetUsages, ) -> Result<Image, TextureError>
Load a bytes buffer in a Image, according to type image_type, using the image
crate
Sourcepub fn is_compressed(&self) -> bool
pub fn is_compressed(&self) -> bool
Whether the texture format is compressed or uncompressed
Sourcepub fn pixel_data_offset(
&self,
coords: UVec3,
) -> Result<usize, TextureAccessError>
pub fn pixel_data_offset( &self, coords: UVec3, ) -> Result<usize, TextureAccessError>
Compute the byte offset where the data of a specific pixel is stored
Returns an error if the provided coordinates are out of bounds.
For 2D textures, Z is the layer number. For 1D textures, Y and Z are ignored.
Sourcepub fn pixel_bytes(&self, coords: UVec3) -> Result<&[u8], TextureAccessError>
pub fn pixel_bytes(&self, coords: UVec3) -> Result<&[u8], TextureAccessError>
Get a reference to the data bytes where a specific pixel’s value is stored.
Sourcepub fn pixel_bytes_mut(
&mut self,
coords: UVec3,
) -> Result<&mut [u8], TextureAccessError>
pub fn pixel_bytes_mut( &mut self, coords: UVec3, ) -> Result<&mut [u8], TextureAccessError>
Get a mutable reference to the data bytes where a specific pixel’s value is stored.
Examples found in repository?
38fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
39 commands.spawn(Camera2d);
40
41 // Create an image that we are going to draw into
42 let mut image = Image::new_fill(
43 // 2D image of size 256x256
44 Extent3d {
45 width: IMAGE_WIDTH,
46 height: IMAGE_HEIGHT,
47 depth_or_array_layers: 1,
48 },
49 TextureDimension::D2,
50 // Initialize it with a beige color
51 &(css::BEIGE.to_u8_array()),
52 // Use the same encoding as the color we set
53 TextureFormat::Rgba8UnormSrgb,
54 RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
55 );
56
57 // To make it extra fancy, we can set the Alpha of each pixel,
58 // so that it fades out in a circular fashion.
59 for y in 0..IMAGE_HEIGHT {
60 for x in 0..IMAGE_WIDTH {
61 let center = Vec2::new(IMAGE_WIDTH as f32 / 2.0, IMAGE_HEIGHT as f32 / 2.0);
62 let max_radius = IMAGE_HEIGHT.min(IMAGE_WIDTH) as f32 / 2.0;
63 let r = Vec2::new(x as f32, y as f32).distance(center);
64 let a = 1.0 - (r / max_radius).clamp(0.0, 1.0);
65
66 // Here we will set the A value by accessing the raw data bytes.
67 // (it is the 4th byte of each pixel, as per our `TextureFormat`)
68
69 // Find our pixel by its coordinates
70 let pixel_bytes = image.pixel_bytes_mut(UVec3::new(x, y, 0)).unwrap();
71 // Convert our f32 to u8
72 pixel_bytes[3] = (a * u8::MAX as f32) as u8;
73 }
74 }
75
76 // Add it to Bevy's assets, so it can be used for rendering
77 // this will give us a handle we can use
78 // (to display it in a sprite, or as part of UI, etc.)
79 let handle = images.add(image);
80
81 // Create a sprite entity using our image
82 commands.spawn(Sprite::from_image(handle.clone()));
83 commands.insert_resource(MyProcGenImage(handle));
84
85 // We're seeding the PRNG here to make this example deterministic for testing purposes.
86 // This isn't strictly required in practical use unless you need your app to be deterministic.
87 let seeded_rng = ChaCha8Rng::seed_from_u64(19878367467712);
88 commands.insert_resource(SeededRng(seeded_rng));
89}Sourcepub fn clear(&mut self, pixel: &[u8])
pub fn clear(&mut self, pixel: &[u8])
Clears the content of the image with the given pixel. The image needs to be initialized on the cpu otherwise this is a noop.
This does nothing if the image data is not already initialized
Sourcepub fn get_color_at_1d(&self, x: u32) -> Result<Color, TextureAccessError>
pub fn get_color_at_1d(&self, x: u32) -> Result<Color, TextureAccessError>
Read the color of a specific pixel (1D texture).
See get_color_at for more details.
Sourcepub fn get_color_at(&self, x: u32, y: u32) -> Result<Color, TextureAccessError>
pub fn get_color_at(&self, x: u32, y: u32) -> Result<Color, TextureAccessError>
Read the color of a specific pixel (2D texture).
This function will find the raw byte data of a specific pixel and
decode it into a user-friendly Color struct for you.
Supports many of the common TextureFormats:
- RGBA/BGRA 8-bit unsigned integer, both sRGB and Linear
- 16-bit and 32-bit unsigned integer
- 16-bit and 32-bit float
Be careful: as the data is converted to Color (which uses f32 internally),
there may be issues with precision when using non-f32 TextureFormats.
If you read a value you previously wrote using set_color_at, it will not match.
If you are working with a 32-bit integer TextureFormat, the value will be
inaccurate (as f32 does not have enough bits to represent it exactly).
Single channel (R) formats are assumed to represent grayscale, so the value
will be copied to all three RGB channels in the resulting Color.
Other TextureFormats are unsupported, such as:
- block-compressed formats
- non-byte-aligned formats like 10-bit
- signed integer formats
Examples found in repository?
92fn draw(
93 my_handle: Res<MyProcGenImage>,
94 mut images: ResMut<Assets<Image>>,
95 // Used to keep track of where we are
96 mut i: Local<u32>,
97 mut draw_color: Local<Color>,
98 mut seeded_rng: ResMut<SeededRng>,
99) {
100 if *i == 0 {
101 // Generate a random color on first run.
102 *draw_color = Color::linear_rgb(
103 seeded_rng.0.random(),
104 seeded_rng.0.random(),
105 seeded_rng.0.random(),
106 );
107 }
108
109 // Get the image from Bevy's asset storage.
110 let mut image = images.get_mut(&my_handle.0).expect("Image not found");
111
112 // Compute the position of the pixel to draw.
113
114 let center = Vec2::new(IMAGE_WIDTH as f32 / 2.0, IMAGE_HEIGHT as f32 / 2.0);
115 let max_radius = IMAGE_HEIGHT.min(IMAGE_WIDTH) as f32 / 2.0;
116 let rot_speed = 0.0123;
117 let period = 0.12345;
118
119 let r = ops::sin(*i as f32 * period) * max_radius;
120 let xy = Vec2::from_angle(*i as f32 * rot_speed) * r + center;
121 let (x, y) = (xy.x as u32, xy.y as u32);
122
123 // Get the old color of that pixel.
124 let old_color = image.get_color_at(x, y).unwrap();
125
126 // If the old color is our current color, change our drawing color.
127 let tolerance = 1.0 / 255.0;
128 if old_color.distance(&draw_color) <= tolerance {
129 *draw_color = Color::linear_rgb(
130 seeded_rng.0.random(),
131 seeded_rng.0.random(),
132 seeded_rng.0.random(),
133 );
134 }
135
136 // Set the new color, but keep old alpha value from image.
137 image
138 .set_color_at(x, y, draw_color.with_alpha(old_color.alpha()))
139 .unwrap();
140
141 *i += 1;
142}Sourcepub fn get_color_at_3d(
&self,
x: u32,
y: u32,
z: u32,
) -> Result<Color, TextureAccessError>
pub fn get_color_at_3d( &self, x: u32, y: u32, z: u32, ) -> Result<Color, TextureAccessError>
Read the color of a specific pixel (2D texture with layers or 3D texture).
See get_color_at for more details.
Sourcepub fn set_color_at_1d(
&mut self,
x: u32,
color: Color,
) -> Result<(), TextureAccessError>
pub fn set_color_at_1d( &mut self, x: u32, color: Color, ) -> Result<(), TextureAccessError>
Change the color of a specific pixel (1D texture).
See set_color_at for more details.
Sourcepub fn set_color_at(
&mut self,
x: u32,
y: u32,
color: Color,
) -> Result<(), TextureAccessError>
pub fn set_color_at( &mut self, x: u32, y: u32, color: Color, ) -> Result<(), TextureAccessError>
Change the color of a specific pixel (2D texture).
This function will find the raw byte data of a specific pixel and
change it according to a Color you provide. The Color struct
will be encoded into the Image’s TextureFormat.
Supports many of the common TextureFormats:
- RGBA/BGRA 8-bit unsigned integer, both sRGB and Linear
- 16-bit and 32-bit unsigned integer (with possibly-limited precision, as
Colorusesf32) - 16-bit and 32-bit float
Be careful: writing to non-f32 TextureFormats is lossy! The data has to be converted,
so if you read it back using get_color_at, the Color you get will not equal the value
you used when writing it using this function.
For RG formats, only the respective values from the linear RGB Color will be used.
For R formats the linear RGB Color will be converted to grayscale
and the R channel will be the luminance.
Other TextureFormats are unsupported, such as:
- block-compressed formats
- non-byte-aligned formats like 10-bit
- signed integer formats
Examples found in repository?
208fn try_plot(
209 event: On<TryPlot>,
210 sprite: Query<(&Sprite, &Anchor, &GlobalTransform), With<SpriteToSave>>,
211 camera: Single<(&Camera, &GlobalTransform)>,
212 texture_atlases: Res<Assets<TextureAtlasLayout>>,
213 draw_color: Res<DrawColor>,
214 mut images: ResMut<Assets<Image>>,
215) {
216 let Ok((sprite, anchor, sprite_transform)) = sprite.get(event.entity) else {
217 return;
218 };
219 let (camera, camera_transform) = camera.into_inner();
220 let Ok(world_position) = camera.viewport_to_world_2d(camera_transform, event.location.position)
221 else {
222 return;
223 };
224 let relative_to_sprite = sprite_transform
225 .affine()
226 .inverse()
227 .transform_point3(world_position.extend(0.0));
228 let Ok(pixel_space) = sprite.compute_pixel_space_point(
229 relative_to_sprite.xy(),
230 *anchor,
231 &images,
232 &texture_atlases,
233 ) else {
234 return;
235 };
236 let pixel_coordinates = pixel_space.floor().as_uvec2();
237 let mut image = images.get_mut(&sprite.image).unwrap();
238 // For an actual drawing app, you'd at least draw a line from the last point, but this is
239 // simpler.
240 image
241 .set_color_at(pixel_coordinates.x, pixel_coordinates.y, draw_color.0)
242 .unwrap();
243}More examples
92fn draw(
93 my_handle: Res<MyProcGenImage>,
94 mut images: ResMut<Assets<Image>>,
95 // Used to keep track of where we are
96 mut i: Local<u32>,
97 mut draw_color: Local<Color>,
98 mut seeded_rng: ResMut<SeededRng>,
99) {
100 if *i == 0 {
101 // Generate a random color on first run.
102 *draw_color = Color::linear_rgb(
103 seeded_rng.0.random(),
104 seeded_rng.0.random(),
105 seeded_rng.0.random(),
106 );
107 }
108
109 // Get the image from Bevy's asset storage.
110 let mut image = images.get_mut(&my_handle.0).expect("Image not found");
111
112 // Compute the position of the pixel to draw.
113
114 let center = Vec2::new(IMAGE_WIDTH as f32 / 2.0, IMAGE_HEIGHT as f32 / 2.0);
115 let max_radius = IMAGE_HEIGHT.min(IMAGE_WIDTH) as f32 / 2.0;
116 let rot_speed = 0.0123;
117 let period = 0.12345;
118
119 let r = ops::sin(*i as f32 * period) * max_radius;
120 let xy = Vec2::from_angle(*i as f32 * rot_speed) * r + center;
121 let (x, y) = (xy.x as u32, xy.y as u32);
122
123 // Get the old color of that pixel.
124 let old_color = image.get_color_at(x, y).unwrap();
125
126 // If the old color is our current color, change our drawing color.
127 let tolerance = 1.0 / 255.0;
128 if old_color.distance(&draw_color) <= tolerance {
129 *draw_color = Color::linear_rgb(
130 seeded_rng.0.random(),
131 seeded_rng.0.random(),
132 seeded_rng.0.random(),
133 );
134 }
135
136 // Set the new color, but keep old alpha value from image.
137 image
138 .set_color_at(x, y, draw_color.with_alpha(old_color.alpha()))
139 .unwrap();
140
141 *i += 1;
142}Sourcepub fn set_color_at_3d(
&mut self,
x: u32,
y: u32,
z: u32,
color: Color,
) -> Result<(), TextureAccessError>
pub fn set_color_at_3d( &mut self, x: u32, y: u32, z: u32, color: Color, ) -> Result<(), TextureAccessError>
Change the color of a specific pixel (2D texture with layers or 3D texture).
See set_color_at for more details.
Source§impl Image
impl Image
Sourcepub fn from_dynamic(
dyn_img: DynamicImage,
is_srgb: bool,
asset_usage: RenderAssetUsages,
) -> Image
pub fn from_dynamic( dyn_img: DynamicImage, is_srgb: bool, asset_usage: RenderAssetUsages, ) -> Image
Converts a DynamicImage to an Image.
Sourcepub fn try_into_dynamic(self) -> Result<DynamicImage, IntoDynamicImageError>
pub fn try_into_dynamic(self) -> Result<DynamicImage, IntoDynamicImageError>
Convert a Image to a DynamicImage. Useful for editing image
data. Not all TextureFormat are covered, therefore it will return an
error if the format is unsupported. Supported formats are:
TextureFormat::R8UnormTextureFormat::Rg8UnormTextureFormat::Rgba8UnormSrgbTextureFormat::Bgra8UnormSrgb
To convert Image to a different format see: Image::convert.
Examples found in repository?
450fn update(
451 images_to_save: Query<&ImageToSave>,
452 receiver: Res<MainWorldReceiver>,
453 mut images: ResMut<Assets<Image>>,
454 mut scene_controller: ResMut<SceneController>,
455 mut app_exit_writer: MessageWriter<AppExit>,
456 mut file_number: Local<u32>,
457) {
458 if let SceneState::Render(n) = scene_controller.state {
459 if n < 1 {
460 // We don't want to block the main world on this,
461 // so we use try_recv which attempts to receive without blocking
462 let mut image_data = Vec::new();
463 while let Ok(data) = receiver.try_recv() {
464 // image generation could be faster than saving to fs,
465 // that's why use only last of them
466 image_data = data;
467 }
468 if !image_data.is_empty() {
469 for image in images_to_save.iter() {
470 // Fill correct data from channel to image
471 let mut img_bytes = images.get_mut(image.id()).unwrap();
472
473 // We need to ensure that this works regardless of the image dimensions
474 // If the image became wider when copying from the texture to the buffer,
475 // then the data is reduced to its original size when copying from the buffer to the image.
476 let row_bytes = img_bytes.width() as usize
477 * img_bytes.texture_descriptor.format.pixel_size().unwrap();
478 let aligned_row_bytes = RenderDevice::align_copy_bytes_per_row(row_bytes);
479 if row_bytes == aligned_row_bytes {
480 img_bytes.data.as_mut().unwrap().clone_from(&image_data);
481 } else {
482 // shrink data to original image size
483 img_bytes.data = Some(
484 image_data
485 .chunks(aligned_row_bytes)
486 .take(img_bytes.height() as usize)
487 .flat_map(|row| &row[..row_bytes.min(row.len())])
488 .cloned()
489 .collect(),
490 );
491 }
492
493 // Create RGBA Image Buffer
494 let img = match img_bytes.clone().try_into_dynamic() {
495 Ok(img) => img.to_rgba8(),
496 Err(e) => panic!("Failed to create image buffer {e:?}"),
497 };
498
499 // Prepare directory for images, test_images in bevy folder is used here for example
500 // You should choose the path depending on your needs
501 let images_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test_images");
502 info!("Saving image to: {images_dir:?}");
503 std::fs::create_dir_all(&images_dir).unwrap();
504
505 // Choose filename starting from 000.png
506 let image_path = images_dir.join(format!("{:03}.png", file_number.deref()));
507 *file_number.deref_mut() += 1;
508
509 // Finally saving image to file, this heavy blocking operation is kept here
510 // for example simplicity, but in real app you should move it to a separate task
511 if let Err(e) = img.save(image_path) {
512 panic!("Failed to save image: {e}");
513 };
514 }
515 if scene_controller.single_image {
516 app_exit_writer.write(AppExit::Success);
517 }
518 }
519 } else {
520 // clears channel for skipped frames
521 while receiver.try_recv().is_ok() {}
522 scene_controller.state = SceneState::Render(n - 1);
523 }
524 }
525}More examples
29fn main() -> AnyhowResult<()> {
30 let url = format!("http://{DEFAULT_ADDR}:{DEFAULT_PORT}/");
31
32 // Step 1: Take a screenshot via BRP
33 // The window must be visible (not fully occluded) for the GPU to render content
34 // If the window is hidden, the screenshot will be black
35 println!("Spawning Screenshot entity...");
36 let spawn_response = brp_request(
37 &url,
38 BRP_SPAWN_ENTITY_METHOD,
39 1,
40 &BrpSpawnEntityParams {
41 components: HashMap::from([(
42 type_name::<Screenshot>().to_string(),
43 serde_json::json!({"Window": "Primary"}),
44 )]),
45 },
46 )?;
47 let screenshot_entity = &spawn_response["result"]["entity"];
48
49 println!("Observing ScreenshotCaptured on entity {screenshot_entity}...");
50 let observe_response = ureq::post(&url).send_json(BrpRequest {
51 method: BRP_OBSERVE_METHOD.to_string(),
52 id: Some(serde_json::to_value(2)?),
53 params: Some(serde_json::to_value(BrpObserveParams {
54 event: type_name::<ScreenshotCaptured>().to_string(),
55 entity: Some(serde_json::from_value(screenshot_entity.clone())?),
56 })?),
57 })?;
58
59 println!("Waiting for screenshot capture...");
60 let reader = std::io::BufReader::new(observe_response.into_body().into_reader());
61 for line in reader.lines() {
62 let line = line?;
63 if let Some(json_str) = line.strip_prefix("data: ") {
64 let response: serde_json::Value = serde_json::from_str(json_str)?;
65 if let Some(error) = response.get("error") {
66 anyhow::bail!("Observe error: {error}");
67 }
68 if let Some(result) = response.get("result") {
69 let events = result.as_array().expect("Expected events array");
70 let event = &events[0];
71
72 let image_data = &event["image"];
73 let width = image_data["texture_descriptor"]["size"]["width"]
74 .as_u64()
75 .unwrap();
76 let height = image_data["texture_descriptor"]["size"]["height"]
77 .as_u64()
78 .unwrap();
79 println!("Screenshot captured! Image size: {width}x{height}");
80
81 let image: bevy::image::Image = serde_json::from_value(image_data.clone())?;
82 let dyn_img = image
83 .try_into_dynamic()
84 .expect("Failed to convert screenshot to dynamic image");
85 let path = "screenshot.png";
86 dyn_img.to_rgb8().save(path)?;
87 println!("Screenshot saved to {path}");
88 break;
89 }
90 }
91 }
92
93 // Step 2: Find the button entity, and its global transform
94 println!("Querying for button entity...");
95 let button_query = brp_request(
96 &url,
97 BRP_QUERY_METHOD,
98 3,
99 &BrpQueryParams {
100 data: BrpQuery {
101 components: vec![type_name::<UiGlobalTransform>().to_string()],
102 option: ComponentSelector::default(),
103 has: Vec::default(),
104 },
105 strict: false,
106 filter: BrpQueryFilter {
107 with: vec![type_name::<Button>().to_string()],
108 without: Vec::default(),
109 },
110 },
111 )?;
112
113 let button_result = button_query["result"]
114 .as_array()
115 .expect("Expected result array");
116 let button = &button_result[0];
117
118 // UiGlobalTransform wraps an Affine2, serialized as a flat array:
119 // [_, _, _, _, translation_x, translation_y]
120 // The translation gives the node's center in physical pixels.
121 let transform = &button["components"][type_name::<UiGlobalTransform>()];
122 let transform_arr = transform.as_array().expect("Expected transform array");
123 let phys_x = transform_arr[4].as_f64().unwrap();
124 let phys_y = transform_arr[5].as_f64().unwrap();
125 println!("Found button at physical ({phys_x}, {phys_y})");
126
127 // Step 3: Find the window entity and scale factor
128 println!("Querying for window entity...");
129 let window_query = brp_request(
130 &url,
131 BRP_QUERY_METHOD,
132 4,
133 &BrpQueryParams {
134 data: BrpQuery {
135 components: vec![type_name::<Window>().to_string()],
136 option: ComponentSelector::default(),
137 has: Vec::default(),
138 },
139 strict: false,
140 filter: BrpQueryFilter::default(),
141 },
142 )?;
143
144 let window_result = window_query["result"]
145 .as_array()
146 .expect("Expected result array");
147 let window = &window_result[0];
148 let window_entity = &window["entity"];
149 let window_data = &window["components"][type_name::<Window>()];
150 let scale_factor = window_data["resolution"]["scale_factor"].as_f64().unwrap();
151 println!("Found window entity: {window_entity}, scale_factor: {scale_factor}");
152
153 // Step 4: Convert button center from physical to logical pixels
154 let logical_x = phys_x / scale_factor;
155 let logical_y = phys_y / scale_factor;
156 println!("Clicking at logical position: ({logical_x}, {logical_y})");
157
158 // Step 5: Send CursorMoved via WindowEvent message
159 // This lets the picking system know where the pointer is.
160 println!("Sending CursorMoved message...");
161 brp_request(
162 &url,
163 BRP_WRITE_MESSAGE_METHOD,
164 5,
165 &BrpWriteMessageParams {
166 message: type_name::<WindowEvent>().to_string(),
167 value: Some(serde_json::json!({
168 "CursorMoved": {
169 "window": window_entity,
170 "position": [logical_x, logical_y],
171 "delta": null
172 }
173 })),
174 },
175 )?;
176
177 // Step 6: Send MouseButtonInput Pressed + Released via WindowEvent messages.
178 // The picking system needs both press and release to generate a Pointer<Click>.
179 println!("Sending mouse press...");
180 brp_request(
181 &url,
182 BRP_WRITE_MESSAGE_METHOD,
183 6,
184 &BrpWriteMessageParams {
185 message: type_name::<WindowEvent>().to_string(),
186 value: Some(serde_json::json!({
187 "MouseButtonInput": {
188 "button": "Left",
189 "state": "Pressed",
190 "window": window_entity,
191 }
192 })),
193 },
194 )?;
195
196 println!("Sending mouse release...");
197 brp_request(
198 &url,
199 BRP_WRITE_MESSAGE_METHOD,
200 7,
201 &BrpWriteMessageParams {
202 message: type_name::<WindowEvent>().to_string(),
203 value: Some(serde_json::json!({
204 "MouseButtonInput": {
205 "button": "Left",
206 "state": "Released",
207 "window": window_entity,
208 }
209 })),
210 },
211 )?;
212
213 Ok(())
214}Trait Implementations§
impl Asset for Image
Source§impl<'de> Deserialize<'de> for Image
impl<'de> Deserialize<'de> for Image
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Image, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Image, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Source§impl FromReflect for Image
impl FromReflect for Image
Source§fn from_reflect(reflect: &(dyn PartialReflect + 'static)) -> Option<Image>
fn from_reflect(reflect: &(dyn PartialReflect + 'static)) -> Option<Image>
Self from a reflected value.Source§fn take_from_reflect(
reflect: Box<dyn PartialReflect>,
) -> Result<Self, Box<dyn PartialReflect>>
fn take_from_reflect( reflect: Box<dyn PartialReflect>, ) -> Result<Self, Box<dyn PartialReflect>>
Self using,
constructing the value using from_reflect if that fails. Read moreSource§impl GetTypeRegistration for Image
impl GetTypeRegistration for Image
Source§fn get_type_registration() -> TypeRegistration
fn get_type_registration() -> TypeRegistration
TypeRegistration for this type.Source§fn register_type_dependencies(_registry: &mut TypeRegistry)
fn register_type_dependencies(_registry: &mut TypeRegistry)
Source§impl IntoReturn for Image
impl IntoReturn for Image
Source§impl PartialReflect for Image
impl PartialReflect for Image
Source§fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
fn get_represented_type_info(&self) -> Option<&'static TypeInfo>
Source§fn to_dynamic(&self) -> Box<dyn PartialReflect>
fn to_dynamic(&self) -> Box<dyn PartialReflect>
Source§fn try_apply(
&mut self,
value: &(dyn PartialReflect + 'static),
) -> Result<(), ApplyError>
fn try_apply( &mut self, value: &(dyn PartialReflect + 'static), ) -> Result<(), ApplyError>
Source§fn reflect_kind(&self) -> ReflectKind
fn reflect_kind(&self) -> ReflectKind
Source§fn reflect_ref(&self) -> ReflectRef<'_>
fn reflect_ref(&self) -> ReflectRef<'_>
Source§fn reflect_mut(&mut self) -> ReflectMut<'_>
fn reflect_mut(&mut self) -> ReflectMut<'_>
Source§fn reflect_owned(self: Box<Image>) -> ReflectOwned
fn reflect_owned(self: Box<Image>) -> ReflectOwned
Source§fn try_into_reflect(
self: Box<Image>,
) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
fn try_into_reflect( self: Box<Image>, ) -> Result<Box<dyn Reflect>, Box<dyn PartialReflect>>
Source§fn try_as_reflect(&self) -> Option<&(dyn Reflect + 'static)>
fn try_as_reflect(&self) -> Option<&(dyn Reflect + 'static)>
Source§fn try_as_reflect_mut(&mut self) -> Option<&mut (dyn Reflect + 'static)>
fn try_as_reflect_mut(&mut self) -> Option<&mut (dyn Reflect + 'static)>
Source§fn into_partial_reflect(self: Box<Image>) -> Box<dyn PartialReflect>
fn into_partial_reflect(self: Box<Image>) -> Box<dyn PartialReflect>
Source§fn as_partial_reflect(&self) -> &(dyn PartialReflect + 'static)
fn as_partial_reflect(&self) -> &(dyn PartialReflect + 'static)
Source§fn as_partial_reflect_mut(&mut self) -> &mut (dyn PartialReflect + 'static)
fn as_partial_reflect_mut(&mut self) -> &mut (dyn PartialReflect + 'static)
Source§fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn debug(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Source§fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
fn reflect_clone(&self) -> Result<Box<dyn Reflect>, ReflectCloneError>
Self using reflection. Read moreSource§fn apply(&mut self, value: &(dyn PartialReflect + 'static))
fn apply(&mut self, value: &(dyn PartialReflect + 'static))
Source§fn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
fn reflect_clone_and_take<T>(&self) -> Result<T, ReflectCloneError>
PartialReflect, combines reflect_clone and
take in a useful fashion, automatically constructing an appropriate
ReflectCloneError if the downcast fails.Source§fn reflect_hash(&self) -> Option<u64>
fn reflect_hash(&self) -> Option<u64>
Source§fn reflect_partial_eq(
&self,
_value: &(dyn PartialReflect + 'static),
) -> Option<bool>
fn reflect_partial_eq( &self, _value: &(dyn PartialReflect + 'static), ) -> Option<bool>
Source§fn reflect_partial_cmp(
&self,
_value: &(dyn PartialReflect + 'static),
) -> Option<Ordering>
fn reflect_partial_cmp( &self, _value: &(dyn PartialReflect + 'static), ) -> Option<Ordering>
Source§fn is_dynamic(&self) -> bool
fn is_dynamic(&self) -> bool
Source§impl Reflect for Image
impl Reflect for Image
Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut dyn Any. Read moreSource§fn into_reflect(self: Box<Image>) -> Box<dyn Reflect>
fn into_reflect(self: Box<Image>) -> Box<dyn Reflect>
Source§fn as_reflect(&self) -> &(dyn Reflect + 'static)
fn as_reflect(&self) -> &(dyn Reflect + 'static)
Source§fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
fn as_reflect_mut(&mut self) -> &mut (dyn Reflect + 'static)
Source§impl Serialize for Image
impl Serialize for Image
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
impl StructuralPartialEq for Image
Source§impl TypePath for Image
impl TypePath for Image
Source§fn type_path() -> &'static str
fn type_path() -> &'static str
Source§fn short_type_path() -> &'static str
fn short_type_path() -> &'static str
Source§fn type_ident() -> Option<&'static str>
fn type_ident() -> Option<&'static str>
Source§fn crate_name() -> Option<&'static str>
fn crate_name() -> Option<&'static str>
Source§impl VisitAssetDependencies for Image
impl VisitAssetDependencies for Image
fn visit_dependencies(&self, visit: &mut impl FnMut(UntypedAssetId))
Auto Trait Implementations§
impl Freeze for Image
impl RefUnwindSafe for Image
impl Send for Image
impl Sync for Image
impl Unpin for Image
impl UnsafeUnpin for Image
impl UnwindSafe for Image
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
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
impl<T> Brush for T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConditionalSend for Twhere
T: Send,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn 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>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which 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)
&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)
&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> 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>
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>
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)
&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)
&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> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
impl<S, T> Duplex<S> for Twhere
T: FromSample<S> + ToSample<S>,
Source§impl<T> DynamicTypePath for Twhere
T: TypePath,
impl<T> DynamicTypePath for Twhere
T: TypePath,
Source§fn reflect_type_path(&self) -> &str
fn reflect_type_path(&self) -> &str
TypePath::type_path.Source§fn reflect_short_type_path(&self) -> &str
fn reflect_short_type_path(&self) -> &str
Source§fn reflect_type_ident(&self) -> Option<&str>
fn reflect_type_ident(&self) -> Option<&str>
TypePath::type_ident.Source§fn reflect_crate_name(&self) -> Option<&str>
fn reflect_crate_name(&self) -> Option<&str>
TypePath::crate_name.Source§fn reflect_module_path(&self) -> Option<&str>
fn reflect_module_path(&self) -> Option<&str>
Source§impl<T> DynamicTyped for Twhere
T: Typed,
impl<T> DynamicTyped for Twhere
T: Typed,
Source§fn reflect_type_info(&self) -> &'static TypeInfo
fn reflect_type_info(&self) -> &'static TypeInfo
Typed::type_info.impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> FromTemplate for T
impl<T> FromTemplate for T
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self using default().
Source§impl<T> GetPath for T
impl<T> GetPath for T
Source§fn reflect_path<'p>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path<'p>( &self, path: impl ReflectPath<'p>, ) -> Result<&(dyn PartialReflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn reflect_path_mut<'p>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
fn reflect_path_mut<'p>( &mut self, path: impl ReflectPath<'p>, ) -> Result<&mut (dyn PartialReflect + 'static), ReflectPathError<'p>>
path. Read moreSource§fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
fn path<'p, T>(
&self,
path: impl ReflectPath<'p>,
) -> Result<&T, ReflectPathError<'p>>where
T: Reflect,
path. Read moreSource§fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
fn path_mut<'p, T>(
&mut self,
path: impl ReflectPath<'p>,
) -> Result<&mut T, ReflectPathError<'p>>where
T: Reflect,
path. Read moreSource§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T> HitDataExtra for T
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> InitializeFromFunction<T> for T
impl<T> InitializeFromFunction<T> for T
Source§fn initialize_from_function(f: fn() -> T) -> T
fn initialize_from_function(f: fn() -> T) -> T
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> ⓘ
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> ⓘ
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 moreSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
Source§fn into_result(self) -> Result<T, RunSystemError>
fn into_result(self) -> Result<T, RunSystemError>
Source§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
Source§impl<G> PatchFromTemplate for Gwhere
G: FromTemplate,
impl<G> PatchFromTemplate for Gwhere
G: FromTemplate,
Source§fn patch<F>(func: F) -> TemplatePatch<F, <G as PatchFromTemplate>::Template>
fn patch<F>(func: F) -> TemplatePatch<F, <G as PatchFromTemplate>::Template>
func, and turns it into a TemplatePatch.Source§impl<T> PatchTemplate for Twhere
T: Template,
impl<T> PatchTemplate for Twhere
T: Template,
Source§fn patch_template<F>(func: F) -> TemplatePatch<F, T>
fn patch_template<F>(func: F) -> TemplatePatch<F, T>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().impl<T> Reflectable for T
Source§impl<T> Serialize for T
impl<T> Serialize for T
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>
fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>
impl<T> Settings for T
Source§impl<Ret> SpawnIfAsync<(), Ret> for Ret
impl<Ret> SpawnIfAsync<(), Ret> for Ret
Source§impl<T, O> SuperFrom<T> for Owhere
O: From<T>,
impl<T, O> SuperFrom<T> for Owhere
O: From<T>,
Source§fn super_from(input: T) -> O
fn super_from(input: T) -> O
Source§impl<T, O, M> SuperInto<O, M> for Twhere
O: SuperFrom<T, M>,
impl<T, O, M> SuperInto<O, M> for Twhere
O: SuperFrom<T, M>,
Source§fn super_into(self) -> O
fn super_into(self) -> O
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.Source§impl<T> Template for T
impl<T> Template for T
Source§fn build_template(
&self,
_context: &mut TemplateContext<'_, '_>,
) -> Result<<T as Template>::Output, BevyError>
fn build_template( &self, _context: &mut TemplateContext<'_, '_>, ) -> Result<<T as Template>::Output, BevyError>
entity context to produce a Template::Output.Source§fn clone_template(&self) -> T
fn clone_template(&self) -> T
Clone.