Search Unity

Dynamic mesh help

Discussion in 'Scripting' started by recon, Nov 11, 2010.

  1. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    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!
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I cant guarantee that this is going to work perfectly, but its kind of the code you are looking for.

    Code (csharp):
    1.  
    2. var i;
    3. var r;
    4. var c;
    5. var verts=new Vector3[rows * cols];
    6. var tris=new Vector3[(rows-1) * (cols-1)];
    7. var norms=new Vector3[rows * cols];
    8. var uvs = new Vector2[rows * cols];
    9. //setup your verts
    10. for(c=0; c<cols; c++){ // 0 to 15
    11.     for(r=0; r<rows; r++){ // 0 to 15
    12.         verts[r+c*rows]=Vector3(r * spacing, Random.value * maxHeight, c * spacing);
    13.         norms[r+c*rows]=Vector3(0,1,0);
    14.         uvs[r+c*rows]=Vector2(r / (rows-1), c / (cols-1));
    15.     }
    16. }
    17. var seg=0;
    18. for(c=0; c<cols-1; c++){ // 0 to 14
    19.     for(r=0; r<rows-1; r++){ // 0 to 14
    20.         tris[seg * 6+0]=r+c*cols;
    21.         tris[seg * 6+1]=r+(c+1)*cols;
    22.         tris[seg * 6+2]=r+1+c*cols;
    23.  
    24.         tris[seg * 6+3]=r+(c+1)*(cols+1);
    25.         tris[seg * 6+4]=r+1+c*cols;
    26.         tris[seg * 6+5]=r+1+(c+1)*cols;
    27.     }
    28.     seg++;
    29. }
    30.  
     
  3. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    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?
     
  4. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    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)
     
    Last edited: Nov 11, 2010
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    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):
    1.  
    2. vertices[segmentCount * 4 + 0] = last.posl;
    3. vertices[segmentCount * 4 + 1] = last.posr;
    4. vertices[segmentCount * 4 + 2] = curr.posl;
    5. vertices[segmentCount * 4 + 3] = curr.posr;
    6.  
    7. uvs[segmentCount * 4 + 0] = new Vector2(0, 1);
    8. uvs[segmentCount * 4 + 1] = new Vector2(0, 0);
    9. uvs[segmentCount * 4 + 2] = new Vector2(1, 1);
    10. uvs[segmentCount * 4 + 3] = new Vector2(1, 0);
    11.  
    12. triangles[segmentCount * 6 + 0] = segmentCount * 4 + 0;
    13. triangles[segmentCount * 6 + 2] = segmentCount * 4 + 1;
    14. triangles[segmentCount * 6 + 1] = segmentCount * 4 + 2;
    15.  
    16. triangles[segmentCount * 6 + 3] = segmentCount * 4 + 2;
    17. triangles[segmentCount * 6 + 5] = segmentCount * 4 + 1;
    18. triangles[segmentCount * 6 + 4] = segmentCount * 4 + 3;
    19.  
     
  6. recon

    recon

    Joined:
    Nov 28, 2009
    Posts:
    119
    The segment counting worked like a charm, thank you so much for helping me with this problem!