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

Bevy 0.13 #236

Merged
merged 13 commits into from
Feb 19, 2024
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ name = "ui"
required-features = ["render"]

[dependencies]
bevy = { version = "0.12", default-features = false, features = [
bevy = { version = "0.13", default-features = false, features = [
"bevy_asset",
] }
egui = { version = "0.24.0", default-features = false, features = ["bytemuck"] }
Expand All @@ -52,7 +52,7 @@ thread_local = { version = "1.1.0", optional = true }
[dev-dependencies]
once_cell = "1.16.0"
version-sync = "0.9.4"
bevy = { version = "0.12", default-features = false, features = [
bevy = { version = "0.13", default-features = false, features = [
"x11",
"png",
"bevy_pbr",
Expand Down
17 changes: 6 additions & 11 deletions examples/render_to_image_widget.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use bevy::{
core_pipeline::clear_color::ClearColorConfig,
prelude::*,
render::{
camera::RenderTarget,
camera::{ClearColorConfig, RenderTarget},
render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
Expand Down Expand Up @@ -70,7 +69,7 @@ fn setup(
egui_user_textures.add_image(image_handle.clone());
commands.insert_resource(CubePreviewImage(image_handle.clone()));

let cube_handle = meshes.add(Mesh::from(shape::Cube { size: 4.0 }));
let cube_handle = meshes.add(Mesh::from(Cuboid::new(4.0, 4.0, 4.0)));
vladbat00 marked this conversation as resolved.
Show resolved Hide resolved
let default_material = StandardMaterial {
base_color: Color::rgb(0.8, 0.7, 0.6),
reflectance: 0.02,
Expand Down Expand Up @@ -102,14 +101,11 @@ fn setup(

commands
.spawn(Camera3dBundle {
camera_3d: Camera3d {
clear_color: ClearColorConfig::Custom(Color::rgba(1.0, 1.0, 1.0, 0.0)),
..default()
},
camera: Camera {
// render before the "main pass" camera
order: -1,
target: RenderTarget::Image(image_handle),
clear_color: ClearColorConfig::Custom(Color::rgba(1.0, 1.0, 1.0, 0.0)),
..default()
},
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0))
Expand All @@ -119,7 +115,7 @@ fn setup(
.insert(preview_pass_layer);

let cube_size = 4.0;
let cube_handle = meshes.add(Mesh::from(shape::Box::new(cube_size, cube_size, cube_size)));
let cube_handle = meshes.add(Mesh::from(Cuboid::new(cube_size, cube_size, cube_size)));
vladbat00 marked this conversation as resolved.
Show resolved Hide resolved

let main_material_handle = materials.add(default_material);

Expand Down Expand Up @@ -210,13 +206,12 @@ fn color_picker_widget(ui: &mut egui::Ui, color: &mut Color) -> egui::Response {
egui::color_picker::Alpha::Opaque,
);
let [r, g, b, a] = egui_color.to_srgba_unmultiplied();
*color = [
*color = Color::rgba(
r as f32 / 255.0,
g as f32 / 255.0,
b as f32 / 255.0,
a as f32 / 255.0,
]
.into();
);
res
}

Expand Down
11 changes: 4 additions & 7 deletions examples/side_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,13 @@ fn setup_system(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
mesh: meshes.add(Mesh::from(Plane3d::default().mesh().size(5.0, 5.0))),
vladbat00 marked this conversation as resolved.
Show resolved Hide resolved
material: materials.add(Color::rgb(0.3, 0.5, 0.3)),
..Default::default()
});
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
mesh: meshes.add(Mesh::from(Cuboid::new(1.0, 1.0, 1.0))),
vladbat00 marked this conversation as resolved.
Show resolved Hide resolved
material: materials.add(Color::rgb(0.8, 0.7, 0.6)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
Expand Down
2 changes: 1 addition & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn configure_ui_state_system(mut ui_state: ResMut<UiState>) {
}

fn update_ui_scale_factor_system(
keyboard_input: Res<Input<KeyCode>>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut toggle_scale_factor: Local<Option<bool>>,
mut egui_settings: ResMut<EguiSettings>,
windows: Query<&Window, With<PrimaryWindow>>,
Expand Down
90 changes: 46 additions & 44 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ use bevy::{
ecs::world::{FromWorld, World},
prelude::{Entity, Handle, Resource},
render::{
render_asset::RenderAssetUsages,
render_graph::{Node, NodeRunError, RenderGraphContext},
render_resource::{
BindGroupLayout, BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingType,
BlendComponent, BlendFactor, BlendOperation, BlendState, Buffer, BufferAddress,
BufferBindingType, BufferDescriptor, BufferUsages, ColorTargetState, ColorWrites,
Extent3d, FragmentState, FrontFace, IndexFormat, LoadOp, MultisampleState, Operations,
PipelineCache, PrimitiveState, RenderPassColorAttachment, RenderPassDescriptor,
RenderPipelineDescriptor, SamplerBindingType, Shader, ShaderStages, ShaderType,
SpecializedRenderPipeline, TextureDimension, TextureFormat, TextureSampleType,
TextureViewDimension, VertexBufferLayout, VertexFormat, VertexState, VertexStepMode,
BindGroupLayout, BindGroupLayoutEntry, BindingType, BlendComponent, BlendFactor,
BlendOperation, BlendState, Buffer, BufferAddress, BufferBindingType, BufferDescriptor,
BufferUsages, ColorTargetState, ColorWrites, Extent3d, FragmentState, FrontFace,
IndexFormat, LoadOp, MultisampleState, Operations, PipelineCache, PrimitiveState,
RenderPassColorAttachment, RenderPassDescriptor, RenderPipelineDescriptor,
SamplerBindingType, Shader, ShaderStages, ShaderType, SpecializedRenderPipeline,
StoreOp, TextureDimension, TextureFormat, TextureSampleType, TextureViewDimension,
VertexBufferLayout, VertexFormat, VertexState, VertexStepMode,
},
renderer::{RenderContext, RenderDevice, RenderQueue},
texture::{Image, ImageSampler},
Expand All @@ -42,43 +43,41 @@ impl FromWorld for EguiPipeline {
fn from_world(render_world: &mut World) -> Self {
let render_device = render_world.get_resource::<RenderDevice>().unwrap();

let transform_bind_group_layout =
render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("egui transform bind group layout"),
entries: &[BindGroupLayoutEntry {
let transform_bind_group_layout = render_device.create_bind_group_layout(
"egui transform bind group layout",
&[BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: Some(EguiTransform::min_size()),
},
count: None,
}],
);

let texture_bind_group_layout = render_device.create_bind_group_layout(
"egui texture bind group layout",
&[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: true,
min_binding_size: Some(EguiTransform::min_size()),
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Texture {
sample_type: TextureSampleType::Float { filterable: true },
view_dimension: TextureViewDimension::D2,
multisampled: false,
},
count: None,
}],
});

let texture_bind_group_layout =
render_device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("egui texture bind group layout"),
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Texture {
sample_type: TextureSampleType::Float { filterable: true },
view_dimension: TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Sampler(SamplerBindingType::Filtering),
count: None,
},
],
});
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Sampler(SamplerBindingType::Filtering),
count: None,
},
],
);

EguiPipeline {
transform_bind_group_layout,
Expand Down Expand Up @@ -200,7 +199,7 @@ impl Node for EguiNode {

let render_device = world.get_resource::<RenderDevice>().unwrap();

let scale_factor = window_size.scale_factor * egui_settings.scale_factor as f32;
let scale_factor = window_size.scale_factor * egui_settings.scale_factor;
if window_size.physical_width == 0.0 || window_size.physical_height == 0.0 {
return;
}
Expand Down Expand Up @@ -345,10 +344,12 @@ impl Node for EguiNode {
resolve_target: None,
ops: Operations {
load: LoadOp::Load,
store: true,
store: StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});

let Some(pipeline_id) = egui_pipelines.get(&extracted_window.entity) else {
Expand Down Expand Up @@ -450,6 +451,7 @@ pub(crate) fn color_image_as_bevy_image(
TextureDimension::D2,
pixels,
TextureFormat::Rgba8UnormSrgb,
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
)
}
}
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ use bevy::{
use bevy::{
app::{App, Plugin, PostUpdate, PreStartup, PreUpdate},
ecs::{
query::{QueryEntityError, WorldQuery},
query::{QueryData, QueryEntityError},
schedule::apply_deferred,
system::SystemParam,
},
Expand Down Expand Up @@ -140,7 +140,7 @@ pub struct EguiSettings {
/// }
/// }
/// ```
pub scale_factor: f64,
pub scale_factor: f32,
/// Will be used as a default value for hyperlink [target](https://www.w3schools.com/tags/att_a_target.asp) hints.
/// If not specified, `_self` will be used. Only matters in a web browser.
#[cfg(feature = "open_url")]
Expand Down Expand Up @@ -731,8 +731,8 @@ impl Plugin for EguiPlugin {
}

/// Queries all the Egui related components.
#[derive(WorldQuery)]
#[world_query(mutable)]
#[derive(QueryData)]
#[query_data(mutable)]
pub struct EguiContextQuery {
/// Window entity.
pub window_entity: Entity,
Expand Down Expand Up @@ -904,6 +904,7 @@ mod tests {
..Default::default()
},
),
..Default::default()
})
.build()
.disable::<WinitPlugin>(),
Expand Down
29 changes: 19 additions & 10 deletions src/render_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
render::{
extract_resource::ExtractResource,
render_asset::RenderAssets,
render_graph::RenderGraph,
render_graph::{RenderGraph, RenderLabel},
render_resource::{
BindGroup, BindGroupEntry, BindingResource, BufferId, CachedRenderPipelineId,
DynamicUniformBuffer, PipelineCache, ShaderType, SpecializedRenderPipelines,
Expand Down Expand Up @@ -54,6 +54,13 @@ pub struct ExtractedEguiTextures<'w> {
pub user_textures: Res<'w, EguiUserTextures>,
}

/// RenderLabel type for the egui pass
#[derive(Debug, Hash, PartialEq, Eq, Clone, RenderLabel)]
pub struct EguiPass {
window_index: u32,
window_generation: u32,
}

impl ExtractedEguiTextures<'_> {
/// Returns an iterator over all textures (both Egui and Bevy managed).
pub fn handles(&self) -> impl Iterator<Item = (EguiTextureId, AssetId<Image>)> + '_ {
Expand All @@ -78,16 +85,16 @@ pub fn setup_new_windows_render_system(
mut render_graph: ResMut<RenderGraph>,
) {
for window in windows.iter() {
let egui_pass = format!("egui-{}-{}", window.index(), window.generation());
let egui_pass = EguiPass {
window_index: window.index(),
window_generation: window.generation(),
};

let new_node = EguiNode::new(window);

render_graph.add_node(egui_pass.clone(), new_node);

render_graph.add_node_edge(
bevy::render::main_graph::node::CAMERA_DRIVER,
egui_pass.to_string(),
);
render_graph.add_node_edge(bevy::render::graph::CameraDriverLabel, egui_pass);
}
}

Expand Down Expand Up @@ -140,10 +147,12 @@ pub fn prepare_egui_transforms_system(
egui_transforms.offsets.clear();

for (window, size) in window_sizes.iter() {
let offset = egui_transforms.buffer.push(EguiTransform::from_window_size(
*size,
egui_settings.scale_factor as f32,
));
let offset = egui_transforms
.buffer
.push(&EguiTransform::from_window_size(
*size,
egui_settings.scale_factor,
));
egui_transforms.offsets.insert(window, offset);
}

Expand Down
Loading
Loading