UTexture2D in Canvas driving me insane

I’m trying to create a texture at runtime and draw it in my Canvas, I’ve been trying to get it work all day with now without any success at all!

Texture is defined as class UTexture2D *Texture with a UPROPERTY in my HUD header

// Consts
const FColor ColorOne = FColor(0, 255, 0);
const FColor ColorTwo = FColor(255, 0, 0);
const int32 CheckerSize = 512;
const int32 HalfPixelNum = CheckerSize >> 1;

// Create the texture
Texture = UTexture2D::CreateTransient(CheckerSize, CheckerSize, PF_B8G8R8A8);

// Lock the checkerboard texture so it can be modified
FColor* MipData = static_cast<FColor*>(Texture->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE));

// Fill in the colors in a checkerboard pattern
for (int32 RowNum = 0; RowNum < CheckerSize; ++RowNum)
{
	for (int32 ColNum = 0; ColNum < CheckerSize; ++ColNum)
	{
		FColor& CurColor = MipData[(ColNum + (RowNum * CheckerSize))];

		if (ColNum < HalfPixelNum)
		{
			CurColor = ColorOne;
		}
		else
		{
			CurColor = ColorTwo;
		}
	}
}

// Unlock the texture
Texture->PlatformData->Mips[0].BulkData.Unlock();
Texture->UpdateResource();

However, when I get to DrawHUD and try to print this texture, it looks nothing like the thing I’m looking for (the format is BGRA, so left side blue, right side green).

Canvas->DrawTile(Texture, 0, 0, 1, 512, 512, 0, 0, 1, 1);

Can someone enlighten me what I’m doing wrong, cause it’s driving me insane! I also tried the Memcpy and ENQUEUE_UNIQUE_RENDER_COMMAND_TWOPARAMETER approaches, NOTHING WORKS! D:

#What is Your Ultimate Goal?

Is your ultimate goal to create a T2D from a file name? an array of FColor Data?

Are you trying to draw on a texture / like a scripted texture? Using CanvasItems ?

What is your ultimate goal?

I’ve done all of the above and can point you in a direciton

Rama

The ultimate goal is dynamically updating a texture. I’m aware of the perfomance issues and how to solve it, this code was a final resort to be SURE I’m actually putting sensible information in.

Here’s the result of above code:

http://puu.sh/8VUIw.png

Where’s I’d expect a clean left and right side (not blurred), and also green on the left, and blue on the right…

Apparantly Canvas->DrawIcon(Canvas->MakeIcon(texture), 0, 0, 1); works, giving me the result I expect, not sure why, probably the UV coordinates.

1 Like