Dynamically Generate Plane Mesh in App from Vertices

Hi,

I am trying to generate a plane mesh in the app from 4 user defined vertices. This is for a hololens application, and I am getting the vertices from the cursor position. I have the following script attached to an empty game object with a material, a mesh filter, and a mesh renderer component. currently my application recognizes the 4 vertices inputted by the user, but no visual mesh seems to be created. Can anyone please tell me what I need to do differently? Thanks.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EditPlane : MonoBehaviour {

public TempStateManager stateManager;
public TextMesh debug;
private List<Vector3> vertexHolder;
private Vector3 firstVertex;
public GameObject cursor;
private int input;
// Use this for initialization
void Start() {
    vertexHolder = new List<Vector3>();
    input = 0;
}

void OnSelect()
{
    if (stateManager.state == "setVertices")
    {

        if (input < 4)
        {
            if (input == 0)//first time
            {
                firstVertex = cursor.transform.position;//save first y position
                //add first vertex (vertex 0)
                vertexHolder.Add(firstVertex);
                Debug.Log("added first vertex");
                debug.text = "added first vertex";
            }
            else 
            {
                Vector3 newVertex = new Vector3(cursor.transform.position.x, firstVertex.y, cursor.transform.position.z);
                //add vertices 1, 2, and 3
                vertexHolder.Add(newVertex);
                Debug.Log("added subsequent vertex");
                debug.text = "added subsequent vertex";
                if(input ==3)//last input
                {
                    debug.text = "last vertex";

                    //triangles
                    int[] tri = new int[6];
                    tri[0] = 0;
                    tri[1] = 2;
                    tri[2] = 1;

                    tri[3] = 2;
                    tri[4] = 3;
                    tri[5] = 1;

                    //normals
                    Vector3[] normals = new Vector3[4];
                    normals[0] = Vector3.up;
                    normals[1] = Vector3.up;
                    normals[2] = Vector3.up;
                    normals[3] = Vector3.up;

                    Mesh mesh = new Mesh();
                    GetComponent<MeshFilter>().mesh = mesh;
                    mesh.SetVertices(vertexHolder);
                    mesh.triangles = tri;
                    mesh.normals = normals;
                    debug.text = "set mesh";
                }
            }
            input++;
        }
    }

}

}

you can try my below suggestions :

  • Apply the new generated mesh to meshfilter after applying its vertices and normals etc
  • Where are the UVs ?
  • Try this triangle sequence 0,2,1,0,3,2
  • You are setting Vector3.Up as the normales, make sure that your camera is looking from correct diection
  • You are setting the positions of vertices according to the position of gameobject cursor, make sure this gameobject is changing its position otherwise all your vertices are going to be placed on exact same position and you won’t be able to see the mesh