Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Original UV when using sprite atlasing

Discussion in 'Shaders' started by crushy, Dec 29, 2013.

  1. crushy

    crushy

    Joined:
    Jan 31, 2012
    Posts:
    35
    I'm facing a rather specific issue that's giving me a headache. It should be fairly easy to solve if someone is familiar with how Unity's new systems work.

    Unity's atlas mechanism for sprites changes a mesh's UV coordinates. Is there a way I can obtain the original UV's before atlasing?

    I'm trying to blend each sprite in the atlas with another single texture but with the UVs all jumbled the second texture is really hard to line up unless I also turn it into a texture atlas with similar positions.

    If this comes across as confusing feel free to say so and I'll try to come up with a few diagrams.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,727
    The UV coordinate it set up in the mesh data itself, not the shader. So as you've found, Unity calculates the UV's based on where in the texture the sprites are, to get the right part of the image to show. That same UV coord would likely then be used for your other texture too. The only way around it is either to a) add UV2 coordinate data to your mesh and use that in the shader, or b) generate UV2 coordinates in the vertex shader somehow and pass it into the fragment shader.
     
    milanhenkrich likes this.
  3. crushy

    crushy

    Joined:
    Jan 31, 2012
    Posts:
    35
    I figured as much... Thanks!

    Do you have any suggestions on how I could generate UV data for a simple quad sprite on the vertex shader?
     
  4. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,727
    Hmm.. well, each vertex will need to output a different UV coordinate, ie 0 or 1, but the vertex shader doesn't really know where the vertex is in relation to other vertices, so it doesn't know which corner of the shape the vertex represents... you'd have to either feed it extra data (like set the UV2 coords per vertex) or make an assumption.
     
unityunity