How to manipulate texture parameter with RenderTarget2D? C++

I have a Material which is created under Editor Material Graph. This material has a Parameter2D texture named as Render, I want to load the material instance of this material in C++ to manipulate with my SceneCapture2D->TextureTarget;
But it seems to me it is not working in C++. Doing this in blueprint works fine, I attach the blueprint in the end of this post to make it more clear what I want to do.

PROCESS OF IMPLEMENTATION

SceneComponentRoot = CreateDefaultSubobject<USceneComponent>(FName(TEXT("SceneComponentRoot")));
RootComponent = SceneComponentRoot;
SceneComponentRoot->CreationMethod = EComponentCreationMethod::Native;

MyStaticMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyStaticMesh"));
MyStaticMesh->SetupAttachment(SceneComponentRoot);

SceneCapture2D = CreateDefaultSubobject<USceneCaptureComponent2D>(TEXT("SceneCapture2D"));
SceneCapture2D->SetupAttachment(MyStaticMesh);

I created a Material Instance of the material I have in Editor and want to load using C++.

static ConstructorHelpers::FObjectFinder<UMaterialInterface>FindMaterial(TEXT("/Game/Mat_Inst.Mat_Inst"));

Now I need to make a MaterialDynamicInstace from this loaded material, to make it possible to manipulate the Parameters located in material.

DynMaterial= MyStaticMesh->CreateDynamicMaterialInstance(3, FindMaterial.Object, FName("None"));

So Now I want to create a Render Target by calling the function CreateRenderTarget2D from UKismetRenderingLibrary.

MyRenderTarget = UKismetRenderingLibrary::CreateRenderTarget2D(this, 128, 128, ETextureRenderTargetFormat::RTF_RGBA16f, FLinearColor::Black, false);

Now I have the render target ready and want to set it to a SceneCapture2D->TextureTarget

SceneCapture2D->TextureTarget = MyRenderTarget ;

So Now I have to manipulate the Parameter2D by using the function SetTextureParameterValue to replace with the TextureTarget

DynMaterial->SetTextureParameterValue(FName(TEXT(“Render”)), SceneCapture2D->TextureTarget);

CODE: This function ManipulateTexture is called in Constructor and it gives an error:

NewObject with empty name can't be used to create default subobjects (inside of UObject derived class constructor) as it produces inconsistent object names. Use ObjectInitializer.CreateDefaultSubobject<> instead.
UE4Editor-Win64-DebugGame.exe has triggered a breakpoint.
void AMyActor::ManipulateTexture()
{
	if (!IsValid(SceneCapture2D->TextureTarget))
	{
		SceneCapture2D->USceneCaptureComponent::HideActorComponents(this, true);
		SceneCapture2D->FOVAngle = MyFOV;
		MyRenderTarget = UKismetRenderingLibrary::CreateRenderTarget2D(this, 128, 128, ETextureRenderTargetFormat::RTF_RGBA16f, FLinearColor::Black, false);
		SceneCapture2D->TextureTarget = MyRenderTarget;

		if (::IsValid(MyStaticMesh))
		{
			static ConstructorHelpers::FObjectFinder<UMaterialInterface>FindMaterial(TEXT("/Game/Mat_Inst.Mat_Inst"));
			if (FindMaterial.Succeeded())
			{												  
				DynMaterial= MyStaticMesh->CreateDynamicMaterialInstance(3, FindMaterial.Object, FName("None"));
					DynMaterial->SetTextureParameterValue(FName(TEXT("Render")), SceneCapture2D->TextureTarget);
			}
		}
	}
}

BLUEPRINT EXAMPLE

any help appreciated thanks

CreateRenderTarget2D() calls NewObject with no name. So maybe that’s the problem? Out of curiosity, why are you calling ManipulateTexture() in your CDO? Why not do it in an existing in-world instance instead?

In UE, both the default Constructor and the FObjectInitializer Constructor are only called on the CDO (Class Default Object). All actual in game instances are copies of these objects.

Sorry if you already know this, but here’s a reference:

Perhaps you could call ManipulateTexture() inside BeginPlay() or OnConstruction() instead?
Creating the components is fine in the constructors though.

1 Like