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