I was trying to use the code from the Procedural example called "Heightmap generator" to create a tile grid for a 2d game but I just realized that I need a unique set of uv coordinates for each square. I tried expanding the vertices and uv arrays to be 4 times as big to ensure that each tile can keep their own uvs, but now Im having trouble assigning the triangle indexes since Im not entirely sure how that part works. Am I making any sense? Is what I am trying to accomplish even possible? Thanks a bunch!
I cant guarantee that this is going to work perfectly, but its kind of the code you are looking for. Code (csharp): var i; var r; var c; var verts=new Vector3[rows * cols]; var tris=new Vector3[(rows-1) * (cols-1)]; var norms=new Vector3[rows * cols]; var uvs = new Vector2[rows * cols]; //setup your verts for(c=0; c<cols; c++){ // 0 to 15 for(r=0; r<rows; r++){ // 0 to 15 verts[r+c*rows]=Vector3(r * spacing, Random.value * maxHeight, c * spacing); norms[r+c*rows]=Vector3(0,1,0); uvs[r+c*rows]=Vector2(r / (rows-1), c / (cols-1)); } } var seg=0; for(c=0; c<cols-1; c++){ // 0 to 14 for(r=0; r<rows-1; r++){ // 0 to 14 tris[seg * 6+0]=r+c*cols; tris[seg * 6+1]=r+(c+1)*cols; tris[seg * 6+2]=r+1+c*cols; tris[seg * 6+3]=r+(c+1)*(cols+1); tris[seg * 6+4]=r+1+c*cols; tris[seg * 6+5]=r+1+(c+1)*cols; } seg++; }
Yes that is correct, although what I wanted was to build the mesh so that every polygon pair (aka tile) has their edges separate from each other. If I use that setup I cannot get the uv placement of the tile textures to render properly. Any ideas?
This image might help explain the pickle Im in: As you can se the uvs are getting messed up in the first three tiles (9 polygons) because they share uv coordinates with the last tile. My question is basically how do I make sure all vertices have their own uv coordinates assigned to them? I tried so many ways but always end up with weird polygon shapes >_< Edit - (The part at the bottom left is the sprite atlas in which the tile textures are stored)
you could build the verts and messes at the same time... so do a count from 0 to 15 both ways, each time you build a "face, you build it with tverts and independent verts. That is almost exactly what the stock skidmarks script did. Code (csharp): vertices[segmentCount * 4 + 0] = last.posl; vertices[segmentCount * 4 + 1] = last.posr; vertices[segmentCount * 4 + 2] = curr.posl; vertices[segmentCount * 4 + 3] = curr.posr; uvs[segmentCount * 4 + 0] = new Vector2(0, 1); uvs[segmentCount * 4 + 1] = new Vector2(0, 0); uvs[segmentCount * 4 + 2] = new Vector2(1, 1); uvs[segmentCount * 4 + 3] = new Vector2(1, 0); triangles[segmentCount * 6 + 0] = segmentCount * 4 + 0; triangles[segmentCount * 6 + 2] = segmentCount * 4 + 1; triangles[segmentCount * 6 + 1] = segmentCount * 4 + 2; triangles[segmentCount * 6 + 3] = segmentCount * 4 + 2; triangles[segmentCount * 6 + 5] = segmentCount * 4 + 1; triangles[segmentCount * 6 + 4] = segmentCount * 4 + 3;