Dynamic Runtime Textures


I’m having trouble creating / applying a dynamic texture at runtime.

This is my test code:

	_width = 2;
	_height = 2;
	uint8 pixels] = {
		255, 0, 0, 255,
		0, 0, 255, 255,
		255, 0, 0, 255,
		0, 0, 255, 255
	_texture = UTexture2D::CreateTransient(_width, _height, PF_R8G8B8A8);
	FTexture2DMipMap& mip = _texture->PlatformData->Mips[0];
	void* mipdata = mip.BulkData.Lock(LOCK_READ_WRITE);
	FMemory::Memcpy(mipdata, pixels, _width * _height * sizeof(uint8) * 4);

In this test, I am creating a 2x2 texture and manually specifying 4 RGBA pixels, alternating the colors between red and blue.

I have a simple cube in the scene with 0-1 uv layout on all sides.

I would expect to see a checkered coloured cube. I get cube with just the first pixel’s (or maybe a mix), color, quite a weak color.

I do believe the above code is having an effect. In our “real” case I am sending over quite large amounts of image data, 1024x1024 etc, and still just getting a single color (though again, different textures do produce differing results).

Am I to assume it is the way the shader is set up in Unreal (this I have no experience of)?


It’s the baseImage i am setting the texture on.

Any help/advice much appreciated!

hi Nick.Block you are wrong in the size _width * _height * sizeof(uint8) * 4 , if you have 4 pixels this must be 2 * 2 *4.
try with this code:

TArray<FColor> colorData;
for (int i = 0; i < 4; i++)
	colorData.Add(FColor(255, 0, 0, 255));
texture = UTexture2D::CreateTransient(2, 2, PF_R8G8B8A8);
FTexture2DMipMap& mip = texture->PlatformData->Mips[0];
void* mipdata = mip.BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(mipdata, colorData.GetData(), 2 * 2 * 4);

anyway please dont use MemCpy, because is super slow, i test with RHISystem and some guy in the forums too, is almost 1/3 faster, watch this tutorial