Calculate UVs to show only a region from the texture in a mesh

Godot Version

4.2

Question

Say i have a texture Atlas thats 16x16 pixels and in it there are individual textures of 8x8. How do i calculate the UVs on a cube so that they have a certain region on the atlas?

Okay, figured it out.

Here is how you can set the texture region of an atlas using only UV coords.
(This might only be a workaround since i dont know if Godot has a dedicated Region Class, as i was unable to find one)

You will need 8 variables

     var xFrame = (the selected frame x position)
     var yFrame = (the selected frame y position)
      var numFramesAcross  = (The number of regions across your atlas is. )
var frameSize = float(1.0/numFramesAcross) 
var vFrameX = float(frameSize * xFrame)
var vFrameY = float(frameSize * yFrame)

var vFrameEndX = float(vFrameX+ frameSize)
var vFrameEndY = float(vFrameY+ frameSize)

Top Left vertex point UV is Vector2(vFrameX, vFrameEndY)
Top Right vertex point UV is Vector2(vFrameEndX, vFrameEndY)
Bottom Left vertex point is Vector2(vFrameX, vFrameY)
Bottom Right vertex point is Vector2(vFrameEndX, vFrameY)

And that will display the correct atlas region on a quad mesh or two triangle polygons.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.