mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-21 06:50:53 +01:00
Integrate main's read limits and preserve the font, evaluator, converter, and measured preview repairs. Complete the scoped type, comment, and spacing cleanup. Reuse one validated XML document for library layout and graph reads. Associate cache maintenance completion with its submitted revision so old failures cannot consume new settings or clear requests.
82 lines
2.9 KiB
Rust
82 lines
2.9 KiB
Rust
use materialcraft_core::{PatternGraph, PatternNodeKind, Value};
|
|
use materialcraft_nodes::{CookContext, find, resolve_params};
|
|
use std::collections::BTreeMap;
|
|
|
|
fn render(depth: usize, roughness: f32, stack: bool, color: Option<[f32; 4]>) -> materialcraft_image::Image {
|
|
let mut graph = PatternGraph::with_root(PatternNodeKind::Quadrant);
|
|
for index in 0..depth {
|
|
let child = graph.add(PatternNodeKind::Quadrant).unwrap();
|
|
graph.nodes[index].children.fill(Some(child));
|
|
}
|
|
for (index, node) in graph.nodes.iter_mut().enumerate() {
|
|
node.parameters.insert("patterntype".into(), Value::Int(if stack || index == depth { 2 } else { 0 }));
|
|
node.parameters.insert("opacity".into(), Value::Float4(color.unwrap_or([1.0; 4])));
|
|
}
|
|
let definition = find("fx_map").unwrap();
|
|
let params = resolve_params(
|
|
definition,
|
|
&BTreeMap::from([
|
|
("pattern".into(), Value::PatternGraph(Box::new(graph))),
|
|
("roughness".into(), Value::Float(roughness)),
|
|
("opacity".into(), Value::Float(0.125)),
|
|
("color_mode".into(), Value::Bool(color.is_some())),
|
|
("background".into(), Value::Color([0.0; 4])),
|
|
]),
|
|
);
|
|
let context = CookContext {
|
|
format: materialcraft_core::OutputFormat::F32,
|
|
w: 64,
|
|
h: 64,
|
|
seed: 0,
|
|
tile: true,
|
|
inputs: &[],
|
|
params: ¶ms,
|
|
resource: &|_| None,
|
|
vars: &BTreeMap::new(),
|
|
};
|
|
|
|
(definition.cook)(&context).unwrap().remove(0)
|
|
}
|
|
|
|
#[test]
|
|
fn quadrant_opacity_matches_measured_depth_and_stack_values() {
|
|
// Pixel values come from an original constant-square 32-bit float diagnostic
|
|
// Empty ancestors affect depth normalization; zero roughness bypasses it
|
|
for (roughness, depth, expected) in [
|
|
(0.0, 0, 0.125),
|
|
(0.0, 1, 0.125),
|
|
(0.0, 4, 0.125),
|
|
(0.42, 0, 0.125),
|
|
(0.42, 1, 0.036971834),
|
|
(0.42, 4, 0.0022858537),
|
|
(0.5, 0, 0.125),
|
|
(0.5, 1, 0.041666668),
|
|
(0.5, 4, 0.004032258),
|
|
(1.0, 0, 0.125),
|
|
(1.0, 1, 0.0625),
|
|
(1.0, 4, 0.025),
|
|
] {
|
|
let image = render(depth, roughness, false, None);
|
|
|
|
assert!(
|
|
image.data.iter().all(|value| (*value - expected).abs() < 2e-7),
|
|
"roughness {roughness}, depth {depth}: {:?}, expected {expected}",
|
|
image.mean()
|
|
);
|
|
}
|
|
|
|
for roughness in [0.42, 1.0] {
|
|
let image = render(4, roughness, true, None);
|
|
|
|
assert!(image.data.iter().all(|value| (*value - 0.125).abs() < 2e-7), "stack roughness {roughness}: {:?}", image.mean());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn colored_patterns_premultiply_rgb_by_their_opacity_alpha() {
|
|
let image = render(0, 0.0, false, Some([0.4, 0.2, 0.1, 0.5]));
|
|
for (actual, expected) in image.get(32, 32, false).into_iter().zip([0.025, 0.0125, 0.00625, 0.0625]) {
|
|
assert!((actual - expected).abs() < 1e-7, "{actual} != {expected}");
|
|
}
|
|
}
|