mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-21 23:10:43 +01:00
changeMesh: keep the painted pixels when the UV layout and topology are unchanged
preserve_strokes sampled the nearest world-space point, so a mesh moved a little along its surface with the same indices and UVs shifted existing marks, and texels outside reach were cleared. An unchanged layout now skips the reprojection. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0791e9fe35
commit
d180ee3112
1 file changed
+35
-1
@@ -17,8 +17,13 @@ const REACH: f32 = 0.05;
|
||||
|
||||
/// Re-project the painted pixels of `next` (a session on the reimported mesh holding `old`'s
|
||||
/// document) from `old`'s UV layout. Returns how many texels found the old surface;
|
||||
/// a texture set where none does keeps its pixels as they are.
|
||||
/// a texture set where none does keeps its pixels as they are. Nothing is re-projected when the
|
||||
/// UV layout and topology are unchanged (same triangles, materials and UVs): the pixels already
|
||||
/// sit where they belong, however far the mesh moved.
|
||||
pub(crate) fn preserve_strokes(old: &Session, next: &mut Session) -> usize {
|
||||
if same_layout(&old.mesh, &next.mesh) {
|
||||
return 0;
|
||||
}
|
||||
let reach = REACH * 2.0 * old.mesh.radius().max(next.mesh.radius());
|
||||
let mut found = 0;
|
||||
for ts in 0..next.set_count() {
|
||||
@@ -69,6 +74,12 @@ pub(crate) fn preserve_strokes(old: &Session, next: &mut Session) -> usize {
|
||||
found
|
||||
}
|
||||
|
||||
/// Same index buffer, same UVs per vertex and the same material ranges.
|
||||
fn same_layout(a: &texturecraft_geom::Mesh, b: &texturecraft_geom::Mesh) -> bool {
|
||||
let ranges = |m: &texturecraft_geom::Mesh| m.submeshes.iter().map(|s| (s.material, s.start, s.count)).collect::<Vec<_>>();
|
||||
a.indices == b.indices && a.uvs == b.uvs && ranges(a) == ranges(b)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -129,4 +140,27 @@ mod tests {
|
||||
let img = next.doc.pixels.layer(id, Channel::BaseColor).unwrap();
|
||||
assert_eq!(img.get(10, 40)[3], 65535, "the painted texel is still there");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_translated_mesh_with_the_same_uvs_keeps_every_pixel() {
|
||||
let mut s = Session::new_project("p", primitives::builtin("plane").unwrap(), "builtin:plane", None, 128).unwrap();
|
||||
let id = s.doc.project.texture_sets[0].stack[0];
|
||||
let img = s.doc.pixels.layer_mut(id, Channel::BaseColor, (128, 128));
|
||||
for x in 0..128 {
|
||||
img.set(x, 40, &[65535, 0, 0, 65535]);
|
||||
}
|
||||
// Slid along its own surface by a fraction of the plane: half of it still overlaps the old one.
|
||||
let mut mesh = (*s.mesh).clone();
|
||||
let shift = mesh.radius() * 0.5;
|
||||
for p in &mut mesh.positions {
|
||||
p.x += shift;
|
||||
}
|
||||
let mut next = Session::new(s.doc.clone(), mesh, None).unwrap();
|
||||
assert_eq!(preserve_strokes(&s, &mut next), 0);
|
||||
let img = next.doc.pixels.layer(id, Channel::BaseColor).unwrap();
|
||||
for x in 0..128 {
|
||||
assert_eq!(img.get(x, 40)[3], 65535, "texel {x} of the painted row moved");
|
||||
assert_eq!(img.get(x, 41)[3], 0, "texel {x} of the next row got paint");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user