-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial world space UI support (#304)
* implement worldspace ui Signed-off-by: Schmarni <marnistromer@gmail.com> * clean render to texture logic from EguiNode Signed-off-by: Schmarni <marnistromer@gmail.com> * fix warnings Signed-off-by: Schmarni <marnistromer@gmail.com> * require render feature for rtt example Signed-off-by: Schmarni <marnistromer@gmail.com> * Implement paint callbacks for rendering to a texture * Fix compilation of not(feature = render) flag --------- Signed-off-by: Schmarni <marnistromer@gmail.com> Co-authored-by: Schmarni <marnistromer@gmail.com> Co-authored-by: Schmarni <51007916+Schmarni-Dev@users.noreply.github.com>
- Loading branch information
1 parent
0815a1d
commit c6895db
Showing
8 changed files
with
831 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use bevy::prelude::*; | ||
use bevy_egui::{EguiContexts, EguiPlugin, EguiRenderToTextureHandle}; | ||
use wgpu_types::{Extent3d, TextureUsages}; | ||
|
||
fn main() { | ||
let mut app = App::new(); | ||
app.add_plugins(DefaultPlugins); | ||
app.add_plugins(EguiPlugin); | ||
app.add_systems(Startup, setup_worldspace); | ||
app.add_systems(Update, (update_screenspace, update_worldspace)); | ||
app.run(); | ||
} | ||
|
||
fn update_screenspace(mut contexts: EguiContexts) { | ||
egui::Window::new("Screenspace UI").show(contexts.ctx_mut(), |ui| { | ||
ui.label("I'm rendering to screenspace!"); | ||
}); | ||
} | ||
|
||
fn update_worldspace( | ||
mut contexts: Query<&mut bevy_egui::EguiContext, With<EguiRenderToTextureHandle>>, | ||
) { | ||
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!"); | ||
}); | ||
} | ||
} | ||
|
||
fn setup_worldspace( | ||
mut images: ResMut<Assets<Image>>, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<StandardMaterial>>, | ||
mut commands: Commands, | ||
) { | ||
let output_texture = images.add({ | ||
let size = Extent3d { | ||
width: 256, | ||
height: 256, | ||
depth_or_array_layers: 1, | ||
}; | ||
let mut output_texture = 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 | ||
}); | ||
|
||
commands.spawn(PbrBundle { | ||
mesh: meshes.add(Cuboid::new(1.0, 1.0, 1.0).mesh()), | ||
material: materials.add(StandardMaterial { | ||
base_color: Color::WHITE, | ||
base_color_texture: Some(Handle::clone(&output_texture)), | ||
alpha_mode: AlphaMode::Blend, | ||
// Remove this if you want it to use the world's lighting. | ||
unlit: true, | ||
..default() | ||
}), | ||
..default() | ||
}); | ||
commands.spawn(EguiRenderToTextureHandle(output_texture)); | ||
commands.spawn(Camera3dBundle { | ||
transform: Transform::from_xyz(1.5, 1.5, 1.5).looking_at(Vec3::new(0., 0., 0.), Vec3::Y), | ||
..default() | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.