Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/WorldCoastlines.cs

44 lines
2.1 KiB
C#

using System;
using System.Globalization;
using UnityEngine;
namespace BigShipTycoon
{
internal static class WorldCoastlines
{
private static readonly (Vector2[][] Shapes, Rect[] Bounds) Outlines = ReadOutlines();
public static Vector2[][] Shapes => Outlines.Shapes;
public static Rect[] Bounds => Outlines.Bounds;
private static (Vector2[][] Shapes, Rect[] Bounds) ReadOutlines()
{
// Public-domain Natural Earth 1:50m land, simplified with extra detail around the ports.
// Each line is one exterior outline, with longitude and latitude in thousandths of a degree.
TextAsset data = Resources.Load<TextAsset>("Coastlines");
if (data == null)
return (Array.Empty<Vector2[]>(), Array.Empty<Rect>());
string[] outlines = data.text.Split('\n', StringSplitOptions.RemoveEmptyEntries);
var shapes = new Vector2[outlines.Length][];
var bounds = new Rect[outlines.Length];
for (int ring = 0; ring < outlines.Length; ring++)
{
string[] coordinates = outlines[ring].Split(' ', StringSplitOptions.RemoveEmptyEntries);
var polygon = shapes[ring] = new Vector2[coordinates.Length / 2];
Vector2 lower = Vector2.positiveInfinity, upper = Vector2.negativeInfinity;
for (int vertex = 0; vertex < polygon.Length; vertex++)
{
int lon = int.Parse(coordinates[vertex * 2], CultureInfo.InvariantCulture);
int lat = int.Parse(coordinates[vertex * 2 + 1], CultureInfo.InvariantCulture);
polygon[vertex] = new Vector2((lon + 180_000) / 360_000f, (76_000 - lat) / 137_000f);
var degrees = new Vector2(lon * .001f, -lat * .001f);
lower = Vector2.Min(lower, degrees);
upper = Vector2.Max(upper, degrees);
}
bounds[ring] = Rect.MinMaxRect(lower.x, lower.y, upper.x, upper.y);
}
return (shapes, bounds);
}
}
}