0

I am trying to create a plane mesh in unity. I am able to obtain a mesh with two triangles, but I am not able to obtain a mesh with more than two in a row. I am trying to obtain a lengthy mesh with many triangles in a row connected together. I hope u would understand if you have a look at my code.

My code has no error but I am not able to see the mesh. I need help.

private float space = 40 / 200;
    MeshFilter mf;
    Mesh mesh;
    private Vector3[] vertices = new Vector3[400];

    void Start () 
    {
        mf = GetComponent<MeshFilter>();
        mesh = mf.mesh;
        mesh.Clear();

        #region Vertices
        for (int i = 0; i < 200; i++)
        {
            vertices[i] = new Vector3(-20 + i * space, -6, 0);          //Top vertices
            vertices[i+200] = new Vector3(-20 + i * space, -10, 0);     //Bottom vertices
        }
        #endregion

        #region Normals
        Vector3[] Normals = new Vector3[400];

        for(int i = 0; i < 400; i++)
            Normals[i] = Vector3.back;
        #endregion

        #region uvs
        Vector2[] uvs = new Vector2[400];

        Vector2 _00 = new Vector2(0, 0);
        Vector2 _10 = new Vector2(1, 0);
        Vector2 _01 = new Vector2(0, 1);
        Vector2 _11 = new Vector2(1, 1);

        for(int i=0; i<200; i++)
        {
            uvs[i] = new Vector2(i/199, 1);
            uvs[i+200] = new Vector2(0, i/199);
        }

        #endregion

        #region Triangles
        int[] Triangle = new int[(200-1) * 2 * 3];
        int t = 0;

        for(int i=0; i<200 - 1; i++)
        {
            Triangle[t++] = i;
            Triangle[t++] = i + 200 + 1;
            Triangle[t++] = i + 200;

            Triangle[t++] = i;
            Triangle[t++] = i + 1;
            Triangle[t++] = i + 200 + 1;
        }
        #endregion

        mesh.vertices = vertices;
        mesh.normals = Normals;
        mesh.uv = uvs;
        mesh.triangles = Triangle;

        mesh.RecalculateBounds();
        mesh.Optimize();
    }
Steven
  • 166,672
  • 24
  • 332
  • 435
RameshRaju
  • 23
  • 1
  • 5
  • Check for minor but crucial mistakes: if you put a `Debug.Log()` call somewhere in your code, is it shown in the console? Does the object in question have a MeshRenderer attached? – rutter Sep 12 '13 at 00:23
  • @rutter Yes, its attached. I will check again. – RameshRaju Sep 12 '13 at 03:15
  • 1
    Just in case that you need it in editor mode, there is a [CreatePlane script](http://wiki.unity3d.com/index.php?title=CreatePlane) in the Unify Community Wiki. – Kay Sep 12 '13 at 09:15

1 Answers1

1

It's not clear if this is the root problem but you are doing integer arithmetic. float space = 40 / 200; always equals 0, i/199 always equals 0 for i < 199.

That means your vertices look like:

vertices[i] = new Vector3(-20 + i * 0, -6, 0);          //Top vertices
vertices[i+200] = new Vector3(-20 + i * 0, -10, 0);

And UVs all look like:

uvs[i] = new Vector2(0, 1);
uvs[i+200] = new Vector2(0, 0);

You could change float space = 40 / 200; to float space = (float)40 / 200; and i/199 to (float)i/199

Community
  • 1
  • 1
Jerdak
  • 3,997
  • 1
  • 24
  • 36