Skip to content

Commit

Permalink
Merge pull request #72 from tangrams/optimize-builders
Browse files Browse the repository at this point in the history
Optimize PolygonBuilder
  • Loading branch information
karimnaaji authored Nov 1, 2017
2 parents e671fdf + 1f43567 commit a9f87ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 54 deletions.
11 changes: 0 additions & 11 deletions Assets/Mapzen/Unity/MeshData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,5 @@ public void AddElements(IEnumerable<Vector3> vertices, IEnumerable<Vector2> uvs,
submesh.Indices.Add(index + offset);
}
}

public void FlipIndices()
{
foreach (var bucket in Meshes)
{
foreach (var submesh in bucket.Submeshes)
{
submesh.Indices.Reverse();
}
}
}
}
}
59 changes: 18 additions & 41 deletions Assets/Mapzen/Unity/PolygonBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,24 @@ public PolygonBuilder(MeshData outputMeshData, Options options, Matrix4x4 transf
this.transform = transform;
this.outputMeshData = outputMeshData;
this.options = options;
this.coordinates = new List<float>();
this.lastPoint = new Point();
this.rings = new List<int>();
this.pointsInRing = 0;
this.extrusionVertices = new List<Vector3>();
this.extrusionUVs = new List<Vector2>();
this.extrusionIndices = new List<int>();
this.polygonUVs = new List<Vector2>();
}

private Matrix4x4 transform;
private MeshData outputMeshData;
private Options options;

// Values for the tesselator.
private List<float> coordinates;
private List<int> rings;
private int pointsInRing;
private List<float> coordinates = new List<float>();
private List<int> holes = new List<int>();
private int pointsInRing = 0;
private int pointsInPolygon = 0;

// Values for extrusions.
private Point lastPoint;
private List<Vector3> extrusionVertices;
private List<Vector2> extrusionUVs;
private List<Vector2> polygonUVs;
private List<int> extrusionIndices;
private Point lastPoint = new Point();
private List<Vector3> extrusionVertices = new List<Vector3>();
private List<Vector2> extrusionUVs = new List<Vector2>();
private List<Vector2> polygonUVs = new List<Vector2>();
private List<int> extrusionIndices = new List<int>();

public void OnPoint(Point point)
{
Expand Down Expand Up @@ -93,10 +86,10 @@ public void OnPoint(Point point)
extrusionUVs.Add(new Vector2(0.0f, 0.0f));

extrusionIndices.Add(indexOffset + 1);
extrusionIndices.Add(indexOffset + 2);
extrusionIndices.Add(indexOffset + 3);
extrusionIndices.Add(indexOffset + 2);
extrusionIndices.Add(indexOffset + 1);
extrusionIndices.Add(indexOffset + 2);
extrusionIndices.Add(indexOffset + 0);
}

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

pointsInRing++;
pointsInPolygon++;
}

public void AddUV(Vector2 uv)
Expand All @@ -128,21 +122,25 @@ public void OnEndLineString()
public void OnBeginLinearRing()
{
pointsInRing = 0;
if (pointsInPolygon > 0)
{
holes.Add(pointsInPolygon);
}
}

public void OnEndLinearRing()
{
rings.Add(pointsInRing);
}

public void OnBeginPolygon()
{
coordinates.Clear();
rings.Clear();
holes.Clear();
extrusionVertices.Clear();
extrusionUVs.Clear();
extrusionIndices.Clear();
polygonUVs.Clear();
pointsInPolygon = 0;
}

public void OnEndPolygon()
Expand All @@ -152,29 +150,8 @@ public void OnEndPolygon()

if (coordinates.Count > 0)
{
List<List<Vector3>> polygon = new List<List<Vector3>>();
int ringOffset = 0;

for (int i = 0; i < rings.Count; ++i)
{
polygon.Add(new List<Vector3>());
for (int ring = 0; ring < rings[i]; ++ring)
{
float x = coordinates[(ring + ringOffset) * 2];
float y = coordinates[(ring + ringOffset) * 2 + 1];

polygon[i].Add(new Vector3(x, 0.0f, y));
}

ringOffset += rings[i];
}

var flatData = EarcutLibrary.Flatten(polygon);

// Then tesselate polygon interior and add vertices and indices.
var indices = EarcutLibrary.Earcut(flatData.Vertices, flatData.Holes, flatData.Dim);

indices.Reverse();
var indices = EarcutLibrary.Earcut(coordinates, holes, 2);

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

Expand Down
2 changes: 0 additions & 2 deletions Assets/Mapzen/Unity/SceneGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public static void Generate(SceneGroup group, Transform parent, GameObjectOption
}
else
{
group.meshData.FlipIndices();

if (group.meshData.Meshes.Count > 1)
{
var gameObject = new GameObject(group.ToString());
Expand Down

0 comments on commit a9f87ef

Please sign in to comment.