mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-29 01:23:53 +01:00
80 lines
4.2 KiB
Rust
80 lines
4.2 KiB
Rust
use materialcraft_engine::{Session, core::NodeId, execute, formats, new_session};
|
|
use serde_json::{Value, json};
|
|
|
|
fn run(s: &mut Session, id: &str, params: Value) -> Value {
|
|
execute(s, id, ¶ms).unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn grouping_noise_preserves_pixels_seed_and_undo() {
|
|
for seed in [0, 42, 12345] {
|
|
let mut s = new_session();
|
|
s.preview_size = None;
|
|
run(&mut s, "graph.size", json!({"width": 64, "height": 32}));
|
|
run(&mut s, "graph.format", json!({"format": "f32"}));
|
|
run(&mut s, "graph.seed", json!({"seed": seed}));
|
|
let noise = run(&mut s, "graph.node.add", json!({"def": "pixel_processor"}))["id"].as_u64().unwrap();
|
|
let mut function = materialcraft_engine::core::FunctionGraph::default();
|
|
let maximum = function.add_constant(materialcraft_engine::core::Value::Float(1.0));
|
|
function.output = Some(function.add("rand", &[maximum], None));
|
|
run(&mut s, "graph.node.param", json!({"id": noise, "param": "function", "value": materialcraft_engine::core::Value::Function(Box::new(function))}));
|
|
run(&mut s, "graph.node.base", json!({"id": noise, "seed": 7}));
|
|
let output = NodeId(run(&mut s, "graph.node.add", json!({"def": "output", "connectFrom": {"node": noise}}))["id"].as_u64().unwrap());
|
|
run(&mut s, "graph.select", json!({"ids": [noise]}));
|
|
let before_graphs = s.pkg.graphs.clone();
|
|
let before = s.cook().image(output, 0).unwrap().clone();
|
|
run(&mut s, "graph.createFromSelection", json!({"name": "Grouped_noise"}));
|
|
|
|
// Force an actual cook instead of relying on the pre-grouping cache entry
|
|
s.cache.lock().unwrap().clear();
|
|
let after = s.cook().image(output, 0).unwrap().clone();
|
|
|
|
assert!(before.data() == after.data(), "grouping changed the material at graph seed {seed}");
|
|
assert_eq!((after.width(), after.height()), (64, 32));
|
|
assert_eq!(s.pkg.graphs[1].seed, seed);
|
|
|
|
let grouped_graphs = s.pkg.graphs.clone();
|
|
run(&mut s, "edit.undo", json!({}));
|
|
|
|
assert_eq!(s.pkg.graphs, before_graphs);
|
|
assert_eq!(s.cook().image(output, 0).unwrap().data(), before.data());
|
|
|
|
run(&mut s, "edit.redo", json!({}));
|
|
|
|
assert_eq!(s.pkg.graphs, grouped_graphs);
|
|
assert_eq!(s.cook().image(output, 0).unwrap().data(), before.data());
|
|
|
|
let bytes = formats::sbs::write_sbs(&s.pkg).unwrap();
|
|
let mut reopened = Session::new(formats::sbs::read_sbs(&bytes, "seed.sbs", &|_| None).unwrap().pkg);
|
|
reopened.preview_size = None;
|
|
let reopened_output = reopened.graph().unwrap().nodes.iter().find(|node| node.def == "output").unwrap().id;
|
|
|
|
assert_eq!(reopened.cook().image(reopened_output, 0).unwrap().data(), before.data());
|
|
run(&mut s, "edit.undo", json!({}));
|
|
for changed_seed in [5, 89] {
|
|
run(&mut s, "graph.seed", json!({"seed": changed_seed}));
|
|
run(&mut reopened, "graph.seed", json!({"seed": changed_seed}));
|
|
assert_eq!(reopened.cook().image(reopened_output, 0).unwrap().data(), s.cook().image(output, 0).unwrap().data());
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn editing_a_computed_graph_seed_preserves_the_expression_until_a_literal_is_set() {
|
|
use materialcraft_engine::core::{FunctionGraph, ParameterInheritance};
|
|
let mut session = new_session();
|
|
let mut function = FunctionGraph::default();
|
|
function.output = Some(function.add_constant(materialcraft_engine::core::Value::Int(5)));
|
|
session.pkg.graphs[0].seed_function = Some(function.clone());
|
|
run(&mut session, "graph.seed", json!({"inheritance": "relativeToParent"}));
|
|
assert_eq!(session.pkg.graphs[0].seed_mode, ParameterInheritance::RelativeToParent);
|
|
assert_eq!(session.pkg.graphs[0].seed_function.as_ref(), Some(&function));
|
|
let inspected = run(&mut session, "session.inspect", json!({}));
|
|
assert_eq!(inspected["graph"]["seedFunction"], serde_json::to_value(&function).unwrap());
|
|
run(&mut session, "graph.seed", json!({"seed": 17}));
|
|
assert_eq!(session.pkg.graphs[0].seed_function, None);
|
|
assert_eq!(session.pkg.graphs[0].seed, 17);
|
|
run(&mut session, "edit.undo", json!({}));
|
|
assert_eq!(session.pkg.graphs[0].seed_function, Some(function));
|
|
}
|