diff --git a/client/Look.cs b/client/Look.cs
index 14d5e04..dbe0640 100644
--- a/client/Look.cs
+++ b/client/Look.cs
@@ -22,6 +22,13 @@ public static class Look
/// The radius (m) the puppet's boxes are rounded by, so the isosurface has no sawtooth bands on their edges.
public const float BoxRound = 0.03f;
+ ///
+ /// The width (m) each side of a puppet edge over which the facet normal turns to the edge's bisector: 4 mm, the recommended
+ /// option of the open question lodur:question:bds-puppet-bevel-width (0 keeps hard facets; 10 mm makes curved limbs read as smooth).
+ /// An answer changes this constant and nothing else.
+ ///
+ public const float BevelWidth = 0.004f;
+
public static Godot.Environment Environment() => new()
{
BackgroundMode = Godot.Environment.BGMode.Color,
diff --git a/client/Puppet.cs b/client/Puppet.cs
index 9a35b12..11a0739 100644
--- a/client/Puppet.cs
+++ b/client/Puppet.cs
@@ -27,6 +27,7 @@ public partial class Puppet : Node3D
public Puppet()
{
_material.SetShaderParameter("albedo", Look.Figure);
+ _material.SetShaderParameter("bevel_width", Look.BevelWidth);
for (int i = 0; i < PuppetModel.MaxBones; i++) _bones.Add(new Projection(Transform3D.Identity));
_skin.MaterialOverride = _material;
}
@@ -39,6 +40,9 @@ public partial class Puppet : Node3D
/// The colour the puppet is drawn in: the shader's albedo.
public Color Color => _material.GetShaderParameter("albedo").AsColor();
+ /// Draws the shading normal (in view space, as n * 0.5 + 0.5) instead of the lit puppet: RenderCheck's view of what the bevel did.
+ public bool ShowNormals { set => _material.SetShaderParameter("show_normals", value); }
+
/// What the puppet is made of now, or null before the first build or while it draws nothing (too many bodies).
public PuppetShape? Shape => _model.Shape;
@@ -149,23 +153,49 @@ public partial class Puppet : Node3D
_skin.Mesh = _mesh;
}
- /// The shape as one surface: VERTEX in the spread bind, the facet NORMAL, and CUSTOM1 (bone A, bone B, weight A).
+ ///
+ /// The shape as one surface: VERTEX in the spread bind, the facet NORMAL, CUSTOM0 (the edge-distance basis: vertex k of a triangle holds
+ /// its altitude h_k = 2 * area / |edge k| in component k and 0 in the others, the triangle's smallest altitude in w), CUSTOM1 (bone A,
+ /// bone B, weight A) and CUSTOM2 (the triangle's three creases, the same at all three vertices).
+ ///
private static ArrayMesh MeshOf(PuppetShape s)
{
int n = s.Positions.Length;
- var vertex = new Vector3[n]; var normal = new Vector3[n]; var skin = new float[3 * n];
+ var vertex = new Vector3[n]; var normal = new Vector3[n]; var edge = new float[4 * n]; var skin = new float[3 * n]; var crease = new float[3 * n];
for (int i = 0; i < n; i++)
{
vertex[i] = new Vector3(s.Positions[i].x, s.Positions[i].y, s.Positions[i].z);
normal[i] = new Vector3(s.Normals[i].x, s.Normals[i].y, s.Normals[i].z);
skin[3 * i] = s.BoneA[i]; skin[3 * i + 1] = s.BoneB[i]; skin[3 * i + 2] = s.WeightA[i];
}
+ for (int t = 0; t < n; t += 3)
+ {
+ var p = new[] { s.Positions[t], s.Positions[t + 1], s.Positions[t + 2] };
+ float twiceArea = CultMath.math.length(CultMath.math.cross(p[1] - p[0], p[2] - p[0]));
+ var h = new float[3];
+ for (int k = 0; k < 3; k++)
+ {
+ float length = CultMath.math.length(p[(k + 1) % 3] - p[(k + 2) % 3]);
+ h[k] = length > 0 ? twiceArea / length : 0;
+ }
+ float smallest = Mathf.Min(h[0], Mathf.Min(h[1], h[2]));
+ for (int k = 0; k < 3; k++)
+ {
+ int i = t + k;
+ edge[4 * i + k] = h[k]; edge[4 * i + 3] = smallest;
+ for (int e = 0; e < 3; e++) crease[3 * i + e] = s.Crease[t + e];
+ }
+ }
var arrays = new Godot.Collections.Array();
arrays.Resize((int)Mesh.ArrayType.Max);
arrays[(int)Mesh.ArrayType.Vertex] = vertex;
arrays[(int)Mesh.ArrayType.Normal] = normal;
+ arrays[(int)Mesh.ArrayType.Custom0] = edge;
arrays[(int)Mesh.ArrayType.Custom1] = skin;
- var format = (Mesh.ArrayFormat)((int)Mesh.ArrayCustomFormat.RgbFloat << (int)Mesh.ArrayFormat.FormatCustom1Shift);
+ arrays[(int)Mesh.ArrayType.Custom2] = crease;
+ var format = (Mesh.ArrayFormat)(((int)Mesh.ArrayCustomFormat.RgbaFloat << (int)Mesh.ArrayFormat.FormatCustom0Shift)
+ | ((int)Mesh.ArrayCustomFormat.RgbFloat << (int)Mesh.ArrayFormat.FormatCustom1Shift)
+ | ((int)Mesh.ArrayCustomFormat.RgbFloat << (int)Mesh.ArrayFormat.FormatCustom2Shift));
var mesh = new ArrayMesh();
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, arrays, null, null, format);
return mesh;
diff --git a/client/puppet.gdshader b/client/puppet.gdshader
index a6cc03c..23e0bcb 100644
--- a/client/puppet.gdshader
+++ b/client/puppet.gdshader
@@ -3,10 +3,24 @@ render_mode cull_back;
// The puppet is one mesh skinned to the bodies: each vertex carries two bones and the first one's weight in CUSTOM1
// (x: bone A, y: bone B, z: weight of A). bones[i] is world-from-spread-bind for body i. The shadow and depth passes
-// run vertex() too, so they skin the same way. NORMAL is the facet normal, so the shading is flat.
+// run vertex() too, so they skin the same way. NORMAL is the facet normal, turned by the skin blend.
+//
+// The soft bevel is the facet normal tilted toward each edge's bisector, within bevel_width of the edge. Two more attributes
+// carry what it needs from the mesh, the same at the three corners of a triangle where noted:
+// CUSTOM0: vertex k holds its own altitude h_k in component k and 0 in the others (so the interpolated component k is the
+// distance, in bind metres, to edge k, the edge opposite vertex k); w is the triangle's smallest altitude.
+// CUSTOM2: the triangle's three creases, crease[k] for edge k: half the turn from this facet's normal to the neighbour's,
+// positive where the edge is convex (PuppetShape.Crease).
+// The in-plane direction to each edge comes from screen derivatives of the distances, so the shader never evaluates the field.
+// At an edge the tilt is exactly the bisector from both sides, so the normal is continuous across it.
const int MAX_BONES = 32;
uniform mat4 bones[MAX_BONES];
uniform vec3 albedo : source_color = vec3(0.9, 0.9, 0.88);
+uniform float bevel_width = 0.0;
+uniform bool show_normals = false; // RenderCheck's view of the shading normal: the normal in view space as emission, rgb = n * 0.5 + 0.5.
+
+varying vec4 edge;
+varying vec3 crease;
void vertex() {
int a = int(CUSTOM1.x + 0.5);
@@ -15,10 +29,37 @@ void vertex() {
mat4 m = bones[a] * w + bones[b] * (1.0 - w);
VERTEX = (m * vec4(VERTEX, 1.0)).xyz;
NORMAL = normalize(mat3(m) * NORMAL);
+ edge = CUSTOM0;
+ crease = CUSTOM2.xyz;
}
void fragment() {
- ALBEDO = albedo;
+ vec3 n = normalize(NORMAL);
+ // Derivatives are undefined in non-uniform control flow: take them all here, branch afterwards.
+ vec3 dx = dFdx(VERTEX);
+ vec3 dy = dFdy(VERTEX);
+ vec3 r1 = cross(dy, n);
+ vec3 r2 = cross(n, dx);
+ float det = dot(dx, r1);
+ vec3 fx = dFdx(edge.xyz);
+ vec3 fy = dFdy(edge.xyz);
+ float width = min(bevel_width, 0.5 * edge.w);
+ if (width > 0.0 && abs(det) > 1e-20) {
+ vec3 tilt = vec3(0.0);
+ for (int k = 0; k < 3; k++) {
+ // The in-plane gradient of the distance to edge k points into the facet; the normal tips the other way.
+ vec3 grad = (fx[k] * r1 + fy[k] * r2) / det;
+ float len = length(grad);
+ if (crease[k] != 0.0 && len > 1e-12) {
+ float s = 1.0 - smoothstep(0.0, width, edge[k]);
+ tilt += s * tan(clamp(crease[k], -1.4, 1.4)) * grad / len;
+ }
+ }
+ n = normalize(n - tilt);
+ }
+ NORMAL = n;
+ ALBEDO = show_normals ? vec3(0.0) : albedo;
+ EMISSION = show_normals ? n * 0.5 + 0.5 : vec3(0.0);
ROUGHNESS = 1.0;
SPECULAR = 0.0;
}