Hi! I was fighting with this problem for whole day, and could not figure out, is is a bug, or my own mistake. Basically, I want to save renderTexture into texture array. All works fine, if I save textutes 1 by 1.
Here is how I collect texture data:
TArray<FColor> outBMP;
FTextureRenderTargetResource *rtResource = InRenderTarget->GameThread_GetRenderTargetResource();
...
rtResource->ReadPixels(outBMP, readPixelFlags);
Now, istead of saving 1 by 1, I want to merge all textures I collected into atlas and save it. Here is what I do:
int32 atlasSideSize = FMath::Sqrt(this->frameAmountToSave);
atlasSideSize = FMath::RoundUpToPowerOfTwo(atlasSideSize);
TArray<FColor> outBMPAtlas;
for (int32 s = 0; s < this->outBMPCollection.Num(); s++)
{
outBMPAtlas.Append(this->outBMPCollection[s].sub);
}
int32 sizePixelWidth = (atlasSideSize * surfWidth);
int32 sizePixelHeight = (atlasSideSize * surfHeight);
int32 uninitializedLeft = (sizePixelWidth * sizePixelHeight) - (this->frameAmountToSave * surfWidth * surfHeight);
outBMPAtlas.AddUninitialized(uninitializedLeft);
FString fileDestination = FPaths::GameDir().Append(this->SaveFolder).Append("/").Append(this->AnimationName).Append(".bmp");
UE_LOG(LogTemp, Warning, TEXT("Saving to %s"), *fileDestination);
int32 imageSavedOk = (int32)FFileHelper::CreateBitmap(*fileDestination, surfWidth * atlasSideSize, surfHeight * atlasSideSize, outBMPAtlas.GetData());
UE_LOG(LogTemp, Warning, TEXT("If file saved? %i"), imageSavedOk);
}
If I have 1 frame, everything is fine. If I have, say, 4 frames, I get this: (instead of 4 frames, I see 8 and stretched)
I assume, that problem happens, when I append my texture data into 1 atlas here:
TArray<FColor> outBMPAtlas;
for (int32 s = 0; s < this->outBMPCollection.Num(); s++)
{
outBMPAtlas.Append(this->outBMPCollection[s].sub);
}
this->outBMPCollection[s].sub is TArray<FColor>, and my idea was to put textures together. But seems like I am not filling array memory properly with data, but I can figure out, why and where.