Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Applying a texture to a mesh for a tilemap

Discussion in 'Scripting' started by Afropenguinn, Jun 25, 2015.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I have a mesh that is created via code, it is a flat-ish plane (there are heights on some tiles). It is made for a tilemap. I have some tile data layed out but I understand there are several ways of applying a texture to the mesh. The two ways I know of would be to essentially double the number of vertices and then I can UV map a texture atlas, or to just create a texture that spans the entire map from a color array. What are your thoughts on this, what other methods are there? How would you compare these two methods that I know of?

    Here is the mesh generation code if you wanted to have a look:
    Code (CSharp):
    1. //generate mesh data
    2. Vector3[] verticies = new Vector3[(x + 1) * (y + 1)];
    3. int[] triangles = new int[(3 * 2) * (x * y)];
    4. Vector3[] normals = new Vector3[verticies.Length];
    5. Vector2[] uv = new Vector2[verticies.Length];
    6.  
    7. for (int i=0; i<x+1; i+=1) //verticies, normals, and uv maps
    8. {
    9.     for (int ii=0; ii<y+1; ii+=1)
    10.     {
    11.         verticies[ii * (x + 1) + i] = new Vector3(i * tileSize.x, 0f, ii * tileSize.y) - new Vector3(Mathf.Floor((x * tileSize.x) / 2), Random.Range(0f, .1f), Mathf.Floor((y * tileSize.y) / 2));
    12.         normals[ii * (x + 1) + i] = Vector3.up;
    13.         uv[ii * (x + 1) + i] = new Vector2((float)i / x, (float)ii / y);
    14.     }
    15. }
    16.  
    17. for (int i=0; i<x; i+=1) //triangles
    18. {
    19.     for (int ii=0; ii<y; ii+=1)
    20.     {
    21.         int tileIndex = (ii * x + i) * 6;
    22.         triangles[tileIndex + 0] = ii * (x + 1) + i;
    23.         triangles[tileIndex + 1] = (ii + 1) * (x + 1) + i;
    24.         triangles[tileIndex + 2] = (ii + 1) * (x + 1) + (i + 1);
    25.  
    26.         triangles[tileIndex + 3] = ii * (x + 1) + i;
    27.         triangles[tileIndex + 4] = (ii + 1) * (x + 1) + (i + 1);
    28.         triangles[tileIndex + 5] = ii * (x + 1) + (i + 1);
    29.         }
    30. }
    x and y are the tile map size, and tileSize is a vector2 that controls the size of each tile

    And as always, thanks for your help in advance Unity Community :)
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    This is probably my favourite. If you're targeting DX11 you could also just output points, and let the GPU render quads at those points using shaders.
    This would probably limit the resolution of the textures pretty heavily, or force you to split into a lot more mesh chunks. If you're aiming for a single color per tile, you'd probably be better off just setting vertex colors rather than using a texture.
     
  3. Foo-Byte

    Foo-Byte

    Joined:
    Jun 26, 2015
    Posts:
    12
    I have been working on something similar, although I'd been trying to use vertex colours to act like a splat map for smoother texture transitions than strict tiles. I haven't been able to find (or write) a splat-type shader that works on Meshes instead of Terrains, though.
     
  4. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Currently I am using the UV map method with a texture atlas for the tiles. The problem I am having now is that there are small lines in between every tile. Not entirely sure why.

    Look like a problem you guys would recognize? When I just use a single tile texture in stead of the atlas (though I still use the atlas code) the lines vanish.
     
  5. reinfeldx

    reinfeldx

    Joined:
    Nov 23, 2013
    Posts:
    162
    Re: Lines between tiles - I'm not sure if you're still looking for an answer to this, but I recently solved a similar-looking problem. The issue seemed to be that Unity has a hard time applying the textures precisely, so I reworked my atlas to have a pixel or two of "bleed" around each sprite. Solved it for me and appeared to be the best option after a reasonable amount of research.

    See more info here: http://forum.unity3d.com/threads/tile-map-tearing-problems.225777/#post-1507246
     
    Last edited: Aug 18, 2015