viewport_node example: Remove main world image initialization (#19098)

# Objective

The new viewport example allocates a texture in main memory, even though
it's only needed on the GPU. Also fix an unnecessary warning when a
viewport's texture doesn't exist CPU-side.

## Testing

Run the `viewport_node` example.
This commit is contained in:
SpecificProtagonist 2025-05-26 19:20:29 +02:00 committed by GitHub
parent b641aa0ecf
commit 1c8d2ee3e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 32 deletions

View File

@ -171,6 +171,11 @@ pub fn update_viewport_render_target_size(
height: u32::max(1, size.y as u32), height: u32::max(1, size.y as u32),
..default() ..default()
}; };
images.get_mut(image_handle).unwrap().resize(size); let image = images.get_mut(image_handle).unwrap();
if image.data.is_some() {
image.resize(size);
} else {
image.texture_descriptor.size = size;
}
} }
} }

View File

@ -2,17 +2,14 @@
//! pick entities visible in the widget's view. //! pick entities visible in the widget's view.
use bevy::{ use bevy::{
image::{TextureFormatPixelInfo, Volume}, asset::RenderAssetUsages,
picking::pointer::PointerInteraction, picking::pointer::PointerInteraction,
prelude::*, prelude::*,
render::{ render::{
camera::RenderTarget, camera::RenderTarget,
render_resource::{ render_resource::{TextureDimension, TextureFormat, TextureUsages},
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
}, },
ui::widget::ViewportNode, ui::widget::ViewportNode,
window::PrimaryWindow,
}; };
fn main() { fn main() {
@ -29,7 +26,6 @@ struct Shape;
fn test( fn test(
mut commands: Commands, mut commands: Commands,
window: Query<&Window, With<PrimaryWindow>>,
mut images: ResMut<Assets<Image>>, mut images: ResMut<Assets<Image>>,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
@ -37,31 +33,16 @@ fn test(
// Spawn a UI camera // Spawn a UI camera
commands.spawn(Camera3d::default()); commands.spawn(Camera3d::default());
// Set up an texture for the 3D camera to render to // Set up an texture for the 3D camera to render to.
let window = window.single().unwrap(); // The size of the texture will be based on the viewport's ui size.
let window_size = window.physical_size(); let mut image = Image::new_uninit(
let size = Extent3d { default(),
width: window_size.x, TextureDimension::D2,
height: window_size.y, TextureFormat::Bgra8UnormSrgb,
..default() RenderAssetUsages::all(),
}; );
let format = TextureFormat::Bgra8UnormSrgb; image.texture_descriptor.usage =
let image = Image { TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
data: Some(vec![0; size.volume() * format.pixel_size()]),
texture_descriptor: TextureDescriptor {
label: None,
size,
dimension: TextureDimension::D2,
format,
mip_level_count: 1,
sample_count: 1,
usage: TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_DST
| TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
},
..default()
};
let image_handle = images.add(image); let image_handle = images.add(image);
// Spawn the 3D camera // Spawn the 3D camera