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.
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.
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.
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.
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: