Newer
Older
using Mapbox.Vector.Tile;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
public struct PolygonIndices
{
public List<int> indices;
}
bool valid;
bool finished;
bool triangulated;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
bool Valid => valid;
bool Finished => finished;
Task parseTileTask;
// These lists must match.
List<VectorTileLayer> layers;
List<List<PolygonIndices>> polygonIndices;
bool IsFinished()
{
if (finished)
return true;
if (!request.isDone)
return false;
if (request.webRequest.result == UnityWebRequest.Result.Success)
{
if (parseTileTask == null)
{
parseTileTask = Task.Run( () =>
{
var stream = new MemoryStream( request.webRequest.downloadHandler.data );
layers = VectorTileParser.Parse( stream );
} );
}
else if (parseTileTask.IsCompleted)
{
valid = parseTileTask.IsCompletedSuccessfully;
finished = true;
parseTileTask = null;
}
}
return finished;
}
void Triangulate()
{
polygonIndices = new List<List<PolygonIndices>>();
List<double> vertices = new List<double>();
List<int> holeIndices = new List<int>();
for ( int i =0; i < layers.Count; i++ )
{
var features = layers[i].VectorTileFeatures;
var featurePolygonIndices = new List<PolygonIndices>();
polygonIndices.Add( featurePolygonIndices );
for (int j = 0; j< features.Count; j++)
{
if (features[j].GeometryType != Tile.GeomType.Polygon)
continue;
vertices.Clear();
holeIndices.Clear();
var rings = features[j].Geometry;
for (int k = 0;k < rings.Count;k++)
{
if (k != 0) // if is not first ring, this is a hole
{
holeIndices.Add( vertices.Count );
}
var ring = rings[k];
for (int q = 0;q < ring.Count;q++)
{
vertices.Add( ring[q].X );
vertices.Add( ring[q].Y );
}
}
PolygonIndices polyIndices = new PolygonIndices();
polyIndices.indices = EarcutNet.Earcut.Tessellate( vertices, holeIndices );
featurePolygonIndices.Add( polyIndices );
}
}
}