Skip to content

viewport_node example: Remove main world image initialization #19098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/bevy_ui/src/widget/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
43 changes: 12 additions & 31 deletions examples/ui/viewport_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -29,39 +26,23 @@ 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>>,
) {
// 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
Expand Down