Expose 4K editing previews in the material views

This commit is contained in:
Rafael Mendes committed 2026-11-04 23:05:25 -03:00
1 parent 698ea23380
commit 4c7aeabb74
7 files changed
+117 -5

No files matched your search

+6 -1
View File
@@ -226,7 +226,12 @@ pub fn handle(app: &mut MaterialCraftApp, ctx: &egui::Context, method: &str, p:
let Some(id) = p.get("id").and_then(Value::as_str) else { return err("`id` is required") };
let params = p.get("params").cloned().unwrap_or(Value::Null);
match materialcraft_engine::execute(&mut app.session, id, &params) {
Ok(v) => ok(v),
Ok(v) => {
if id == "view.previewSize" {
app.ui.prefs.preview_size = app.session.preview_size.unwrap_or(0);
}
ok(v)
}
Err(e) => err(e),
}
}
+7 -3
View File
@@ -122,15 +122,19 @@ impl MaterialCraftApp {
}
let p = if self.ui.prefs.preview_size == 0 { None } else { Some(self.ui.prefs.preview_size.clamp(64, 8192)) };
if self.session.preview_size != p {
self.session.preview_size = p;
self.session.dirty = true;
self.run("view.previewSize", serde_json::json!({"size": p.unwrap_or(0)}));
}
}
/// Run an engine command, reporting errors as toasts. Returns the result on success.
pub fn run(&mut self, id: &str, params: Value) -> Option<Value> {
match materialcraft_engine::execute(&mut self.session, id, &params) {
Ok(v) => Some(v),
Ok(v) => {
if id == "view.previewSize" {
self.ui.prefs.preview_size = self.session.preview_size.unwrap_or(0);
}
Some(v)
}
Err(e) => {
self.toast(e.to_string(), true);
None
+20
View File
@@ -21,6 +21,25 @@ pub struct View2dState {
const BINS: usize = 64;
/// Both views use the same cooked material resolution and the same engine command.
pub(crate) fn resolution_menu(app: &mut MaterialCraftApp, ui: &mut Ui) {
let selected = app.session.preview_size.map_or_else(|| tr("Full size").to_string(), |size| format!("{size} px"));
let response = egui::ComboBox::from_id_salt("preview_resolution")
.selected_text(selected)
.width(85.0)
.show_ui(ui, |ui| {
for size in [256u32, 512, 1024, 2048, 4096, 0] {
let label = if size == 0 { tr("Full size").to_string() } else { format!("{size} px") };
if ui.selectable_label(app.session.preview_size.unwrap_or(0) == size, label).clicked() {
app.run("view.previewSize", json!({"size": size}));
}
}
})
.response;
crate::widgets::name(&response, egui::Role::ComboBox, "Preview resolution");
response.on_hover_text(tr("Preview size while editing"));
}
/// Histograms of the displayed 8-bit values: one for gray or isolated channels, else R, G and B.
fn histogram(px: &[u8], channels: usize) -> Vec<[u32; BINS]> {
let mut h = vec![[0u32; BINS]; channels];
@@ -58,6 +77,7 @@ pub fn show(app: &mut MaterialCraftApp, ui: &mut Ui) {
// Toolbar.
egui::Frame::NONE.fill(tk.panel).inner_margin(egui::Margin::symmetric(6, 3)).show(ui, |ui| {
ui.horizontal(|ui| {
resolution_menu(app, ui);
for (c, label) in [("rgba", "RGBA"), ("r", "R"), ("g", "G"), ("b", "B"), ("a", "A")] {
if crate::widgets::chip(ui, label, app.session.view2d.channel == c).clicked() {
app.run("view.2d", json!({"channel": c}));
+2
View File
@@ -141,6 +141,8 @@ fn menus(app: &mut MaterialCraftApp, ui: &mut Ui) {
}
});
ui.menu_button(tr("Display"), |ui| {
crate::view2d::resolution_menu(app, ui);
ui.separator();
ui.checkbox(&mut app.view3d.show_env, tr("Environment background"));
ui.separator();
let mut t = app.session.view3d.transparency;
+1 -1
View File
@@ -293,7 +293,7 @@ fn preferences(app: &mut MaterialCraftApp, ctx: &egui::Context) {
tr("Preview size while editing"),
if p.preview_size == 0 { tr("Full size").to_string() } else { format!("{} px", p.preview_size) },
|ui| {
for s in [256u32, 512, 1024, 2048, 0] {
for s in [256u32, 512, 1024, 2048, 4096, 0] {
let label = if s == 0 { tr("Full size").to_string() } else { format!("{s} px") };
ui.selectable_value(&mut p.preview_size, s, label);
}
+75
View File
@@ -0,0 +1,75 @@
//! Resolution changes must reach both material maps and the displayed image, and survive frames.
use egui::vec2;
use egui_kittest::{Harness, kittest::Queryable};
use materialcraft_ui_egui::{MaterialCraftApp, UiState, control, view2d};
use serde_json::json;
fn app() -> MaterialCraftApp {
let mut app = MaterialCraftApp::headless(materialcraft_engine::new_session(), UiState::default());
app.run("file.new", json!({"template": "empty", "size": 4096})).unwrap();
let node = app.run("graph.node.add", json!({"def": "uniform", "params": {"mode": 1}})).unwrap()["id"].as_u64().unwrap();
app.run("graph.node.add", json!({"def": "output", "connectFrom": {"node": node}})).unwrap();
app.run("view.2d", json!({"node": node})).unwrap();
app.session.cook();
app
}
fn choose_4k(h: &mut Harness<'static, MaterialCraftApp>) {
h.get_by_label("Preview resolution").click();
h.run();
h.get_by_label("4096 px").click();
h.run();
assert_eq!(h.state().session.preview_size, Some(4096));
assert_eq!(h.state().ui.prefs.preview_size, 4096);
h.state_mut().session.cook();
h.step();
let app = h.state();
let (_, image) = app.session.view2d_image().unwrap();
assert_eq!((image.w, image.h), (4096, 4096));
assert!(app.session.material_maps().iter().all(|(_, image)| (image.w, image.h) == (4096, 4096)));
assert!(!app.session.material_maps().is_empty());
assert_eq!(app.view2d.shown.as_ref().map(|(w, h, _)| (*w, *h)), Some((4096, 4096)));
}
#[test]
fn selecting_4k_cooks_and_displays_4k_material_maps() {
let mut h = Harness::builder().with_context_setup(materialcraft_ui_egui::theme::install).with_size(vec2(1200.0, 800.0)).build_ui_state(
|ui, app: &mut MaterialCraftApp| {
app.apply_prefs();
view2d::show(app, ui);
},
app(),
);
choose_4k(&mut h);
}
#[test]
fn command_resolution_survives_preferences_and_invalid_requests() {
let mut app = app();
let ctx = egui::Context::default();
for size in [4096, 0, 512] {
let response = control::handle(&mut app, &ctx, "command.run", &json!({"id": "view.previewSize", "params": {"size": size}}));
assert_eq!(response["ok"], true);
app.apply_prefs();
assert_eq!(app.ui.prefs.preview_size, size);
assert_eq!(app.session.preview_size, if size == 0 { None } else { Some(size) });
}
assert!(app.run("view.previewSize", json!({"size": -1})).is_none());
assert_eq!(app.ui.prefs.preview_size, 512);
}
#[test]
#[ignore = "requires an offscreen GPU; run explicitly with --ignored"]
fn resolution_snapshot() {
let mut h = Harness::builder().with_context_setup(materialcraft_ui_egui::theme::install).with_size(vec2(1200.0, 800.0)).wgpu().build_ui_state(
|ui, app: &mut MaterialCraftApp| {
app.apply_prefs();
view2d::show(app, ui);
},
app(),
);
choose_4k(&mut h);
let dir = std::path::PathBuf::from(std::env::var_os("MC_SNAPSHOT_DIR").expect("MC_SNAPSHOT_DIR"));
std::fs::create_dir_all(&dir).unwrap();
h.render().unwrap().save(dir.join("resolution-4k.png")).unwrap();
}
+6
View File
@@ -47,6 +47,12 @@ offset by powers of two or set absolutely. While editing, outputs are capped at
(512 px by default), with pixel-based parameters scaled by `w / 256` so previews and full-size
exports look the same. Exports cook at full size through the same cache.
The 2D toolbar and the 3D view's Display menu expose the shared preview limit (256, 512, 1024,
2048 or 4096 px, or Full size). This is a cap, so a 4K preview requires a graph/node configured
for 4096 px or larger. Both controls run `view.previewSize` and persist the choice in preferences;
control-channel calls to the same command also retain their size across UI frames. The 3D view
uses those cooked maps, while its rendered frame follows the panel's physical pixel dimensions.
The desktop UI cooks on a worker thread (`ui-egui/src/cook.rs`) from a snapshot of the package and
keeps drawing the last results; the browser cooks on the UI thread.