diff --git a/client/BloodRenderer.cs b/client/BloodRenderer.cs new file mode 100644 index 0000000..49319f0 --- /dev/null +++ b/client/BloodRenderer.cs @@ -0,0 +1,91 @@ +using System; +using Godot; +using Ymir.Box3D; + +namespace BadDay.Client; + +/// +/// Draws the body's blood from Ymir's lanes and writes nothing back: one MultiMesh of the live fluid drops (a small box of side 2 x the +/// particle's radius) and one of the wet patches (a flat disc of the patch's radius, lying horizontal at the patch's point carried by the body it +/// is on, in that body's pose). The drawing is not scaled (lodur:ruling:bds-blood-gain-simulated): the volume the physiology loses is +/// scaled where the blood is emitted, and what is shown is what the world holds. Splat decals, painted textures and billboards are a later cut. +/// +public partial class BloodRenderer : Node3D +{ + /// A patch is a disc this thick (m), lifted this far (m) so it does not z-fight the surface it lies on. + private const float PatchThickness = 0.004f, PatchLift = 0.003f; + + private readonly MultiMeshInstance3D _drops = Instances(new BoxMesh { Size = Vector3.One }, "Drops"); + private readonly MultiMeshInstance3D _patches = Instances(new CylinderMesh { TopRadius = 1f, BottomRadius = 1f, Height = 1f, RadialSegments = 8, Rings = 0 }, "Patches"); + + /// Drops and wet patches drawn by the latest : the live fluid lanes and the live wet lanes. + public int DropCount { get; private set; } + public int PatchCount { get; private set; } + + public override void _Ready() + { + var material = Look.Ps1(null, Look.Blood); + foreach (var node in new[] { _drops, _patches }) + { + node.MaterialOverride = material; + node.Multimesh.CustomAabb = new Aabb(new Vector3(-50, -5, -50), new Vector3(100, 20, 100)); // instances move every frame: never culled on a stale box + } + AddChild(_drops); + AddChild(_patches); + } + + public void Sync(in Box3DSoaView view) + { + var live = view.ParticleLive; + var phase = view.ParticlePhase; + var radius = view.ParticleRadius; + int drops = 0; + for (int i = 0; i < live.Length; i++) if (live[i] != 0 && (phase[i] & Box3DSoaPhase.Fluid) != 0) drops++; + Reserve(_drops.Multimesh, drops); + int d = 0; + for (int i = 0; i < live.Length && d < drops; i++) + { + if (live[i] == 0 || (phase[i] & Box3DSoaPhase.Fluid) == 0) continue; + _drops.Multimesh.SetInstanceTransform(d++, new Transform3D(Basis.FromScale(Vector3.One * 2f * radius[i]), new Vector3(view.ParticleX[i], view.ParticleY[i], view.ParticleZ[i]))); + } + DropCount = drops; + _drops.Multimesh.VisibleInstanceCount = drops; + + var wet = view.WetLive; + int patches = 0; + for (int i = 0; i < wet.Length; i++) if (wet[i] != 0) patches++; + Reserve(_patches.Multimesh, patches); + int p = 0; + for (int i = 0; i < wet.Length && p < patches; i++) + { + if (wet[i] == 0) continue; + var local = new Vector3(view.WetX[i], view.WetY[i], view.WetZ[i]); + Vector3 at; + int body = view.WetBody[i], particle = view.WetParticle[i]; + if (body >= 0) + { + var q = new Quaternion(view.RotationX[body], view.RotationY[body], view.RotationZ[body], view.RotationW[body]); + at = new Vector3(view.PositionX[body], view.PositionY[body], view.PositionZ[body]) + q * local; + } + else if (particle >= 0) at = new Vector3(view.ParticleX[particle], view.ParticleY[particle], view.ParticleZ[particle]); + else at = local; + float r = view.WetRadius[i]; + _patches.Multimesh.SetInstanceTransform(p++, new Transform3D(Basis.FromScale(new Vector3(r, PatchThickness, r)), at + Vector3.Up * PatchLift)); + } + PatchCount = patches; + _patches.Multimesh.VisibleInstanceCount = patches; + } + + /// Grows the multimesh's capacity (doubling) so instances fit; it never shrinks, and only the visible ones are drawn. + private static void Reserve(MultiMesh mm, int count) + { + if (count <= mm.InstanceCount) return; + mm.InstanceCount = Math.Max(count, Math.Max(16, mm.InstanceCount * 2)); + } + + private static MultiMeshInstance3D Instances(Mesh mesh, string name) => new() + { + Name = name, + Multimesh = new MultiMesh { TransformFormat = MultiMesh.TransformFormatEnum.Transform3D, Mesh = mesh }, + }; +} diff --git a/client/Main.cs b/client/Main.cs index 4414c12..68ae4f6 100644 --- a/client/Main.cs +++ b/client/Main.cs @@ -26,9 +26,12 @@ public partial class Main : Node3D private Chamber _chamber = null!; private OrbitCamera _camera = null!; private Hud _hud = null!; + private BloodRenderer _blood = null!; private IBodyRun? _run; private double _intensity = new PainBoxParameters().BoxIntensity; + public BloodRenderer Blood => _blood; + public Scenario Scenario { get; private set; } = Scenario.Chamber; /// The live run, whichever scenario. @@ -77,6 +80,8 @@ public partial class Main : Node3D Look.Apply(); _bodies = new BodyRenderer { Name = "Bodies" }; AddChild(_bodies); + _blood = new BloodRenderer { Name = "Blood" }; + AddChild(_blood); _chamber = new Chamber { Name = "Chamber" }; AddChild(_chamber); _camera = new OrbitCamera { Name = "Camera", Pivot = () => _bodies.PuppetCentre }; @@ -218,6 +223,7 @@ public partial class Main : Node3D { var s = Run; _bodies.Sync(s.View, teleport); + _blood.Sync(s.View); if (s is BodyPainBoxSession box) _chamber.Sync(box); else _chamber.Hide(); Span events = stackalloc BodyEvent[8]; int n; diff --git a/client/tests/Smoke.cs b/client/tests/Smoke.cs index 8968786..40b1f61 100644 --- a/client/tests/Smoke.cs +++ b/client/tests/Smoke.cs @@ -191,7 +191,19 @@ public partial class Smoke : Node Check(office.Result().Timeline.Count > 0, "no timeline after a cut"); await UntilTick(600); + // The blood drawn is the blood the world holds: a drop per live fluid lane, a disc per live wet lane, counted from the lanes here. + var view = office.View; + int liveDrops = 0, livePatches = 0; + for (int i = 0; i < view.ParticleCount; i++) if (view.ParticleLive[i] != 0 && (view.ParticlePhase[i] & Box3DSoaPhase.Fluid) != 0) liveDrops++; + for (int i = 0; i < view.WetCount; i++) if (view.WetLive[i] != 0) livePatches++; + Check(liveDrops + livePatches > 0, "no blood in the world 480 ticks after the cut"); + Check(_main.Blood.DropCount == liveDrops && _main.Blood.PatchCount == livePatches, + $"blood draws {_main.Blood.DropCount} drops and {_main.Blood.PatchCount} patches; the lanes hold {liveDrops} and {livePatches}"); + // The bars are the session's will and arousal, to the bit, once a frame has drawn them (stepping paused so the session holds still). + // Run on until the cut has cost his will, so a bar bound to resolve instead shows a different number. + await UntilTick(1800); + Check(office.Will < office.Resolve - 0.1, $"will {office.Will} and resolve {office.Resolve} are too close at tick 1800 for the bars to tell them apart"); _main.SetPhysicsProcess(false); await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);