I’ve been trying to load up a dynamically created texture on a custom actor created as a plugin. Currently I can create a cube, load up my custom material and it shows up in the editor and game. If I then load up a 2D texture created dynamically (filled with noise so it is obvious it is being used) and set it on my material’a texture property it gets set but shows up as a blank grey or off-white. I’m using the method from the wiki to create and update the texture: A new, community-hosted Unreal Engine Wiki - Announcements - Unreal Engine Forums
Below is the relevant portion of the header:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = StaticMeshComponents) TSubobjectPtr<UStaticMeshComponent> schwein;
UPROPERTY(EditDefaultsOnly, Category = Materials) UMaterialInterface *MasterMaterialRef;
UPROPERTY() UTexture2D *DynamicTexture;
UPROPERTY() UMaterialInstanceDynamic* RV_MatInst;
Below is the relevant portion of the actor implementation:
// create cube
schwein = PCIP.CreateDefaultSubobject<UStaticMeshComponent>(this, TEXT("schwein"));
// load material
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh(TEXT("StaticMesh'/Game/Shapes/Shape_Cube.Shape_Cube'"));
static ConstructorHelpers::FObjectFinder<UMaterial> goldMaterial(TEXT("Material'/Game/Ibex/DesktopMaterial'"));
// create my custom material
MasterMaterialRef = goldMaterial.Object;
schwein->SetStaticMesh(StaticMesh.Object);
RootComponent = schwein;
// create random noise for texture
const int SizeX = 1024;
const int SizeY = 1024;
static uint8 data[SizeX * SizeY * 4] = { 0 };
srand(1982);
for (int i = 0; i < SizeX * SizeY * 4; ++i) {
data[i] = rand() % 255;
}
// create dynamic texture
DynamicTexture = UTexture2D::CreateTransient(SizeX, SizeY);
DynamicTexture->AddToRoot();
DynamicTexture->UpdateResource();
// update dynamic texture
FUpdateTextureRegion2D r(0, 0, 0, 0, SizeX, SizeY);
UpdateTextureRegions(DynamicTexture, (int32)0, (uint32)1, &r, (uint32)(4 * SizeX), (uint32)4, data, false); // Using the UpdateTextureRegions from https://wiki.unrealengine.com/Dynamic_Textures
DynamicTexture->UpdateResource();
// update texture property on material and set object to use material
RV_MatInst->SetTextureParameterValue(FName("T2DTexture"), DynamicTexture);
schwein->SetMaterial(0, RV_MatInst);