Client: white on dark grey (ruling bds-look-white-on-grey-sdf-puppet); the puppet's mesh from the lanes lives in one class, Puppet

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 02:34:30 +01:00
commit 06774227a6
6 changed files with 140 additions and 83 deletions

View file

@ -5,89 +5,43 @@ using Ymir.Box3D;
namespace BadDay.Client;
/// <summary>
/// Draws Ymir's world: one MeshInstance3D per StableId, its mesh from the Kind and Size lanes, its transform from the
/// pose lanes every tick. The renderer owns no dimensions and writes nothing back; the lanes are the only source.
/// Meshes are stylised low poly with flat normals.
/// Draws Ymir's world from its lanes and writes nothing back. The static bodies (floor, stool) are plain boxes drawn
/// here; every dynamic body belongs to the <see cref="Puppet"/>, the one swappable owner of the puppet's mesh.
/// </summary>
public partial class BodyRenderer : Node3D
{
private readonly Dictionary<ulong, MeshInstance3D> _nodes = new();
private readonly HashSet<ulong> _seen = new();
private readonly StandardMaterial3D _dynamic = Flat(new Color(0.88f, 0.10f, 0.08f));
private readonly StandardMaterial3D _fixed = Flat(new Color(0.74f, 0.74f, 0.72f));
private readonly Puppet _puppet = new() { Name = "Puppet" };
private readonly Dictionary<ulong, MeshInstance3D> _scenery = new();
private readonly StandardMaterial3D _material = Look.Matte(Look.Scenery);
public int NodeCount => _nodes.Count;
public int NodeCount => _puppet.NodeCount + _scenery.Count;
public MeshInstance3D? NodeFor(ulong stableId) => _nodes.GetValueOrDefault(stableId);
public MeshInstance3D? NodeFor(ulong stableId) => _puppet.NodeFor(stableId) ?? _scenery.GetValueOrDefault(stableId);
public override void _Ready() => AddChild(_puppet);
/// <summary>Makes the nodes match the lanes: creates the missing, frees the gone, then poses every node. <paramref name="teleport"/> skips the interpolation from the previous pose.</summary>
public void Sync(in Box3DSoaView view, bool teleport = false)
{
_puppet.Sync(view, teleport);
var ids = view.StableId;
var kind = view.Kind;
if (ids.Length != _nodes.Count || !AllPresent(ids))
{
_seen.Clear();
for (int i = 0; i < ids.Length; i++) _seen.Add(ids[i]);
foreach (var id in new List<ulong>(_nodes.Keys))
{
if (_seen.Contains(id)) continue;
_nodes[id].QueueFree();
_nodes.Remove(id);
}
for (int i = 0; i < ids.Length; i++)
{
if (_nodes.ContainsKey(ids[i])) continue;
var node = new MeshInstance3D { Name = $"Body{ids[i]}", Mesh = MeshFor((Box3DSoaKind)kind[i], view.Size0[i], view.Size1[i], view.Size2[i]) };
node.MaterialOverride = (Box3DSoaKind)kind[i] == Box3DSoaKind.StaticBox ? _fixed : _dynamic;
AddChild(node);
_nodes[ids[i]] = node;
teleport = true;
}
}
var px = view.PositionX; var py = view.PositionY; var pz = view.PositionZ;
var rx = view.RotationX; var ry = view.RotationY; var rz = view.RotationZ; var rw = view.RotationW;
for (int i = 0; i < ids.Length; i++)
{
var node = _nodes[ids[i]];
node.Transform = new Transform3D(new Basis(new Quaternion(rx[i], ry[i], rz[i], rw[i])), new Vector3(px[i], py[i], pz[i]));
if (teleport) node.ResetPhysicsInterpolation();
if ((Box3DSoaKind)kind[i] != Box3DSoaKind.StaticBox) continue;
if (!_scenery.TryGetValue(ids[i], out var node))
{
node = new MeshInstance3D
{
Name = $"Static{ids[i]}",
Mesh = new BoxMesh { Size = new Vector3(2f * view.Size0[i], 2f * view.Size1[i], 2f * view.Size2[i]) },
MaterialOverride = _material,
};
AddChild(node);
_scenery[ids[i]] = node;
}
node.Transform = new Transform3D(
new Basis(new Quaternion(view.RotationX[i], view.RotationY[i], view.RotationZ[i], view.RotationW[i])),
new Vector3(view.PositionX[i], view.PositionY[i], view.PositionZ[i]));
}
}
private bool AllPresent(System.ReadOnlySpan<ulong> ids)
{
foreach (var id in ids) if (!_nodes.ContainsKey(id)) return false;
return true;
}
/// <summary>The mesh for a body: sphere radius Size0; capsule radius Size0 and half-length Size1 along Y (Godot's height counts the caps); box half-extents Size0..2.</summary>
public static Mesh MeshFor(Box3DSoaKind kind, float size0, float size1, float size2) => kind switch
{
Box3DSoaKind.Sphere => FlatShaded(new SphereMesh { Radius = size0, Height = 2f * size0, RadialSegments = 8, Rings = 5 }),
Box3DSoaKind.Capsule => FlatShaded(new CapsuleMesh { Radius = size0, Height = 2f * (size1 + size0), RadialSegments = 8, Rings = 2 }),
_ => FlatShaded(new BoxMesh { Size = new Vector3(2f * size0, 2f * size1, 2f * size2) }),
};
/// <summary>The same triangles with one normal per face.</summary>
private static ArrayMesh FlatShaded(PrimitiveMesh source)
{
var arrays = source.GetMeshArrays();
var vertices = arrays[(int)Mesh.ArrayType.Vertex].AsVector3Array();
var indices = arrays[(int)Mesh.ArrayType.Index].AsInt32Array();
var st = new SurfaceTool();
st.Begin(Mesh.PrimitiveType.Triangles);
st.SetSmoothGroup(uint.MaxValue);
foreach (int i in indices) st.AddVertex(vertices[i]);
st.GenerateNormals();
return st.Commit();
}
private static StandardMaterial3D Flat(Color color) => new()
{
AlbedoColor = color,
Roughness = 1f,
MetallicSpecular = 0f,
};
}

View file

@ -8,13 +8,13 @@ namespace BadDay.Client;
public partial class Chamber : Node3D
{
private readonly MeshInstance3D _volume = new() { Mesh = new BoxMesh { Size = Vector3.One } };
private readonly OmniLight3D _glow = new() { OmniRange = 1.6f, LightColor = new Color(1f, 0.85f, 0.15f), LightEnergy = 0f };
private readonly OmniLight3D _glow = new() { OmniRange = 0.9f, LightColor = Look.Glow, LightEnergy = 0f };
private readonly StandardMaterial3D _material = new()
{
Transparency = BaseMaterial3D.TransparencyEnum.Alpha,
AlbedoColor = new Color(1f, 0.85f, 0.15f, 0.2f),
AlbedoColor = new Color(Look.Glow, 0.2f),
EmissionEnabled = true,
Emission = new Color(1f, 0.85f, 0.15f),
Emission = Look.Glow,
CullMode = BaseMaterial3D.CullModeEnum.Disabled,
};
@ -32,7 +32,7 @@ public partial class Chamber : Node3D
Position = new Vector3(box.CenterX, box.CenterY, box.CenterZ);
_volume.Scale = new Vector3(2f * box.HalfX, 2f * box.HalfY, 2f * box.HalfZ);
float glow = (float)Math.Min(1.0, box.Intensity / 1.5);
_material.AlbedoColor = new Color(1f, 0.85f, 0.15f, 0.06f + 0.3f * glow);
_material.AlbedoColor = new Color(Look.Glow, 0.06f + 0.3f * glow);
_material.EmissionEnergyMultiplier = 0.1f + 2.5f * glow;
_glow.LightEnergy = 3f * glow;
}

View file

@ -27,7 +27,7 @@ public partial class Hud : CanvasLayer
var row = new HBoxContainer { MouseFilter = Control.MouseFilterEnum.Ignore };
margin.AddChild(row);
_readout.AddThemeColorOverride("font_color", new Color(0.08f, 0.08f, 0.08f));
_readout.AddThemeColorOverride("font_color", new Color(0.92f, 0.92f, 0.9f));
_readout.AddThemeFontSizeOverride("font_size", 16);
_readout.SizeFlagsHorizontal = Control.SizeFlags.ExpandFill;
_readout.MouseFilter = Control.MouseFilterEnum.Ignore;
@ -37,18 +37,18 @@ public partial class Hud : CanvasLayer
row.AddChild(controls);
var seedRow = new HBoxContainer();
seedRow.AddChild(new Label { Text = "Seed", Modulate = new Color(0.08f, 0.08f, 0.08f) });
seedRow.AddChild(new Label { Text = "Seed", Modulate = new Color(0.92f, 0.92f, 0.9f) });
seedRow.AddChild(_seed);
controls.AddChild(seedRow);
controls.AddChild(Button("Reset", () => _main.Reset((ulong)_seed.Value)));
controls.AddChild(new Label { Text = "Intensity", Modulate = new Color(0.08f, 0.08f, 0.08f) });
controls.AddChild(new Label { Text = "Intensity", Modulate = new Color(0.92f, 0.92f, 0.9f) });
_intensity.Value = _main.Intensity;
_intensity.ValueChanged += v => _main.SetIntensity(v);
controls.AddChild(_intensity);
controls.AddChild(Button("Box off", () => _main.SwitchOff()));
var slow = new CheckButton { Text = "Slow motion", Modulate = new Color(0.08f, 0.08f, 0.08f) };
var slow = new CheckButton { Text = "Slow motion", Modulate = new Color(0.92f, 0.92f, 0.9f) };
slow.Toggled += on => _main.SetSlowMotion(on);
controls.AddChild(slow);
}

View file

@ -2,22 +2,29 @@ using Godot;
namespace BadDay.Client;
/// <summary>The scene's stylised look: a bright flat world, one hard light, saturated red figures.</summary>
/// <summary>The scene's stylised look: white on dark grey, flat and high-contrast, one hard light.</summary>
public static class Look
{
public static readonly Color Background = new(0.10f, 0.10f, 0.11f);
public static readonly Color Scenery = new(0.15f, 0.15f, 0.16f);
public static readonly Color Figure = new(0.9f, 0.9f, 0.88f);
public static readonly Color Glow = new(0.85f, 0.95f, 1f);
public static Godot.Environment Environment() => new()
{
BackgroundMode = Godot.Environment.BGMode.Color,
BackgroundColor = new Color(0.94f, 0.94f, 0.92f),
BackgroundColor = Background,
AmbientLightSource = Godot.Environment.AmbientSource.Color,
AmbientLightColor = new Color(1f, 1f, 1f),
AmbientLightEnergy = 0.3f,
AmbientLightEnergy = 0.2f,
};
public static DirectionalLight3D Sun() => new()
{
RotationDegrees = new Vector3(-50f, 35f, 0f),
LightEnergy = 1.5f,
LightEnergy = 0.5f,
ShadowEnabled = true,
};
public static StandardMaterial3D Matte(Color color) => new() { AlbedoColor = color, Roughness = 1f, MetallicSpecular = 0f };
}

96
client/Puppet.cs Normal file
View file

@ -0,0 +1,96 @@
using System.Collections.Generic;
using Godot;
using Ymir.Box3D;
namespace BadDay.Client;
/// <summary>
/// The puppet's mesh: made from the shape lanes (Kind and Size) and the body transforms (the pose lanes) of every
/// dynamic body, one MeshInstance3D per StableId with flat-shaded low-poly limbs. This class is the only owner of
/// "the puppet's mesh from the lanes"; a different model (the SDF isosurface) replaces it and nothing else.
/// </summary>
public partial class Puppet : Node3D
{
private readonly Dictionary<ulong, MeshInstance3D> _nodes = new();
private readonly HashSet<ulong> _seen = new();
private readonly StandardMaterial3D _dynamic = Look.Matte(Look.Figure);
public int NodeCount => _nodes.Count;
public MeshInstance3D? NodeFor(ulong stableId) => _nodes.GetValueOrDefault(stableId);
/// <summary>Makes the nodes match the lanes: creates the missing, frees the gone, then poses every node. <paramref name="teleport"/> skips the interpolation from the previous pose.</summary>
public void Sync(in Box3DSoaView view, bool teleport = false)
{
var ids = view.StableId;
var kind = view.Kind;
if (CountDynamic(kind) != _nodes.Count || !AllPresent(ids, kind))
{
_seen.Clear();
for (int i = 0; i < ids.Length; i++) if (IsPuppet(kind[i])) _seen.Add(ids[i]);
foreach (var id in new List<ulong>(_nodes.Keys))
{
if (_seen.Contains(id)) continue;
_nodes[id].QueueFree();
_nodes.Remove(id);
}
for (int i = 0; i < ids.Length; i++)
{
if (!IsPuppet(kind[i]) || _nodes.ContainsKey(ids[i])) continue;
var node = new MeshInstance3D { Name = $"Body{ids[i]}", Mesh = MeshFor((Box3DSoaKind)kind[i], view.Size0[i], view.Size1[i], view.Size2[i]) };
node.MaterialOverride = _dynamic;
AddChild(node);
_nodes[ids[i]] = node;
teleport = true;
}
}
var px = view.PositionX; var py = view.PositionY; var pz = view.PositionZ;
var rx = view.RotationX; var ry = view.RotationY; var rz = view.RotationZ; var rw = view.RotationW;
for (int i = 0; i < ids.Length; i++)
{
if (!IsPuppet(kind[i])) continue;
var node = _nodes[ids[i]];
node.Transform = new Transform3D(new Basis(new Quaternion(rx[i], ry[i], rz[i], rw[i])), new Vector3(px[i], py[i], pz[i]));
if (teleport) node.ResetPhysicsInterpolation();
}
}
private static bool IsPuppet(uint kind) => (Box3DSoaKind)kind != Box3DSoaKind.StaticBox;
private static int CountDynamic(System.ReadOnlySpan<uint> kind)
{
int n = 0;
foreach (var k in kind) if (IsPuppet(k)) n++;
return n;
}
private bool AllPresent(System.ReadOnlySpan<ulong> ids, System.ReadOnlySpan<uint> kind)
{
for (int i = 0; i < ids.Length; i++) if (IsPuppet(kind[i]) && !_nodes.ContainsKey(ids[i])) return false;
return true;
}
/// <summary>The mesh for a body: sphere radius Size0; capsule radius Size0 and half-length Size1 along Y (Godot's height counts the caps); box half-extents Size0..2.</summary>
public static Mesh MeshFor(Box3DSoaKind kind, float size0, float size1, float size2) => kind switch
{
Box3DSoaKind.Sphere => FlatShaded(new SphereMesh { Radius = size0, Height = 2f * size0, RadialSegments = 8, Rings = 5 }),
Box3DSoaKind.Capsule => FlatShaded(new CapsuleMesh { Radius = size0, Height = 2f * (size1 + size0), RadialSegments = 8, Rings = 2 }),
_ => FlatShaded(new BoxMesh { Size = new Vector3(2f * size0, 2f * size1, 2f * size2) }),
};
/// <summary>The same triangles with one normal per face.</summary>
private static ArrayMesh FlatShaded(PrimitiveMesh source)
{
var arrays = source.GetMeshArrays();
var vertices = arrays[(int)Mesh.ArrayType.Vertex].AsVector3Array();
var indices = arrays[(int)Mesh.ArrayType.Index].AsInt32Array();
var st = new SurfaceTool();
st.Begin(Mesh.PrimitiveType.Triangles);
st.SetSmoothGroup(uint.MaxValue);
foreach (int i in indices) st.AddVertex(vertices[i]);
st.GenerateNormals();
return st.Commit();
}
}

View file

@ -79,8 +79,8 @@ public partial class Smoke : Node
$"hand at tick {ParityTick} ({h.X}, {h.Y}, {h.Z}) differs from the headless run ({_reference.X}, {_reference.Y}, {_reference.Z})");
var view = s.View;
Check(_main.Bodies.NodeCount == view.Count && _main.Bodies.GetChildCount() == view.Count,
$"{_main.Bodies.GetChildCount()} nodes for {view.Count} bodies");
Check(_main.Bodies.NodeCount == view.Count && Count(_main.Bodies, n => n is MeshInstance3D) == view.Count,
$"{_main.Bodies.NodeCount} nodes for {view.Count} bodies");
for (int i = 0; i < view.Count; i++)
{
var node = _main.Bodies.NodeFor(view.StableId[i]);