# Building Mesh topology the math behind it

This topic is 1428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to know the math of building a topology like that from a set of vertices P1,P2,P3, and get the desired indices for those triangles. Currently I'm just making a plane but I don't take account of vertices P1, P2 or make the first triangle and the last one.

public static void CreatePlane(int width, int height, Transform transform, int segmentsWidth, int segmentsHeight)
{
int ix,
iy,
widthHalf = width / 2,
heightHalf = height / 2,
gridX = segmentsWidth,
gridY = segmentsHeight,
gridX1 = gridX + 1,
gridY1 = gridY + 1,
segmentWidth = width / gridX,
segmentHeight = height / gridY;

Vector3 normal = new Vector3(0, 0, 1);
List<Vector3> vertices = new List<Vector3>();
List<Vector3> vertexNormals = new List<Vector3>();
List<int> faces = new List<int>();
List<Vector2> faceVertexUvs = new List<Vector2>();

int cnt = 0;
for (iy = 0; iy < gridY1; iy++)
{
for (ix = 0; ix < gridX1; ix++)
{
float x = ix * segmentWidth - widthHalf;
float y = iy * segmentHeight - heightHalf;
}
}

for (iy = 0; iy < gridY; iy++)
{
for (ix = 0; ix < gridX; ix++)
{
int a = ix + gridX1 * iy;
int b = ix + gridX1 * (iy + 1);
int c = (ix + 1) + gridX1 * (iy + 1);
int d = (ix + 1) + gridX1 * iy;
faceVertexUvs.Add(new Vector2(ix / gridX, iy / gridY));
faceVertexUvs.Add(new Vector2(ix / gridX, (iy + 1) / gridY));
faceVertexUvs.Add(new Vector2((ix + 1) / gridX, (iy + 1) / gridY));
faceVertexUvs.Add(new Vector2((ix + 1) / gridX, iy / gridY));
}
}
// create mesh
GameObject plane = new GameObject("TreePlane");
plane.transform.position = transform.position;
plane.transform.rotation = transform.transform.rotation;

Mesh m = mf.mesh;
m.vertices = vertices.ToArray();
m.uv = faceVertexUvs.ToArray();
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;

if (renderer != null)
{
renderer.sharedMaterial = material;
}
m.RecalculateBounds();
m.RecalculateNormals();
}

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015575
×