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

Update to egui 0.17 #78

Merged
merged 7 commits into from
Mar 12, 2022
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bevy = { version = "0.6", default-features = false, features = [
"bevy_winit",
"bevy_core_pipeline"
] }
egui = "0.16"
egui = { version = "0.17", features = ["convert_bytemuck"] }
webbrowser = { version = "0.5.5", optional = true }
winit = { version = "0.26.0", features = ["x11"], default-features = false }
bytemuck = { version = "1.7.0", features = ["derive"] }
Expand Down
17 changes: 16 additions & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct UiState {
value: f32,
painting: Painting,
inverted: bool,
egui_texture_handle: Option<egui::TextureHandle>,
}

fn load_assets(mut egui_context: ResMut<EguiContext>, assets: Res<AssetServer>) {
Expand All @@ -36,7 +37,7 @@ fn load_assets(mut egui_context: ResMut<EguiContext>, assets: Res<AssetServer>)

fn configure_visuals(mut egui_ctx: ResMut<EguiContext>) {
egui_ctx.ctx_mut().set_visuals(egui::Visuals {
window_corner_radius: 0.0,
window_rounding: 0.0.into(),
..Default::default()
});
}
Expand Down Expand Up @@ -66,6 +67,15 @@ fn ui_example(
mut ui_state: ResMut<UiState>,
assets: Res<AssetServer>,
) {
let egui_texture_handle = ui_state
.egui_texture_handle
.get_or_insert_with(|| {
egui_ctx
.ctx_mut()
.load_texture("example-image", egui::ColorImage::example())
})
.clone();

let mut load = false;
let mut remove = false;
let mut invert = false;
Expand All @@ -80,6 +90,11 @@ fn ui_example(
ui.text_edit_singleline(&mut ui_state.label);
});

ui.add(egui::widgets::Image::new(
egui_texture_handle.id(),
egui_texture_handle.size_vec2(),
));

ui.add(egui::Slider::new(&mut ui_state.value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
ui_state.value += 1.0;
Expand Down
6 changes: 4 additions & 2 deletions src/egui.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ struct VertexOutput {
[[builtin(position)]] pos: vec4<f32>;
};

// 0-1 linear from 0-255 sRGB
fn linear_from_srgb(srgb: vec3<f32>) -> vec3<f32> {
let cutoff = vec3<f32>(srgb < vec3<f32>(10.31475));
let lower = srgb / vec3<f32>(3294.6);
let higher = pow((srgb + vec3<f32>(14.025)) / vec3<f32>(269.025), vec3<f32>(2.4));
return mix(higher, lower, cutoff);
}

// 0-1 linear from 0-255 sRGBA
fn linear_from_srgba(srgba: vec4<f32>) -> vec4<f32> {
return vec4<f32>(linear_from_srgb(srgba.rgb), srgba.a / 255.0);
}
Expand All @@ -27,11 +29,11 @@ fn linear_from_srgba(srgba: vec4<f32>) -> vec4<f32> {
fn vs_main(
[[location(0)]] position: vec2<f32>,
[[location(1)]] uv: vec2<f32>,
[[location(2)]] color: vec4<f32>,
[[location(2)]] color: vec4<f32>, // 0-1 range
) -> VertexOutput {
var out: VertexOutput;
out.uv = uv;
out.color = linear_from_srgba(color);
out.color = linear_from_srgba(color * 255.0);
out.pos = vec4<f32>(position * egui_transform.scale + egui_transform.translation, 0.0, 1.0);
return out;
}
Expand Down
56 changes: 31 additions & 25 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{
core::{bytes_of, cast_slice},
prelude::{FromWorld, World},
core::cast_slice,
ecs::world::{FromWorld, World},
render::{
render_graph::{Node, NodeRunError, RenderGraphContext},
render_resource::{
Expand All @@ -22,7 +22,7 @@ use wgpu::{BufferDescriptor, SamplerBindingType, ShaderModuleDescriptor, ShaderS

use crate::render_systems::{
EguiTexture, EguiTextureBindGroups, EguiTransform, EguiTransforms, ExtractedEguiContext,
ExtractedEguiSettings, ExtractedShapes, ExtractedWindowSizes,
ExtractedEguiSettings, ExtractedRenderOutput, ExtractedWindowSizes,
};

pub struct EguiPipeline {
Expand Down Expand Up @@ -95,7 +95,7 @@ impl FromWorld for EguiPipeline {
module: &shader_module,
entry_point: "vs_main",
buffers: &[wgpu::VertexBufferLayout {
array_stride: 32,
array_stride: 20,
step_mode: VertexStepMode::Vertex,
attributes: &[
VertexAttribute {
Expand All @@ -109,7 +109,7 @@ impl FromWorld for EguiPipeline {
shader_location: 1,
},
VertexAttribute {
format: VertexFormat::Float32x4,
format: VertexFormat::Unorm8x4,
offset: 16,
shader_location: 2,
},
Expand Down Expand Up @@ -189,7 +189,7 @@ impl EguiNode {

impl Node for EguiNode {
fn update(&mut self, world: &mut World) {
let mut shapes = world.get_resource_mut::<ExtractedShapes>().unwrap();
let mut shapes = world.get_resource_mut::<ExtractedRenderOutput>().unwrap();
let shapes = match shapes.0.get_mut(&self.window_id) {
Some(shapes) => shapes,
None => return,
Expand Down Expand Up @@ -231,14 +231,8 @@ impl Node for EguiNode {
continue;
}

for vertex in &triangles.vertices {
self.vertex_data
.extend_from_slice(bytes_of(&[vertex.pos.x, vertex.pos.y]));
self.vertex_data
.extend_from_slice(bytes_of(&[vertex.uv.x, vertex.uv.y]));
self.vertex_data
.extend_from_slice(bytes_of(&vertex.color.to_array().map(|c| c as f32)));
}
self.vertex_data
.extend_from_slice(cast_slice(triangles.vertices.as_slice()));
let indices_with_offset = triangles
.indices
.iter()
Expand All @@ -249,7 +243,7 @@ impl Node for EguiNode {
index_offset += triangles.vertices.len() as u32;

let texture_handle = match triangles.texture_id {
egui::TextureId::Egui => EguiTexture::Font(self.window_id),
egui::TextureId::Managed(id) => EguiTexture::Managed(self.window_id, id),
egui::TextureId::User(id) => EguiTexture::User(id),
};

Expand Down Expand Up @@ -395,20 +389,32 @@ impl Node for EguiNode {
}
}

pub fn as_wgpu_image(egui_texture: &egui::FontImage) -> Image {
let mut pixels = Vec::with_capacity(4 * egui_texture.pixels.len());
for &alpha in egui_texture.pixels.iter() {
pixels.extend(
egui::color::Color32::from_white_alpha(alpha)
.to_array()
.iter(),
);
pub fn as_color_image(image: &egui::ImageData) -> egui::ColorImage {
match image {
egui::ImageData::Color(image) => image.clone(),
egui::ImageData::Alpha(image) => alpha_image_as_color_image(image),
}
}

pub fn alpha_image_as_color_image(image: &egui::AlphaImage) -> egui::ColorImage {
let gamma = 1.0;
egui::ColorImage {
size: image.size,
pixels: image.srgba_pixels(gamma).collect(),
}
}

pub fn color_image_as_bevy_image(egui_image: &egui::ColorImage) -> Image {
let pixels = egui_image
.pixels
.iter()
.flat_map(|color| color.to_array())
.collect();

Image::new(
Extent3d {
width: egui_texture.width as u32,
height: egui_texture.height as u32,
width: egui_image.width() as u32,
height: egui_image.height() as u32,
depth_or_array_layers: 1,
},
TextureDimension::D2,
Expand Down
Loading