fix(bake): paint the skew map in place instead of copying it per stroke step; a temp folder per leaning_session

This commit is contained in:
bflatastic committed 2026-11-02 16:21:41 +07:00
1 parent 63b54f1cec
commit 5acfba00e8
2 files changed
+38 -11

No files matched your search

+17 -8
View File
@@ -654,18 +654,27 @@ fn skew_paint(s: &mut Session, p: &Value) -> Result<Value> {
if !(16..=4096).contains(&size) {
return Err(bad(cmd, "`size` is 16 to 4096"));
}
let mut map = match s.bake_meshes.skew.as_deref() {
// A constant map has no resolution to paint at: widen it, keeping its value everywhere.
Some(k) if k.values.len() > 1 => k.clone(),
Some(k) => k.resized(size as u32, size as u32).map_err(|e| bad(cmd, e.to_string()))?,
None => SkewMap::constant(0.0).resized(size as u32, size as u32).map_err(|e| bad(cmd, e.to_string()))?,
};
let (value, radius) = (f32p(p, "value").unwrap_or(1.0), f32p(p, "radius").unwrap_or(0.03));
if !(radius.is_finite() && radius > 0.0 && radius <= 1.0) {
return Err(bad(cmd, "`radius` is above 0 and at most 1 (uv units)"));
}
map.paint(&points, radius, value, f32p(p, "hardness").unwrap_or(0.5), f32p(p, "strength").unwrap_or(1.0)).map_err(|e| bad(cmd, e.to_string()))?;
s.bake_meshes.skew = Some(Arc::new(map));
let (hardness, strength) = (f32p(p, "hardness").unwrap_or(0.5), f32p(p, "strength").unwrap_or(1.0));
let stroke = |m: &mut SkewMap| m.paint(&points, radius, value, hardness, strength).map_err(|e| bad(cmd, e.to_string()));
match s.bake_meshes.skew.as_mut() {
// In place: the map is copied only while a running bake still holds it, and `paint`
// refuses a stroke before it changes a texel.
Some(k) if k.values.len() > 1 => stroke(Arc::make_mut(k))?,
// A constant map has no resolution to paint at: widen it, keeping its value everywhere.
k => {
let mut map = match k {
Some(k) => k.resized(size as u32, size as u32),
None => SkewMap::constant(0.0).resized(size as u32, size as u32),
}
.map_err(|e| bad(cmd, e.to_string()))?;
stroke(&mut map)?;
s.bake_meshes.skew = Some(Arc::new(map));
}
}
s.bake_meshes.skew_rev += 1;
s.modified = true;
Ok(meshes_json(s))
+21 -3
View File
@@ -324,8 +324,16 @@ fn leaning_session(lean: bool) -> Session {
}
let high_text = obj(&mesh, 0.1, f32::MAX, [1.0; 3]);
let mut s = Session::new_project("t", mesh, "builtin:plane", None, 256).unwrap();
let high = temp_file(if lean { "lean_high.obj" } else { "flat_high.obj" }, &high_text);
execute(&mut s, "bake.highPoly.add", &json!({ "path": high })).unwrap();
// A folder of its own per call: tests run in parallel, and another one removes the shared folder.
static CALLS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
let call = CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let dir = std::env::temp_dir().join(format!("texturecraft-lean-test-{}-{call}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let high = dir.join("high.obj");
std::fs::write(&high, high_text).unwrap();
execute(&mut s, "bake.highPoly.add", &json!({ "path": high.display().to_string() })).unwrap();
// The mesh is parsed and kept on add, so the file can go.
let _ = std::fs::remove_dir_all(&dir);
execute(
&mut s,
"bake.settings",
@@ -403,7 +411,7 @@ fn painting_skew_widens_a_map_and_bad_strokes_change_nothing() {
let r = execute(&mut s, "bake.skew.paint", &json!({ "points": [[0.5, 0.5]], "radius": 0.1, "size": 64 })).unwrap();
assert_eq!(r["skew"]["width"], 64);
let map = s.bake_meshes.skew.clone().unwrap();
assert!(map.values.iter().any(|v| *v > 0.99) && map.values.iter().any(|v| *v == 0.0));
assert!(map.values.iter().any(|v| *v > 0.99) && map.values.contains(&0.0));
let rev = s.bake_meshes.skew_rev;
// Erasing with value 0 brings it back down; the size stays.
execute(&mut s, "bake.skew.paint", &json!({ "points": [[0.5, 0.5]], "radius": 0.2, "value": 0.0, "hardness": 1.0 })).unwrap();
@@ -437,3 +445,13 @@ fn painting_skew_widens_a_map_and_bad_strokes_change_nothing() {
let back = Session::from_project_bytes(&s.to_project_bytes().unwrap()).unwrap();
assert_eq!(back.bake_meshes.skew, s.bake_meshes.skew);
}
#[test]
fn a_stroke_paints_the_skew_map_in_place() {
let mut s = plane_session();
let stroke = json!({ "points": [[0.5, 0.5]], "radius": 0.1, "size": 64 });
execute(&mut s, "bake.skew.paint", &stroke).unwrap();
let first = s.bake_meshes.skew.as_ref().map(std::sync::Arc::as_ptr);
execute(&mut s, "bake.skew.paint", &stroke).unwrap();
assert_eq!(s.bake_meshes.skew.as_ref().map(std::sync::Arc::as_ptr), first, "a stroke step doesn't copy the whole map");
}