Skip to content
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

implement worldspace ui #299

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ required-features = ["render"]
[[example]]
name = "ui"
required-features = ["render"]
[[example]]
name = "render_egui_to_texture"
required-features = ["render"]

[dependencies]
bevy = { version = "0.14.0", default-features = false, features = [
Expand All @@ -45,6 +48,7 @@ bevy = { version = "0.14.0", default-features = false, features = [
egui = { version = "0.28", default-features = false, features = ["bytemuck"] }
bytemuck = "1"
webbrowser = { version = "1.0.1", optional = true }
wgpu-types = "0.20"

[target.'cfg(not(any(target_arch = "wasm32", target_os = "android")))'.dependencies]
arboard = { version = "3.2.0", optional = true }
Expand Down
68 changes: 68 additions & 0 deletions examples/render_egui_to_texture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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()
});
}
57 changes: 25 additions & 32 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ impl SpecializedRenderPipeline for EguiPipeline {
}

#[derive(Debug)]
struct DrawCommand {
vertices_count: usize,
egui_texture: EguiTextureId,
clipping_zone: (u32, u32, u32, u32), // x, y, w, h
pub(crate) struct DrawCommand {
pub(crate) vertices_count: usize,
pub(crate) egui_texture: EguiTextureId,
pub(crate) clipping_zone: (u32, u32, u32, u32), // x, y, w, h
}

/// Egui render node.
Expand Down Expand Up @@ -306,21 +306,13 @@ impl Node for EguiNode {
let pipeline_cache = world.get_resource::<PipelineCache>().unwrap();

let extracted_windows = &world.get_resource::<ExtractedWindows>().unwrap().windows;
let extracted_window =
if let Some(extracted_window) = extracted_windows.get(&self.window_entity) {
extracted_window
} else {
return Ok(()); // No window
let extracted_window = extracted_windows.get(&self.window_entity);
let swap_chain_texture_view =
match extracted_window.and_then(|v| v.swap_chain_texture_view.as_ref()) {
None => return Ok(()),
Some(window) => window,
};

let swap_chain_texture_view = if let Some(swap_chain_texture_view) =
extracted_window.swap_chain_texture_view.as_ref()
{
swap_chain_texture_view
} else {
return Ok(()); // No swapchain texture
};

let render_queue = world.get_resource::<RenderQueue>().unwrap();

let (vertex_buffer, index_buffer) = match (&self.vertex_buffer, &self.index_buffer) {
Expand Down Expand Up @@ -353,9 +345,7 @@ impl Node for EguiNode {
occlusion_query_set: None,
});

let Some(pipeline_id) = egui_pipelines.get(&extracted_window.entity) else {
return Ok(());
};
let pipeline_id = egui_pipelines.get(&self.window_entity).unwrap();
let Some(pipeline) = pipeline_cache.get_render_pipeline(*pipeline_id) else {
return Ok(());
};
Expand All @@ -371,10 +361,15 @@ impl Node for EguiNode {
let transform_buffer_bind_group = &egui_transforms.bind_group.as_ref().unwrap().1;
render_pass.set_bind_group(0, transform_buffer_bind_group, &[transform_buffer_offset]);

let (physical_width, physical_height) = match extracted_window {
Some(window) => (window.physical_width, window.physical_height),
None => unreachable!(),
};

let mut vertex_offset: u32 = 0;
for draw_command in &self.draw_commands {
if draw_command.clipping_zone.0 < extracted_window.physical_width
&& draw_command.clipping_zone.1 < extracted_window.physical_height
if draw_command.clipping_zone.0 < physical_width
&& draw_command.clipping_zone.1 < physical_height
{
let texture_bind_group = match bind_groups.get(&draw_command.egui_texture) {
Some(texture_resource) => texture_resource,
Expand All @@ -389,16 +384,14 @@ impl Node for EguiNode {
render_pass.set_scissor_rect(
draw_command.clipping_zone.0,
draw_command.clipping_zone.1,
draw_command.clipping_zone.2.min(
extracted_window
.physical_width
.saturating_sub(draw_command.clipping_zone.0),
),
draw_command.clipping_zone.3.min(
extracted_window
.physical_height
.saturating_sub(draw_command.clipping_zone.1),
),
draw_command
.clipping_zone
.2
.min(physical_width.saturating_sub(draw_command.clipping_zone.0)),
draw_command
.clipping_zone
.3
.min(physical_height.saturating_sub(draw_command.clipping_zone.1)),
);

render_pass.draw_indexed(
Expand Down
Loading