Search Unity

Resolved Generated Cube Mesh has strange edge lighting

Discussion in 'Scripting' started by Crepican55, Jan 29, 2021.

  1. Crepican55

    Crepican55

    Joined:
    Sep 11, 2019
    Posts:
    2
    First of all, I wasn't sure whether to put this in scripting or graphics, so I apologise if I have chosen incorrectly

    I have just started generating 3D meshes by script and am trying to generate a mesh based on a 3D array - and I was successful, except for the fact that the mesh has strange lighting, the quads near the edges and corners of the mesh seem to be brighter and reflect more light, and this is not what I want so I was wondering if there was something I'd missed

    I'd also had similar issues when generating a single quad

    here are some images of my mesh vs a normal cube using the same material
    sad mesh lighting.png

    sad mesh lighting 2.png


    here is the code I am using :
    Code (CSharp):
    1. int TotalVertexNum = (Sizes.x+1) * (Sizes.y+1) * (Sizes.z+1);
    2.         Vector3[] Vertices = new Vector3[TotalVertexNum];
    3.  
    4.         // x->, y->, z->
    5.         // generating the vertices
    6.         for (int i = 0, z = 0; z <= Sizes[2]; z++)
    7.         {
    8.             for (int y = 0; y <= Sizes[1]; y++)
    9.             {
    10.                 for (int x = 0; x <= Sizes[0]; x++)
    11.                 {
    12.                     Vertices[i] = new Vector3(x, y, z);
    13.                     i++;
    14.                 }
    15.             }
    16.         }
    17.  
    18.         int yRow = Sizes.x + 1; // a constant representing 1 in the positive y direction
    19.         int zRow = (Sizes.x+1) * (Sizes.y+1); // a constant representing 1 in the z direction
    20.  
    21.         // these are functions that, given an offset (a position) return an array of the triangles that represent a quad
    22.         int[] XNegFace(int Offset) { return new int[6] { Offset, Offset+zRow, Offset+zRow+yRow, Offset+zRow+yRow, Offset+yRow, Offset }; }  // xneg (left)
    23.         int[] XPosFace(int Offset) { return new int[6] { Offset+1, Offset+yRow+1, Offset+zRow+yRow+1, Offset+zRow+yRow+1, Offset+zRow+1, Offset+1 }; } // xpos (right)
    24.         int[] YNegFace(int Offset) { return new int[6] { Offset, Offset+1, Offset+zRow+1, Offset+zRow+1, Offset+zRow, Offset }; } // yneg (down)
    25.         int[] YPosFace(int Offset) { return new int[6] { Offset+yRow, Offset+zRow+yRow, Offset+zRow+yRow+1, Offset+zRow+yRow+1, Offset+yRow+1, Offset+yRow}; } // ypos (up)
    26.         int[] ZNegFace(int Offset) { return new int[6] { Offset, Offset+yRow, Offset+yRow+1, Offset+yRow+1, Offset+1, Offset }; } // zneg (backwards)
    27.         int[] ZPosFace(int Offset) { return new int[6] { Offset+zRow, Offset+zRow+1, Offset+zRow+yRow+1, Offset+zRow+yRow+1, Offset+zRow+yRow, Offset+zRow }; } // zpos (forward)
    that is all the code I think is relevant but if it in not enough I will add more