Search Unity

Can I make repeating UVs on a Mesh

Discussion in 'Scripting' started by Endy2024, Sep 2, 2021.

  1. Endy2024

    Endy2024

    Joined:
    Jun 26, 2017
    Posts:
    1
    (If somebody thinks I wrote something wrong just point it out, I wanna learn... This is my first post here. I searched for this answer before I posted this question here)

    In Unity I'm working on making my own Voxel Engine for a game. I recently optimised it by implementing Greedy Meshing into it.. but when it comes to the texture/UV part I just can't wrap my head around it how can I do it efficiently. Right now each mesh gets assigned one Material, that contains all the blocks in the game. When I set the UVs I give a ID to it, and then divide it with the size of the grid.

    Example with grid size 10:

    Dirt Id:1, Uv:

    0.1f,0.1f;
    0.2f,0.1f;
    0.1f,0.2f;
    0.2f,0.2f;
    My question is, how can I make the UV repeatable/tilable? I can set the Sprite of the Texture to repeat.. but if I set the UV to any higher it will repeat every block instead of just the one I want. Can I do this with one Material assigned to the Mesh, or do I have to bite the bullet and make a separate Material to every single block? Or maybe can I do it somehow with one Material if I make the Sprite Mode into Multiple and slice it?

    Thank you for your answers in advance.

    Code (CSharp):
    1. void AddGreedyTexture(long textureID, int xTiling, int yTiling)
    2.     {
    3.  
    4.         float y = textureID / VoxelData.TextureAtlasSizeInBlocks;
    5.         float x = textureID - (y * VoxelData.TextureAtlasSizeInBlocks);
    6.  
    7.         x *= VoxelData.NormalizedBlockTextureSize;
    8.         y *= VoxelData.NormalizedBlockTextureSize;
    9.  
    10.         y = 1f - y - VoxelData.NormalizedBlockTextureSize;
    11.        
    12.         greedyuvs.Add(new Vector2(x, y));
    13.         greedyuvs.Add(new Vector2(x, y + yTiling + VoxelData.NormalizedBlockTextureSize));
    14.         greedyuvs.Add(new Vector2(x + xTiling + VoxelData.NormalizedBlockTextureSize, y));
    15.         greedyuvs.Add(new Vector2(x + xTiling + VoxelData.NormalizedBlockTextureSize, y + yTiling + VoxelData.NormalizedBlockTextureSize));
    16.         Debug.Log(x + " - " + y);
    17.  
    18.     }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    You can not make it tile / repeat unless you write a shader that actually uses the worldspace or localspace position as a "hint" in which case you don't really need UVs but rather some tile indices that are the same for all 4 corners. Of course there are some limitations when using such an approach. Your meshes need to be axis aligned and snapped to the grid.

    In most cases it's not worth to go down this route. It's usually much simpler and much more flexible to create a seperate quad for each tile. That's how pretty much all tile based games work including minecraft.
     
    Endy2024 likes this.
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    @Bunny83
    Shouldn't a triplanar shader work for this case?

    I concur.
     
    Endy2024 likes this.