Search Unity

Ideas for repeating textures

Discussion in 'Scripting' started by delzhand, Jul 27, 2012.

  1. delzhand

    delzhand

    Joined:
    Jul 20, 2012
    Posts:
    26
    I have a hex-based level editor where each hex is the same model, but can have a different height. The sides of the hex all have the same uv coordinates.

    Challenge 1: If it's the same Model using the same Material, can I set it to repeat 5 times when the model has a Y scale of 5, repeat 4 times when the model has a Y scale of 4, etc?

    Challenge 2: The top repetition of the image needs to be different. For example, on a hex that's 5 high, the texture needs to be 1 unit of grass, then 4 of dirt.

    I'm just fishing for ideas at this point - I have almost no constraints. I'm coming from years of XNA, and here's what I did there - For each tile that was greater than 1 unit tall, I created a renderTarget that was 1 x the Y scale, drew the "top" texture at 0,0, then drew the regular texture at intervals to fill the rest. That renderTarget was then applied to the model as the new texture.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Make an atlas containing the textures for the level, and adjust the UVs for the sections of the mesh as appropriate to contain the textures you want.

    --Eric
     
  3. delzhand

    delzhand

    Joined:
    Jul 20, 2012
    Posts:
    26
    Never worked with Atlases before, but that already sounds like a fantastically easy solution. I'm guessing the only caveat is that you'd have to know the maximum height a tile could be, but that's not a huge challenge.

    Thanks!
     
  4. delzhand

    delzhand

    Joined:
    Jul 20, 2012
    Posts:
    26
    My first pass at implementing this involved having a single large texture where grass occupied the space from 0,0 to .25,1, and stone from .25,0 to .5,1. In my editor script, I would get the UVs of the selected hex, and shift all UVs to the right to change them from grass to stone, but Unity told me to use SharedMesh instead of Mesh because I was leaking materials.

    The problem is, that's what I want. Each stage in the game will have a unique map - I don't want to create a material in the project for every tile type (grass/stone and quite a few more). Nor do I want to create identical meshes that differ only in the position of their UVs.

    I did try out SharedMesh, and it did exactly what I expected - every hex in the stage is the same, varying only in position and scale, so it changed the UVs for all of them, which is functionally useless.

    What I'd done with a prior engine was to simply build the stage with instances of hexes (which were actually identical meshes with their own textures) minecraft-style, but that leads to a lot of "underground" hexes that are never seen, and I don't know what kind of performance hit that creates. I may fall back onto this plan, but I'd like to know if it's possible to edit the mesh of just one (of many identical) object without triggering errors.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're using 1 mesh per hex? You should really have a combined mesh which contains many hexes, which is more complicated to implement but which will increase rendering speed by a lot. Using sharedMesh is correct; you should use a new instance of each mesh that's being edited, so that sharedMesh will be unique for each object.

    --Eric