Skip to content

Commit

Permalink
Refactor EguiNode to support rendering to images (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbat00 authored Dec 3, 2024
1 parent 96c8663 commit 581a0e1
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 610 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ required-features = ["render"]
name = "ui"
required-features = ["render"]
[[example]]
name = "render_egui_to_texture"
name = "render_egui_to_image"
required-features = ["render"]

[dependencies]
Expand Down
12 changes: 6 additions & 6 deletions examples/paint_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bevy::{
};
use bevy_egui::{
egui_node::{EguiBevyPaintCallback, EguiBevyPaintCallbackImpl, EguiPipelineKey},
EguiContexts, EguiPlugin, EguiRenderToTextureHandle,
EguiContexts, EguiPlugin, EguiRenderToImage,
};
use std::path::Path;
use wgpu_types::{Extent3d, TextureUsages};
Expand All @@ -25,7 +25,7 @@ fn main() {
.add_systems(Startup, setup_worldspace)
.add_systems(
Update,
(ui_example_system, ui_render_to_texture_example_system),
(ui_example_system, ui_render_to_image_example_system),
)
.run();
}
Expand Down Expand Up @@ -209,22 +209,22 @@ fn setup_worldspace(
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0).mesh())),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::WHITE,
base_color_texture: Some(Handle::clone(&output_texture)),
base_color_texture: Some(output_texture.clone()),
alpha_mode: AlphaMode::Blend,
// Remove this if you want it to use the world's lighting.
unlit: true,
..default()
})),
));
commands.spawn(EguiRenderToTextureHandle(output_texture));
commands.spawn(EguiRenderToImage::new(output_texture.clone_weak()));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::new(0., 0., 0.), Vec3::Y),
));
}

fn ui_render_to_texture_example_system(
mut contexts: Query<&mut bevy_egui::EguiContext, With<EguiRenderToTextureHandle>>,
fn ui_render_to_image_example_system(
mut contexts: Query<&mut bevy_egui::EguiContext, With<EguiRenderToImage>>,
) {
for mut ctx in contexts.iter_mut() {
egui::Window::new("Worldspace UI").show(ctx.get_mut(), |ui| {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_egui::{EguiContexts, EguiPlugin, EguiRenderToTextureHandle};
use bevy_egui::{EguiContexts, EguiPlugin, EguiRenderToImage};
use wgpu_types::{Extent3d, TextureUsages};

fn main() {
Expand All @@ -17,12 +17,10 @@ fn update_screenspace(mut contexts: EguiContexts) {
});
}

fn update_worldspace(
mut contexts: Query<&mut bevy_egui::EguiContext, With<EguiRenderToTextureHandle>>,
) {
fn update_worldspace(mut contexts: Query<&mut bevy_egui::EguiContext, With<EguiRenderToImage>>) {
for mut ctx in contexts.iter_mut() {
egui::Window::new("Worldspace UI").show(ctx.get_mut(), |ui| {
ui.label("I'm rendering to a texture in worldspace!");
ui.label("I'm rendering to an image in worldspace!");
});
}
}
Expand All @@ -33,34 +31,34 @@ fn setup_worldspace(
mut materials: ResMut<Assets<StandardMaterial>>,
mut commands: Commands,
) {
let output_texture = images.add({
let image = images.add({
let size = Extent3d {
width: 256,
height: 256,
depth_or_array_layers: 1,
};
let mut output_texture = Image {
let mut image = Image {
// You should use `0` so that the pixels are transparent.
data: vec![0; (size.width * size.height * 4) as usize],
..default()
};
output_texture.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
output_texture.texture_descriptor.size = size;
output_texture
image.texture_descriptor.usage |= TextureUsages::RENDER_ATTACHMENT;
image.texture_descriptor.size = size;
image
});

commands.spawn((
Mesh3d(meshes.add(Cuboid::new(1.0, 1.0, 1.0).mesh())),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::WHITE,
base_color_texture: Some(Handle::clone(&output_texture)),
base_color_texture: Some(Handle::clone(&image)),
alpha_mode: AlphaMode::Blend,
// Remove this if you want it to use the world's lighting.
unlit: true,
..default()
})),
));
commands.spawn(EguiRenderToTextureHandle(output_texture));
commands.spawn(EguiRenderToImage::new(image));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::new(0., 0., 0.), Vec3::Y),
Expand Down
Loading

0 comments on commit 581a0e1

Please sign in to comment.