How to Make a Texture 2D Array during Runtime in c++? Or Texture Atlas

Hello everyone, I’m trying to combine several individual textures into one, either with a Texture 2D Array or a Texture 2D, but I can’t find the correct way to do it.

I wrote this code I found where they copy the data from each of the individual textures to a Texture 2D Array, but the final image is just random pixels.

I searched for more information about these Unreal Engine classes, but there is no clear documentation. I did the same thing in Godot, and it’s easier to create a Texture Atlas.

Code:

void SetVoxelTextureArray(TArray<UTexture2D*>& ArrayTextures){

	// INIT TEXTURE ARRAY
	VoxelTextureArray = UTexture2DArray::CreateTransient(16, 16, ArrayTextures.Num() + 1);
	VoxelTextureArray->UpdateResource();

	// GET THE DATA OF THE TEXTURE
	void* TextureData = VoxelTextureArray->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);

	// DEFINE THE SIZE OF THE TEXTURES 16X16 PIXELS
	int64 MemoryOffset = 0;
	uint32 TextureSize = 16 * 16 * 4;

	for (int32 i = 0; i < ArrayTextures.Num(); i++){
		// ASSIGN THE MEMORY TO THE VARIABLE SLICE DATA
		void* SliceData = (uint8*)TextureData + MemoryOffset;

		// GET THE DATA FOR EACH INDIVIDUAL TEXTURE
		const void* SourceData = ArrayTextures[i]->GetPlatformData()->Mips[0].BulkData.LockReadOnly();
		
		// COPY THE DATA
		FMemory::Memcpy(SliceData, SourceData, TextureSize);

		ArrayTextures[i]->GetPlatformData()->Mips[0].BulkData.Unlock();

		// INCREMENTE THE MEMORY OFFSET TO THE SIZE OF EACH TEXTURE
		MemoryOffset += TextureSize;
	}

	VoxelTextureArray->GetPlatformData()->Mips[0].BulkData.Unlock();

	VoxelTextureArray ->UpdateResource();

}

For anyone with the same problem, i downloaded the Rama’s Extra Blueprint Nodes for UE5]

And this is my code in Blueprints, the only issue is that all the textures are 1 unit size in Y-axis.

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