- Edited
A question came up on how to combine multiple smaller sprites or images into one big sprite. And as someone who likes working with low level graphics I thought I'd do a quick test project. (Also I recently thought about how to draw single pixels by hand so this would be a good first look into that direction as well.)
GitHub project: https://github.com/Toxe/godot-combine-images
From main.gd:
func combine_images(sprites: Array[Sprite2D]) -> void:
assert(!sprites.is_empty())
var format := sprites[0].texture.get_image().get_format()
var bounding_box := calc_bounding_box(sprites)
assert(sprites.all(func(sprite: Sprite2D) -> bool: return sprite.texture.get_image().get_format() == format))
assert(bounding_box.size.x > 0 and bounding_box.size.y > 0)
var image := Image.create(int(bounding_box.size.x), int(bounding_box.size.y), false, format)
for sprite in sprites:
image.blend_rect(sprite.texture.get_image(), Rect2i(Vector2i.ZERO, sprite.texture.get_image().get_size()), get_sprite_rect(sprite).position - bounding_box.position)
var texture := ImageTexture.create_from_image(image)
if combined_sprite:
combined_sprite.queue_free()
combined_sprite = Sprite2D.new()
combined_sprite.texture = texture
combined_sprite.position = Vector2(600, 300)
add_child(combined_sprite)
Basically:
- Create a new Image.
- Blit or blend the smaller sprite texture images into the new image.
- Create a new ImageTexture out of that bigger image.
- Create a new Sprite2D and assign the ImageTexture to it.