SetTextureParameterValue With RenderTarget Issues

I’m attempting to pass the output of one material into another but am struggling to find the right combination of code to accomplish this. Perhaps I’ve got the wrong impression about Render Targets.

My thoughts, however wrong they might be, have been to do the following:

  1. Generate UTextureRenderTarget2D owned by the UObject that’s running this code
  2. Draw the “input material” to it. This material plugs to the EmissiveColor.
  3. Later, I create an instance of the “output material”, passing the render target I created in steps 1 & 2 as a parameter
  4. Draw the output material to another target
// A UTextureRenderTarget2D* UPROPERTY() on the class
RenderTarget = UKismetRenderingLibrary::CreateRenderTarget2D(this, 512, 512);

UKismetRenderingLibrary::DrawMaterialToRenderTarget(this, RenderTarget, InputMaterial);

// ...

// Later on
UMaterialInstanceDynamic* OutputMatInstance = UMaterialInstanceDynamic::Create(BaseOutputMaterial, this);

OutputMatInstance->SetTextureParameterValue("NamedTextureParameter", RenderTarget);

ProceduralMesh->SetMaterial(0, OutputMatInstance);

However the results are not what I expect and quite poor. It’s hard to tell what it should be.

Expected:

As it looks from the “Input Material”

Current Result:

Plugged into the Base Color and rendered in Unlit

Questions:

  • Does the DrawMaterialToRenderTarget use the Base Color or Emissive channel? Both? Hard to tell. Perhaps it’s all based on the lighting model but I’ve tried changing it to Unlit on the input material to no avail.
  • Is there a more standard approach for this sort of thing? The idea is the entire flow is contained within code to make it portable and not require any manual editor hookups.

The plot thickens. If I export the generated map with something like:

FImage Img;
FImageUtils::GetRenderTargetImage(RenderTarget, Img);
FImageUtils::SaveImageByExtension(TEXT("C:/temp/foo.exr"), Img);

The output looks correct. So I’m more confident that the issue lies in the way I’m hooking it up to this output material but it’s very unclear as to why.

Turns out I just needed to handle the compression correctly in the render target.

FTextureFormatSettings Settings;
Settings.CompressionSettings = TC_Normalmap;
RenderTarget->SetLayerFormatSettings(0, Settings);
RenderTarget->UpdateResource();

Once done, the sampler was able to read from the map I produced huzzah!