From 1be96b8248d6d23eeba5c281b672c8d700d68076 Mon Sep 17 00:00:00 2001 From: Rebornim Date: Tue, 10 Nov 2026 07:10:52 +0000 Subject: [PATCH] Assets: environments as tiles with their panorama, the shown one outlined --- crates/ui-egui/src/preview_image.rs | 17 ++++++++++++++++ crates/ui-egui/src/shelf.rs | 15 -------------- crates/ui-egui/src/shelf_entry.rs | 31 ++++++++++++++++++++++------- crates/ui-egui/src/shelf_preview.rs | 7 ++++++- crates/ui-egui/src/thumbs.rs | 5 +++++ 5 files changed, 52 insertions(+), 23 deletions(-) diff --git a/crates/ui-egui/src/preview_image.rs b/crates/ui-egui/src/preview_image.rs index 9b753b74..16709385 100644 --- a/crates/ui-egui/src/preview_image.rs +++ b/crates/ui-egui/src/preview_image.rs @@ -194,3 +194,20 @@ pub fn stroke_preview(b: &BrushSettings, n: usize) -> ColorImage { } ColorImage::new([n, n], cover.into_iter().map(|v| Color32::from_gray((v * b.opacity * 255.0 + 0.5) as u8)).collect()) } + +/// Environment `name`'s panorama across a `size²` swatch, transparent above and below +pub fn environment_image(name: &str, size: usize) -> ColorImage { + let panorama = texturecraft_render::ibl::environment(name, u32::try_from(size).unwrap_or(u32::MAX)); + let (width, height) = (panorama.width as usize, panorama.height as usize); + let mut image = ColorImage::filled([size, size], Color32::TRANSPARENT); + let top = size.saturating_sub(height) / 2; + for (y, row) in panorama.data.chunks(width.max(1)).enumerate() { + for (x, linear) in row.iter().enumerate().take(size) { + let Some(pixel) = image.pixels.get_mut((top + y) * size + x) else { continue }; + + let [r, g, b] = linear.map(|component| (linear_to_srgb(aces(component * EXPOSURE)) * 255.0 + 0.5) as u8); + *pixel = Color32::from_rgb(r, g, b); + } + } + image +} diff --git a/crates/ui-egui/src/shelf.rs b/crates/ui-egui/src/shelf.rs index 1890c30e..0ff1cc6a 100644 --- a/crates/ui-egui/src/shelf.rs +++ b/crates/ui-egui/src/shelf.rs @@ -12,7 +12,6 @@ use serde_json::json; use texturecraft_engine::formats::mcmat::MCMAT_EXT; use texturecraft_engine::geom::glam::Vec2; use texturecraft_engine::orb_preview::MAX_PREVIEW_SIZE_PX; -use texturecraft_render::ibl::ENVIRONMENTS; use crate::TextureCraftApp; use crate::decal_drop::DecalDrop; @@ -271,9 +270,6 @@ pub fn body(app: &mut TextureCraftApp, ui: &mut Ui) { if searching || shown_tab == Tab::Brushes { my_brushes(app, ui, &filter); } - if searching || shown_tab == Tab::Environments { - environments(app, ui, &filter); - } }); drag_preview(&ctx); } @@ -423,17 +419,6 @@ fn my_brushes(app: &mut TextureCraftApp, ui: &mut Ui, filter: &str) { #[cfg(target_arch = "wasm32")] fn my_brushes(_: &mut TextureCraftApp, _: &mut Ui, _: &str) {} -fn environments(app: &mut TextureCraftApp, ui: &mut Ui, filter: &str) { - let shown: Vec<&&str> = ENVIRONMENTS.iter().filter(|e| matches(filter, &[e])).collect(); - for env in shown { - let on = app.session.doc.project.display.environment == **env; - if ui.selectable_label(on, format!("{} {env}", ic::SUN_HORIZON)).clicked() { - app.run("display.set", json!({"environment": env})); - } - } - ui.add_space(6.0); -} - /// The Textures tab's import button, and a hint while the project has no images fn import_images(app: &mut TextureCraftApp, ui: &mut Ui, ctx: &egui::Context, empty: bool) { let tk = Tokens::get(ctx); diff --git a/crates/ui-egui/src/shelf_entry.rs b/crates/ui-egui/src/shelf_entry.rs index be43977b..c44ecb1c 100644 --- a/crates/ui-egui/src/shelf_entry.rs +++ b/crates/ui-egui/src/shelf_entry.rs @@ -2,6 +2,7 @@ use serde_json::json; use texturecraft_engine::core::ProceduralKind; +use texturecraft_engine::core::project::ENVIRONMENTS; use texturecraft_engine::filter_lib::{FILTERS, FilterDef}; use texturecraft_engine::materials::{MATERIALS, SmartMaterial}; use texturecraft_engine::paint::alphas::{self, LibraryAlpha}; @@ -22,6 +23,8 @@ pub(crate) enum ShelfEntry { Brush(&'static BrushPreset), Alpha(&'static LibraryAlpha), Procedural(ProceduralKind), + /// A built-in lighting environment, by name + Environment(&'static str), /// An image resource of the project, by name Image(String), } @@ -36,7 +39,8 @@ impl ShelfEntry { .chain(FILTERS.iter().map(ShelfEntry::Filter)) .chain(PRESETS.iter().map(ShelfEntry::Brush)) .chain(alphas::library().iter().map(ShelfEntry::Alpha)) - .chain(ProceduralKind::ALL.into_iter().map(ShelfEntry::Procedural)); + .chain(ProceduralKind::ALL.into_iter().map(ShelfEntry::Procedural)) + .chain(ENVIRONMENTS.into_iter().map(ShelfEntry::Environment)); built_in.chain(app.session.doc.resources.keys().cloned().map(ShelfEntry::Image)).collect() } @@ -50,6 +54,7 @@ impl ShelfEntry { ShelfEntry::Brush(_) => Tab::Brushes, ShelfEntry::Alpha(_) => Tab::Alphas, ShelfEntry::Procedural(_) => Tab::Procedurals, + ShelfEntry::Environment(_) => Tab::Environments, ShelfEntry::Image(_) => Tab::Textures, } } @@ -63,6 +68,7 @@ impl ShelfEntry { ShelfEntry::Brush(preset) => format!("brush:{}", preset.id), ShelfEntry::Alpha(alpha) => format!("alpha:{}", alpha.id), ShelfEntry::Procedural(kind) => format!("procedural:{kind:?}"), + ShelfEntry::Environment(name) => format!("environment:{name}"), ShelfEntry::Image(name) => format!("image:{name}"), } } @@ -76,6 +82,7 @@ impl ShelfEntry { ShelfEntry::Brush(preset) => preset.label, ShelfEntry::Alpha(alpha) => &alpha.label, ShelfEntry::Procedural(kind) => tr(kind.label()), + ShelfEntry::Environment(name) => name, ShelfEntry::Image(name) => name, } } @@ -89,6 +96,7 @@ impl ShelfEntry { ShelfEntry::Brush(preset) => &[preset.label, preset.category, preset.id], ShelfEntry::Alpha(alpha) => &[&alpha.label, alpha.category, &alpha.id], ShelfEntry::Procedural(kind) => &[kind.label(), tr(kind.label())], + ShelfEntry::Environment(name) => &[name], ShelfEntry::Image(name) => &[name], }; crate::shelf::matches(filter, texts) @@ -103,6 +111,7 @@ impl ShelfEntry { ShelfEntry::Brush(preset) => app.thumbs.brush_preset(ctx, preset), ShelfEntry::Alpha(alpha) => app.thumbs.alpha(ctx, alpha), ShelfEntry::Procedural(kind) => app.thumbs.procedural(ctx, *kind), + ShelfEntry::Environment(name) => app.thumbs.environment(ctx, name), ShelfEntry::Image(name) => { let image = app.session.images.get(name).cloned()?; Some(app.thumbs.image(ctx, name, &image)) @@ -119,6 +128,7 @@ impl ShelfEntry { ShelfEntry::Brush(preset) => ShelfPreview::Brush(preset), ShelfEntry::Alpha(alpha) => ShelfPreview::Alpha(alpha), ShelfEntry::Procedural(kind) => ShelfPreview::Procedural(*kind), + ShelfEntry::Environment(name) => ShelfPreview::Environment(name), ShelfEntry::Image(name) => ShelfPreview::Image(name.clone(), app.session.images.get(name).cloned()?), }) } @@ -131,16 +141,20 @@ impl ShelfEntry { ShelfEntry::Filter(filter) => Some(Asset::Filter(filter.id)), ShelfEntry::Alpha(alpha) => Some(Asset::Alpha(&alpha.id)), ShelfEntry::Image(name) => Some(Asset::Texture(name.clone())), - ShelfEntry::Brush(_) | ShelfEntry::Procedural(_) => None, + ShelfEntry::Brush(_) | ShelfEntry::Procedural(_) | ShelfEntry::Environment(_) => None, } } - /// Whether the tile is outlined as in use: the brush's library alpha + /// Whether the tile is outlined as in use: the brush's library alpha, the shown environment pub(crate) fn in_use(&self, app: &TextureCraftApp) -> bool { - let ShelfEntry::Alpha(alpha) = self else { return false }; - - let brush = &app.session.tool.brush; - brush.alpha_image.is_none() && brush.alpha_library.as_deref() == Some(alpha.id.as_str()) + match self { + ShelfEntry::Alpha(alpha) => { + let brush = &app.session.tool.brush; + brush.alpha_image.is_none() && brush.alpha_library.as_deref() == Some(alpha.id.as_str()) + } + ShelfEntry::Environment(name) => app.session.doc.project.display.environment == *name, + _ => false, + } } /// What a click on the tile does @@ -153,6 +167,9 @@ impl ShelfEntry { app.run("layer.add.fill", json!({"name": kind.label()})); app.run("layer.fill.set", json!({"channel": "baseColor", "procedural": kind.label(), "low": [0.1, 0.1, 0.1], "high": [0.9, 0.9, 0.9]})); } + ShelfEntry::Environment(name) => { + app.run("display.set", json!({"environment": name})); + } entry => { if let Some(asset) = entry.asset() { apply(app, &asset); diff --git a/crates/ui-egui/src/shelf_preview.rs b/crates/ui-egui/src/shelf_preview.rs index b512f250..c4b4ae36 100644 --- a/crates/ui-egui/src/shelf_preview.rs +++ b/crates/ui-egui/src/shelf_preview.rs @@ -11,7 +11,7 @@ use texturecraft_engine::paint::alphas::LibraryAlpha; use texturecraft_engine::paint::presets::BrushPreset; use texturecraft_engine::preview; -use crate::preview_image::{alpha_image, image_preview, procedural_image, sphere_image, stroke_preview}; +use crate::preview_image::{alpha_image, environment_image, image_preview, procedural_image, sphere_image, stroke_preview}; /// The asset a shelf tile previews pub enum ShelfPreview { @@ -31,6 +31,8 @@ pub enum ShelfPreview { Brush(&'static BrushPreset), /// An image resource by name Image(String, Arc), + /// A built-in lighting environment's panorama + Environment(&'static str), } impl ShelfPreview { @@ -53,6 +55,7 @@ impl ShelfPreview { ShelfPreview::Alpha(alpha) => format!("alpha:{}", alpha.id), ShelfPreview::Brush(preset) => format!("brush:{}", preset.id), ShelfPreview::Image(name, _) => format!("image:{name}"), + ShelfPreview::Environment(name) => format!("environment:{name}"), } } @@ -67,6 +70,7 @@ impl ShelfPreview { ShelfPreview::Alpha(alpha) => Some(alpha_image(alpha, size)), ShelfPreview::Brush(preset) => Some(stroke_preview(&preset.applied(&BrushSettings::default()), size)), ShelfPreview::Image(_, image) => Some(image_preview(image, size)), + ShelfPreview::Environment(name) => Some(environment_image(name, size)), } } } @@ -93,6 +97,7 @@ mod tests { ShelfPreview::Alpha(&alphas::library()[0]), ShelfPreview::Brush(&PRESETS[0]), ShelfPreview::Image("image".into(), Arc::new(image)), + ShelfPreview::Environment(texturecraft_engine::core::project::ENVIRONMENTS[0]), ]; for preview in previews { let key = preview.key(); diff --git a/crates/ui-egui/src/thumbs.rs b/crates/ui-egui/src/thumbs.rs index 059cc0a3..3aa97413 100644 --- a/crates/ui-egui/src/thumbs.rs +++ b/crates/ui-egui/src/thumbs.rs @@ -212,6 +212,11 @@ impl Thumbs { self.shelf(ctx, ShelfPreview::Alpha(alpha), SWATCH_PX) } + /// A lighting environment's panorama + pub fn environment(&mut self, ctx: &egui::Context, name: &'static str) -> Option { + self.shelf(ctx, ShelfPreview::Environment(name), SWATCH_PX) + } + /// A brush preset's stroke along an S curve pub fn brush_preset(&mut self, ctx: &egui::Context, preset: &'static BrushPreset) -> Option { self.shelf(ctx, ShelfPreview::Brush(preset), SWATCH_PX)