简体   繁体   中英

Unity mesh triangles flipped

I'm trying to make a function that takes in a height map and then creates a single isometric mesh based on the input. I got it to make the triangles that would represent the top of the cubes fairly easily but getting it to go down the sides of the cubes is where I seem to be having trouble. The function isnt complete and only makes two of the sides as I ran into trouble before finishing it.

Basically what seems to be happening is that some triangles are flipped and others aren't and I have no idea why. You can see there are some sides that have both triangles facing the same direction while others only have one of the triangles visible as the second on of the square is facing the other way.

It also seems to be occasionally making a square that runs from one side of the mesh to the other leaving those long lines.

I'm really new to meshes and 3d in general and though this would be much easier than it has turned out to be. I am also pretty sure that the way I map the UV is totally wrong but that is something that will come at a later time.

public static MeshData generateIsoMesh(float [,] heightMap, int heightMax, AnimationCurve heightCurve)
{
    int height = heightMap.GetLength(0);
    int width = heightMap.GetLength(1);

    int triangleNum = 0;

    int blockHeight = 0 ;
    int nextHeight = 0;
    int heightDif = 0;

    List<Vector3> vecList = new List<Vector3>();
    List<int> triangleList = new List<int>();
    List<Vector2> uvList = new List<Vector2>();



    for (int y = 0; y < height; y ++)
    {
        for (int x = 0; x < width; x ++)
        {

            blockHeight = ((int)(heightMap[x, y]*10)) * heightMax;

            vecList.Add(new Vector3(x, blockHeight , y));
            vecList.Add(new Vector3(x, blockHeight, y+ 1));
            vecList.Add(new Vector3(x+1, blockHeight, y+1));
            vecList.Add(new Vector3(x + 1, blockHeight, y));

            uvList.Add(new Vector2(x / (float)width, y / (float)height));
            uvList.Add(new Vector2(x / (float)width, y / (float)height));
            uvList.Add(new Vector2(x / (float)width, y / (float)height));
            uvList.Add(new Vector2(x / (float)width, y / (float)height));

            if ((x < width-1) && (y < height-1))
            {
                triangleList.Add(triangleNum);
                triangleList.Add(triangleNum + 1);
                triangleList.Add(triangleNum + 2);

                triangleList.Add(triangleNum );
                triangleList.Add(triangleNum + 2 );
                triangleList.Add(triangleNum + 3);
                triangleNum += 4;
            }
            if (x < width-1 && x > 3 && x > 0)
            {

                nextHeight = ((int)(heightMap[x+1,y]*10)) * heightMax;
                heightDif = (nextHeight - blockHeight)/heightMax;
                if (heightDif > 0)
                {
                    for (int z = 0; z < heightDif; z ++)
                    {
                        uvList.Add(new Vector2(x / (float)width, y / (float)height));
                        uvList.Add(new Vector2(x / (float)width, y / (float)height));
                        vecList.Add(new Vector3(x + 1, blockHeight - (1 + z), y));
                        vecList.Add(new Vector3(x + 1, blockHeight - (1 + z), y + 1));

                        triangleList.Add(triangleNum - 1);
                        triangleList.Add(triangleNum - 2);
                        triangleList.Add(triangleNum + 1);

                        triangleList.Add(triangleNum - 1);
                        triangleList.Add(triangleNum + 1);
                        triangleList.Add(triangleNum    );
                        triangleNum += 4;

                    }
                }
                else if (heightDif < 0)
                {
                    ;
                }
            }
        }
    }

The output mesh: and the same mesh without the loop that does the step down code

http://imageshack.com/a/img924/9072/HdrZFm.png

thank you for all your help. https://imageshack.us/i/poHdrZFmp

As you have stated the problem is in rendering and its cause can be orientations of vertex index.

Which side a triangle is visible from is determined by the orientation of its vertex indices. By default, if they are arranged in a clockwise direction the triangle is considered to be forward-facing and visible. Counter-clockwise triangles are discarded so we don't need to spend time rendering the insides of objects, which are typically not meant to be seen anyway.

So you always have to be sure that vertex index orientation is clockwise.

And your other problem of long line try changing the indexs order before assigning it to triangles. Like 012 to 120 be sure about orientation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM