Am I using Atlas Correctly?

New to Godot - working on my first card game. I’ve been testing my game on lower-tier Android devices and I have come across some serious performance issues. In researching I came across the recommendation of using an Atlas for my images. I want to use an Atlas for my 50+ card images, but is this the most efficient way to do so?

var atlas_image = Image.new()
atlas_image.load("res://Assets/Atlas/AtlasCards.png")

Card #1:

var card_1_image: AtlasTexture = AtlasTexture.new()
var card_1_texture = ImageTexture.create_from_image(atlas_image)
card_1_image.atlas = card_1_texture
var card_1_region = Rect2(Vector2(0,0), Vector2(100, 150))
card_1_image.region = card_1_region

Card #2:

var card_2_image: AtlasTexture = AtlasTexture.new()
var card_2_texture = ImageTexture.create_from_image(atlas_image)
card_2_image.atlas = card_2_texture
var card_2_region = Rect2(Vector2(100,0), Vector2(100, 150))
card_2_image.region = card_2_region

Then I assign:

%Card_1.texture = card_1_image
%Card_2.texture = card_2_image

This is supposed to increased the performance, less memory for images. However, compared to current method where I just have a single image saved in my Assets folder and make an assignment like so:

%Card_1.texture  = preload("res://Assets/image_card_one.webp")
%Card_2.texture = preload("res://Assets/image_card_two.webp")

The option to use an Atlas seems far more complicated, especially since I have to now create/use 3 variables to make an image assignment - or 100+ to make assignments for each card. So I was wondering if my above setup was correct. It works, but is it the most efficient way to make the image assignments using an Atlas? Thanks.

You can use one texture for the atlas image for all cards, instead of creating a new ImageTexture from an Image for each one. Since you just need an ImageTexture and not an Image to do that, I believe you can just load the file as an ImageTexture:

var atlas_image_texture = load("res://Assets/Atlas/AtlasCards.png")

[...]

card_1_image.atlas = atlas_image_texture

Creating AtlasTextures does get repetitive, which makes it a good problem to use a helper function for. Probably something like this:

const card_texture_size = Vector2(100, 150)
func make_card_texture(row: int, column: int) -> AtlasTexture:
    var texture = AtlasTexture.new()
    texture.atlas = atlas_image_texture
    texture.region = Rect2(
        Vector2(column * card_texture_size.x, row * card_texture_size.y),
        card_texture_size
    )
    return texture

So then for the assignments you’d have something like this:

%Card_1.texture = make_card_texture(0,0)
%Card_2.texture = make_card_texture(0,1)
1 Like

I was able to resolve my issue in another way, but this is a great answer. I’ll surely use this in the future - thank you!

1 Like