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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UV Mesh modification via script to support texture atlas concept with hexagonal tiles

Discussion in 'General Graphics' started by Romexus, Mar 26, 2015.

  1. Romexus

    Romexus

    Joined:
    Mar 26, 2015
    Posts:
    19
    Unity Community,
    This is my first post to the forums as I am a believer in figuring stuff out through research before asking questions. I am unable to find a clear example which will help me adjust the UV mesh coordinates of a hexagon to support a texture atlas. I have scoured the forums, read up on the Mesh-UV API but still don't have a clear picture of how to generate a script which will adjust the UV of the mesh to swap between the two textures in the atlas (goal is to have many textures in atlas once I complete this proof of concept). Utilizing an offset won't work as this doesn't reduce the draw calls.

    The hexagon maps to 128x128 pixel texture and I currently have two in the atlas for testing (256x128).




    The mesh was generated in Blender and not procedurally generated.
    Any help is thoroughly appreciated. I included some of my troubleshooting code as I think this is on the correct path.

    Code (CSharp):
    1.  
    2. Mesh mesh = LastHitObject.GetComponent<MeshFilter>().mesh;
    3. Vector3[] vertices = mesh.vertices;
    4. Vector2[] uvs = new Vector2[vertices.Length];
    5.  
    6. for (int i=0; i < uvs.Length; i++) {
    7.     //Some modification to the 'x' component based on the number of textures?  Possibly 0.5?
    8.     uvs[i] = new Vector2(vertices[i].x, vertices[i].z);
    9. }
    10. mesh.uv = uvs;
    11. LastHitObject.GetComponent<MeshFilter>().mesh.uv = mesh.uv;
    -Romexus
     
    Last edited: Mar 26, 2015
  2. Romexus

    Romexus

    Joined:
    Mar 26, 2015
    Posts:
    19
    This problem continued to haunt me until I found a creative way to handle the issue. Instead of finding a way to adjust the UV associated, I created a similar effect by using a floating hexagonal tile with a different material. I placed this 'selected hexagon' a few units above the hexagonal grid by matching the transforms through ray casting. From the top down perspective it looks like the material has adjusted when the mouse scrolls over a playable grid.



    The green hexagon above is the hover effect in the game.