Files
cruiseSimulator/Assets/Soundings/Runtime/PassengerCrowd.cs

419 lines
20 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace Soundings
{
internal enum GuestActivity { Walk, Rail, Talk, Photograph, Read, Dine, Drink, Watch, Perform, Rest, Guitar, Revue }
internal sealed class DeckGuest
{
internal readonly GuestActivity Activity;
internal readonly Vector3 Station;
internal readonly int Service, Seed;
internal readonly float Heading, RequiredOccupancy, SeatHeight, RouteEnd, LaneRadius, RouteStart;
internal readonly EntertainmentProgram? Program;
internal float Presence;
internal bool Initialized;
internal DeckGuest(GuestActivity activity, Vector3 station, int seed, float heading, int service, float occupancy,
float seatHeight, EntertainmentProgram? program = null)
{
Activity = activity;
Station = station;
Seed = seed;
Heading = heading;
Service = service;
RequiredOccupancy = occupancy;
SeatHeight = seatHeight;
Program = program;
}
internal DeckGuest(Vector3 start, float end, float laneRadius, float phase, int seed, float occupancy)
: this(GuestActivity.Walk, start, seed, 0, -1, occupancy, 0)
{
RouteEnd = end;
LaneRadius = laneRadius;
RouteStart = phase;
}
}
internal sealed class PassengerCrowd : System.IDisposable
{
private static readonly Color32[] Shirts =
{
new(222, 213, 191, 255), new(61, 115, 125, 255), new(172, 94, 75, 255),
new(55, 73, 96, 255), new(195, 161, 101, 255), new(129, 147, 122, 255),
new(103, 86, 110, 255), new(125, 167, 179, 255), new(200, 124, 129, 255)
};
private static readonly Color32[] Trousers =
{
new(52, 66, 76, 255), new(66, 78, 92, 255), new(169, 154, 128, 255), new(104, 105, 100, 255)
};
private static readonly Color32[] Skin =
{
new(222, 175, 145, 255), new(178, 123, 91, 255), new(199, 150, 116, 255),
new(111, 73, 55, 255), new(236, 190, 160, 255)
};
private static readonly Color32[] Hair =
{
new(53, 43, 39, 255), new(97, 69, 48, 255), new(170, 130, 78, 255), new(140, 139, 130, 255)
};
private static readonly Color32 Shoes = new(54, 47, 42, 255);
private static readonly Color32 Paper = new(219, 202, 161, 255);
private static readonly Color32 Dark = new(46, 57, 61, 255);
private static readonly Color32 Glass = new(188, 160, 110, 255);
private static readonly Primitive Globe = new(8, 4);
private static readonly Primitive Joint = new(6, 3);
private readonly List<Vector3> vertices = new(22000);
private readonly List<Vector3> normals = new(22000);
private readonly List<Color32> colors = new(22000);
private readonly List<int> indices = new(76000);
private readonly List<Mesh> meshes = new();
private readonly Material material;
private Matrix4x4 guestMatrix;
private Quaternion guestRotation;
private byte coverage;
internal PassengerCrowd()
{
material = new Material(Resources.Load<Shader>("PassengerSurface")) { name = "Passenger clothing and skin" };
}
internal Mesh CreateMesh(GameObject root, float length, float beam)
{
var mesh = new Mesh { name = "Articulated deck passengers", indexFormat = IndexFormat.UInt32 };
mesh.MarkDynamic();
root.AddComponent<MeshFilter>().sharedMesh = mesh;
root.AddComponent<MeshRenderer>().sharedMaterial = material;
mesh.bounds = new Bounds(Vector3.up * 16, new Vector3(length, 40, beam * 2));
meshes.Add(mesh);
return mesh;
}
internal void Draw(Mesh mesh, List<DeckGuest> people, OnboardSession session, bool outside, float clock, float delta)
{
vertices.Clear();
normals.Clear();
colors.Clear();
indices.Clear();
float voyageOccupancy = Mathf.Clamp01(session.Quote.Guests / (float)Mathf.Max(1, session.Ship.Beds));
EntertainmentProgram? playing = session.PerformanceActive ? session.Quote.Plan?.Program : null;
float promenadeAvailability = session.Quote.Weather switch
{
WeatherCondition.Storm => 0,
WeatherCondition.Swell => .66f,
_ => 1
};
foreach (DeckGuest guest in people)
{
float occupancy = voyageOccupancy;
if (guest.Service >= 0)
{
OnboardSample service = session.Get((OnboardService)guest.Service);
occupancy = service.Served / (float)Mathf.Max(1, service.Capacity);
}
if (guest.Program.HasValue)
occupancy = guest.Program == playing ? 1 : 0;
else if (guest.Activity == GuestActivity.Perform && playing.HasValue)
occupancy = 0;
if (outside)
occupancy *= promenadeAvailability;
float goal = Mathf.SmoothStep(0, 1, Mathf.InverseLerp(guest.RequiredOccupancy - .13f,
guest.RequiredOccupancy + .13f, occupancy));
if (session.Quote.Guests == 0 || occupancy <= 0)
goal = 0;
if (!guest.Initialized)
{
guest.Presence = goal;
guest.Initialized = true;
}
else
guest.Presence = Mathf.MoveTowards(guest.Presence, goal, delta * .47f);
if (guest.Presence > .012f)
RenderGuest(guest, clock);
}
Bounds bounds = mesh.bounds;
mesh.Clear();
mesh.SetVertices(vertices);
mesh.SetNormals(normals);
mesh.SetColors(colors);
mesh.SetTriangles(indices, 0, false);
mesh.bounds = bounds;
}
private void RenderGuest(DeckGuest guest, float clock)
{
int seed = guest.Seed;
float variation = Hash(seed);
float stature = Mathf.Lerp(.91f, 1.075f, variation);
float time = clock + Hash(seed + 15) * 80;
bool walking = guest.Activity == GuestActivity.Walk;
float gait = 0;
float heading = guest.Heading;
Vector3 station = guest.Station;
if (walking)
{
float speed = guest.Station.z < 0 ? .91f : .97f;
float distance = clock * speed;
station = WalkRoute(guest, distance, out heading);
gait = (distance + guest.RouteStart * 5) / (.90f * stature);
}
guestRotation = Quaternion.Euler(0, heading, 0);
guestMatrix = Matrix4x4.TRS(station, guestRotation, Vector3.one * stature);
coverage = (byte)Mathf.RoundToInt(guest.Presence * 255f);
Color32 shirt = Shirts[seed % Shirts.Length];
Color32 pants = Trousers[(seed / 4 + seed) % Trousers.Length];
Color32 skin = Skin[(seed / 2 + seed) % Skin.Length];
Color32 hair = Hair[(seed / 3 + seed) % Hair.Length];
bool seated = guest.SeatHeight > 0;
float breathe = Mathf.Sin(time * 2.2f) * .013f;
float step = walking ? Mathf.Sin(gait * Mathf.PI * 2) : 0;
float armSwing = walking ? Mathf.Cos(gait * Mathf.PI * 2) : 0;
float sway = walking ? .031f * step : .02f * Mathf.Sin(time * 1.25f);
float hipY = seated ? guest.SeatHeight / stature : .862f + (walking ? .018f * Mathf.Cos(gait * Mathf.PI * 4) : breathe * .2f);
Vector3 pelvis = new(sway, hipY, 0);
Vector3 chest = pelvis + new Vector3(-sway * .54f, .382f + breathe, seated ? .083f : .022f + Mathf.Abs(step) * .021f);
Vector3 neck = chest + new Vector3(0, .195f, .016f);
float glance = Mathf.Sin(time * .66f) * (walking ? .16f : .39f);
Quaternion headTurn = Quaternion.Euler(Mathf.Sin(time * 1.02f) * 5, glance * Mathf.Rad2Deg, 0);
Vector3 face = neck + new Vector3(.02f * Mathf.Sin(time), .115f, .018f);
Ellipsoid(Globe, pelvis + Vector3.up * .11f, new Vector3(.176f, .178f, .12f), Quaternion.identity, pants);
Ellipsoid(Globe, Vector3.Lerp(pelvis, chest, .66f) + Vector3.up * .058f,
new Vector3(.205f + variation * .021f, .284f, .127f), Quaternion.Euler(seated ? 12 : 3, step * 8, -step * 3), shirt);
Ellipsoid(Joint, neck - Vector3.up * .017f, new Vector3(.057f, .103f, .060f), Quaternion.identity, skin);
Ellipsoid(Globe, face, new Vector3(.102f, .136f, .103f), headTurn, skin);
Ellipsoid(Globe, face + headTurn * new Vector3(0, .067f, -.022f),
new Vector3(.104f, seed % 3 == 0 ? .104f : .079f, .104f), headTurn, hair);
Ellipsoid(Joint, face + headTurn * new Vector3(0, -.011f, .099f), new Vector3(.024f, .029f, .026f), headTurn, skin);
if (seed % 4 == 2 || guest.Activity == GuestActivity.Rail)
{
for (int side = -1; side <= 1; side += 2)
Ellipsoid(Joint, face + headTurn * new Vector3(side * .044f, .026f, .086f),
new Vector3(.042f, .025f, .018f), headTurn, Dark);
}
if (guest.Activity == GuestActivity.Guitar)
{
Quaternion guitar = Quaternion.Euler(-13, 0, 22);
Ellipsoid(Globe, pelvis + new Vector3(.10f, .285f, .225f), new Vector3(.194f, .206f, .055f), guitar, Paper);
Ellipsoid(Joint, pelvis + new Vector3(.062f, .31f, .279f), new Vector3(.051f, .053f, .013f), guitar, Dark);
Limb(pelvis + new Vector3(.002f, .329f, .229f), pelvis + new Vector3(-.337f, .563f, .252f), .037f, Shoes);
}
for (int side = -1; side <= 1; side += 2)
{
Vector3 hip = pelvis + new Vector3(side * .102f, 0, 0);
Vector3 foot = Foot(side, walking, seated, gait, hipY);
Vector3 knee = seated ? new Vector3(side * .130f, hipY - .08f, .37f) : Bend(hip, foot, .43f, Vector3.forward);
Limb(hip, knee, .086f, pants);
Limb(knee, foot, .065f, pants);
Ellipsoid(Joint, foot + new Vector3(0, -.034f, .061f), new Vector3(.079f, .070f, .159f), Quaternion.identity, Shoes);
Vector3 shoulder = chest + new Vector3(side * .18f, .088f, 0);
Vector3 hand = ArmGesture(guest.Activity, side, hipY, time, armSwing, face);
Vector3 elbow = Bend(shoulder, hand, .30f, new Vector3(.55f * side, -.58f, -.60f));
Limb(shoulder, elbow, .067f, shirt);
Limb(elbow, hand, .043f, skin);
Ellipsoid(Joint, hand, new Vector3(.046f, .061f, .047f), Quaternion.identity, skin);
if (side > 0)
RenderProp(guest.Activity, hand);
}
}
private static Vector3 WalkRoute(DeckGuest guest, float distance, out float heading)
{
float length = guest.RouteEnd - guest.Station.x;
float radius = guest.LaneRadius;
float turn = Mathf.PI * radius;
float lap = (length + turn) * 2;
float point = Mathf.Repeat(distance + guest.RouteStart * lap, lap);
Vector3 anchor = guest.Station;
if (point < length)
{
heading = 90;
return anchor + new Vector3(point, 0, -radius);
}
point -= length;
if (point < turn)
{
float arc = point / radius;
heading = 90 - arc * Mathf.Rad2Deg;
return anchor + new Vector3(length + radius * Mathf.Sin(arc), 0, -radius * Mathf.Cos(arc));
}
point -= turn;
if (point < length)
{
heading = -90;
return anchor + new Vector3(length - point, 0, radius);
}
float lastArc = (point - length) / radius;
heading = -90 - lastArc * Mathf.Rad2Deg;
return anchor + new Vector3(-radius * Mathf.Sin(lastArc), 0, radius * Mathf.Cos(lastArc));
}
private static Vector3 Foot(int side, bool walking, bool seated, float gait, float hipY)
{
if (seated)
return new Vector3(side * .135f, Mathf.Max(.095f, hipY - .49f), .42f);
if (!walking)
return new Vector3(side * .124f, .104f, side * .057f);
const float stance = .59f;
float phase = Mathf.Repeat(gait + (side > 0 ? .5f : 0), 1);
float z = .266f - phase * .90f;
float height = 0;
if (phase >= stance)
{
float swing = (phase - stance) / (1 - stance);
z = Mathf.SmoothStep(-.265f, .266f, swing);
height = .128f * Mathf.Sin(Mathf.PI * swing);
}
return new Vector3(side * .107f, .104f + height, z);
}
private static Vector3 Bend(Vector3 root, Vector3 tip, float length, Vector3 direction)
{
Vector3 axis = tip - root;
float half = axis.magnitude * .5f;
if (half < .001f)
return root;
axis /= half * 2;
Vector3 plane = (direction - axis * Vector3.Dot(axis, direction)).normalized;
return (root + tip) * .5f + plane * Mathf.Sqrt(Mathf.Max(0, length * length - half * half));
}
private static Vector3 ArmGesture(GuestActivity activity, int side, float hipY, float time, float stride, Vector3 face)
{
float beat = Mathf.Repeat(time * .167f + (side < 0 ? .47f : 0), 1);
float action = Mathf.SmoothStep(0, 1, Mathf.InverseLerp(.61f, .85f, beat))
* (1 - Mathf.SmoothStep(0, 1, Mathf.InverseLerp(.89f, 1, beat)));
switch (activity)
{
case GuestActivity.Walk:
return new Vector3(side * .25f, .914f + Mathf.Abs(stride) * .037f, side * stride * .245f);
case GuestActivity.Rail:
return new Vector3(side * .19f, hipY + .25f, .37f);
case GuestActivity.Read:
return new Vector3(side * .12f, hipY + .33f, .37f);
case GuestActivity.Dine:
case GuestActivity.Drink:
Vector3 table = new(side * .16f, hipY + .27f, activity == GuestActivity.Dine ? .43f : .35f);
return side > 0 ? Vector3.Lerp(table, face + new Vector3(.056f, -.047f, .134f), action) : table;
case GuestActivity.Watch:
float applause = action * (.63f + .37f * Mathf.Sin(time * 19f));
return new Vector3(side * Mathf.Lerp(.19f, .047f, applause), hipY + .15f + action * .32f, .28f + action * .10f);
case GuestActivity.Photograph:
return Vector3.Lerp(new Vector3(side * .125f, hipY + .28f, .29f),
face + new Vector3(side * .098f, -.045f, .24f), action);
case GuestActivity.Talk:
return new Vector3(side * (.248f + action * .18f), hipY + .12f + action * .34f, .13f + action * .14f);
case GuestActivity.Perform:
return side > 0 ? face + new Vector3(.065f, -.067f, .17f)
: new Vector3(-.28f - action * .26f, hipY + .21f + action * .41f, .13f + action * .17f);
case GuestActivity.Guitar:
return side > 0 ? new Vector3(.108f, hipY + .30f + Mathf.Sin(time * 12.7f) * .080f, .302f)
: new Vector3(-.25f + Mathf.Sin(time * 2.8f) * .045f, hipY + .52f, .268f);
case GuestActivity.Revue:
float arc = time * 3.2f + side * 1.1f;
return new Vector3(side * (.32f + Mathf.Cos(arc) * .16f), hipY + .52f + Mathf.Sin(arc) * .35f, .24f);
default:
return new Vector3(side * .21f, hipY + .15f, .23f);
}
}
private void RenderProp(GuestActivity activity, Vector3 hand)
{
if (activity is GuestActivity.Drink or GuestActivity.Photograph or GuestActivity.Read or GuestActivity.Perform)
{
Vector3 size = activity switch
{
GuestActivity.Drink => new Vector3(.052f, .091f, .052f),
GuestActivity.Read => new Vector3(.136f, .081f, .023f),
GuestActivity.Photograph => new Vector3(.063f, .105f, .019f),
_ => new Vector3(.037f, .103f, .037f)
};
Color32 color = activity == GuestActivity.Drink ? Glass : activity == GuestActivity.Read ? Paper : Dark;
Ellipsoid(Joint, hand + new Vector3(-size.x * .5f, size.y * .54f, size.z + .025f), size,
activity == GuestActivity.Read ? Quaternion.Euler(-37, 0, 0) : Quaternion.identity, color);
}
}
private void Limb(Vector3 from, Vector3 to, float radius, Color32 color)
{
Vector3 direction = to - from;
float distance = direction.magnitude;
if (distance > .001f)
Ellipsoid(Joint, (from + to) * .5f, new Vector3(radius, distance * .55f + radius * .33f, radius),
Quaternion.FromToRotation(Vector3.up, direction / distance), color);
}
private void Ellipsoid(Primitive primitive, Vector3 center, Vector3 size, Quaternion rotation, Color32 color)
{
int offset = vertices.Count;
Matrix4x4 position = guestMatrix * Matrix4x4.TRS(center, rotation, size);
Quaternion orient = guestRotation * rotation;
Vector3 inverseSize = new(1f / size.x, 1f / size.y, 1f / size.z);
color.a = coverage;
foreach (Vector3 point in primitive.Points)
{
vertices.Add(position.MultiplyPoint3x4(point));
normals.Add(orient * Vector3.Scale(point, inverseSize).normalized);
colors.Add(color);
}
foreach (int index in primitive.Indices)
indices.Add(offset + index);
}
private static float Hash(int seed)
{
uint value = unchecked((uint)seed + 105789u);
value = (value ^ (value >> 16)) * 0x7feb352d;
value = (value ^ (value >> 15)) * 0x846ca68b;
return ((value ^ (value >> 16)) & 0xffff) / 65535f;
}
public void Dispose()
{
foreach (Mesh mesh in meshes)
Object.Destroy(mesh);
Object.Destroy(material);
}
private sealed class Primitive
{
internal readonly Vector3[] Points;
internal readonly int[] Indices;
internal Primitive(int sides, int bands)
{
Points = new Vector3[sides * (bands + 1)];
Indices = new int[sides * bands * 6];
for (int band = 0; band <= bands; band++)
{
float vertical = (band / (float)bands - .5f) * Mathf.PI;
for (int side = 0; side < sides; side++)
{
float azimuth = side * Mathf.PI * 2 / sides;
Points[band * sides + side] = new Vector3(Mathf.Cos(azimuth) * Mathf.Cos(vertical), Mathf.Sin(vertical),
Mathf.Sin(azimuth) * Mathf.Cos(vertical));
if (band == bands)
continue;
int a = band * sides + side, b = band * sides + (side + 1) % sides;
int offset = a * 6;
Indices[offset] = a;
Indices[offset + 1] = a + sides;
Indices[offset + 2] = b + sides;
Indices[offset + 3] = a;
Indices[offset + 4] = b + sides;
Indices[offset + 5] = b;
}
}
}
}
}
}