Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Texture2D.PackTextures Get Coords as Int

Discussion in 'Scripting' started by 1SDAN, Jun 29, 2016.

  1. 1SDAN

    1SDAN

    Joined:
    Jun 25, 2016
    Posts:
    5
    So I've been working on a Voxel game in Unity and decided to get my hands dirty with creating a texture atlas at runtime. I do this by getting all png files in a directory with a pre-set resolution, put them all in an array of texture2Ds and their file names in a matching array of strings and then run PackTextures on them.

    I'm now trying to figure out how I can keep track of the position of each texture as an int relating to the x and y coords of the textures on the Array's grid. I've already tried guessing the coords based on the order of each texture in the array, but it seems that the input array's order has no bearing on the output.

    Any help would be appreciated. Thanks.
     
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    The output rects are in the same order as the input textures.

    In this example:

    Code (csharp):
    1. rects = atlas.PackTextures(atlasTextures, 2, 8192);
    rects[5] will be the texture coordinates where atlasTextures[5] was place in the atlas.
     
  3. 1SDAN

    1SDAN

    Joined:
    Jun 25, 2016
    Posts:
    5
    I tried doing:

    Code (CSharp):
    1. Rect[] r = Atlas.PackTextures(textureArray, 0, AtlasTiles * textureSize, false);
    2.      
    3. for(int i = 0; i < r.Length; i++)
    4. {
    5.     textureCoords[textureNames[i]] = new int[2] { (int) r[i].x, (int) r[i].y };
    6. }
    7.  
    Where Atlas is my texture atlas, tileSize is the size of each texture, and textureCoords is a Dictionary matching string generic list textureNames with the x and y position, but for some reason both the xMin and x values in every single rect is 0. Am I doing something wrong?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Values are normalised texture coordinates (ie from 0 to 1)
    Casting to int each time will always result in 0
     
    Dave-Carlile likes this.
  5. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    This. If you want them in pixels then multiply the value by the width or height of the final texture.
     
  6. 1SDAN

    1SDAN

    Joined:
    Jun 25, 2016
    Posts:
    5
    Thanks, I got it working. Apparently after getting the texture coords, I had to also find the interval of new textures for my coord determining equation. For future reference, this is the code that got it working:


    Code (CSharp):
    1. Rect[] r = Atlas.PackTextures(textureArray, 0, AtlasTiles * textureSize, false);
    2.      
    3.         for(int i = 0; i < r.Length; i++)
    4.         {
    5.             int x = (int) (r.x * Atlas.width / textureSize);
    6.             int y = (int) (r.y * Atlas.height / textureSize);
    7.             textureCoords[textureNames] = new int[2] { x, y };
    8.             if (x > 0 && (float) x > tileSizeX) { tileSizeX = x; }
    9.             if (y > 0 && (float) y > tileSizeY) { tileSizeY = y; }
    10.         }
    11.      
    12.         tileSizeX += 1;
    13.         tileSizeY += 1;
    14.      
    15.         tileSizeX = 1 / tileSizeX;
    16.         tileSizeY = 1 / tileSizeY;

    Again, thank you all very much for the help!