Texture2DArray as resource

Godot Version

4.2

Question

I’m a little confused how to create a 2D texture array resource. I want to have an array of textures as a single resource that I can utilize in a shader. Is this possible?

You’ll need to import a texture as Texture2DArray. With the texture selected, in the Import tab, select Texture2DArray in the Import as dropdown. Set the horizontal and vertical slices of the texture and click Reimport

If you mean create it from code then you’ll need to have an array of Images and use ImageTextureLayered.create_from_images() like:

func _ready() -> void:
	var img_red = Image.create(16, 16, true, Image.FORMAT_RGBA8)
	img_red.fill(Color.RED)
	var img_green = Image.create(16, 16, true, Image.FORMAT_RGBA8)
	img_green.fill(Color.GREEN)
	var img_blue = Image.create(16, 16, true, Image.FORMAT_RGBA8)
	img_blue.fill(Color.BLUE)
	var img_yellow = Image.create(16, 16, true, Image.FORMAT_RGBA8)
	img_yellow.fill(Color.YELLOW)

	var texture_2d_array = Texture2DArray.new()
	texture_2d_array.create_from_images([img_red, img_green, img_blue, img_yellow])

	var material = sprite_2d.material as ShaderMaterial
	material.set_shader_parameter("tex", texture_2d_array)

Your first paragraph is what I want but I can’t figure out how to import multiple images as a single Texture2DArray, or maybe this isn’t possible in Godot?

A Texture2DArray is just a single file with each texture set in a grid. It’s not multiple files.

Texture2DArray example

The left side is the Texture2DArray and the right side is what you get.

If you want to create a Texture2DArray from different images then you’ll need to do it in code. All images have to have the same dimensions, format, and mipmap settings.

1 Like

Ah ok, that makes sense. Thank you for explaining.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.