Skip to content

Commit 851284c

Browse files
committed
Add compositor for UI overlay, property getters for all element types
Add a Compositor to the renderling crate (compositor_vertex + compositor_fragment shaders) that alpha-blends a source texture onto a target framebuffer via a fullscreen quad. This fixes the black screen when overlaying the UI renderer on a 3D stage with MSAA enabled: the UI now renders to an intermediate texture, resolves MSAA there, and the compositor blends it onto the final view preserving the scene beneath. Add property getters to all UI element types (UiRect, UiCircle, UiEllipse, UiImage, UiPath, UiText) so current values can be read back — a prerequisite for the upcoming tweening/animation system. Fix the example app's UI renderer clearing the 3D scene by removing the erroneous .with_background_color(Vec4::ZERO) call, which was causing a LoadOp::Clear that wiped the stage output.
1 parent 5c05d55 commit 851284c

11 files changed

Lines changed: 601 additions & 16 deletions

File tree

crates/example/src/lib.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ struct AppUi {
8383
}
8484

8585
impl AppUi {
86-
fn make_fps_widget(
87-
fps_counter: &FPSCounter,
88-
ui: &mut UiRenderer,
89-
) -> (UiText, UiRect) {
86+
fn make_fps_widget(fps_counter: &FPSCounter, ui: &mut UiRenderer) -> (UiText, UiRect) {
9087
let offset = Vec2::new(2.0, 2.0);
9188
let text = format!("{}fps", fps_counter.current_fps_string());
9289
let fps_text = ui.add_text(
@@ -116,8 +113,7 @@ impl AppUi {
116113
// Remove old text and background before recreating.
117114
self.ui.remove_text(&self.fps_text);
118115
self.ui.remove_rect(&self.fps_background);
119-
let (fps_text, background) =
120-
Self::make_fps_widget(&self.fps_counter, &mut self.ui);
116+
let (fps_text, background) = Self::make_fps_widget(&self.fps_counter, &mut self.ui);
121117
self.fps_text = fps_text;
122118
self.fps_background = background;
123119
self.last_fps_display = now;
@@ -171,7 +167,7 @@ impl App {
171167
})
172168
.unwrap();
173169

174-
let mut ui = UiRenderer::new(ctx).with_background_color(Vec4::ZERO);
170+
let mut ui = UiRenderer::new(ctx);
175171
let _ = ui.add_font(FontArc::try_from_slice(FONT_BYTES).unwrap());
176172
let fps_counter = FPSCounter::default();
177173
let (fps_text, fps_background) = AppUi::make_fps_widget(&fps_counter, &mut ui);
@@ -234,6 +230,24 @@ impl App {
234230
self.stage.use_ibl(&ibl);
235231
}
236232

233+
fn set_model(&mut self, model: Model) {
234+
match std::mem::replace(&mut self.model, model) {
235+
Model::Gltf(gltf_document) => {
236+
// Remove all the things that was loaded by the document
237+
for prim in gltf_document.primitives.values().flatten() {
238+
self.stage.remove_primitive(prim);
239+
}
240+
for light in gltf_document.lights.iter() {
241+
self.stage.remove_light(light);
242+
}
243+
}
244+
Model::Default(primitive) => {
245+
self.stage.remove_primitive(&primitive);
246+
}
247+
Model::None => {}
248+
}
249+
}
250+
237251
pub fn load_default_model(&mut self) {
238252
log::info!("loading default model");
239253
let mut min = Vec3::splat(f32::INFINITY);
@@ -260,7 +274,8 @@ impl App {
260274
BoundingSphere::from((min, max))
261275
});
262276

263-
self.model = Model::Default(primitive);
277+
self.set_model(Model::Default(primitive));
278+
264279
self.camera_controller.reset(Aabb::new(min, max));
265280
self.camera_controller
266281
.update_camera(self.stage.get_size(), &self.camera);
@@ -271,7 +286,6 @@ impl App {
271286
self.camera_controller
272287
.reset(Aabb::new(Vec3::NEG_ONE, Vec3::ONE));
273288
self.stage.clear_images().unwrap();
274-
self.model = Model::None;
275289
let doc = match self.stage.load_gltf_document_from_bytes(bytes) {
276290
Err(e) => {
277291
log::error!("gltf loading error: {e}");
@@ -391,13 +405,13 @@ impl App {
391405

392406
// self.lighting
393407
// .shadow_map
394-
// .update(&self.lighting.lighting, doc.primitives.values().flatten());
395-
// self.lighting.light = light.light.clone();
396-
// self.lighting.light_details = dir.clone();
397-
// }
408+
// .update(&self.lighting.lighting,
409+
// doc.primitives.values().flatten()); self.lighting.light =
410+
// light.light.clone(); self.lighting.light_details =
411+
// dir.clone(); }
398412
// }
399413

400-
self.model = Model::Gltf(Box::new(doc));
414+
self.set_model(Model::Gltf(Box::new(doc)));
401415
}
402416

403417
pub fn tick_loads(&mut self) {

0 commit comments

Comments
 (0)