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

Atlasing: Assigning new UV to objects

Discussion in 'Scripting' started by Catsoft-Studios, Jun 11, 2012.

  1. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    701
    Greetings everyone;

    I've made an atlasing system. For now it packs a bunch of textures into one single big texture and creates a material for it.

    The problem now relies in how to assign the Rect[] to the objects's uv.
    For instance, I have an object and I want to assign it the uv coordinates (3,4) with a length of 2 for 'x' and 4 for 'y'.

    Code (csharp):
    1.  
    2. +------------------------------+
    3. |   (x0,y0)                    |
    4. |   o---------+                |
    5. |   |    A    |                |
    6. |   +---------o                |
    7. |               (x1,y1)        |
    8. |                              |
    9. +------------------------------+
    10.  
    The image above 'shows' the big texture with the Rect I want to assign to the mesh.

    How can this be done? Mesh.uv is a Vector2, which I suppose it's the X0 and Y0 of the Rect, but how to define the X1 and Y2 (the ending points to define a quadrangle/rectangle)?

    Thanks in advance!
     
  2. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,721
    Because a rectangle uvs map is about 4 points in 2d space?

    Well 4 point in 2d space = four vector 2?

    But only if one vertex of your object share one uvs....

    With mesh class you will probably need to define 2 triangles that is about 6 vertex and then 6uvs points.

    Not sure the mesh classe handle quad stripping, I never successed to make it work as openGL with an unique vertex list connected each other....
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Mesh.uv is actually a Vector2[] not a Vector2. :)

    sama pretty much has the right of it. Mesh.uv needs to be the same size as Mesh.vertices. Mesh.triangles is the index into each of those arrays that the mesh looks to get the information it needs for each vertex.
     
  4. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    701
    Ok, I don't know if I got it.

    So the thing is that I need to set the position of every vertex vertex uv according to the desired position within the texture?
    That means that the Mesh.uv's Vector2[] has the same size as the number of vertex of the whole mesh?

    Something like this:

    Code (csharp):
    1.  
    2. +------------------------------+
    3. |                              |
    4. |    A +-----------------+ B   |
    5. |      |                 |     |
    6. |      |                 |     |
    7. |    C +-----------------+ D   |
    8. +------------------------------+
    9.  
    Setting for vertex A it's position, B's position C's and D's position relative to the points (in pixels?) of the atlas texture?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You got it. The only thing to clarify is that UV isn't measured in pixels but rather percentage of area where 0,0 is bottom left and 1,1 is top right.
     
  6. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    701
    Ok, thanks both! Tomorrow I'll give it a try and post the results in case someone else wonders the same. Thanks again!
     
  7. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,169
  8. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    Yes any up please looking exemple with uv mesh