Skip to content

Commit

Permalink
make triangulation methods optional (triangulation.net vs single clas…
Browse files Browse the repository at this point in the history
…s thing)
  • Loading branch information
brnkhy committed Oct 4, 2016
1 parent 350fd04 commit a5dc69f
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions Assets/MapzenGo/Models/Factories/BuildingFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace MapzenGo.Models.Factories
{
public class BuildingFactory : Factory
{
[SerializeField] private bool _useTriangulationNet;
public override string XmlTag { get { return "buildings"; } }
private HashSet<string> _active = new HashSet<string>();
[SerializeField] protected BuildingFactorySettings FactorySettings;
Expand Down Expand Up @@ -215,29 +216,10 @@ private static void SetProperties(JSONObject geo, Building building, BuildingSet

private void CreateMesh(List<Vector3> corners, float height, BuildingSettings typeSettings, MeshData data, Vector2 min, Vector2 size)
{
_mesh = new TriangleNet.Mesh();
var vertsStartCount = _useTriangulationNet
? CreateRoofTriangulation(corners, height, data)
: CreateRoofClass(corners, height, data);

var inp = new InputGeometry(corners.Count);

for (int i = 0; i < corners.Count; i++)
{
var v = corners[i];
inp.AddPoint(v.x, v.z);
inp.AddSegment(i, (i + 1) % corners.Count);
}
_mesh.Behavior.Algorithm = TriangulationAlgorithm.SweepLine;
_mesh.Behavior.Quality = true;
_mesh.Triangulate(inp);

var vertsStartCount = data.Vertices.Count;
data.Vertices.AddRange(corners.Select(x => new Vector3(x.x, height, x.z)).ToList());

foreach (var tri in _mesh.Triangles)
{
data.Indices.Add(vertsStartCount + tri.P1);
data.Indices.Add(vertsStartCount + tri.P0);
data.Indices.Add(vertsStartCount + tri.P2);
}

foreach (var c in corners)
{
Expand Down Expand Up @@ -301,6 +283,41 @@ private void CreateMesh(List<Vector3> corners, float height, BuildingSettings ty
}
}

private static int CreateRoofClass(List<Vector3> corners, float height, MeshData data)
{
var vertsStartCount = data.Vertices.Count;
var tris = new Triangulator(corners);
data.Vertices.AddRange(corners.Select(x => new Vector3(x.x, height, x.z)).ToList());
data.Indices.AddRange(tris.Triangulate().Select(x => vertsStartCount + x));
return vertsStartCount;
}

private int CreateRoofTriangulation(List<Vector3> corners, float height, MeshData data)
{
_mesh = new TriangleNet.Mesh();
var inp = new InputGeometry(corners.Count);
for (int i = 0; i < corners.Count; i++)
{
var v = corners[i];
inp.AddPoint(v.x, v.z);
inp.AddSegment(i, (i + 1)%corners.Count);
}
_mesh.Behavior.Algorithm = TriangulationAlgorithm.SweepLine;
_mesh.Behavior.Quality = true;
_mesh.Triangulate(inp);

var vertsStartCount = data.Vertices.Count;
data.Vertices.AddRange(corners.Select(x => new Vector3(x.x, height, x.z)).ToList());

foreach (var tri in _mesh.Triangles)
{
data.Indices.Add(vertsStartCount + tri.P1);
data.Indices.Add(vertsStartCount + tri.P0);
data.Indices.Add(vertsStartCount + tri.P2);
}
return vertsStartCount;
}

private void CreateGameObject(BuildingType kind, MeshData data, GameObject main)
{
var go = new GameObject(kind + " Buildings");
Expand Down

0 comments on commit a5dc69f

Please sign in to comment.