c# - Unity mesh triangles flipped -
i'm trying make function takes in height map , creates single isometric mesh based on input. got make triangles represent top of cubes getting go down sides of cubes seem having trouble. function isnt complete , makes 2 of sides ran trouble before finishing it.
basically seems happening triangles flipped , others aren't , have no idea why. can see there sides have both triangles facing same direction while others have 1 of triangles visible second on of square facing other way.
it seems making square runs 1 side of mesh other leaving long lines.
i'm new meshes , 3d in general , though easier has turned out be. pretty sure way map uv totally wrong come @ 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>(); (int y = 0; y < height; y ++) { (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) { (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: , same mesh without loop step down code
http://imageshack.com/a/img924/9072/hdrzfm.png
thank help. https://imageshack.us/i/pohdrzfmp
as have stated problem in rendering , cause can orientations of vertex index.
which side triangle visible determined orientation of vertex indices. default, if arranged in clockwise direction triangle considered forward-facing , visible. counter-clockwise triangles discarded don't need spend time rendering insides of objects, typically not meant seen anyway.
so have sure vertex index orientation clockwise.
and other problem of long line try changing indexs order before assigning triangles. 012 120 sure orientation.
Comments
Post a Comment