diff --git a/README.md b/README.md index 2d70445..adaaa79 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Synaptic Induction Chamber, the dummy strapped to a stool with its right hand in input only. Godot's physics engine is set to Dummy; each 120 Hz physics frame the client calls `BodyPainBoxSession.Step()` once and draws Ymir's lanes: one pose-carrying node per stable id, and the body drawn as one skinned SDF mesh (a surface-nets isosurface of the shape lanes, limbs smooth-blended into the body they hang from and -joined by a ball at each joint, built once at startup and rebuilt only when the bodies or joints change; flat-shaded). +joined by a ball at each joint, built once at startup and rebuilt only when the bodies or joints change; flat-shaded, each edge softened by a 4 mm bevel: `Look.BevelWidth`, the facet normal tilted to the edge's bisector in the fragment shader from per-edge creases `PuppetField` derives from the mesh). Needs the .NET 8+ SDK, cmake and a C compiler (MinGW gcc on Windows), the sibling Lodur, Ymir (with joint origin lanes, ABI 8) and CultLib checkouts (`..\Lodur`, `..\Ymir`, `..\CultLib`, or `LODUR_ROOT`, `YMIR_ROOT` and `CULTLIB_ROOT`; CultLib must be a clean checkout of 42dce361, the revision Ymir admits, or the build refuses), and Godot 4.7.2 mono. @@ -84,8 +84,8 @@ Exports need export templates (not installed here yet), so the client runs from The smoke has no renderer. `RenderCheck.tscn` is the check that does: it poses the puppet in a bent pose under a moved parent, renders one frame (gl_compatibility, the client's renderer), and compares the drawn silhouette with the mesh skinned on the CPU (intersection over union, at -least 0.999: a correct render measures 1.0000, and a forearm bone 10 degrees off, a bone 2 cm off and hard weights fail), and checks the lit puppet is not clipped to white and that the chamber's light still lights it (a glow of 0 fails). Add `-- --png=` to keep the frame. It needs a display. -Known gaps: the silhouette does not see the shading normal (a puppet whose NORMAL is left unskinned passes) or the culling box (a 1 cm `CustomAabb` passes). +least 0.999: a correct render measures 1.0000, and a forearm bone 10 degrees off, a bone 2 cm off and hard weights fail), and checks the lit puppet is not clipped to white and that the chamber's light still lights it (a glow of 0 fails). It also draws the shading normal close up on the torso (the soft bevel) and compares it with a CPU reference: 90th percentile error at most 2 degrees, and 1.5 across the bevel band (a bevel that is off, unskinned normals or the wrong width fail). Add `-- --png=` or `-- --normals-png=` to keep a frame. It needs a display. +Known gap: the culling box (a 1 cm `CustomAabb` passes). ### Modeled diff --git a/client/tests/RenderCheck.cs b/client/tests/RenderCheck.cs index 5da8dc2..574610b 100644 --- a/client/tests/RenderCheck.cs +++ b/client/tests/RenderCheck.cs @@ -12,7 +12,11 @@ namespace BadDay.Client.Tests; /// puppet alone on black, and compares the rendered silhouette with the silhouette of the same mesh skinned on the CPU from the /// carriers and the spread bind (not from anything Puppet computed) and projected through the camera. Intersection over union must be /// at least . It also looks at the light, with the chamber near full glow: the puppet's faces must not clip to white and must differ from each other, and the chamber's light must lift the puppet over the same frame with that light off (so the glow is bounded from below as well). -/// godot --path client res://tests/RenderCheck.tscn [-- --png=path]; exit code 0 passes, 1 fails. It needs a window, so it is +/// It also reads the shading normal the shader draws (ShowNormals: the view-space normal as emission) close up on the torso and compares it +/// pixel by pixel with a CPU reference: the skinned facet normal tilted toward each edge's bisector within , from +/// a depth raster of the skinned triangles, the shape's creases and the edge distances from barycentrics. The 90th percentile error over +/// the interior pixels must be at most degrees and the median over the bevel band at most . +/// godot --path client res://tests/RenderCheck.tscn [-- --png=path] [-- --normals-png=path]; exit code 0 passes, 1 fails. It needs a window, so it is /// not part of the headless smoke. /// public partial class RenderCheck : Node @@ -21,8 +25,7 @@ public partial class RenderCheck : Node /// A correct render measures 1.0000 in this pose, on every run so far, so the floor sits at the measurement's noise. Soul's /// gross skinning mutants (a flipped weight, no inverse bind, the creation pose as the bind, an inverted CUSTOM1 weight, an /// ignored parent) measure 0.10 to 0.61; the localized ones (a forearm bone 10 degrees off 0.982, a bone 2 cm off 0.973, hard - /// weights 0.998) were under a floor of 0.95 and only fail at this one. Gaps that remain: the silhouette cannot see the shading - /// normal (unskinned normals measure 1.0000) or the culling box (the puppet stays on screen with a 1 cm box). + /// weights 0.998) were under a floor of 0.95 and only fail at this one. Gap that remains: the culling box (the puppet stays on screen with a 1 cm box). /// private const float MinIou = 0.999f; @@ -35,6 +38,16 @@ public partial class RenderCheck : Node /// The chamber's glow must light the puppet: with the light on, the mean level of the puppet's pixels must exceed the same frame with the light off by at least this many of 255 levels (the shipped light lifts it 9.8). A glow of 0 fails. private const float MinGlowLift = 5f; + /// The 90th percentile angular error (degrees) of the drawn normal against the CPU reference, over the interior pixels. A correct render measures under 1. + private const float MaxNormalP90 = 2f; + + /// The median angular error (degrees) over the bevel band: pixels within 0.6 of the bevel width of an edge that turns by more than 0.1 radian. A correct render measures under 0.5. + private const float MaxBandMedian = 1.5f; + + /// Vacuity floors: the check must reach this many interior pixels and this many band pixels, and the band's reference normal must lie this many degrees (median) from the facet's, or a bevel that is off would pass. + private const int MinKept = 200000, MinBand = 20000; + private const float MinBandTilt = 4f; + private const int BendSeed = 7; private const float BendDegrees = 60f; @@ -156,6 +169,128 @@ public partial class RenderCheck : Node if (share > MaxClipped) { GD.PrintErr($"RENDER FAIL: {share:P1} of the puppet's pixels are clipped to white, over {MaxClipped:P0}; the facets cannot read"); ok = false; } if (spread < MinSpread) { GD.PrintErr($"RENDER FAIL: the puppet's pixels span only {spread:F0} levels between the 10th and 90th percentile, under {MinSpread}; the facets do not differ"); ok = false; } if (lift < MinGlowLift) { GD.PrintErr($"RENDER FAIL: the chamber's light lifts the puppet by {lift:F2} levels, under {MinGlowLift}; the box's glow does not light the body"); ok = false; } + ok &= await NormalCheck(main, shape, carriers, world); + return ok; + } + + /// + /// The drawn shading normal against the shader's formula evaluated on the CPU. The puppet is seen close up from the torso's front-top + /// (the camera 0.35, 0.2, 0.55 m off body 101, FOV 30) in the same bent pose; the view-space normal is read raw from the frame + /// (n * 0.5 + 0.5, not sRGB-decoded) and turned to world by the camera's basis. + /// + private async Task NormalCheck(Main main, PuppetShape shape, Node3D[] carriers, Transform3D[] world) + { + int torso = Array.IndexOf(shape.Ids, 101UL); + if (torso < 0) { GD.PrintErr("RENDER FAIL: the puppet has no body 101 to look at"); return false; } + var target = carriers[torso].GlobalPosition; + var camera = new Camera3D { Fov = 30f, Current = true }; + AddChild(camera); + camera.GlobalPosition = target + new Vector3(0.35f, 0.2f, 0.55f); + camera.LookAt(target, Vector3.Up); + var puppet = main.Bodies.GetNode("Puppet"); + puppet.ShowNormals = true; + for (int i = 0; i < 4; i++) await ToSignal(RenderingServer.Singleton, RenderingServer.SignalName.FramePostDraw); + var image = GetViewport().GetTexture().GetImage(); + image.Convert(Image.Format.Rgba8); + puppet.ShowNormals = false; + foreach (var arg in OS.GetCmdlineUserArgs()) + if (arg.StartsWith("--normals-png=")) image.SavePng(arg["--normals-png=".Length..]); + int w = image.GetWidth(), h = image.GetHeight(); + + // The skinned corners and their normals: the facet normal turned by the blended bone, as vertex() does. + int n = shape.Positions.Length; + var skinned = new Vector3[n]; var turned = new Vector3[n]; + for (int v = 0; v < n; v++) + { + var p = new Vector3(shape.Positions[v].x, shape.Positions[v].y, shape.Positions[v].z); + var f = new Vector3(shape.Normals[v].x, shape.Normals[v].y, shape.Normals[v].z); + float wa = shape.WeightA[v]; + skinned[v] = world[shape.BoneA[v]] * p * wa + world[shape.BoneB[v]] * p * (1f - wa); + turned[v] = (world[shape.BoneA[v]].Basis * f * wa + world[shape.BoneB[v]].Basis * f * (1f - wa)).Normalized(); + } + + // Depth raster: the nearest triangle under each pixel centre, with its barycentrics. + var origin = camera.GlobalPosition; var basis = camera.GlobalBasis; + float tanHalf = Mathf.Tan(Mathf.DegToRad(camera.Fov) / 2f), aspect = (float)w / h; + Vector3 Ray(int x, int y) => (basis * new Vector3((2f * (x + 0.5f) / w - 1f) * tanHalf * aspect, -(2f * (y + 0.5f) / h - 1f) * tanHalf, -1f)).Normalized(); + var depth = new float[w * h]; var tri = new int[w * h]; var bary = new Vector3[w * h]; + Array.Fill(depth, float.MaxValue); Array.Fill(tri, -1); + var scale = new Vector2(w, h) / GetViewport().GetVisibleRect().Size; + for (int t = 0; t < n / 3; t++) + { + Vector3 a = skinned[3 * t], b = skinned[3 * t + 1], c = skinned[3 * t + 2]; + var pa = camera.UnprojectPosition(a) * scale; var pb = camera.UnprojectPosition(b) * scale; var pc = camera.UnprojectPosition(c) * scale; + int x0 = Math.Max(0, (int)Math.Floor(Math.Min(pa.X, Math.Min(pb.X, pc.X))) - 1), x1 = Math.Min(w - 1, (int)Math.Ceiling(Math.Max(pa.X, Math.Max(pb.X, pc.X))) + 1); + int y0 = Math.Max(0, (int)Math.Floor(Math.Min(pa.Y, Math.Min(pb.Y, pc.Y))) - 1), y1 = Math.Min(h - 1, (int)Math.Ceiling(Math.Max(pa.Y, Math.Max(pb.Y, pc.Y))) + 1); + Vector3 e1 = b - a, e2 = c - a; + for (int y = y0; y <= y1; y++) + for (int x = x0; x <= x1; x++) + { + var d = Ray(x, y); + var pv = d.Cross(e2); + float det = e1.Dot(pv); + if (Mathf.Abs(det) < 1e-12f) continue; + var tv = origin - a; + float u = tv.Dot(pv) / det; + var qv = tv.Cross(e1); + float vv = d.Dot(qv) / det; + if (u < 0 || vv < 0 || u + vv > 1) continue; + float dist = e2.Dot(qv) / det; + if (dist <= 0 || dist >= depth[y * w + x]) continue; + depth[y * w + x] = dist; tri[y * w + x] = t; bary[y * w + x] = new Vector3(1 - u - vv, u, vv); + } + } + + float bevel = Look.BevelWidth; + var errors = new System.Collections.Generic.List(); var band = new System.Collections.Generic.List(); var bandTilt = new System.Collections.Generic.List(); + for (int y = 1; y < h - 1; y++) + for (int x = 1; x < w - 1; x++) + { + int t = tri[y * w + x]; + if (t < 0) continue; + bool interior = true; + for (int dy = -1; dy <= 1 && interior; dy++) for (int dx = -1; dx <= 1; dx++) if (tri[(y + dy) * w + x + dx] != t) { interior = false; break; } + if (!interior) continue; + var bc = bary[y * w + x]; + var a = skinned[3 * t]; var b = skinned[3 * t + 1]; var c = skinned[3 * t + 2]; + var nf = (turned[3 * t] * bc.X + turned[3 * t + 1] * bc.Y + turned[3 * t + 2] * bc.Z).Normalized(); + // The bind altitudes give the distances; the skinned triangle gives the directions. + var bind = new[] { shape.Positions[3 * t], shape.Positions[3 * t + 1], shape.Positions[3 * t + 2] }; + float twiceArea = CultMath.math.length(CultMath.math.cross(bind[1] - bind[0], bind[2] - bind[0])); + var alt = new float[3]; var corner = new[] { a, b, c }; + for (int k = 0; k < 3; k++) alt[k] = twiceArea / CultMath.math.length(bind[(k + 1) % 3] - bind[(k + 2) % 3]); + float width = Mathf.Min(bevel, 0.5f * Mathf.Min(alt[0], Mathf.Min(alt[1], alt[2]))); + var tilt = Vector3.Zero; bool inBand = false; + var triNormal = (b - a).Cross(c - a).Normalized(); + for (int k = 0; k < 3; k++) + { + float d = bc[k] * alt[k]; + float crease = shape.Crease[3 * t + k]; + if (crease == 0f || width <= 0f) continue; + var edge = corner[(k + 2) % 3] - corner[(k + 1) % 3]; + var inward = triNormal.Cross(edge).Normalized(); + if (inward.Dot(corner[k] - corner[(k + 1) % 3]) < 0) inward = -inward; + float s = 1f - Mathf.SmoothStep(0f, width, d); + tilt += s * Mathf.Tan(Mathf.Clamp(crease, -1.4f, 1.4f)) * inward; + if (d < 0.6f * bevel && Mathf.Abs(crease) > 0.1f) inBand = true; + } + var reference = (nf - tilt).Normalized(); + var px = image.GetPixel(x, y); + var drawn = (basis * new Vector3(px.R * 2f - 1f, px.G * 2f - 1f, px.B * 2f - 1f)).Normalized(); + float err = Mathf.RadToDeg(Mathf.Acos(Mathf.Clamp(drawn.Dot(reference), -1f, 1f))); + errors.Add(err); + if (inBand) { band.Add(err); bandTilt.Add(Mathf.RadToDeg(Mathf.Acos(Mathf.Clamp(reference.Dot(nf), -1f, 1f)))); } + } + errors.Sort(); band.Sort(); bandTilt.Sort(); + float p90 = errors.Count == 0 ? float.MaxValue : errors[(int)(errors.Count * 0.9f)]; + float bandMedian = band.Count == 0 ? float.MaxValue : band[band.Count / 2]; + float tiltMedian = bandTilt.Count == 0 ? 0 : bandTilt[bandTilt.Count / 2]; + GD.Print($"RENDER normals: p90 error {p90:F2} deg over {errors.Count} interior pixels; band median error {bandMedian:F2} deg over {band.Count} pixels, reference tilt from the facet {tiltMedian:F1} deg"); + bool ok = true; + if (errors.Count < MinKept || band.Count < MinBand) { GD.PrintErr($"RENDER FAIL: the normal check reached {errors.Count} interior and {band.Count} band pixels, under {MinKept} and {MinBand}; it proves nothing"); ok = false; } + if (tiltMedian < MinBandTilt) { GD.PrintErr($"RENDER FAIL: the bevel band's reference normal is {tiltMedian:F1} degrees from the facet's, under {MinBandTilt}; the check cannot see a bevel"); ok = false; } + if (p90 > MaxNormalP90) { GD.PrintErr($"RENDER FAIL: the drawn shading normal is {p90:F2} degrees from the CPU reference at the 90th percentile, over {MaxNormalP90}"); ok = false; } + if (bandMedian > MaxBandMedian) { GD.PrintErr($"RENDER FAIL: across the bevel band the drawn normal is {bandMedian:F2} degrees from the CPU reference (median), over {MaxBandMedian}; the bevel is not where or as large as the creases say"); ok = false; } return ok; }