This site uses strictly necessary cookies. More Information

X- Home /

# Need Help with 3D triangulation for Android

I just ran into the first big problem with my current Android project. I want to display a 3d Level that is generated from 2d data. (List of obstacles with defined corner points + a fix height stored in a json file.) I managed to draw them onto the floor using some code I found online and the triangulation code from the wiki. Now to make them 3d I would have to move the top part (which I already have) up (that is eays :P) and then draw a border/walls around it.

The Problem is that all the triangulation code I found online is 2d only. In order to draw my walls/ borders I need a 3rd axis(up). But whenever I tried to modify my code to add 3d support Unity stopped rendering any triangles at all :(

Would be great If anyone could help me with this.

This is what I currently use:

```
private static Mesh CreateMesh(List<Vector3> verts)
{
var tris = new Triangulator(verts.Select(x => x.ToVector2xz()).ToArray());
var mesh = new Mesh();
var vertices = verts.Select(x => new Vector3(x.x, x.y, x.z)).ToList();
var indices = tris.Triangulate().ToList();
//var uv = new List<Vector2>();
var n = vertices.Count;
for (int index = 0; index < n; index++)
{
var v = vertices[index];
vertices.Add(new Vector3(v.x, v.y, v.z));
}
for (int i = 0; i < n - 1; i++)
{
indices.Add(i);
indices.Add(i + n);
indices.Add(i + n + 1);
indices.Add(i);
indices.Add(i + n + 1);
indices.Add(i + 1);
}
indices.Add(n - 1);
indices.Add(n);
indices.Add(0);
indices.Add(n - 1);
indices.Add(n + n - 1);
indices.Add(n);
mesh.vertices = vertices.ToArray();
mesh.triangles = indices.ToArray();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;
}
```

**Answer** by Jacky2611
·
Jul 16, 2016 at 07:00 PM

Figured it out on my own. Sometimes a cup of hot choclat works wonders...

```
private static Mesh CreateMesh(List<Vector3> verts, float height)
{
var tris = new Triangulator(verts.Select(x => x.ToVector2xz()).ToArray());
var mesh = new Mesh();
var vertices = verts.Select(x => new Vector3(x.x, height, x.z)).ToList();
var indices = tris.Triangulate().ToList();
//var uv = new List<Vector2>();
var numberOfPointsPerRing = vertices.Count;
for (int index = 0; index < numberOfPointsPerRing; index++)
{
var v = vertices[index];
vertices.Add(new Vector3(v.x, 0, v.z));
}
for (int i = 0; i < numberOfPointsPerRing - 1; i++)
{
indices.Add(i);
indices.Add(i + numberOfPointsPerRing);
indices.Add(i + numberOfPointsPerRing + 1);
indices.Add(i);
indices.Add(i + numberOfPointsPerRing + 1);
indices.Add(i + 1);
}
indices.Add(numberOfPointsPerRing - 1);
indices.Add(numberOfPointsPerRing);
indices.Add(0);
indices.Add(numberOfPointsPerRing - 1);
indices.Add(numberOfPointsPerRing + numberOfPointsPerRing - 1);
indices.Add(numberOfPointsPerRing);
//Lower pointposition hinzufügen
foreach(Vector3 point in verts)
{
vertices.Add(new Vector3(point.x, 0, point.z));
}
int pointAbove = numberOfPointsPerRing;
for(int i=0; i<numberOfPointsPerRing; i++)
{
//right upper triangle
indices.Add(i); //right down
indices.Add(i+1+pointAbove);//left up
indices.Add(i+ pointAbove); //right up
//left lower triangle
indices.Add(i +1); //right down
indices.Add(i + 1 + pointAbove);//left up
indices.Add(i); //right up
}
mesh.vertices = vertices.ToArray();
mesh.triangles = indices.ToArray();
mesh.RecalculateNormals();
mesh.RecalculateBounds();
return mesh;
}
}
}
```

Hi, Is this code specific to only your case of building walls or would it work in general with any gameobject? I'm new at this so I'm not able to figure this out.

Thanks, Pranav

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.