Dynamic Mesh Through Points

Hi Guys.

I am trying to generate a mesh through some points, for now I’m just using game objects and setting the vertices to that, but eventually once I have it working in basic form I will get more advanced and dynamically feed vector3s to it to constantly generate my mesh.

But what I don’t really understand is triangles :S can someone help me out?

Here is my code: it should create a plane sort of mesh through 6 points, it does 4 of them fine, but wort do six:

using UnityEngine;
using System.Collections;
//PlaneExtrude1
[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))]
public class Extrusion : MonoBehaviour
{
    public Transform Point1;
    public Transform Point2;
	public Transform Point3;
    public Transform Point4;
	public Transform Point5;
    public Transform Point6;

    private Mesh m_Mesh = null;
	void Start ()
	{
        m_Mesh = new Mesh();
        Vector3[] vertices = new Vector3[6];
		
        int[] tris = new int[9];
		
		for (int i = 0;i<4;i++)
            vertices[i] = new Vector3();        
        
        tris[0] = 0;
        tris[1] = 1;
        tris[2] = 2;

        tris[3] = 2;
        tris[4] = 3;
        tris[5] = 0;
		
		tris[6] = 0;
        tris[7] = 1;
        tris[8] = 2;


        m_Mesh.vertices = vertices;
        m_Mesh.triangles = tris;
        MeshFilter filter = GetComponent<MeshFilter>();
        filter.sharedMesh = m_Mesh;
	}
	
	public Vector3[] newVertices;
	
    void UpdateMesh(Vector3 P1, Vector3 P2)
    {
        //Vector3[] newVertices = m_Mesh.vertices;
        newVertices = m_Mesh.vertices;

        //vertices[2] = P2 + newDir;
        //vertices[3] = P1 + newDir;
		
        newVertices[0] = P1;
        newVertices[1] = P2;
		newVertices[2] = Point3.transform.position;
		newVertices[3] = Point4.transform.position;
		newVertices[4] = Point5.transform.position;
		newVertices[5] = Point6.transform.position;
		
        m_Mesh.vertices = newVertices;
        m_Mesh.RecalculateBounds();
        m_Mesh.RecalculateNormals();
    }
	
	void Update ()
	{
        UpdateMesh(Point1.position, Point2.position);
	}
}

Any help would be appreciated! Thanks!

1 Like

triangle groups need to be orderd clock wise when facing the face of the triangle. So for instance:

you are facing the face of a square. The top left point is v1, top right is v 2, bottom left is vert 3 bottom right is vert 3.

A square consists of two triangles. So you would need a 6 length long int[ ].
An example ordering of triangles would be:
123, 243 or 312, 432 both of them are ordered in a clock wise pattern around the triangle meaning the face will be towards direction you are looking from.

Clearly its a bit difficult to explain in text.
Be sure to check out Polygon mesh - Wikipedia for more detailed info on polygon meshes.