From 950934832006b086347e8ec973c83bc7104fc57f Mon Sep 17 00:00:00 2001 From: jeromehzl <51324719+jeromehzl@users.noreply.github.com> Date: Thu, 29 Oct 2026 13:14:15 +0000 Subject: [PATCH] Viewport: editing a material's colour redraws the bodies that use it (#8764) (#8814) body_appearance_key hashed only the name of a body's assigned material, so after material.edit changed that material's colour (or an undo or redo changed it back) the key stayed the same and the viewport kept the cached old look. The key now also hashes the material's resolved look; with an unchanged material the cache is reused as before. --- crates/ui-egui/src/viewport.rs | 6 +++-- .../ui-egui/src/viewport_appearance_tests.rs | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index d48fc523d..39505d09c 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -941,7 +941,7 @@ fn hash_look(look: Option<&solidcraft_engine::doc::appearance::Look>, h: &mut im } /// What a body's resolved look depends on: its geometry (imported paint), the body/component -/// looks, its material, its face appearances, and component colouring. +/// looks, its material and that material's look, its face appearances, and component colouring. fn body_appearance_key( doc: &solidcraft_engine::doc::Document, b: &solidcraft_engine::doc::ModelBody, @@ -954,7 +954,9 @@ fn body_appearance_key( let name = b.definition_name(); hash_look(doc.appearances.bodies.get(name), &mut h); hash_look(doc.appearances.components.get(&b.component), &mut h); - doc.materials.get(name).hash(&mut h); + let material = doc.materials.get(name); + material.hash(&mut h); + hash_look(material.and_then(|m| doc.material_look(m)).as_ref(), &mut h); faces.get(name).copied().unwrap_or(0).hash(&mut h); colored.hash(&mut h); if colored { diff --git a/crates/ui-egui/src/viewport_appearance_tests.rs b/crates/ui-egui/src/viewport_appearance_tests.rs index 133e2319e..3f3f7580b 100644 --- a/crates/ui-egui/src/viewport_appearance_tests.rs +++ b/crates/ui-egui/src/viewport_appearance_tests.rs @@ -201,6 +201,29 @@ fn a_colour_edit_reuses_every_block_vertex() { assert_eq!(first_vertex(&second, &block.bytes.vertices).1, Some([51, 102, 153, 255]), "the palette carries the new colour"); } +#[test] +fn a_material_colour_edit_its_undo_and_redo_reach_the_viewport() { + let mut app = SolveApp::new(Session::default(), Services::default()); + app.ui.ground_shadow = false; + app.set_origin_shown(false); + app.run("solid.box", json!({"length": 20, "width": 15, "height": 10})).unwrap(); + let name = app.session.world_state().bodies[0].name.clone(); + app.run("material.create", json!({"name": "Wood dark", "density_g_cm3": 0.7, "color": "#c8a573"})).unwrap(); + app.run("material.assign", json!({"bodies": [name], "material": "Wood dark"})).unwrap(); + let colour = |app: &SolveApp| { + let scene = build_scene(app); + let block = scene.blocks.iter().find(|b| !b.bytes.vertices.is_empty()).expect("the box has a block"); + first_vertex(&scene, &block.bytes.vertices).1 + }; + assert_eq!(colour(&app), Some([0xc8, 0xa5, 0x73, 255])); + app.run("material.edit", json!({"material": "Wood dark", "color": "#ccb799"})).unwrap(); + assert_eq!(colour(&app), Some([0xcc, 0xb7, 0x99, 255]), "the edited material colour is drawn"); + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(colour(&app), Some([0xc8, 0xa5, 0x73, 255]), "undo draws the old colour again"); + app.run("edit.redo", json!({})).unwrap(); + assert_eq!(colour(&app), Some([0xcc, 0xb7, 0x99, 255]), "redo draws the edited colour again"); +} + #[test] fn an_opacity_class_change_keeps_vertices_and_rewrites_only_the_runs() { let mut app = SolveApp::new(Session::default(), Services::default());