Can i split texture to multiple parts or crop it?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nahumus

I’m making a game which is simillar to the Rush Hour puzzle game, I want to split a texture into some parts, and apply it to different textures.
My texture is on a 1:N ratio and I want to split it on N sprites.
for example, I have the car image below:
full car

As it is a 1:2 ratio, I want to apply it on 2 different sprites, as represented here:
enter image description here

I would like to do it all through the code as I want the user to be able to add their own car images, I’ve heard about atlas but I don’t understand how can I use it only through code.

:bust_in_silhouette: Reply From: Krasapan

Yes, you can by using TextureAstlas
Tutorial: https://www.youtube.com/watch?v=oEBUXW674dc

I’ll add this video to my godot watchlist. Thank you for sharing!

Amateur.game.dev. | 2021-01-14 16:45

:bust_in_silhouette: Reply From: ivanskodje

You can use the same strategy as originally posted in this QA post.

I was working on something similar, and ended up with the following:

func your_function_here():
	var texture := _tile_map.tile_set.tile_get_texture(id)
	var region := Rect2(0, 0, 16, 16)
	texture_button.texture_normal = get_cropped_texture(texture, region)

func get_cropped_texture(texture : Texture, region : Rect2) -> AtlasTexture:
	var atlas_texture := AtlasTexture.new()
	atlas_texture.set_atlas(texture)
	atlas_texture.set_region(region)
	return atlas_texture