Add class-specific vessel geometry and sea motion, a guided helm qualification path, dockside sailing orders, and local excursions at all 24 ports.
205 lines
7.6 KiB
C#
205 lines
7.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BigShipTycoon
|
|
{
|
|
internal sealed class HarborMesh
|
|
{
|
|
private readonly List<Vector3> vertices = new List<Vector3>();
|
|
private readonly List<int> triangles = new List<int>();
|
|
private List<Vector4> waterCoordinates;
|
|
|
|
internal int VertexCount => vertices.Count;
|
|
|
|
internal void PlaceFrom(int start, Quaternion rotation, Vector3 offset)
|
|
{
|
|
for (int i = start; i < vertices.Count; i++)
|
|
vertices[i] = rotation * vertices[i] + offset;
|
|
}
|
|
|
|
public Mesh Bake()
|
|
{
|
|
var mesh = new Mesh();
|
|
if (vertices.Count > ushort.MaxValue)
|
|
mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
|
|
mesh.SetVertices(vertices);
|
|
mesh.SetTriangles(triangles, 0);
|
|
if (waterCoordinates != null)
|
|
{
|
|
FillWaterCoordinates();
|
|
mesh.SetUVs(0, waterCoordinates);
|
|
}
|
|
mesh.RecalculateNormals();
|
|
return mesh;
|
|
}
|
|
|
|
public void WaterSurface(Vector2[] outline, Vector2 center, float height, Vector2 halfSize)
|
|
{
|
|
waterCoordinates ??= new List<Vector4>();
|
|
FillWaterCoordinates();
|
|
int start = vertices.Count;
|
|
foreach (Vector2 edge in outline)
|
|
{
|
|
Vector2 offset = edge - center;
|
|
vertices.Add(Point(edge, height));
|
|
waterCoordinates.Add(new Vector4(offset.x, offset.y, halfSize.x, halfSize.y));
|
|
}
|
|
int middle = vertices.Count;
|
|
vertices.Add(Point(center, height));
|
|
waterCoordinates.Add(new Vector4(0, 0, halfSize.x, halfSize.y));
|
|
for (int i = 0; i < outline.Length; i++)
|
|
AddTriangle(start + i, middle, start + (i + 1) % outline.Length);
|
|
}
|
|
|
|
private void FillWaterCoordinates()
|
|
{
|
|
while (waterCoordinates.Count < vertices.Count)
|
|
waterCoordinates.Add(Vector4.zero);
|
|
}
|
|
|
|
public void Quad(Vector3 a, Vector3 b, Vector3 c, Vector3 d)
|
|
{
|
|
int start = vertices.Count;
|
|
vertices.Add(a);
|
|
vertices.Add(b);
|
|
vertices.Add(c);
|
|
vertices.Add(d);
|
|
Face(start, start + 1, start + 2, start + 3);
|
|
}
|
|
|
|
public void Box(Vector3 center, Vector3 size) => Box(center, size, Vector3.right);
|
|
|
|
public void Box(Vector3 center, Vector3 size, Vector3 along)
|
|
{
|
|
Vector3 x = along * (size.x * .5f);
|
|
Vector3 z = Vector3.Cross(along, Vector3.up) * (size.z * .5f);
|
|
Vector3 height = Vector3.up * size.y;
|
|
Vector3 low = center - height * .5f;
|
|
Vector3 a = low - x - z;
|
|
Vector3 b = low + x - z;
|
|
Vector3 c = low + x + z;
|
|
Vector3 d = low - x + z;
|
|
|
|
Quad(b, a, a + height, b + height);
|
|
Quad(c, b, b + height, c + height);
|
|
Quad(d, c, c + height, d + height);
|
|
Quad(a, d, d + height, a + height);
|
|
Quad(a + height, d + height, c + height, b + height);
|
|
Quad(a, b, c, d);
|
|
}
|
|
|
|
public void Disc(Vector3 center, Vector3 horizontal, Vector3 vertical, int sides)
|
|
{
|
|
int start = vertices.Count;
|
|
vertices.Add(center);
|
|
for (int i = 0; i < sides; i++)
|
|
{
|
|
float angle = i * Mathf.PI * 2 / sides;
|
|
vertices.Add(center + horizontal * Mathf.Cos(angle) + vertical * Mathf.Sin(angle));
|
|
AddTriangle(start, start + i + 1, start + (i + 1) % sides + 1);
|
|
}
|
|
}
|
|
|
|
public void Loft(Vector2[] outline, Vector2 center, bool lid, params Vector3[] sections)
|
|
{
|
|
int start = vertices.Count;
|
|
int points = outline.Length;
|
|
foreach (Vector3 section in sections)
|
|
{
|
|
foreach (Vector2 p in outline)
|
|
{
|
|
Vector2 offset = p - center;
|
|
vertices.Add(new Vector3(center.x + offset.x * section.x, section.y, center.y + offset.y * section.z));
|
|
}
|
|
}
|
|
|
|
for (int height = 0; height < sections.Length - 1; height++)
|
|
for (int point = 0; point < points; point++)
|
|
{
|
|
int a = start + height * points + point;
|
|
int b = start + height * points + (point + 1) % points;
|
|
Face(b, a, a + points, b + points);
|
|
}
|
|
|
|
if (!lid)
|
|
return;
|
|
int cap = vertices.Count;
|
|
Vector3 top = sections[sections.Length - 1];
|
|
for (int i = 0; i < points; i++)
|
|
vertices.Add(vertices[start + (sections.Length - 1) * points + i]);
|
|
vertices.Add(new Vector3(center.x, top.y, center.y));
|
|
for (int i = 0; i < points; i++)
|
|
AddTriangle(cap + i, cap + points, cap + (i + 1) % points);
|
|
}
|
|
|
|
public void Rod(Vector3 from, Vector3 to, float radius, int sides = 6, bool capped = false)
|
|
{
|
|
Vector3 axis = (to - from).normalized;
|
|
Vector3 cross = Vector3.Cross(axis, Vector3.up);
|
|
if (cross.sqrMagnitude < .001f)
|
|
cross = Vector3.Cross(axis, Vector3.forward);
|
|
Vector3 u = cross.normalized * radius;
|
|
Vector3 v = Vector3.Cross(axis, u);
|
|
int start = vertices.Count;
|
|
for (int i = 0; i < sides; i++)
|
|
{
|
|
float angle = i * Mathf.PI * 2 / sides;
|
|
Vector3 offset = u * Mathf.Cos(angle) + v * Mathf.Sin(angle);
|
|
vertices.Add(from + offset);
|
|
vertices.Add(to + offset);
|
|
}
|
|
for (int i = 0; i < sides; i++)
|
|
{
|
|
int a = start + i * 2;
|
|
int b = start + ((i + 1) % sides) * 2;
|
|
Face(a, b, b + 1, a + 1);
|
|
}
|
|
if (capped)
|
|
{
|
|
Disc(from, u, -v, sides);
|
|
Disc(to, u, v, sides);
|
|
}
|
|
}
|
|
|
|
public void Radome(Vector3 center, float radius) => Ellipsoid(center, Vector3.one * radius);
|
|
|
|
internal void Ellipsoid(Vector3 center, Vector3 radii, int bands = 8, int slices = 12)
|
|
{
|
|
int start = vertices.Count;
|
|
for (int j = 0; j <= bands; j++)
|
|
{
|
|
float elevation = Mathf.Lerp(-Mathf.PI * .5f, Mathf.PI * .5f, j / (float)bands);
|
|
for (int i = 0; i < slices; i++)
|
|
{
|
|
float rotation = i * Mathf.PI * 2 / slices;
|
|
Vector3 normal = new Vector3(Mathf.Cos(rotation) * Mathf.Cos(elevation), Mathf.Sin(elevation),
|
|
Mathf.Sin(rotation) * Mathf.Cos(elevation));
|
|
vertices.Add(center + Vector3.Scale(normal, radii));
|
|
}
|
|
}
|
|
for (int j = 0; j < bands; j++)
|
|
for (int i = 0; i < slices; i++)
|
|
{
|
|
int a = start + j * slices + i;
|
|
int b = start + j * slices + (i + 1) % slices;
|
|
Face(b, a, a + slices, b + slices);
|
|
}
|
|
}
|
|
|
|
public static Vector3 Point(Vector2 position, float height) => new Vector3(position.x, height, position.y);
|
|
|
|
private void Face(int a, int b, int c, int d)
|
|
{
|
|
AddTriangle(a, b, c);
|
|
AddTriangle(a, c, d);
|
|
}
|
|
|
|
private void AddTriangle(int a, int b, int c)
|
|
{
|
|
triangles.Add(a);
|
|
triangles.Add(b);
|
|
triangles.Add(c);
|
|
}
|
|
}
|
|
}
|