Difference between Sprite2D and using regions vs. AtlasTexture

Hi!

I am currently using a lot of large PNG images. Each contains several elements, such as platforms and backgrounds. In Godot, I use Sprite2D and regions to select the correct sprite from the PNGs.

However, I have read that AtlasTexture (which loads the same PNG) is better, even though it also uses regions.

I am confused. Do they differ?

Second question:
I have also read that it is better to use MeshInstance2D and load a texture there. However, without “regions”, this may not be convenient for my single PNGs.

Any thoughts?

The main difference is that the AtlasTexture is a Resource, while Sprite2D is a Node, so they are not interchangeable.

Your follow-up question about MeshInstance2D exactly demonstrates how they are different - you can load an AtlasTexture into the MeshInstance2D’s texture parameter, but you can’t load a Sprite2D there.

You can even load an AtlasTexture into the Sprite2D’s texture parameter if you want.

2 Likes

Thank you. I see. But what about performance? Is there a difference?

I noticed that MeshInstance2D does not support regions. It simply displays the entire loaded image. In that case, Sprite2D is the only option.

TLDR: It doesn’t matter. Let Godot handle memory management. It’s better than you will be at it.

Under the hood, both use a Texture2D. If you load your image, Godot loads that Texture2D in memory. If you try to load it again, by using another AtlasTexture or Sprite2D it’s going to check to see if that Texture2D has already been loaded, because it inherits from RefCounted. Then, instead of loading the file from disk again, it’s going to just be another reference preventing that Texture2D from being cleared from memory until both things using it are cleaned up using queue_free().

The one thing you can control is removing any images from that texture that you’re not using in your game to reduce GPU memory footprint.

2 Likes

Agreed 100% with @dragonforge-dev, don’t try to optimize a problem you don’t have.

It’s not the only option, you can use AtlasTexture with MeshInstance2D if you need a region of a texture.

2 Likes

@dragonforge-dev: thank you!

@wchc: thanks, regarding MeshInstance2D, it only shows the entire PNG and not the defined region.

Indeed I can’t get MeshInstance2D to work with an AtlasTexture, I’m not sure if that’s a bug or not. @dragonforge-dev do you know? Here the AtlasTexture region works on the Sprite2D, but not MeshInstance2D, even though the same AtlasTexture resource is shared in both.

Nevertheless, you should be fine working with Sprite2D for most cases, unless you have any specific reason to use the MeshInstance2D instead.

3 Likes

I’ve never played around with MeshInstance2D. So I did, and it turns out you force it to use an AtlasTexture, but it behaves oddly. You can convert a Sprite2D to a MeshInstance2D.

So, using an AtlasTexture I picked a spaceship out of a sprite sheet from Kenney. Then I converted it, which went fine. And even though it looks correct in the Inspector:

In the game it applied the Mesh to the entire sprite sheet.

Very odd. So it shows the defined region, but uses the whole sprite sheet, which is more visible on the left.

2 Likes