How to create textures at runtime and assign them to a mesh?

Thank you for your detailed explanation. It helped a lot.

I successfully created the texture. But I’m having trouble assigning it to the material. This is the code that I have:

	RuntimeTexture = UTexture2D::CreateTransient(100, 100);
	FTexturePlatformData** PlatformData = RuntimeTexture->GetRunningPlatformData();
	FTexture2DMipMap FirstMip = (*PlatformData)->Mips[0];
	FByteBulkDataOld ImageData = FirstMip.BulkData;
	uint8* RawImageData = (uint8*)ImageData.Lock(LOCK_READ_WRITE);

	int ArraySize = 100 * 100 * 4;
	for(auto i = 0; i < ArraySize; i += 4)
	{
	    RawImageData[i] = 255;
	}
	
	ImageData.Unlock();
	RuntimeTexture->UpdateResource();

	UMaterialInstanceDynamic* DynamicMaterial = Mesh->CreateDynamicMaterialInstance(0, Mesh->GetMaterial(0));
	DynamicMaterial->SetTextureParameterValue("Texture", RuntimeTexture);

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?

1 Like