[SOLVED] How understand atlas coordinates?

How get image x and y coordinate from atlas? coordinates system seem unintuitive. Trying to get correct uv coordinate and apply it to mesh/quad.

u = minimal u-coordinate / atlas_width

v = (minimal v-coorinate - height of image) / atlas_height

and then you have to invert y coordinate? by:

v = 1 - (v + h)
1 Like

Atlas UVs use a top-left origin, so (0,0) is in top-left corner, (1,1) is in bottom-right corner, just get the data from the resource.get_atlas() (API reference (Resource))

For meshes, the texcoord0 values you put in the buffer are typically normalized [0..1], and orientation is same: (0,0) top-left, (1,1) bottom-right.

So if you’re computing UVs from an atlas rect (x, y, w, h), where x,y are pixels from the top-left of the atlas, no inversion should be needed.

If you need a single image from an atlas, check out its metatadata using resource.get_atlas(), like explained e.g. here: Get animation frames of sprite at runtime, but bear in mind those coordinates will be in pixels (not normalized).

There is one pitfall with rotated images - here you can find a very good explanation on how to set properly images from atlas as textures for the meshes:

Linked topic:

2 Likes

It’s been a while since I’ve checked but if I remember correctly 0,0 position is bottom left and 1,1 is top right. :upside_down_face:

3 Likes

thanks everyone. Can’t test it right now, atlas geometry order seem shuffled, which means you can’t expect image order like in editor outline:

Correct. Lua key-value tables aren’t ordered, so there is no guarantuee about the iteration order.

Ok, I have to check it too!

1 Like

now actually solved, gulev made handy function that search atlas image uv by name(id)

please check github issue:

1 Like

This find_uvrect function could definitly be in the defold API :sweat_smile: this could help a lot of people.
Like a sprite.find_uvrect()