PuppetShape.Crease: per-edge half turn between facet normals, from the mesh's adjacency
PuppetField.Mesh keeps net-vertex indices per emitted triangle, matches each edge by its two indices and gives each side half the turn to the neighbour's normal (atan2 of the bisector in the facet's frame), positive where convex. Edges not shared by exactly two triangles, and folds where the two sides disagree on which way the edge turns, get 0. PuppetChecks measures it independently by welding corners by position: symmetric on both sides, half the facet angle, signed by convexity, zero on loose edges and folds. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
746bccdc27
commit
56a5a53c35
2 changed files with 120 additions and 6 deletions
|
|
@ -63,6 +63,13 @@ public sealed class PuppetShape
|
|||
public required int[] BoneB { get; init; }
|
||||
public required float[] WeightA { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// One entry per triangle corner slot, 3t+k = edge k of triangle t, the edge opposite corner k: half the turn from this facet's
|
||||
/// normal to the neighbour's across that edge, in radians, positive where the edge is convex. An edge not shared by exactly two
|
||||
/// triangles has 0, and so has a fold (two triangles lying over each other, which the Newton step can make). Both sides of an edge carry the same value, so the shader turns both facets to one bisector.
|
||||
/// </summary>
|
||||
public required float[] Crease { get; init; }
|
||||
|
||||
/// <summary>How far a skinned vertex can lie from its bone's origin.</summary>
|
||||
public required float Reach { get; init; }
|
||||
|
||||
|
|
@ -175,29 +182,34 @@ public static class PuppetField
|
|||
|
||||
var pos = new List<float3>(); var nor = new List<float3>();
|
||||
var ba = new List<int>(); var bb = new List<int>(); var wa = new List<float>();
|
||||
void Triangle(float3 a, float3 b, float3 c)
|
||||
var corner = new List<int>();
|
||||
void Triangle(int ia, int ib, int ic)
|
||||
{
|
||||
var a = v[ia]; var b = v[ib]; var c = v[ic];
|
||||
var cross = math.cross(b - a, c - a);
|
||||
float len = math.length(cross);
|
||||
if (len < 1e-12f) return;
|
||||
var g = f.Eval((a + b + c) / 3f, out _, out _, out _);
|
||||
// Godot's front face is clockwise: the counter-clockwise normal points into the body.
|
||||
if (math.dot(cross, new float3(g.x, g.y, g.z)) > 0) { (b, c) = (c, b); cross = -cross; }
|
||||
if (math.dot(cross, new float3(g.x, g.y, g.z)) > 0) { (b, c) = (c, b); (ib, ic) = (ic, ib); cross = -cross; }
|
||||
var normal = -cross / len;
|
||||
foreach (var q in new[] { a, b, c })
|
||||
{
|
||||
f.Eval(q, out int bone, out int other, out float w);
|
||||
pos.Add(q); nor.Add(normal); ba.Add(bone); bb.Add(other); wa.Add(w);
|
||||
}
|
||||
corner.Add(ia); corner.Add(ib); corner.Add(ic);
|
||||
}
|
||||
for (int i = 0; i < nets.Quads.Length; i += 4)
|
||||
{
|
||||
var a = v[nets.Quads[i]]; var b = v[nets.Quads[i + 1]]; var c = v[nets.Quads[i + 2]]; var d = v[nets.Quads[i + 3]];
|
||||
int ia = (int)nets.Quads[i], ib = (int)nets.Quads[i + 1], ic = (int)nets.Quads[i + 2], id = (int)nets.Quads[i + 3];
|
||||
var a = v[ia]; var b = v[ib]; var c = v[ic]; var d = v[id];
|
||||
// Split on the flatter diagonal.
|
||||
bool ac = math.dot(Unit(math.cross(b - a, c - a)), Unit(math.cross(c - a, d - a))) >= math.dot(Unit(math.cross(b - a, d - a)), Unit(math.cross(c - b, d - b)));
|
||||
if (ac) { Triangle(a, b, c); Triangle(a, c, d); }
|
||||
else { Triangle(a, b, d); Triangle(b, c, d); }
|
||||
if (ac) { Triangle(ia, ib, ic); Triangle(ia, ic, id); }
|
||||
else { Triangle(ia, ib, id); Triangle(ib, ic, id); }
|
||||
}
|
||||
var crease = Creases(corner, pos, nor);
|
||||
|
||||
float maxBall = 0;
|
||||
for (int i = 0; i < n; i++) maxBall = MathF.Max(maxBall, f.BallR[i]);
|
||||
|
|
@ -208,11 +220,48 @@ public static class PuppetField
|
|||
return new PuppetShape
|
||||
{
|
||||
Ids = ids, SpreadBind = t, Joints = js,
|
||||
Positions = pos.ToArray(), Normals = nor.ToArray(), BoneA = ba.ToArray(), BoneB = bb.ToArray(), WeightA = wa.ToArray(),
|
||||
Positions = pos.ToArray(), Normals = nor.ToArray(), BoneA = ba.ToArray(), BoneB = bb.ToArray(), WeightA = wa.ToArray(), Crease = crease,
|
||||
Reach = 2 * reach + 2 * maxBall + smin,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Each triangle edge is matched to its neighbour by the two net-vertex indices it joins. An edge that exactly two triangles share
|
||||
/// gets, on each side, half the turn from this facet's normal to the other's: with u the edge's in-plane unit normal pointing away
|
||||
/// from the opposite corner and b the normalized sum of the two facet normals, atan2(b.u, b.n). Any other edge gets 0, and so does a fold.
|
||||
/// </summary>
|
||||
private static float[] Creases(List<int> corner, List<float3> pos, List<float3> nor)
|
||||
{
|
||||
var sides = new Dictionary<(int, int), List<int>>();
|
||||
for (int s = 0; s < corner.Count; s++)
|
||||
{
|
||||
int t = s / 3, k = s % 3;
|
||||
int p = corner[3 * t + (k + 1) % 3], q = corner[3 * t + (k + 2) % 3];
|
||||
var key = p < q ? (p, q) : (q, p);
|
||||
if (!sides.TryGetValue(key, out var list)) sides[key] = list = new List<int>(2);
|
||||
list.Add(s);
|
||||
}
|
||||
var crease = new float[corner.Count];
|
||||
foreach (var list in sides.Values)
|
||||
{
|
||||
if (list.Count != 2) continue;
|
||||
foreach (int s in list)
|
||||
{
|
||||
int t = s / 3, k = s % 3, other = list[0] == s ? list[1] : list[0];
|
||||
var n = nor[3 * t]; var m = nor[3 * (other / 3)];
|
||||
var p = pos[3 * t + (k + 1) % 3]; var q = pos[3 * t + (k + 2) % 3]; var r = pos[3 * t + k];
|
||||
var u = Unit(math.cross(Unit(q - p), n));
|
||||
if (math.dot(u, r - p) > 0) u = -u;
|
||||
var b = Unit(n + m);
|
||||
if (math.lengthsq(b) == 0) continue;
|
||||
crease[s] = MathF.Atan2(math.dot(b, u), math.dot(b, n));
|
||||
}
|
||||
// A fold, two triangles lying over each other, has no edge to bevel: the sides disagree on which way it turns.
|
||||
if (crease[list[0]] * crease[list[1]] < 0) crease[list[0]] = crease[list[1]] = 0;
|
||||
}
|
||||
return crease;
|
||||
}
|
||||
|
||||
private static float3 Unit(float3 v)
|
||||
{
|
||||
float l = math.length(v);
|
||||
|
|
|
|||
|
|
@ -121,6 +121,71 @@ public static class PuppetChecks
|
|||
Check(worstRatio <= StretchLimit, $"every joint bent up to 45 degrees stretches an edge {worstRatio:F2}x its bind length, over {StretchLimit}x");
|
||||
Check(worstLength <= 0.17f, $"every joint bent up to 45 degrees makes a skinned edge {worstLength:F3} m long, over 0.17 m");
|
||||
Check(worstLength90 <= 0.25f, $"every joint bent up to 90 degrees makes a skinned edge {worstLength90:F3} m long, over 0.25 m");
|
||||
|
||||
Creases(puppet, Check);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The bevel's creases, measured without PuppetField's indices: corners are welded by position and each edge is matched by its two
|
||||
/// welded corners. An edge two triangles share carries the same crease on both sides, half the angle between the facet normals,
|
||||
/// positive exactly where the neighbour's far corner lies behind this facet's plane; every other edge carries 0.
|
||||
/// </summary>
|
||||
private static void Creases(PuppetShape shape, Action<bool, string> check)
|
||||
{
|
||||
var id = new Dictionary<(long, long, long), int>();
|
||||
var corner = new int[shape.Positions.Length];
|
||||
for (int i = 0; i < corner.Length; i++)
|
||||
{
|
||||
var p = shape.Positions[i];
|
||||
var key = ((long)Math.Round(p.x * 1e5), (long)Math.Round(p.y * 1e5), (long)Math.Round(p.z * 1e5));
|
||||
if (!id.TryGetValue(key, out corner[i])) id[key] = corner[i] = id.Count;
|
||||
}
|
||||
var sides = new Dictionary<(int, int), List<int>>();
|
||||
for (int s = 0; s < corner.Length; s++)
|
||||
{
|
||||
int t = s / 3, k = s % 3, p = corner[3 * t + (k + 1) % 3], q = corner[3 * t + (k + 2) % 3];
|
||||
var key = p < q ? (p, q) : (q, p);
|
||||
if (!sides.TryGetValue(key, out var list)) sides[key] = list = new List<int>();
|
||||
list.Add(s);
|
||||
}
|
||||
check(shape.Crease.Length == shape.Positions.Length, $"the puppet has {shape.Crease.Length} creases for {shape.Positions.Length} triangle corners");
|
||||
if (shape.Crease.Length != shape.Positions.Length) return;
|
||||
int shared = 0, creased = 0, folds = 0, badFold = 0, asymmetric = 0, notHalf = 0, badSign = 0, loose = 0, badLoose = 0;
|
||||
foreach (var list in sides.Values)
|
||||
{
|
||||
if (list.Count != 2)
|
||||
{
|
||||
loose++;
|
||||
if (list.Any(s => shape.Crease[s] != 0)) badLoose++;
|
||||
continue;
|
||||
}
|
||||
shared++;
|
||||
int a = list[0], b = list[1];
|
||||
float3 na = shape.Normals[3 * (a / 3)], nb = shape.Normals[3 * (b / 3)];
|
||||
float angle = MathF.Atan2(math.length(math.cross(na, nb)), math.dot(na, nb));
|
||||
if (angle < 1e-2f) continue; // a flat edge has no convexity to speak of, and its crease is below the float noise of the sign
|
||||
// Convex from a side: the neighbour's far corner (the corner opposite the shared edge) lies behind this facet's plane.
|
||||
bool convexA = math.dot(na, shape.Positions[b] - shape.Positions[3 * (a / 3) + (a % 3 + 1) % 3]) < 0;
|
||||
bool convexB = math.dot(nb, shape.Positions[a] - shape.Positions[3 * (b / 3) + (b % 3 + 1) % 3]) < 0;
|
||||
if (convexA != convexB)
|
||||
{
|
||||
// The triangles lie over each other: there is no edge to bevel.
|
||||
folds++;
|
||||
if (shape.Crease[a] != 0 || shape.Crease[b] != 0) badFold++;
|
||||
continue;
|
||||
}
|
||||
creased++;
|
||||
if (MathF.Abs(shape.Crease[a] - shape.Crease[b]) > 1e-5f) asymmetric++;
|
||||
if (MathF.Abs(MathF.Abs(shape.Crease[a]) - 0.5f * angle) > 1e-4f || MathF.Abs(MathF.Abs(shape.Crease[b]) - 0.5f * angle) > 1e-4f) notHalf++;
|
||||
if ((shape.Crease[a] > 0) != convexA || (shape.Crease[b] > 0) != convexB) badSign++;
|
||||
}
|
||||
check(shared > 1000 && creased > 100, $"the crease check is vacuous: {shared} shared edges, {creased} turning more than a hundredth of a radian");
|
||||
check(folds < shared / 100, $"{folds} of {shared} shared edges are folds, over one in a hundred");
|
||||
check(badFold == 0, $"{badFold} of {folds} fold edges carry a crease");
|
||||
check(asymmetric == 0, $"{asymmetric} of {shared} shared edges carry different creases on their two sides");
|
||||
check(notHalf == 0, $"{notHalf} of {shared} shared edges have a crease that is not half the angle between the facet normals");
|
||||
check(badSign == 0, $"{badSign} of {creased} turning edges have a crease whose sign does not match the edge's convexity");
|
||||
check(badLoose == 0, $"{badLoose} of {loose} edges not shared by exactly two triangles carry a crease");
|
||||
}
|
||||
|
||||
/// <summary>The mesh's connected pieces with coincident triangle corners welded.</summary>
|
||||
|
|
|
|||
Loading…
Reference in a new issue