diff --git a/crates/compose/src/eval.rs b/crates/compose/src/eval.rs index a1a58358..a4c50ea2 100644 --- a/crates/compose/src/eval.rs +++ b/crates/compose/src/eval.rs @@ -3463,6 +3463,7 @@ impl<'a, 'b> Texel<'a, 'b> { } let coords = self.coords(); + let earlier_micro = generator.earlier_micro().map_or(0.0, |(anchor, amount)| self.earlier_micro_detail(generator.kind, anchor, amount)); // Micro details saved on the earlier model add to its curvature and occlusion inputs too let signal = match (generator.kind, &generator.detail) { @@ -3470,7 +3471,7 @@ impl<'a, 'b> Texel<'a, 'b> { // 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 { maps.sizes.as_deref() }; let curvature = self.curvature_input(generator, maps); - self.curvature_detail(detail, sizes, curvature) + self.curvature_detail(detail, sizes, curvature, earlier_micro) } (GeneratorKind::EdgeWear, None) | (GeneratorKind::FiberGlassEdgeWear, _) => (self.curvature_input(generator, maps).unwrap_or(0.5) - 0.5) * 2.0, (GeneratorKind::Curvature, None) => self.curvature_input(generator, maps).unwrap_or(0.5), @@ -3492,6 +3493,11 @@ impl<'a, 'b> Texel<'a, 'b> { _, ) => 0.0, }; + let signal = if matches!(generator.kind, GeneratorKind::Curvature | GeneratorKind::EdgeWear) && generator.detail.is_some() { + signal + } else { + signal + earlier_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) { @@ -3519,7 +3525,7 @@ impl<'a, 'b> Texel<'a, 'b> { let detail = generator.detail_in_use().unwrap_or_default(); let sizes = if generator.inputs.contains_key(&BakedMap::Curvature) { None } else { maps.sizes.as_deref() }; let curvature = self.curvature_input(generator, maps); - self.curvature_detail(&detail, sizes, curvature) + self.curvature_detail(&detail, sizes, curvature, 0.0) } GeneratorKind::AmbientOcclusion => self.occlusion_input(generator, maps).unwrap_or(1.0), GeneratorKind::Position => { @@ -3682,6 +3688,31 @@ impl<'a, 'b> Texel<'a, 'b> { } } + /// How a micro detail saved before Micro Details moves an earlier-model generator's signal at this texel: anchor point `anchor`'s height times `amount` (0 where the anchor is not evaluated yet), an edge where raised and a cavity where recessed + fn earlier_micro_detail(&self, kind: GeneratorKind, anchor: LayerId, amount: f32) -> f32 { + let response = self.anchor_value(anchor, Channel::Height, ChannelKind::Signed)[0].clamp(-1.0, 1.0) * amount; + match kind { + GeneratorKind::Curvature | GeneratorKind::EdgeWear | GeneratorKind::AmbientOcclusion => response, + + // Dirt gathers in dents + GeneratorKind::Dirt => -response, + + // Kinds the micro detail never moved, or without an earlier model + GeneratorKind::FiberGlassEdgeWear + | GeneratorKind::ThinAreas + | GeneratorKind::Position + | GeneratorKind::Direction + | GeneratorKind::WorldSpaceNormals + | GeneratorKind::ThreeDDistance + | GeneratorKind::ThreeDLinearGradient + | GeneratorKind::UvBorderDistance + | GeneratorKind::GrayscaleConversion + | GeneratorKind::DrippingRust + | GeneratorKind::UvRandomColor + | GeneratorKind::UvChecker => 0.0, + } + } + /// The generator's curvature input at this texel with what its micro details add fn curvature_input(&mut self, generator: &Generator, maps: &GeneratorMaps) -> Option { let curvature = self.generator_input(generator, BakedMap::Curvature)?; @@ -3715,16 +3746,20 @@ 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 { - 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), + /// The curvature of `detail`'s mode at this texel from `curvature` (None: nothing to read), with its blurred detail sizes when given and an earlier micro detail's term (see [`Texel::earlier_micro_detail`]) as an edge where positive and a cavity where negative + fn curvature_detail(&self, detail: &CurvatureDetail, curvature_sizes: Option<&CurvatureSizes>, curvature: Option, earlier_micro: f32) -> f32 { + let combine = |edges: f32, cavities: f32| { + let edges = 1.0 - (1.0 - edges) * (1.0 - earlier_micro.max(0.0)); + let cavities = 1.0 - (1.0 - cavities) * (1.0 + earlier_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 curvature to read only an earlier micro detail supplies edges or cavities let Some(curvature) = curvature else { return combine(0.0, 0.0) }; let Some((baked_x, baked_y)) = self.baked_curvature_texel() else { return combine(0.0, 0.0) }; @@ -7208,6 +7243,37 @@ soft {soft:?}" } } + #[test] + fn a_micro_detail_saved_before_micro_details_renders_as_saved_on_the_earlier_model() { + const SAVED_AMOUNT: f32 = 0.25; + const FLAT_CURVATURE: f32 = 0.4; + let colors = |height: f32, curvature: f32, micro: bool| -> Vec { + let mut fixture = fixture(); + fixture.baked = BakedMaps { resolution: 64, maps: [(BakedMap::Curvature, Arc::new(vec![curvature; 64 * 64]))].into() }; + let low = fixture.project.alloc_id(); + let mut fill = Fill::default(); + fill.sources.insert(Channel::Height, Source::Uniform([height; 3])); + fixture.project.insert_layer(0, Layer::new(low, "Flat", LayerKind::Fill(fill), &[Channel::Height]), None, false).unwrap(); + let anchor = content_fx(&mut fixture, low, EffectKind::Anchor); + let top = fill_layer(&mut fixture.project, [1.0; 3], 0.5); + let earlier = Generator { section: None, detail: None, level: 0.5, softness: 0.5, noise_amount: 0.0, ..Generator::new(GeneratorKind::Curvature) }; + let micro = MicroDetails::of_anchor(GeneratorKind::Curvature, anchor, SAVED_AMOUNT).filter(|_| micro).map(Box::new); + let mut mask = Mask::new(MaskBackground::Black); + mask.effects.push(Effect::new(LayerId(900), EffectKind::Generator(Generator { micro, ..earlier }))); + fixture.project.layer_mut(top).unwrap().mask = Some(mask); + let composite = run(&fixture, None); + (0..64 * 64).map(|index| composite.value(Channel::BaseColor, index % 64, index / 64)[0]).collect() + }; + + // The saved micro detail added the anchor's height times its amount to the curvature signal, even where the height is flat + for height in [0.0, 0.3, 0.6, 1.0] { + let saved = colors(0.0, FLAT_CURVATURE + height * SAVED_AMOUNT, false); + let loaded = colors(height, FLAT_CURVATURE, true); + let largest_change = saved.iter().zip(&loaded).map(|(a, b)| (a - b).abs()).fold(0.0, f32::max); + assert!(largest_change < 1e-3, "height {height}: {largest_change}"); + } + } + #[test] fn fills_inside_folders_take_the_folders_scale_and_rotation_on_top_of_their_own() { // Nested folders (x2 turned 30, then x1.5 turned -10) around a procedural fill with a procedural mask fill diff --git a/crates/core/src/layer.rs b/crates/core/src/layer.rs index 2e4371b5..ef7a535c 100644 --- a/crates/core/src/layer.rs +++ b/crates/core/src/layer.rs @@ -1274,9 +1274,18 @@ impl Generator { self.inputs.values().chain(self.texture.as_ref().and_then(|texture| texture.source.as_ref())).chain(self.grunge.as_ref()).chain(micro) } - /// The micro details as saved, when the generator computes with them: with a slot switched on, also on the earlier model where an anchor's micro detail saved before Micro Details reads (bring them into range before use: saved files are not trusted) + /// The micro details as saved, when the generator computes with them: with a slot switched on, except a micro detail saved before Micro Details on a generator still on the earlier model, which [`Generator::earlier_micro`] reads (bring them into range before use: saved files are not trusted) pub fn micro_in_use(&self) -> Option<&MicroDetails> { - self.micro.as_deref().filter(|micro| micro.in_use(self.kind)) + self.micro.as_deref().filter(|micro| micro.in_use(self.kind) && !(micro.earlier && self.on_earlier_model())) + } + + /// The anchor point and amount (0..1) of a micro detail saved before Micro Details, while the generator is still on the earlier model: its signal adds that anchor's height times the amount, as it did when saved + pub fn earlier_micro(&self) -> Option<(LayerId, f32)> { + let micro = self.micro.as_deref().filter(|micro| micro.earlier && micro.in_use(self.kind) && micro.height && self.on_earlier_model())?; + let Some(Source::Anchor { anchor, .. }) = µ.height_source else { return None }; + + let amount = micro.height_intensity / MicroDetails::HEIGHT_INTENSITY_PER_AMOUNT; + Some((*anchor, if amount.is_finite() { amount.clamp(0.0, 1.0) } else { 0.0 })) } /// Whether the generator is on the level, softness and noise it had before the reference app's controls @@ -2832,6 +2841,25 @@ mod tests { assert!(without.micro.is_none(), "a kind without micro details drops it"); } + #[test] + fn a_micro_detail_saved_on_an_anchor_keeps_its_earlier_reading_until_the_generator_leaves_the_earlier_model() { + // Saved before the reference's controls too: on the earlier model + let earlier = Generator { section: None, ..Generator::new(GeneratorKind::Curvature) }; + let mut saved = serde_json::to_value(earlier).unwrap(); + saved["micro"] = serde_json::json!({"anchor": 9, "amount": 0.25}); + let generator: Generator = serde_json::from_value(saved).unwrap(); + + assert_eq!(generator.earlier_micro(), Some((LayerId(9), 0.25))); + assert!(generator.micro_in_use().is_none(), "the earlier reading replaces the Micro Height's curvature"); + + let reloaded: Generator = serde_json::from_value(serde_json::to_value(&generator).unwrap()).unwrap(); + assert_eq!(reloaded.earlier_micro(), Some((LayerId(9), 0.25)), "saving again keeps the earlier reading"); + + let on_sections = Generator { section: SectionParameters::defaults(GeneratorKind::Curvature), ..generator }; + assert_eq!(on_sections.earlier_micro(), None); + assert!(on_sections.micro_in_use().is_some(), "on the reference's controls the anchor reads as a Micro Height"); + } + #[test] fn compare_masks_saved_before_sources_compare_the_mask_with_the_constant_hard() { let saved = r#"{"Compare":{"op":"GreaterThan","value":0.5,"tolerance":0.1}}"#; diff --git a/crates/core/src/layer/micro_details.rs b/crates/core/src/layer/micro_details.rs index 186f3af4..462cfa0d 100644 --- a/crates/core/src/layer/micro_details.rs +++ b/crates/core/src/layer/micro_details.rs @@ -29,6 +29,9 @@ pub struct MicroDetails { /// The micro normal image (None: an empty slot) #[serde(default, skip_serializing_if = "Option::is_none")] pub normal_source: Option, + /// Saved as the micro detail before Micro Details (an anchor's height times an amount): a generator still on the earlier model adds it that way, see [`super::Generator::earlier_micro`] + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub earlier: bool, } impl MicroDetails { @@ -37,7 +40,7 @@ impl MicroDetails { /// Height Details Intensity per unit of an anchor's micro detail amount (0..1): the earlier default amount, 0.5, reads at the default intensity pub const HEIGHT_INTENSITY_PER_AMOUNT: f32 = 2.0; - /// The micro details reading anchor point `anchor`'s height as their Micro Height at `amount` (0..1) on a `kind` generator, None for kinds without them + /// The micro detail saved before Micro Details, anchor point `anchor`'s height at `amount` (0..1), as Micro Details on a `kind` generator (None for kinds without them): the anchor is the Micro Height, `amount` its intensity pub fn of_anchor(kind: GeneratorKind, anchor: LayerId, amount: f32) -> Option { let amount = if amount.is_finite() { amount.clamp(0.0, 1.0) } else { 0.0 }; let source = Source::Anchor { anchor, channel: Channel::Height, ramp: Vec::new() }; @@ -46,6 +49,7 @@ impl MicroDetails { height: true, height_intensity: amount * MicroDetails::HEIGHT_INTENSITY_PER_AMOUNT, height_source: Some(source), + earlier: true, ..MicroDetails::defaults(kind)? }; Some(micro.clamped(kind)) @@ -64,6 +68,7 @@ impl MicroDetails { ao_depth: 0.05, height_source: None, normal_source: None, + earlier: false, }; match kind { GeneratorKind::Curvature | GeneratorKind::AmbientOcclusion => Some(shared), diff --git a/crates/engine/src/anchored_micro_edit.rs b/crates/engine/src/anchored_micro_edit.rs index 75d2eb54..f509a525 100644 --- a/crates/engine/src/anchored_micro_edit.rs +++ b/crates/engine/src/anchored_micro_edit.rs @@ -42,9 +42,10 @@ impl AnchoredMicroEdit { Ok(Some(AnchoredMicroEdit { anchor, amount })) } - /// The edit made on `generator`: an anchor switches the micro details and their Micro Height on and reads it there + /// The edit made on `generator`: an anchor switches the micro details and their Micro Height on and reads it there; a generator on the earlier model reads it as these keys did before Micro Details pub fn apply(self, command: &str, generator: &mut Generator) -> Result<()> { let kind = generator.kind; + let earlier = generator.on_earlier_model(); let defaults = MicroDetails::defaults(kind); match (self.anchor, &defaults) { (Some(None), _) => { @@ -66,6 +67,9 @@ impl AnchoredMicroEdit { micro.height_intensity = amount * MicroDetails::HEIGHT_INTENSITY_PER_AMOUNT; **micro = micro.as_ref().clone().clamped(kind); } + if let Some(micro) = generator.micro.as_mut() { + micro.earlier = earlier; + } Ok(()) } } diff --git a/crates/engine/src/micro_params.rs b/crates/engine/src/micro_params.rs index 7e0ed083..bd62a209 100644 --- a/crates/engine/src/micro_params.rs +++ b/crates/engine/src/micro_params.rs @@ -60,6 +60,7 @@ pub fn micro_from(command: &str, params: &Value, generator: &mut Generator, earl } let micro = generator.micro.get_or_insert_with(|| Box::new(defaults)); + micro.earlier = false; for (key, value) in [("useMicroDetails", &mut micro.enabled), ("microHeight", &mut micro.height), ("microNormal", &mut micro.normal)] { if let Some(given) = boolp(params, key) { *value = given; diff --git a/crates/engine/tests/it/content_effects.rs b/crates/engine/tests/it/content_effects.rs index b8770541..278bfc04 100644 --- a/crates/engine/tests/it/content_effects.rs +++ b/crates/engine/tests/it/content_effects.rs @@ -211,6 +211,28 @@ fn generators_read_an_anchor_height_as_their_micro_height() { assert_eq!(back.doc.project, session.doc.project); } +#[test] +fn the_earlier_micro_keys_keep_an_earlier_model_generator_on_the_earlier_micro_detail() { + let mut session = session(); + let low = id(execute(&mut session, "layer.add.fill", &json!({"height": 0.5})).unwrap(), "layer"); + let anchor = id(execute(&mut session, "effect.add.anchor", &json!({"layer": low})).unwrap(), "effect"); + let top = id(execute(&mut session, "layer.add.fill", &json!({"color": [1.0, 1.0, 1.0]})).unwrap(), "layer"); + execute(&mut session, "mask.add.black", &json!({"layer": top})).unwrap(); + let earlier_micro = |session: &Session, generator: u64| match session.doc.project.effect(LayerId(generator)).map(|(_, effect)| &effect.kind) { + Some(EffectKind::Generator(generator)) => generator.earlier_micro(), + _ => None, + }; + + // `level` keeps the generator on the earlier model, where these keys read the anchor's height as before Micro Details + let generator = id(execute(&mut session, "effect.add.generator", &json!({"layer": top, "generator": "curvature", "level": 0.5})).unwrap(), "effect"); + execute(&mut session, "effect.set", &json!({"effect": generator, "microAnchor": anchor, "microAmount": 0.25})).unwrap(); + assert_eq!(earlier_micro(&session, generator), Some((LayerId(anchor), 0.25))); + + // A Micro Details control moves it onto the reference's controls and the anchor's curvature + execute(&mut session, "effect.set", &json!({"effect": generator, "microHeightIntensity": 1.0})).unwrap(); + assert_eq!(earlier_micro(&session, generator), None); +} + #[test] fn anchor_effects_added_through_instance_validate_against_source() { let mut session = session();