diff --git a/crates/bevy_ui/src/widget/viewport.rs b/crates/bevy_ui/src/widget/viewport.rs index 9cdc348da5..f68033ea7f 100644 --- a/crates/bevy_ui/src/widget/viewport.rs +++ b/crates/bevy_ui/src/widget/viewport.rs @@ -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; + } } } diff --git a/examples/ui/viewport_node.rs b/examples/ui/viewport_node.rs index 5a94813d39..60caf7ec21 100644 --- a/examples/ui/viewport_node.rs +++ b/examples/ui/viewport_node.rs @@ -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>, mut images: ResMut>, mut meshes: ResMut>, mut materials: ResMut>, @@ -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