Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/HarborPortScenery.cs
josh 290e4cb45d Add riverboat captain career and distinct ship classes
Add class-specific vessel geometry and sea motion, a guided helm qualification path, dockside sailing orders, and local excursions at all 24 ports.
2026-11-29 20:41:00 -05:00

420 lines
22 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace BigShipTycoon
{
public sealed partial class Harbor
{
private enum PortTerrain { Flat, LowHills, Cliffs, Mountains, Volcanic, Desert }
private enum PortArchitecture { Modern, Caribbean, Maritime, Colonial, Mediterranean, Industrial, Arabian, SouthAsian, Tropical, Pacific }
private readonly struct PortProfile
{
public readonly PortTerrain Terrain;
public readonly PortArchitecture Architecture;
public readonly float Height, Density, Trees;
public readonly Color Ground, Facade, Roof, Sky;
public PortProfile(PortTerrain terrain, PortArchitecture architecture, float height, float density, float trees,
string ground, string facade, string roof, string sky)
{
Terrain = terrain;
Architecture = architecture;
Height = height;
Density = density;
Trees = trees;
Ground = PortColor(ground);
Facade = PortColor(facade);
Roof = PortColor(roof);
Sky = PortColor(sky);
}
}
private static readonly string[] PortMatteNames = { "Miami", "Nassau", "KeyWest" };
private readonly Texture2D[] portMattes = new Texture2D[PortMatteNames.Length];
private readonly bool[] portMattesLoaded = new bool[PortMatteNames.Length];
private PortScenery portScenery;
private int sceneryPort = -1;
private float portMatteAspect;
private static Color PortColor(string hex) => ColorUtility.TryParseHtmlString("#" + hex, out Color color) ? color : Color.grey;
private void ShowPortScenery(int port)
{
PortProfile profile = PortProfiles[port];
Texture2D matte = PortMatte(port);
portMatteAspect = matte == null ? 0f : (float)matte.width / matte.height;
water.SetFloat("_PortMatteActive", matte == null ? 0f : 1f);
if (matte != null)
{
portScenery?.SetVisible(false);
water.SetTexture("_PortMatte", matte);
}
else
{
if (sceneryPort != port)
{
portScenery?.Release();
portScenery = new PortScenery(transform, port, profile);
sceneryPort = port;
}
portScenery.SetVisible(true);
}
water.SetColor("_PortSky", profile.Sky);
water.SetFloat("_PortShallows", port switch
{
1 or 3 => .89f,
13 or 16 => .73f,
2 or 4 or 5 or 17 or 19 or 21 => .56f,
7 or 12 or 14 or 18 => .31f,
_ => .19f
});
quayFinishes[0].color = Color.Lerp(new Color(.41f, .44f, .45f), profile.Facade, .35f);
quayFinishes[1].color = Color.Lerp(new Color(.53f, .52f, .47f), profile.Facade, .5f);
}
private Texture2D PortMatte(int port)
{
if (port >= PortMatteNames.Length)
return null;
if (!portMattesLoaded[port])
{
portMattesLoaded[port] = true;
portMattes[port] = Resources.Load<Texture2D>("Art/Ports/" + PortMatteNames[port]);
}
return portMattes[port];
}
private void FitPortMatte(float aspect)
{
float cropX = Mathf.Min(1, aspect / portMatteAspect) * .91f;
float cropY = Mathf.Min(1, portMatteAspect / aspect) * .91f;
water.SetVector("_PortMatteCrop", new Vector4(cropX, cropY, 0, -.49f * (1 - cropY)));
}
internal static bool AllPortsHaveScenery => PortProfiles.Length == Market.Ports.Count;
private sealed partial class PortScenery
{
private enum Coat { Ground, Rock, Sandy, Street, Wall, WallWarm, WallCool, Trim, Roof, Glass, GlassLit, Steel, Foliage, FoliageLight, Wood, Accent, Brick }
private const float ShoreCenter = 46f;
private readonly PortProfile profile;
private readonly int port;
private readonly GameObject root;
private readonly Dictionary<Coat, HarborMesh> batches = new();
private readonly List<Mesh> meshes = new();
private readonly List<Material> materials = new();
public PortScenery(Transform parent, int port, PortProfile profile)
{
this.port = port;
this.profile = profile;
root = new GameObject(Market.Ports[port].Name + " waterfront");
root.transform.SetParent(parent, false);
if (port is 22 or 23)
root.transform.localPosition = Vector3.back * 19f;
BuildCoast();
BuildDistrict();
BuildLandmarks();
Shader surface = Resources.Load<Shader>("PortScenerySurface");
foreach (var (coat, geometry) in batches)
{
Mesh mesh = geometry.Bake();
var material = new Material(surface) { name = "Waterfront " + coat, color = ColorFor(coat) };
material.SetFloat("_Detail", coat switch
{
Coat.Ground or Coat.Rock or Coat.Sandy => 1,
Coat.Roof or Coat.Brick => 2,
Coat.Glass or Coat.GlassLit => 3,
Coat.Foliage or Coat.FoliageLight => 4,
Coat.Wood => 5,
Coat.Street => 6,
Coat.Steel => 7,
_ => 0
});
material.SetFloat("_Glow", coat == Coat.GlassLit ? .28f : 0f);
material.SetColor("_HazeColor", Color.Lerp(new Color(.47f, .60f, .70f), profile.Sky, .30f));
var draw = new GameObject(coat.ToString(), typeof(MeshFilter), typeof(MeshRenderer)) { layer = 8 };
draw.transform.SetParent(root.transform, false);
draw.GetComponent<MeshFilter>().sharedMesh = mesh;
draw.GetComponent<MeshRenderer>().sharedMaterial = material;
meshes.Add(mesh);
materials.Add(material);
}
batches.Clear();
}
public void SetVisible(bool visible) => root.SetActive(visible);
public void Release()
{
root.SetActive(false);
Object.Destroy(root);
foreach (Mesh mesh in meshes)
Object.Destroy(mesh);
foreach (Material material in materials)
Object.Destroy(material);
}
private HarborMesh MeshFor(Coat coat)
{
if (!batches.TryGetValue(coat, out HarborMesh mesh))
batches.Add(coat, mesh = new HarborMesh());
return mesh;
}
private Color ColorFor(Coat coat) => coat switch
{
Coat.Ground => profile.Ground,
Coat.Rock => Color.Lerp(profile.Ground, PortColor("5D6770"), .45f),
Coat.Sandy => Color.Lerp(profile.Ground, PortColor("DFCAA4"), .69f),
Coat.Street => Color.Lerp(profile.Facade, PortColor("565C60"), .58f),
Coat.Wall => profile.Facade,
Coat.WallWarm => Color.Lerp(profile.Facade, PortColor("E19B72"), .58f),
Coat.WallCool => Color.Lerp(profile.Facade, PortColor("99BBC1"), .57f),
Coat.Trim => Color.Lerp(profile.Facade, new Color(.9f, .89f, .82f), .67f),
Coat.Roof => profile.Roof,
Coat.Glass => new Color(.16f, .28f, .35f),
Coat.GlassLit => new Color(.76f, .52f, .31f),
Coat.Steel => new Color(.27f, .33f, .35f),
Coat.Foliage => Color.Lerp(profile.Ground, PortColor("324F37"), .57f),
Coat.FoliageLight => Color.Lerp(profile.Ground, PortColor("7C9148"), .57f),
Coat.Wood => new Color(.41f, .30f, .21f),
Coat.Accent => new Color(.75f, .57f, .26f),
_ => new Color(.49f, .24f, .17f)
};
private float Shore(float x)
{
float bank = Mathf.Sin(x * .015f + port * .76f) * 5.3f + Mathf.Sin(x * .032f - port) * 2.6f;
float headland = profile.Terrain switch
{
PortTerrain.Cliffs => -13f * Mathf.SmoothStep(0, 1, Mathf.InverseLerp(33, 165, Mathf.Abs(x))),
PortTerrain.Volcanic => -9f * Mathf.Sin(x * .011f),
PortTerrain.Desert => 6f * Mathf.Cos(x * .010f),
_ => 0
};
return ShoreCenter + bank + headland;
}
private float GroundHeight(float x, float inland)
{
float town = Mathf.SmoothStep(-.18f, 2.55f, Mathf.InverseLerp(-5f, 19f, inland));
float depth = Mathf.SmoothStep(0, 1, Mathf.InverseLerp(23, 178, inland));
float wave = Mathf.PerlinNoise(x * .010f + port * 3.3f, inland * .013f + 17.6f);
float ridge = 1 - Mathf.Clamp01(Mathf.Abs(wave - .58f) * 2.3f);
float silhouette = profile.Terrain switch
{
PortTerrain.Flat => depth * (.07f + .15f * wave),
PortTerrain.LowHills => depth * (.18f + .62f * wave),
PortTerrain.Cliffs => depth * (.33f + .65f * ridge) + .18f * Mathf.SmoothStep(0, 1, Mathf.InverseLerp(65, 190, Mathf.Abs(x))) * Mathf.SmoothStep(0, 1, inland / 34f),
PortTerrain.Mountains => depth * (.19f + .93f * ridge * ridge),
PortTerrain.Volcanic => depth * (.09f + .26f * wave) + PortCone(x, inland),
_ => depth * (.17f + .56f * wave)
};
return town + profile.Height * silhouette;
}
private float PortCone(float x, float inland)
{
float peakX = port is 16 or 17 ? 84 : -68;
Vector2 a = new((x - peakX) / 98, (inland - 143) / 123);
Vector2 b = new((x + peakX * .68f) / 111, (inland - 206) / 142);
return Mathf.Max(0, 1f - a.magnitude) * .98f + Mathf.Max(0, 1f - b.magnitude) * .63f;
}
private Vector3 WaterfrontPoint(float x, float inland) => new(x, GroundHeight(x, inland), Shore(x) + inland);
private void BuildCoast()
{
float[] rows = { -6, 0, 3, 8, 16, 30, 46, 63, 82, 104, 126, 148, 175, 208, 240, 286, 346, 440, 600 };
for (int across = -33; across < 33; across++)
{
for (int row = 0; row < rows.Length - 1; row++)
{
float a = across * 13.4f, b = (across + 1) * 13.4f;
Coat tone = row < 2 ? Coat.Sandy : row < 4 && profile.Terrain == PortTerrain.Cliffs ? Coat.Rock : Coat.Ground;
MeshFor(tone).Quad(WaterfrontPoint(a, rows[row]), WaterfrontPoint(a, rows[row + 1]),
WaterfrontPoint(b, rows[row + 1]), WaterfrontPoint(b, rows[row]));
}
}
// Developed waterfronts carry a hard quay edge under the first street and a narrower promenade behind it.
float frontage = Mathf.Lerp(103, 231, profile.Density);
Vector3 quay = new(0, .59f, ShoreCenter + 6.8f);
MeshFor(Coat.Rock).Box(quay, new Vector3(frontage * 2, 1.36f, 20.8f));
MeshFor(Coat.Street).Box(quay + new Vector3(0, .77f, 0), new Vector3(frontage * 2 + .28f, .23f, 21f));
for (int post = -12; post <= 12; post++)
{
float x = post * frontage / 12;
MeshFor(Coat.Wood).Box(new Vector3(x, .50f, quay.z - 10.52f), new Vector3(.38f, 1.23f, .31f));
}
for (int block = -23; block < 23; block++)
{
float x0 = block * 11f, x1 = (block + 1) * 11f;
Vector3 a = WaterfrontPoint(x0, 7.5f), b = WaterfrontPoint(x1, 7.5f);
Vector3 c = WaterfrontPoint(x0, 14.6f), d = WaterfrontPoint(x1, 14.6f);
float level = Mathf.Max(Mathf.Max(a.y, b.y), Mathf.Max(c.y, d.y)) + .05f;
a.y = b.y = c.y = d.y = level;
MeshFor(Coat.Street).Quad(a, c, d, b);
Vector3 stripe = Vector3.Lerp(Vector3.Lerp(a, c, .46f), Vector3.Lerp(b, d, .46f), .42f);
Box(Coat.Trim, stripe + Vector3.up * .013f, 3.2f, .026f, .115f);
}
}
private void BuildDistrict()
{
int frontage = Mathf.RoundToInt(Mathf.Lerp(10, 17, profile.Density));
for (int row = 0; row < 3; row++)
{
for (int i = -frontage; i <= frontage; i++)
{
if (Mathf.Abs(i) <= (row == 0 ? 3 : 1))
continue;
float seed = SignedNoise(i, row);
if (seed < .30f - profile.Density * .23f)
continue;
float x = i * 18.4f + (seed - .5f) * 6f - row * 4f;
float inland = 15f + row * 26f;
Vector3 location = WaterfrontPoint(x, inland);
float storeys = Mathf.Lerp(1, 3.5f, seed) + row * .26f;
bool towers = profile.Architecture is PortArchitecture.Modern or PortArchitecture.Pacific or PortArchitecture.SouthAsian;
float height = 2.75f * storeys;
if (towers && row > 0 && seed > .48f)
height += (seed - .35f) * (profile.Architecture == PortArchitecture.Modern ? 65 : 39) * row;
Coat walls = i % 3 == 0 ? Coat.WallWarm : i % 3 == 1 ? Coat.WallCool : Coat.Wall;
float width = 8 + seed * 7;
if (height > 19)
{
Vector3 skyline = WaterfrontPoint(x * 1.20f + row * 4f, inland + 61 + row * 5);
Tower(skyline, width * 1.10f, 9 + seed * 8, height * .87f, walls);
}
else
House(location, width, 7.5f + seed * 3f, height, walls);
}
}
int palms = Mathf.RoundToInt(profile.Trees * 41);
for (int tree = 0; tree < palms; tree++)
{
float x = (tree - palms * .5f) * 13.7f + SignedNoise(tree, 1) * 5.2f;
bool behind = Mathf.Abs(x) < 54 || tree % 3 == 0;
Vector3 place = WaterfrontPoint(x, behind ? 46 + SignedNoise(tree, 4) * 21 : 8);
place.y = Mathf.Max(place.y, 1.56f);
if (profile.Architecture is PortArchitecture.Mediterranean or PortArchitecture.Maritime or PortArchitecture.Industrial)
StreetTree(place, 3.4f + SignedNoise(tree, 2) * 2f);
else
Palm(place, 3.6f + SignedNoise(tree, 2) * 3.1f, tree * .73f);
}
}
private float SignedNoise(int x, int row) => Mathf.PerlinNoise(x * .629f + 21f + port * 4.7f, row * 3.88f + 6.6f);
private void Box(Coat coat, Vector3 center, float width, float height, float depth) =>
MeshFor(coat).Box(center, new Vector3(width, height, depth));
private void House(Vector3 foot, float width, float depth, float height, Coat walls)
{
Box(Coat.Rock, foot + Vector3.up * .18f, width + .40f, .48f, depth + .35f);
Box(walls, foot + Vector3.up * (height * .5f + .32f), width, height, depth);
Box(Coat.Trim, foot + Vector3.up * (height + .29f), width + .3f, .22f, depth + .3f);
if (profile.Architecture is PortArchitecture.Arabian or PortArchitecture.Modern or PortArchitecture.Industrial or PortArchitecture.SouthAsian)
{
Box(Coat.Roof, foot + Vector3.up * (height + .49f), width + .33f, .23f, depth + .33f);
Box(Coat.Trim, foot + new Vector3(0, height + .78f, depth * .42f), width + .11f, .47f, .25f);
}
else
Gable(foot + Vector3.up * (height + .37f), width + .46f, depth + .47f, Mathf.Min(2.33f, width * .18f), walls);
Facade(foot, width, depth, height, false);
}
private void Tower(Vector3 foot, float width, float depth, float height, Coat walls)
{
Box(Coat.Trim, foot + Vector3.up * 1.28f, width + 1.15f, 2.56f, depth + 1.1f);
Box(walls, foot + Vector3.up * (height * .5f + 2f), width, height, depth);
Box(Coat.Glass, foot + new Vector3(0, height * .5f + 2.2f, -depth * .5f - .015f), width * .9f, height * .95f, .035f);
Box(Coat.Glass, foot + new Vector3(-width * .5f, height * .5f + 2.2f, 0), .036f, height * .95f, depth * .90f);
Box(Coat.Glass, foot + new Vector3(width * .5f, height * .5f + 2.2f, 0), .036f, height * .95f, depth * .90f);
Box(Coat.Trim, foot + Vector3.up * (height + 2f), width + .26f, .42f, depth + .2f);
Box(walls, foot + Vector3.up * (height + 2.95f), width * .62f, 1.85f, depth * .66f);
for (int bay = -2; bay <= 2; bay++)
Box(Coat.Steel, foot + new Vector3(bay * width * .17f, height * .5f + 2.17f, -depth * .5f - .045f), .095f, height * .94f, .08f);
Facade(foot + Vector3.up * 2.2f, width, depth, height, true);
}
private void Facade(Vector3 foot, float width, float depth, float height, bool tower)
{
int bays = Mathf.Clamp(Mathf.RoundToInt(width / 2.55f), 2, 8);
int floors = Mathf.Max(1, Mathf.FloorToInt(height / (tower ? 3.2f : 2.65f)));
float gap = width * .85f / bays;
float step = height / floors;
for (int level = 0; level < floors; level++)
{
float y = (level + .57f) * step + .14f;
if (tower)
{
Box(Coat.Steel, foot + new Vector3(0, y + .91f, -depth * .5f - .05f), width * .93f, .075f, .09f);
if (level % 4 != 2)
continue;
Box(Coat.GlassLit, foot + new Vector3((level % 5 - 2) * width * .155f, y, -depth * .5f - .044f), width * .14f, step * .58f, .03f);
continue;
}
for (int bay = 0; bay < bays; bay++)
{
float x = (bay + .5f - bays * .5f) * gap;
Coat windows = (bay + level * 3 + Mathf.RoundToInt(foot.x)) % 7 == 0 ? Coat.GlassLit : Coat.Glass;
Box(Coat.Trim, foot + new Vector3(x, y, -depth * .5f - .03f), gap * .62f, step * .64f, .13f);
Box(windows, foot + new Vector3(x, y + .03f, -depth * .5f - .102f), gap * .50f, step * .54f, .03f);
}
for (int side = -1; side <= 1; side += 2)
Box(Coat.Glass, foot + new Vector3(side * (width * .5f + .02f), y, -.8f), .054f, step * .54f, depth * .29f);
}
}
private void Gable(Vector3 ridgeBase, float width, float depth, float rise, Coat endWall)
{
Vector3 a = ridgeBase + new Vector3(-width * .5f, 0, -depth * .5f);
Vector3 b = ridgeBase + new Vector3(width * .5f, 0, -depth * .5f);
Vector3 c = ridgeBase + new Vector3(width * .5f, 0, depth * .5f);
Vector3 d = ridgeBase + new Vector3(-width * .5f, 0, depth * .5f);
Vector3 crownFront = ridgeBase + new Vector3(0, rise, -depth * .5f);
Vector3 crownBack = ridgeBase + new Vector3(0, rise, depth * .5f);
MeshFor(Coat.Roof).Quad(a, d, crownBack, crownFront);
MeshFor(Coat.Roof).Quad(c, b, crownFront, crownBack);
MeshFor(endWall).Quad(b, a, crownFront, crownFront);
MeshFor(endWall).Quad(d, c, crownBack, crownBack);
MeshFor(Coat.Trim).Rod(a, crownFront, .074f);
MeshFor(Coat.Trim).Rod(crownFront, b, .074f);
}
private void Palm(Vector3 foot, float height, float turn)
{
Vector3 neck = foot + new Vector3(.27f * Mathf.Sin(turn), height, .27f * Mathf.Cos(turn));
MeshFor(Coat.Wood).Rod(foot, neck, .13f, 8);
for (int frond = 0; frond < 7; frond++)
{
float angle = frond * 2.39f + turn;
Vector3 stem = new(Mathf.Cos(angle), 0, Mathf.Sin(angle));
Vector3 side = Vector3.Cross(stem, Vector3.up);
Vector3 mid = neck + stem * (1.10f + .14f * (frond % 3)) + Vector3.up * .45f;
Vector3 tip = neck + stem * (2.05f + .15f * (frond % 3)) - Vector3.up * .18f;
HarborMesh leaf = MeshFor(frond % 2 == 0 ? Coat.Foliage : Coat.FoliageLight);
leaf.Quad(neck, mid + side * .32f, tip, mid - side * .32f);
leaf.Quad(neck, mid - side * .28f, tip - Vector3.up * .14f, mid + side * .28f);
}
}
private void StreetTree(Vector3 foot, float height)
{
MeshFor(Coat.Wood).Rod(foot, foot + Vector3.up * height, .13f, 7);
Vector3 crown = foot + Vector3.up * (height - .25f);
MeshFor(Coat.Foliage).Radome(crown, height * .30f);
MeshFor(Coat.FoliageLight).Radome(crown + new Vector3(.38f, .43f, -.37f), height * .24f);
}
}
}
}