From 31bf2a58caf09600f47c23ebea667e08cac2bbdd Mon Sep 17 00:00:00 2001 From: Rebornim <250083243+Rebornim@users.noreply.github.com> Date: Sat, 28 Nov 2026 02:33:59 +0000 Subject: [PATCH] Assets: Remove unused keeps the package and maps of a material linked in a content effect images_used counted a linked material's package and cooked maps only on a fill layer, so Remove unused dropped them (and a smart material left them out) when the link sat in a content fill effect. --- crates/engine/src/material_lib.rs | 20 ++++++++++++++------ crates/engine/tests/it/materialcraft_link.rs | 2 +- crates/engine/tests/it/resources_used.rs | 20 +++++++++++++++++++- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/material_lib.rs b/crates/engine/src/material_lib.rs index 9d496630..0277c6fc 100644 --- a/crates/engine/src/material_lib.rs +++ b/crates/engine/src/material_lib.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use serde_json::Value; use texturecraft_compose::PixelStore; -use texturecraft_core::{LayerId, LayerKind, Project, Source, TextureSet}; +use texturecraft_core::{EffectKind, Fill, LayerId, LayerKind, Project, Source, TextureSet}; use crate::commands::{CommandSpec, always, has_layer}; use crate::materials::Place; @@ -36,7 +36,15 @@ fn fe(e: texturecraft_formats::FormatError) -> EngineError { EngineError::Other(e.to_string()) } -/// Image resources a layer subtree uses (fill sources and their patterns, mask or content effects and the images its paths paint with) +/// The package and cooked maps of the MaterialCraft material linked to `fill`, if any +fn link_images(fill: &Fill, out: &mut Vec) { + if let Some(link) = &fill.link { + out.push(link.package.clone()); + out.extend(link.outputs.values().cloned()); + } +} + +/// Image resources a layer subtree uses (fill sources and their patterns, linked materials, mask or content effects and the images its paths paint with) pub(crate) fn images_used(project: &Project, id: LayerId, out: &mut Vec, depth: usize) { if depth > texturecraft_core::project::MAX_DEPTH { return; @@ -46,15 +54,15 @@ pub(crate) fn images_used(project: &Project, id: LayerId, out: &mut Vec, if let LayerKind::Fill(fill) = &layer.kind { out.extend(fill.sources.values().flat_map(Source::images).map(str::to_string)); - if let Some(link) = &fill.link { - out.push(link.package.clone()); - out.extend(link.outputs.values().cloned()); - } + link_images(fill, out); } for effect in layer.mask.iter().flat_map(|mask| mask.effects.iter()).chain(&layer.content_effects) { out.extend(effect.images_read().map(str::to_string)); out.extend(crate::path_tool::path_images(&effect.kind)); + if let EffectKind::Material(fill) = &effect.kind { + link_images(fill, out); + } } for child in layer.children() { diff --git a/crates/engine/tests/it/materialcraft_link.rs b/crates/engine/tests/it/materialcraft_link.rs index 4a4a5d03..1ee696e1 100644 --- a/crates/engine/tests/it/materialcraft_link.rs +++ b/crates/engine/tests/it/materialcraft_link.rs @@ -47,7 +47,7 @@ fn cooked(roughness: f32, wear: f32) -> CookedMaterial { CookedMaterial::new(serde_json::from_value(interface).unwrap(), maps, 4) } -fn install(session: &mut Session) -> LayerId { +pub(crate) fn install(session: &mut Session) -> LayerId { session.add_cooked_material(&cooked(0.25, 0.5), Arc::new(b"package bytes".to_vec()), "mcmat", Place::Selection).unwrap() } diff --git a/crates/engine/tests/it/resources_used.rs b/crates/engine/tests/it/resources_used.rs index bd89ee77..fbc15d44 100644 --- a/crates/engine/tests/it/resources_used.rs +++ b/crates/engine/tests/it/resources_used.rs @@ -1,7 +1,7 @@ //! The images a project reads beyond fill sources: a colour selection's ID map, a filter's image input, the mask brush and its paths use serde_json::{Value, json}; -use texturecraft_engine::core::{Channel, LayerId}; +use texturecraft_engine::core::{Channel, EffectKind, LayerId, LayerKind}; use texturecraft_engine::geom::primitives; use texturecraft_engine::{Session, execute}; @@ -53,6 +53,24 @@ fn removing_unused_resources_keeps_the_images_a_selection_a_filter_input_the_mas assert_eq!(reply["removed"], json!([unused]), "only the image nothing reads goes"); } +#[test] +fn removing_unused_resources_keeps_the_package_and_maps_of_a_material_linked_in_a_content_effect() { + let mut session = session(); + let unused = session.import_resource_bytes("unused.png", white_png()).unwrap(); + let linked = crate::materialcraft_link::install(&mut session); + let LayerKind::Fill(fill) = session.doc.project.layer(linked).unwrap().kind.clone() else { panic!("not a fill") }; + + // The linked material moves into a content effect of another layer, the only place that uses it + let layer = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap(); + let effect = LayerId(execute(&mut session, "effect.add.fill", &json!({"layer": layer, "target": "content"})).unwrap()["effect"].as_u64().unwrap()); + session.doc.project.effect_mut(effect).unwrap().kind = EffectKind::Material(fill); + execute(&mut session, "layer.remove", &json!({"layer": linked.0})).unwrap(); + + let reply = execute(&mut session, "file.removeUnused", &Value::Null).unwrap(); + + assert_eq!(reply["removed"], json!([unused]), "the linked package and its maps stay"); +} + #[test] fn a_smart_material_carries_the_images_its_selection_and_filter_input_read() { let mut session = session();