Download

Create level using an Image(UTexture2D)

Hello, I am trying to create levels using images. Till now I am able to read data from images with size 15 x 15, but images bigger than that makes editor crash. Below is the code I am using to read the pixel value from an image.



void ALevelCreater::ReadPixelValues(UTexture2D* TextureToRead)
{
    TextureToRead->UpdateResource();

    const FColor* FormatedImageData = static_cast<const FColor*>(TextureToRead->PlatformData->Mips[0].BulkData.LockReadOnly());

    for (int32 X = 0; X < TextureToRead->PlatformData->Mips[0].SizeX; X++)
    {
        for (int32 Y = 0; Y < TextureToRead->PlatformData->Mips[0].SizeY; Y++)
        {
            FColor PixelColor = FormatedImageData[Y * TextureToRead->PlatformData->Mips[0].SizeX + X];
            UWorld* World = GetWorld();
            if (PixelColor == FColor::Black)
            {
                if (World)
                {
                    const FVector CircleLocation = FVector(X, Y, 0);
                    const FRotator CircleRotation = FRotator::ZeroRotator;

                    AActor* CubeActor = World->SpawnActor<AActor>(
                        CubeClass, CircleLocation, CircleRotation);
                    if (CubeActor != nullptr)
                    {
                        UE_LOG(LogTemp, Warning, TEXT("Spawning cube"));
                    }
                }
            }
        }
    }
    TextureToRead->PlatformData->Mips[0].BulkData.Unlock();
    TextureToRead->UpdateResource();
}


I am also looking a way to map it in level. Like where to place a cube if the black color is found in image and scale it. Also if there is something wrong with the code, then please tell me and I will correct it. Thanks in advance.

Any idea guys?

I followed this tutorial : UE4 - Reading the pixels from a UTexture2D - Isara Tech. it works for an image with size 128x128 and over.

Hey, Thanks for replying. I have followed the same tutorial but for me the memory usage goes over 10GB. The image I use is 512x282 and compression setting as UserInterface2D. Can you tell me what is the max size of image you used to test with the script?

I found out that the issue was trying to save all the pixels in array. Now I want to map out the pixels in real world location and scale. Any idea of how to do that?