using System.Collections.Generic; using UnityEngine; namespace Soundings { internal sealed class HarborMesh { private readonly List vertices = new List(); private readonly List triangles = new List(); 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); mesh.RecalculateNormals(); return mesh; } 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) { Vector3 a = center - size * .5f; Vector3 b = center + size * .5f; var shape = new[] { new Vector2(a.x, a.z), new Vector2(b.x, a.z), new Vector2(b.x, b.z), new Vector2(a.x, b.z) }; for (int i = 0; i < shape.Length; i++) { Vector2 p = shape[i]; Vector2 q = shape[(i + 1) % shape.Length]; Quad(Point(q, a.y), Point(p, a.y), Point(p, b.y), Point(q, b.y)); } Quad(Point(shape[0], b.y), Point(shape[3], b.y), Point(shape[2], b.y), Point(shape[1], b.y)); Quad(Point(shape[0], a.y), Point(shape[1], a.y), Point(shape[2], a.y), Point(shape[3], a.y)); } 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) { 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); } } public void Radome(Vector3 center, float radius) { int start = vertices.Count; const int bands = 8; const int slices = 12; 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 + normal * radius); } } 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); } } }