• Advertisement
Sign in to follow this  

Building Mesh topology the math behind it

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

If you intended to correct an error in the post then please contact us.

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;
vertices.Add(new Vector3(x, -y, 0));
}
}

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;
faces.Add(a);
faces.Add(b);
faces.Add(c);
faces.Add(d);
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;

MeshFilter mf = (MeshFilter)plane.AddComponent(typeof(MeshFilter));
Mesh m = mf.mesh;
m.vertices = vertices.ToArray();
m.SetIndices(faces.ToArray(), MeshTopology.Quads, 0);
m.uv = faceVertexUvs.ToArray();
MeshRenderer renderer = plane.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
Material material = (Material)Resources.Load("Red", typeof(Material));

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

pwE1e.png

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement