Unfortunately I cannot find proper documentations around this question. I want to create a texture of custom size at runtime, assign colors to pixels of the texture, and then assign that texture to a plane mesh to be displayed at runtime.
UTexture2D::CreateTransient() creates an empty UTexture2D for you to modify.
You get the first mipmap of this newly created texture, then need to get and lock the image data.
// Create custom texture and retrieve raw image data
UTexture2D CustomTexture = UTexture2D::CreateTransient(1024, 1024);
FTexture2DMipMap* MipMap = &CustomTexture->PlatformData->Mips[0];
FByteBulkData* ImageData = &MipMap->BulkData;
uint8* RawImageData = (uint8*)ImageData->Lock(LOCK_READ_WRITE);
RawImageData is formatted as such:
[Pixel1 B][Pixel1 G][Pixel1 R][Pixel1 A][Pixel2 B][Pixel2 G][Pixel2 R][Pixel2 A] …
You can iterate this like a 2D array with a nested for-loop, keeping in mind that each pixel takes up four elements.
Once you’ve set your colors, unlock the image data and update the texture.
In editor, you must have a material with an image texture parameter.
Call CreateDynamicMaterialInstance() in your plane mesh and pass in the material.
Finally, SetTextureParameterValue() with your custom texture in the Dynamic Material Instance you just created and set the plane mesh’s material to the Dynamic Material Instance.
RuntimeTexture is a field which I can see in the Details panel. I can see that the texture is being generated but it doesn’t get applied to the material. What am I doing wrong?
The code block shows you created the dynamic material and set the texture parameter, but you haven’t actually set the mesh’s material with SetMaterial().