Auto: Respect curvature modes and filter inherited anchor choices

Combine micro height with edge and cavity responses before mode selection. Build inherited-effect Fill and micro-detail anchor choices from the effect owner. Add focused regressions for both behaviors.
This commit is contained in:
chasse20 committed 2026-11-17 03:56:18 +00:00
1 parent 1794e93545
commit f10d92b997
3 files changed
+93 -16

No files matched your search

+58 -12
View File
@@ -1450,12 +1450,13 @@ impl<'a, 'b> Texel<'a, 'b> {
fn generator_ramp(&mut self, generator: &Generator, curvature_sizes: Option<&CurvatureSizes>) -> f32 {
let coords = self.coords();
let micro = generator.micro.map_or(0.0, |micro| self.micro_detail(generator.kind, micro));
let signal = match (generator.kind, &generator.detail) {
(GeneratorKind::Curvature | GeneratorKind::EdgeWear, Some(detail)) => {
// Detail sizes are blurred copies of the mesh map, so a replaced curvature input reads at its own size only
let sizes = if generator.inputs.contains_key(&BakedMap::Curvature) { None } else { curvature_sizes };
let curvature = self.generator_input(generator, BakedMap::Curvature);
self.curvature_detail(detail, sizes, curvature)
self.curvature_detail(detail, sizes, curvature, micro)
}
(GeneratorKind::EdgeWear, None) => (self.generator_input(generator, BakedMap::Curvature).unwrap_or(0.5) - 0.5) * 2.0,
(GeneratorKind::Curvature, None) => self.generator_input(generator, BakedMap::Curvature).unwrap_or(0.5),
@@ -1465,10 +1466,8 @@ impl<'a, 'b> Texel<'a, 'b> {
(GeneratorKind::Position, _) => coords.pos.dot(Vec3::from(generator.direction).try_normalize().unwrap_or(Vec3::Y)) * 0.5 + 0.5,
(GeneratorKind::Direction, _) => coords.nrm.dot(Vec3::from(generator.direction).try_normalize().unwrap_or(Vec3::Y)) * 0.5 + 0.5,
};
let signal = match generator.micro {
Some(micro) => signal + self.micro_detail(generator.kind, micro),
None => signal,
};
let signal =
if matches!(generator.kind, GeneratorKind::Curvature | GeneratorKind::EdgeWear) && generator.detail.is_some() { signal } else { signal + micro };
let signal = if generator.noise_amount > 0.0 {
let noise = world_noise(coords.pos, generator.noise_scale, generator.seed);
if let (GeneratorKind::Curvature | GeneratorKind::EdgeWear, Some(detail)) = (generator.kind, &generator.detail) {
@@ -1520,15 +1519,19 @@ impl<'a, 'b> Texel<'a, 'b> {
}
/// The curvature of `detail`'s mode at this texel from `curvature` (None: nothing to read), with its blurred detail sizes when given.
fn curvature_detail(&self, detail: &CurvatureDetail, curvature_sizes: Option<&CurvatureSizes>, curvature: Option<f32>) -> f32 {
let combine = |edges: f32, cavities: f32| match detail.mode {
CurvatureMode::Edges => edges,
CurvatureMode::Cavities => cavities,
CurvatureMode::Both => 1.0 - (1.0 - edges) * (1.0 - cavities),
CurvatureMode::Raw => 0.5 + 0.5 * (edges - cavities),
fn curvature_detail(&self, detail: &CurvatureDetail, curvature_sizes: Option<&CurvatureSizes>, curvature: Option<f32>, micro: f32) -> f32 {
let combine = |edges: f32, cavities: f32| {
let edges = 1.0 - (1.0 - edges) * (1.0 - micro.max(0.0));
let cavities = 1.0 - (1.0 - cavities) * (1.0 + micro.min(0.0));
match detail.mode {
CurvatureMode::Edges => edges,
CurvatureMode::Cavities => cavities,
CurvatureMode::Both => 1.0 - (1.0 - edges) * (1.0 - cavities),
CurvatureMode::Raw => 0.5 + 0.5 * (edges - cavities),
}
};
// Without curvature to read the texel is flat: no edges or cavities
// Without baked curvature, only painted micro height supplies edges or cavities
let Some(curvature) = curvature else { return combine(0.0, 0.0) };
let working_size = (self.res, self.inp.gbuf.height);
@@ -3586,6 +3589,49 @@ soft {soft:?}"
assert!((with(&mut fixture, &generator) - flat).abs() < 0.02);
}
#[test]
fn curvature_modes_classify_raised_and_recessed_micro_height() {
let mut fixture = fixture();
fixture.baked = BakedMaps { resolution: 64, maps: [(BakedMap::Curvature, Arc::new(vec![0.5; 64 * 64]))].into() };
let lower = fixture.project.alloc_id();
let mut fill = Fill::default();
fill.sources.insert(Channel::Height, Source::Uniform([0.5; 3]));
fixture.project.insert_layer(0, Layer::new(lower, "Bumps", LayerKind::Fill(fill), &[Channel::Height]), None, false).unwrap();
let anchor = content_fx(&mut fixture, lower, EffectKind::Anchor);
let top = fill_layer(&mut fixture.project, [1.0; 3], 0.5);
for kind in [GeneratorKind::Curvature, GeneratorKind::EdgeWear] {
for (height, mode, responds) in [
(0.5, CurvatureMode::Edges, true),
(-0.5, CurvatureMode::Edges, false),
(0.5, CurvatureMode::Cavities, false),
(-0.5, CurvatureMode::Cavities, true),
(0.5, CurvatureMode::Both, true),
(-0.5, CurvatureMode::Both, true),
] {
if let LayerKind::Fill(fill) = &mut fixture.project.layer_mut(lower).unwrap().kind {
fill.sources.insert(Channel::Height, Source::Uniform([height; 3]));
}
let detail = CurvatureDetail { mode, weights: [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] };
let generator = Generator {
level: 0.5,
softness: 0.5,
noise_amount: 0.0,
detail: Some(detail),
micro: Some(MicroDetail { anchor, amount: 1.0 }),
..Generator::new(kind)
};
let mut mask = Mask::new(MaskBackground::Black);
mask.effects.push(Effect::new(fixture.project.alloc_id(), EffectKind::Generator(generator)));
fixture.project.layer_mut(top).unwrap().mask = Some(mask);
let value = run(&fixture, None).value(Channel::BaseColor, 5, 5)[0];
let expected = if responds { 0.75 } else { 0.5 };
assert!((value - expected).abs() < 0.02, "{kind:?} {mode:?} height {height}: {value}");
}
}
}
#[test]
fn decal_fills_cover_only_their_frame_and_image_alpha() {
let mut f = fixture();
+33 -2
View File
@@ -2197,6 +2197,12 @@ pub(crate) fn anchors_below(app: &TextureCraftApp, reader: LayerId) -> Vec<(Laye
.collect()
}
/// Anchor choices for an effect, validated against its owner even when shown through an instance
pub(crate) fn effect_anchors_below(app: &TextureCraftApp, layer: LayerId, effect: LayerId) -> Vec<(LayerId, String)> {
let owner = app.session.doc.project.effect(effect).map_or(layer, |(owner, _)| owner);
anchors_below(app, owner)
}
/// Whether anchor point `anchor` sits in a mask (it then records the mask's gray, whatever the channel).
fn anchor_in_mask(app: &TextureCraftApp, anchor: LayerId) -> bool {
let project = &app.session.doc.project;
@@ -2245,7 +2251,7 @@ mod mask_anchor_label_tests {
use texturecraft_engine::core::{Channel, LayerId};
use texturecraft_engine::execute;
use super::{anchor_label, anchors_below};
use super::{anchor_label, anchors_below, effect_anchors_below};
use crate::TextureCraftApp;
#[test]
@@ -2267,6 +2273,31 @@ mod mask_anchor_label_tests {
assert!(labels.iter().any(|(id, label)| *id == LayerId(anchor) && !label.contains("(Off)")));
assert!(!anchor_label(harness.state(), LayerId(anchor), Channel::Height).contains("(Off)"));
}
#[test]
fn inherited_effect_pickers_list_anchors_below_the_owner() {
let mut session = texturecraft_engine::sample_session(256, 64).unwrap();
let lower = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap();
let valid = execute(&mut session, "effect.add.anchor", &json!({"layer": lower})).unwrap()["effect"].as_u64().unwrap();
let source = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap();
execute(&mut session, "mask.add.black", &json!({"layer": source})).unwrap();
let fill = execute(&mut session, "effect.add.fill", &json!({"layer": source})).unwrap()["effect"].as_u64().unwrap();
let generator = execute(&mut session, "effect.add.generator", &json!({"layer": source})).unwrap()["effect"].as_u64().unwrap();
let middle = execute(&mut session, "layer.add.fill", &Value::Null).unwrap()["layer"].as_u64().unwrap();
let invalid = execute(&mut session, "effect.add.anchor", &json!({"layer": middle})).unwrap()["effect"].as_u64().unwrap();
let instance = execute(&mut session, "layer.instance", &json!({"layer": source})).unwrap()["layer"].as_u64().unwrap();
assert!(execute(&mut session, "effect.set", &json!({"effect": fill, "anchor": invalid})).is_err());
assert!(execute(&mut session, "effect.set", &json!({"effect": generator, "microAnchor": invalid})).is_err());
let harness = Harness::builder().build_eframe(|context| TextureCraftApp::new(context, session));
assert!(anchors_below(harness.state(), LayerId(instance)).iter().any(|(id, _)| *id == LayerId(invalid)));
for effect in [fill, generator] {
let choices = effect_anchors_below(harness.state(), LayerId(instance), LayerId(effect));
assert!(choices.iter().any(|(id, _)| *id == LayerId(valid)));
assert!(!choices.iter().any(|(id, _)| *id == LayerId(invalid)));
}
}
}
/// Space above a fill's Material caption, setting it apart from the projection
@@ -2571,7 +2602,7 @@ fn effect_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, effe
EffectKind::Levels(levels) => crate::levels_properties::levels_properties(app, ui, layer, effect_id, levels),
EffectKind::Fill { source, .. } => {
ui.label(caption(tr("Fill")));
let anchors = anchors_below(app, layer.id);
let anchors = effect_anchors_below(app, layer.id, effect_id);
let current = match source {
Source::Anchor { anchor, channel } => anchor_label(app, *anchor, *channel),
Source::Uniform(_) => tr("Uniform value").to_string(),
+2 -2
View File
@@ -8,7 +8,7 @@ use texturecraft_engine::core::{
};
use crate::TextureCraftApp;
use crate::dock::{anchor_entries, anchor_label, anchors_below, image_drop, image_menu, mesh_map_color};
use crate::dock::{anchor_entries, anchor_label, anchors_below, effect_anchors_below, image_drop, image_menu, mesh_map_color};
use crate::i18n::{baked_map_label, tr, trf};
use crate::theme::{Tokens, caption};
use crate::widgets::{combo, section, slider_edit};
@@ -79,7 +79,7 @@ fn micro_detail_properties(app: &mut TextureCraftApp, ui: &mut Ui, id: u64, micr
let tokens = Tokens::get(ui.ctx());
ui.label(caption(tr("Micro detail")))
.on_hover_text(tr("Painted height from an anchor point below: raised areas read as edges, recessed ones as cavities."));
let anchors = anchors_below(app, layer);
let anchors = effect_anchors_below(app, layer, LayerId(id));
let current = match micro {
Some(detail) => anchors.iter().find(|(anchor, _)| *anchor == detail.anchor).map_or_else(|| tr("missing").to_string(), |(_, label)| label.clone()),
None => tr("None").to_string(),