Simplify render_to_texture examples (#14855)

# Objective

- The examples use a more verbose than necessary way to initialize the
image
- The order of the camera doesn't need to be specified. At least I
didn't see a difference in my testing

## Solution

- Use `Image::new_fill()` to fill the image instead of abusing
`resize()`
- Remove the camera ordering
This commit is contained in:
IceSentry 2024-08-25 10:15:11 -04:00 committed by GitHub
parent d9527c101c
commit d46a05e387
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 46 deletions

View File

@ -5,12 +5,11 @@ use std::f32::consts::PI;
use bevy::{ use bevy::{
prelude::*, prelude::*,
render::{ render::{
render_resource::{ render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
view::RenderLayers, view::RenderLayers,
}, },
}; };
use bevy_render::render_asset::RenderAssetUsages;
fn main() { fn main() {
App::new() App::new()
@ -41,24 +40,16 @@ fn setup(
}; };
// This is the texture that will be rendered to. // This is the texture that will be rendered to.
let mut image = Image { let mut image = Image::new_fill(
texture_descriptor: TextureDescriptor { size,
label: None, TextureDimension::D2,
size, &[0, 0, 0, 0],
dimension: TextureDimension::D2, TextureFormat::Bgra8UnormSrgb,
format: TextureFormat::Bgra8UnormSrgb, RenderAssetUsages::default(),
mip_level_count: 1, );
sample_count: 1, // You need to set these texture usage flags in order to use the image as a render target
usage: TextureUsages::TEXTURE_BINDING image.texture_descriptor.usage =
| TextureUsages::COPY_DST TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
| TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
},
..default()
};
// fill image.data with zeroes
image.resize(size);
let image_handle = images.add(image); let image_handle = images.add(image);
@ -100,8 +91,6 @@ fn setup(
commands.spawn(( commands.spawn((
Camera3dBundle { Camera3dBundle {
camera: Camera { camera: Camera {
// render before the "main pass" camera
order: -1,
target: image_handle.clone().into(), target: image_handle.clone().into(),
clear_color: Color::WHITE.into(), clear_color: Color::WHITE.into(),
..default() ..default()

View File

@ -7,11 +7,10 @@ use bevy::{
prelude::*, prelude::*,
render::{ render::{
camera::RenderTarget, camera::RenderTarget,
render_resource::{ render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
}, },
}; };
use bevy_render::render_asset::RenderAssetUsages;
fn main() { fn main() {
App::new() App::new()
@ -38,24 +37,16 @@ fn setup(
}; };
// This is the texture that will be rendered to. // This is the texture that will be rendered to.
let mut image = Image { let mut image = Image::new_fill(
texture_descriptor: TextureDescriptor { size,
label: None, TextureDimension::D2,
size, &[0, 0, 0, 0],
dimension: TextureDimension::D2, TextureFormat::Bgra8UnormSrgb,
format: TextureFormat::Bgra8UnormSrgb, RenderAssetUsages::default(),
mip_level_count: 1, );
sample_count: 1, // You need to set these texture usage flags in order to use the image as a render target
usage: TextureUsages::TEXTURE_BINDING image.texture_descriptor.usage =
| TextureUsages::COPY_DST TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
| TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
},
..default()
};
// fill image.data with zeroes
image.resize(size);
let image_handle = images.add(image); let image_handle = images.add(image);
@ -65,8 +56,6 @@ fn setup(
let texture_camera = commands let texture_camera = commands
.spawn(Camera2dBundle { .spawn(Camera2dBundle {
camera: Camera { camera: Camera {
// render before the "main pass" camera
order: -1,
target: RenderTarget::Image(image_handle.clone()), target: RenderTarget::Image(image_handle.clone()),
..default() ..default()
}, },