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.
This commit is contained in:
Rebornim committed 2026-11-28 02:33:59 +00:00
1 parent 95d90ee453
commit 31bf2a58ca
3 files changed
+34 -8

No files matched your search

+14 -6
View File
@@ -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<String>) {
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<String>, 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<String>,
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() {
+1 -1
View File
@@ -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()
}
+19 -1
View File
@@ -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();