The short of it is that I’m trying to slice up a 16 bit heightmap I’ve exported from a GIS program into four 2048x2048 tiles. This appears to be working fine, and I then want to load that into a UTexture2DArray. The relevant code is as follows:
void ABFRTSWorldTerrainSystem::AddTexturesToTexture2DArray(UTexture2DArray* TextureArray, TArray<UTexture2D*> Textures) {
if (Textures.Num() > 0) {
int32 Width = Textures[0]->GetPlatformData()->SizeX;
int32 Height = Textures[0]->GetPlatformData()->SizeY;
ETextureSourceFormat SourceFormat = Textures[0]->Source.GetFormat();
if (TextureArray) {
//If texture array doesn't exist or doesn't have correct width, initialize as such
if (TextureArray->GetSizeX() != Width || TextureArray->GetSizeY() != Height || TextureArray->GetArraySize() != Textures.Num()) {
TextureArray->Source.InitLayered2DWithMipChain(Width, Height, Textures.Num(), &SourceFormat);
TextureArray->UpdateResource();
}
//For each texture to load, get mips and load
for (int32 SliceIndex = 0; SliceIndex < Textures.Num(); ++SliceIndex) {
FTexture2DMipMap& Mip = TextureArray->GetPlatformData()->Mips[SliceIndex];
void* Data = Mip.BulkData.Lock(LOCK_READ_WRITE);
FTexture2DMipMap& SourceMip = Textures[SliceIndex]->GetPlatformData()->Mips[0];
void* SourceData = SourceMip.BulkData.Lock(LOCK_READ_ONLY);
FMemory::Memcpy(Data, SourceData, Mip.BulkData.GetBulkDataSize());
SourceMip.BulkData.Unlock();
Mip.BulkData.Unlock();
}
TextureArray->UpdateResource();
}
}
}
I’m running into issues on TextureArray->Source.InitLayered2DWithMipChain(Width, Height, Textures.Num(), &SourceFormat);
where I hit an error in FImageCoreUtils::ConvertToRawImageFormat(ETextureSourceFormat Format)
, where the first time it is called, the image format is correct (TSF_G16), but in subsequent calls, the format is a random integer, as if pulled from incorrect memory allocation. I think the issue is related to calculation of bytes per pixel, but I’m having troubles diagnosing what the specific issue is here. I’m using the same format as that of the individual texture slices, and didn’t get any errors when those were created.
Any tips would be greatly appreciated here. Thanks!