Skip to content

Commit a9f87ef

Browse files
authored
Merge pull request #72 from tangrams/optimize-builders
Optimize PolygonBuilder
2 parents e671fdf + 1f43567 commit a9f87ef

File tree

3 files changed

+18
-54
lines changed

3 files changed

+18
-54
lines changed

Assets/Mapzen/Unity/MeshData.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,5 @@ public void AddElements(IEnumerable<Vector3> vertices, IEnumerable<Vector2> uvs,
8585
submesh.Indices.Add(index + offset);
8686
}
8787
}
88-
89-
public void FlipIndices()
90-
{
91-
foreach (var bucket in Meshes)
92-
{
93-
foreach (var submesh in bucket.Submeshes)
94-
{
95-
submesh.Indices.Reverse();
96-
}
97-
}
98-
}
9988
}
10089
}

Assets/Mapzen/Unity/PolygonBuilder.cs

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,24 @@ public PolygonBuilder(MeshData outputMeshData, Options options, Matrix4x4 transf
3131
this.transform = transform;
3232
this.outputMeshData = outputMeshData;
3333
this.options = options;
34-
this.coordinates = new List<float>();
35-
this.lastPoint = new Point();
36-
this.rings = new List<int>();
37-
this.pointsInRing = 0;
38-
this.extrusionVertices = new List<Vector3>();
39-
this.extrusionUVs = new List<Vector2>();
40-
this.extrusionIndices = new List<int>();
41-
this.polygonUVs = new List<Vector2>();
4234
}
4335

4436
private Matrix4x4 transform;
4537
private MeshData outputMeshData;
4638
private Options options;
4739

4840
// Values for the tesselator.
49-
private List<float> coordinates;
50-
private List<int> rings;
51-
private int pointsInRing;
41+
private List<float> coordinates = new List<float>();
42+
private List<int> holes = new List<int>();
43+
private int pointsInRing = 0;
44+
private int pointsInPolygon = 0;
5245

5346
// Values for extrusions.
54-
private Point lastPoint;
55-
private List<Vector3> extrusionVertices;
56-
private List<Vector2> extrusionUVs;
57-
private List<Vector2> polygonUVs;
58-
private List<int> extrusionIndices;
47+
private Point lastPoint = new Point();
48+
private List<Vector3> extrusionVertices = new List<Vector3>();
49+
private List<Vector2> extrusionUVs = new List<Vector2>();
50+
private List<Vector2> polygonUVs = new List<Vector2>();
51+
private List<int> extrusionIndices = new List<int>();
5952

6053
public void OnPoint(Point point)
6154
{
@@ -93,10 +86,10 @@ public void OnPoint(Point point)
9386
extrusionUVs.Add(new Vector2(0.0f, 0.0f));
9487

9588
extrusionIndices.Add(indexOffset + 1);
89+
extrusionIndices.Add(indexOffset + 2);
9690
extrusionIndices.Add(indexOffset + 3);
9791
extrusionIndices.Add(indexOffset + 2);
9892
extrusionIndices.Add(indexOffset + 1);
99-
extrusionIndices.Add(indexOffset + 2);
10093
extrusionIndices.Add(indexOffset + 0);
10194
}
10295

@@ -110,6 +103,7 @@ public void OnPoint(Point point)
110103
}
111104

112105
pointsInRing++;
106+
pointsInPolygon++;
113107
}
114108

115109
public void AddUV(Vector2 uv)
@@ -128,21 +122,25 @@ public void OnEndLineString()
128122
public void OnBeginLinearRing()
129123
{
130124
pointsInRing = 0;
125+
if (pointsInPolygon > 0)
126+
{
127+
holes.Add(pointsInPolygon);
128+
}
131129
}
132130

133131
public void OnEndLinearRing()
134132
{
135-
rings.Add(pointsInRing);
136133
}
137134

138135
public void OnBeginPolygon()
139136
{
140137
coordinates.Clear();
141-
rings.Clear();
138+
holes.Clear();
142139
extrusionVertices.Clear();
143140
extrusionUVs.Clear();
144141
extrusionIndices.Clear();
145142
polygonUVs.Clear();
143+
pointsInPolygon = 0;
146144
}
147145

148146
public void OnEndPolygon()
@@ -152,29 +150,8 @@ public void OnEndPolygon()
152150

153151
if (coordinates.Count > 0)
154152
{
155-
List<List<Vector3>> polygon = new List<List<Vector3>>();
156-
int ringOffset = 0;
157-
158-
for (int i = 0; i < rings.Count; ++i)
159-
{
160-
polygon.Add(new List<Vector3>());
161-
for (int ring = 0; ring < rings[i]; ++ring)
162-
{
163-
float x = coordinates[(ring + ringOffset) * 2];
164-
float y = coordinates[(ring + ringOffset) * 2 + 1];
165-
166-
polygon[i].Add(new Vector3(x, 0.0f, y));
167-
}
168-
169-
ringOffset += rings[i];
170-
}
171-
172-
var flatData = EarcutLibrary.Flatten(polygon);
173-
174153
// Then tesselate polygon interior and add vertices and indices.
175-
var indices = EarcutLibrary.Earcut(flatData.Vertices, flatData.Holes, flatData.Dim);
176-
177-
indices.Reverse();
154+
var indices = EarcutLibrary.Earcut(coordinates, holes, 2);
178155

179156
var vertices = new List<Vector3>(coordinates.Count / 2);
180157

Assets/Mapzen/Unity/SceneGraph.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public static void Generate(SceneGroup group, Transform parent, GameObjectOption
4141
}
4242
else
4343
{
44-
group.meshData.FlipIndices();
45-
4644
if (group.meshData.Meshes.Count > 1)
4745
{
4846
var gameObject = new GameObject(group.ToString());

0 commit comments

Comments
 (0)