From 35ac5bc2ea563dc2fd8b25167ec2a2613a220ce6 Mon Sep 17 00:00:00 2001 From: Rebornim <250083243+Rebornim@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:57:56 +0000 Subject: [PATCH] Generators: Position and Light read the mesh bounds, review fixes Position and Light read the mesh's own position across its bounds (an image in the Position input still overrides it): the baked Position map's normalisation is a bake setting, bounding sphere by default, which squeezes the reference's 0..1 per axis. The light dials start at 0 on the left and turn clockwise like the reference's. Commands refuse an input the generator does not read and `direction` on kinds that point no way; the detail size hint follows the sizes in use; tests check the exact blur radius, a blurred position and the axis dropdown by its section. --- crates/compose/src/eval.rs | 24 ++++++++++++-------- crates/compose/src/sections.rs | 4 ++-- crates/compose/tests/generator_sections.rs | 23 ++++++++++++------- crates/core/src/layer.rs | 12 ++++++++++ crates/engine/src/mask_cmds.rs | 9 ++++++-- crates/engine/tests/it/generator_sections.rs | 18 +++++++++++++++ crates/ui-egui/src/generator_panel.rs | 21 +++++++---------- crates/ui-egui/tests/it/generator_panel.rs | 16 ++++++++++--- 8 files changed, 89 insertions(+), 38 deletions(-) diff --git a/crates/compose/src/eval.rs b/crates/compose/src/eval.rs index 1e342957..b0ae6b29 100644 --- a/crates/compose/src/eval.rs +++ b/crates/compose/src/eval.rs @@ -2389,16 +2389,10 @@ fn curvature_sizes_in_use(generator: &Generator) -> [bool; LEVEL_RADII.len()] { return wear::levels_in_use(generator.kind, &wear); } - let detail = curvature_detail_in_use(generator).as_ref().map(CurvatureDetail::blurred_sizes_in_use).unwrap_or_default(); + let detail = generator.detail_in_use().as_ref().map(CurvatureDetail::blurred_sizes_in_use).unwrap_or_default(); std::array::from_fn(|level| detail.get(level).copied().unwrap_or(false)) } -/// The mode and detail sizes `generator` reads its curvature with: its own, the defaults on Curvature's section controls, else None -fn curvature_detail_in_use(generator: &Generator) -> Option { - let on_section = generator.kind == GeneratorKind::Curvature && generator.section_controls().is_some(); - generator.detail.clone().or_else(|| on_section.then(CurvatureDetail::default)) -} - /// The wear controls `generator` computes with: its own, Fiber Glass Edge Wear's defaults when it has none (it has no earlier model), else None fn wear_parameters(generator: &Generator) -> Option { generator.wear.or_else(|| WearParameters::defaults(generator.kind).filter(|_| generator.kind == GeneratorKind::FiberGlassEdgeWear)) @@ -3330,7 +3324,7 @@ impl<'a, 'b> Texel<'a, 'b> { if let Some(light) = &generator.light { let normal = self.generator_input_color(generator, BakedMap::WorldNormal).map_or_else(|| self.coords().nrm, |encoded| Vec3::from(encoded) * 2.0 - 1.0); - let position = self.generator_input_color(generator, BakedMap::Position).map_or_else(|| self.bounds_position(), Vec3::from); + let position = Vec3::from(self.position_input(generator)); return sections::light_value(light, normal, position, generator.invert); } if let Some(section) = generator.section_controls() { @@ -3355,6 +3349,7 @@ impl<'a, 'b> Texel<'a, 'b> { (GeneratorKind::ThinAreas, _) => 1.0 - self.generator_input(generator, BakedMap::Thickness).unwrap_or(1.0), (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, + // World Space Normals always computes with its section controls: this arm only keeps the match whole (GeneratorKind::WorldSpaceNormals, _) => coords.nrm.y * 0.5 + 0.5, }; let signal = @@ -3399,7 +3394,7 @@ impl<'a, 'b> Texel<'a, 'b> { fn section_signal(&mut self, generator: &Generator, section: &SectionParameters, curvature_sizes: Option<&CurvatureSizes>) -> f32 { match generator.kind { GeneratorKind::Curvature => { - let detail = curvature_detail_in_use(generator).unwrap_or_default(); + let detail = generator.detail_in_use().unwrap_or_default(); let sizes = if generator.inputs.contains_key(&BakedMap::Curvature) { None } else { curvature_sizes }; let curvature = self.generator_input(generator, BakedMap::Curvature); let micro = generator.micro.map_or(0.0, |micro| self.micro_detail(generator.kind, micro)); @@ -3407,17 +3402,26 @@ impl<'a, 'b> Texel<'a, 'b> { } GeneratorKind::AmbientOcclusion => self.generator_input(generator, BakedMap::AmbientOcclusion).unwrap_or(1.0), GeneratorKind::Position => { - let position = self.generator_input_color(generator, BakedMap::Position).unwrap_or_else(|| self.bounds_position().to_array()); + let position = self.position_input(generator); sections::axes_signal(position, §ion.axes) } GeneratorKind::WorldSpaceNormals => { let normal = self.generator_input_color(generator, BakedMap::WorldNormal).unwrap_or_else(|| (self.coords().nrm * 0.5 + 0.5).to_array()); sections::axes_signal(normal, §ion.axes) } + // Only the kinds with section controls reach here: this arm only keeps the match whole GeneratorKind::EdgeWear | GeneratorKind::Dirt | GeneratorKind::ThinAreas | GeneratorKind::Direction | GeneratorKind::FiberGlassEdgeWear => 0.0, } } + /// The image in `generator`'s Position input, else this texel's position across the mesh's bounds: the baked Position map is not read, its normalisation is a bake setting + fn position_input(&mut self, generator: &Generator) -> [f32; 3] { + match generator.inputs.get(&BakedMap::Position) { + Some(source) => self.source(source, &Projection::default(), Sampling::default(), ChannelKind::NormalMap), + None => self.bounds_position().to_array(), + } + } + /// This texel's world position across the mesh's bounds, 0..1 on each axis (0 where uncovered) fn bounds_position(&self) -> Vec3 { let (low, high) = self.inp.bounds; diff --git a/crates/compose/src/sections.rs b/crates/compose/src/sections.rs index a9da83bc..bee4012d 100644 --- a/crates/compose/src/sections.rs +++ b/crates/compose/src/sections.rs @@ -232,8 +232,8 @@ mod tests { assert_eq!(blur_sigma(GeneratorKind::Position, §ion), 0.0); assert_eq!(blur_sigma(GeneratorKind::WorldSpaceNormals, §ion), 72.0); assert_eq!(blur_radius_at(GeneratorKind::AmbientOcclusion, &SectionParameters { blur: 0.0, ..section }, 2048), 0); - let radius = blur_radius_at(GeneratorKind::AmbientOcclusion, &SectionParameters { blur: 1.0, ..section }, 2048) as f32; - assert!((radius * (radius + 1.0)).sqrt() - 4.5 < 0.6, "three boxes of radius {radius} spread like sigma 4.5"); + // Three boxes of radius 4 spread a texel with variance 4 × 5, the closest to sigma 4.5 squared + assert_eq!(blur_radius_at(GeneratorKind::AmbientOcclusion, &SectionParameters { blur: 1.0, ..section }, 2048), 4); } #[test] diff --git a/crates/compose/tests/generator_sections.rs b/crates/compose/tests/generator_sections.rs index 27693163..0724f4e6 100644 --- a/crates/compose/tests/generator_sections.rs +++ b/crates/compose/tests/generator_sections.rs @@ -88,6 +88,12 @@ fn with_section(kind: GeneratorKind, change: impl FnOnce(&mut SectionParameters) generator } +/// `generator` with a uniform `value` plugged into its Position input +fn plugged(mut generator: Generator, value: f32) -> Generator { + generator.inputs.insert(BakedMap::Position, Source::Uniform([value; 3])); + generator +} + fn with_light(change: impl FnOnce(&mut LightParameters)) -> Generator { let mut generator = Generator::new(GeneratorKind::Direction); let mut light = generator.light.unwrap(); @@ -159,7 +165,7 @@ fn world_space_normals_combine_the_axes_of_the_normal() { } #[test] -fn position_reads_the_mesh_bounds_without_a_bake_and_the_baked_map_with_one() { +fn position_reads_the_mesh_bounds_unless_an_image_is_plugged_in() { let mut fixture = fixture(); let points = surface(&fixture); let texels: Vec<_> = points.iter().map(|point| point.0).collect(); @@ -168,7 +174,9 @@ fn position_reads_the_mesh_bounds_without_a_bake_and_the_baked_map_with_one() { assert_close(&mask(&mut fixture, Generator::new(GeneratorKind::Position), &texels), &expected, "bounds"); fixture.baked = uniform_map(BakedMap::Position, 0.25); - let balanced = with_section(GeneratorKind::Position, |section| section.balance = 0.25); + assert_close(&mask(&mut fixture, Generator::new(GeneratorKind::Position), &texels), &expected, "a baked Position map is not read"); + + let balanced = plugged(with_section(GeneratorKind::Position, |section| section.balance = 0.25), 0.25); assert_close(&mask(&mut fixture, balanced, &texels), &vec![0.25f32.powf(0.2); texels.len()], "a power curve balance"); } @@ -183,10 +191,9 @@ fn occlusion_balance_is_linear_and_brightness_lerps() { let inverted = with_section(GeneratorKind::AmbientOcclusion, |section| section.invert = true); assert_close(&mask(&mut fixture, inverted, &texels), &vec![0.4; texels.len()], "section invert"); - fixture.baked = uniform_map(BakedMap::Position, 0.4); - let brighter = with_section(GeneratorKind::Position, |section| section.brightness = 0.5); + let brighter = plugged(with_section(GeneratorKind::Position, |section| section.brightness = 0.5), 0.4); assert_close(&mask(&mut fixture, brighter, &texels), &vec![0.7; texels.len()], "brightness 0.5 halves the way to white"); - let darker = with_section(GeneratorKind::Position, |section| section.brightness = -0.5); + let darker = plugged(with_section(GeneratorKind::Position, |section| section.brightness = -0.5), 0.4); assert_close(&mask(&mut fixture, darker, &texels), &vec![0.2; texels.len()], "brightness -0.5 halves the value"); } @@ -203,9 +210,9 @@ fn section_blur_spreads_occlusion_across_a_step() { let blurred = mask(&mut fixture, with_section(GeneratorKind::AmbientOcclusion, |section| section.blur = 16.0), &row); assert!(blurred.iter().all(|value| *value > 0.05 && *value < 0.95), "blurred across the step: {blurred:?}"); - let position = with_section(GeneratorKind::Position, |section| section.blur = 16.0); - fixture.baked = uniform_map(BakedMap::Position, 0.3); - assert_close(&mask(&mut fixture, position, &row), &vec![0.3; row.len()], "position has no section blur"); + let sharp_position = mask(&mut fixture, Generator::new(GeneratorKind::Position), &row); + let blurred_position = mask(&mut fixture, with_section(GeneratorKind::Position, |section| section.blur = 16.0), &row); + assert_close(&blurred_position, &sharp_position, "position has no section blur"); } #[test] diff --git a/crates/core/src/layer.rs b/crates/core/src/layer.rs index fedcca8a..663efd1f 100644 --- a/crates/core/src/layer.rs +++ b/crates/core/src/layer.rs @@ -1082,6 +1082,18 @@ impl Generator { self.section.or_else(|| SectionParameters::defaults(self.kind).filter(|_| self.kind == GeneratorKind::WorldSpaceNormals)) } + /// The mode and detail sizes the generator reads its curvature with: its own, the defaults on Curvature's section controls, else None + pub fn detail_in_use(&self) -> Option { + let on_section = self.kind == GeneratorKind::Curvature && self.section_controls().is_some(); + self.detail.clone().or_else(|| on_section.then(CurvatureDetail::default)) + } + + /// The mesh maps the generator reads: none for Position and Light on the earlier model, which read the mesh itself + pub fn inputs_in_use(&self) -> &'static [BakedMap] { + let reads_geometry = self.on_earlier_model() && matches!(self.kind, GeneratorKind::Position | GeneratorKind::Direction); + if reads_geometry { &[] } else { self.kind.inputs() } + } + /// Whether the generator is on the level, softness and noise it had before the reference app's controls pub fn on_earlier_model(&self) -> bool { self.wear.is_none() && self.light.is_none() && self.section_controls().is_none() && self.kind != GeneratorKind::FiberGlassEdgeWear diff --git a/crates/engine/src/mask_cmds.rs b/crates/engine/src/mask_cmds.rs index 2ed442d1..19a61f09 100644 --- a/crates/engine/src/mask_cmds.rs +++ b/crates/engine/src/mask_cmds.rs @@ -511,6 +511,11 @@ fn section_from(command: &str, params: &Value, generator: &mut Generator) -> Res return Err(bad(command, format!("{} has no `{unused}`", kind.label()))); } + let reads_direction = matches!(kind, GeneratorKind::Position | GeneratorKind::Direction); + if params.get("direction").is_some() && !reads_direction { + return Err(bad(command, format!("{} has no `direction`: only Position and Light point one way", kind.label()))); + } + if earlier { if !new_keys.is_empty() { return Err(bad( @@ -1056,8 +1061,8 @@ fn set(session: &mut Session, params: &Value) -> Result { generator.micro = micro; } if let Some((map, source)) = input { - if !generator.kind.inputs().contains(&map) { - return Err(bad(CMD, format!("this generator has no `{}` input", map.id()))); + if !generator.inputs_in_use().contains(&map) { + return Err(bad(CMD, format!("this generator does not read a `{}` input", map.id()))); } match source { diff --git a/crates/engine/tests/it/generator_sections.rs b/crates/engine/tests/it/generator_sections.rs index 60676684..6a639aaf 100644 --- a/crates/engine/tests/it/generator_sections.rs +++ b/crates/engine/tests/it/generator_sections.rs @@ -89,6 +89,10 @@ fn keys_a_kind_lacks_or_both_models_at_once_are_refused() { assert!(execute(&mut session, "effect.set", &json!({"effect": position, "topBottomBlend": "overlay"})).is_err()); assert_eq!(generator(&session, position).section, SectionParameters::defaults(GeneratorKind::Position), "a refused edit changes nothing"); + let direction = json!([1.0, 0.0, 0.0]); + assert!(execute(&mut session, "effect.set", &json!({"effect": curvature, "direction": direction})).is_err(), "Curvature points no way"); + assert!(execute(&mut session, "effect.set", &json!({"effect": occlusion, "direction": direction})).is_err(), "nor does Ambient Occlusion"); + assert!(!section_key_applies(GeneratorKind::Position, "sectionBlur")); assert!(section_key_applies(GeneratorKind::WorldSpaceNormals, "rightLeftBlend")); } @@ -117,3 +121,17 @@ fn light_takes_its_controls_and_refuses_the_global_ones() { assert_eq!(moved.light.map(|light| light.vertical), Some(60.0)); assert_eq!(moved.global, Generator::new(GeneratorKind::Direction).global, "the light controls have no global adjustments"); } + +/// Position and Light read the mesh itself on the earlier model, so they take no Position input there +#[test] +fn an_input_the_generator_does_not_read_is_refused() { + let mut session = session(); + let earlier = add(&mut session, json!({"generator": "position", "level": 0.4})); + let plug = |effect: u64| json!({"effect": effect, "input": "position", "inputValue": 0.5}); + assert!(execute(&mut session, "effect.set", &plug(earlier)).is_err()); + assert!(generator(&session, earlier).inputs.is_empty()); + + let on_section = add(&mut session, json!({"generator": "position"})); + execute(&mut session, "effect.set", &plug(on_section)).unwrap(); + assert_eq!(generator(&session, on_section).inputs.len(), 1); +} diff --git a/crates/ui-egui/src/generator_panel.rs b/crates/ui-egui/src/generator_panel.rs index effda2d1..7c6f7258 100644 --- a/crates/ui-egui/src/generator_panel.rs +++ b/crates/ui-egui/src/generator_panel.rs @@ -68,10 +68,10 @@ pub(crate) fn generator_properties(app: &mut TextureCraftApp, ui: &mut Ui, layer }); }); }); - if !inputs_in_use(generator).is_empty() { + if !generator.inputs_in_use().is_empty() { section(ui, "generator_inputs", tr("Image inputs"), true, |ui| { - for map in inputs_in_use(generator) { - input_slot(app, ui, (layer, id), *map, generator.inputs.get(map), generator.detail.is_some()); + for map in generator.inputs_in_use() { + input_slot(app, ui, (layer, id), *map, generator.inputs.get(map), generator.detail_in_use().is_some()); } }); } @@ -108,12 +108,6 @@ fn micro_detail_properties(app: &mut TextureCraftApp, ui: &mut Ui, id: u64, micr } } -/// The inputs `generator` reads: none for Position and Light on their earlier model, which read the mesh itself -fn inputs_in_use(generator: &Generator) -> &'static [BakedMap] { - let reads_geometry = generator.on_earlier_model() && matches!(generator.kind, GeneratorKind::Position | GeneratorKind::Direction); - if reads_geometry { &[] } else { generator.kind.inputs() } -} - /// What a generator kind makes, its category and its tags (read only). fn attributes(ui: &mut Ui, kind: GeneratorKind) { let tokens = Tokens::get(ui.ctx()); @@ -493,7 +487,7 @@ fn angle_row(ui: &mut Ui, id: &str, label: &str, degrees: &mut f32) -> Option Option { let tokens = Tokens::get(ui.ctx()); let (rect, response) = ui.allocate_exact_size(egui::vec2(DIAL_SIZE, DIAL_SIZE), egui::Sense::click_and_drag()); @@ -502,7 +496,7 @@ fn angle_dial(ui: &mut Ui, label: &str, degrees: &mut f32) -> Option { && let Some(pointer) = response.interact_pointer_pos() { let offset = pointer - rect.center(); - let turned = (-offset.y).atan2(offset.x).to_degrees().rem_euclid(*LightParameters::ANGLE_RANGE.end()); + let turned = (-offset.y).atan2(-offset.x).to_degrees().rem_euclid(*LightParameters::ANGLE_RANGE.end()); if offset.length() > DIAL_DEAD_ZONE && (turned - *degrees).abs() > f32::EPSILON { *degrees = turned; // Later frames of one drag share its undo step @@ -516,10 +510,11 @@ fn angle_dial(ui: &mut Ui, label: &str, degrees: &mut f32) -> Option { response.widget_info(|| egui::WidgetInfo::slider(true, f64::from(shown), label)); let painter = ui.painter(); - let radius = DIAL_SIZE * 0.5 - 1.0; + // The rim's stroke stays inside the dial + let radius = (DIAL_SIZE - DIAL_NEEDLE_WIDTH) * 0.5; painter.circle(rect.center(), radius, tokens.field, egui::Stroke::new(DIAL_NEEDLE_WIDTH, tokens.button_border)); let (sin, cos) = degrees.to_radians().sin_cos(); - let tip = rect.center() + egui::vec2(cos, -sin) * radius; + let tip = rect.center() + egui::vec2(-cos, -sin) * radius; painter.line_segment([rect.center(), tip], egui::Stroke::new(DIAL_NEEDLE_WIDTH, tokens.accent)); painter.circle_filled(rect.center(), DIAL_HUB_RADIUS, tokens.text); edit diff --git a/crates/ui-egui/tests/it/generator_panel.rs b/crates/ui-egui/tests/it/generator_panel.rs index 6d49c460..513ca72b 100644 --- a/crates/ui-egui/tests/it/generator_panel.rs +++ b/crates/ui-egui/tests/it/generator_panel.rs @@ -171,12 +171,22 @@ fn each_kind_shows_its_section_rows_and_no_earlier_ones() { let (harness, _) = harness_with(false, json!({"generator": "worldSpaceNormals"})); assert_eq!(count(&harness, "Blending mode"), 3); + for axis in ["Right to left", "Top to bottom", "Front to back"] { + assert_eq!(count(&harness, axis), 2, "{axis}: a section heading and its weight slider"); + } let (harness, _) = harness_with(false, json!({"generator": "direction"})); assert_eq!(count(&harness, "Angle"), 2, "an Angle slider beside each dial"); let (harness, _) = harness_with(false, json!({"generator": "position", "level": 0.4})); assert!(count(&harness, "Level") > 0 && count(&harness, "Balance") > 0, "the earlier model keeps its level beside the section at its defaults"); } +/// The blending mode dropdown in the Front to back section: the first dropdown below its heading, the topmost "Front to back" +fn front_to_back_dropdown<'a>(harness: &'a Harness<'_, TextureCraftApp>) -> egui_kittest::Node<'a> { + let heading = harness.query_all_by_label("Front to back").map(|node| node.rect().top()).fold(f32::INFINITY, f32::min); + let below = harness.get_all_by_role(Role::ComboBox).filter(|node| node.rect().top() > heading); + below.min_by(|a, b| a.rect().top().total_cmp(&b.rect().top())).unwrap() +} + #[test] fn each_axis_has_its_own_blending_mode_dropdown() { let (mut harness, effect) = harness_with(false, json!({"generator": "worldSpaceNormals"})); @@ -184,9 +194,9 @@ fn each_axis_has_its_own_blending_mode_dropdown() { harness.get_all_by_role(Role::ComboBox).filter(|node| node.value().as_deref() == Some("Linear Dodge (Add)")).count() }; assert_eq!(added(&harness), 3); - harness.get_all_by_role(Role::ComboBox).filter(|node| node.value().as_deref() == Some("Linear Dodge (Add)")).nth(2).unwrap().scroll_to_me(); + front_to_back_dropdown(&harness).scroll_to_me(); harness.run_steps(3); - harness.get_all_by_role(Role::ComboBox).filter(|node| node.value().as_deref() == Some("Linear Dodge (Add)")).nth(2).unwrap().click(); + front_to_back_dropdown(&harness).click(); harness.run_steps(2); harness.get_by_label("Multiply").click(); harness.run_steps(3); @@ -221,7 +231,7 @@ fn the_light_dial_turns_towards_the_pointer() { harness.drop_at(left); harness.run_steps(3); let horizontal = generator(&harness, effect).light.unwrap().horizontal; - assert!((horizontal - 180.0).abs() < 1.0, "a click left of the centre points the needle left: {horizontal}"); + assert!(!(1.0..=359.0).contains(&horizontal), "0 is on the left, as in the reference: {horizontal}"); } #[test]