Search Unity

[Solved ]What wrong with my pyramid mesh generation code?

Discussion in 'Scripting' started by EternalMe, Jan 17, 2022.

  1. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    Code (CSharp):
    1. private void CreatePyramid()
    2.     {
    3.         Vector3[] vertices = {
    4.             new Vector3 (0, 0, 0),
    5.             new Vector3 (-1, 1, -1),
    6.             new Vector3 (-1, 1, 1),
    7.             new Vector3 (1, 1, 1),
    8.             new Vector3 (1, 1, -1)
    9.         };
    10.  
    11.         int[] triangles = {
    12.             0, 1, 2,
    13.             0, 2, 3,
    14.             0, 3, 4,
    15.             0, 1, 4,
    16.             1, 2, 4,
    17.             4, 2, 3
    18.         };
    19.  
    20.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    21.         mesh.Clear();
    22.         mesh.vertices = vertices;
    23.         mesh.triangles = triangles;
    24.        
    25.         mesh.RecalculateNormals();
    26.         mesh.RecalculateBounds();
    27.         mesh.Optimize();
    28.     }
    Its visible from some view angles and from others not :/
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Are the faces rotated the right way round? That's the first thing that springs to mind for me if the mesh is there.
     
    kittik likes this.
  3. EternalMe

    EternalMe

    Joined:
    Sep 12, 2014
    Posts:
    183
    Yes, the winding order was inccorect.
     
    Lethn likes this.