In my code I am creating a Material Instance Dynamic from a pre-existing material, then adding that Material Instance Dynamic as a blendable onto a PostProcessVolume. The problem I am having is that I am unable to update a scalar value in a Material Parameter Collection that is inside the MID and it only ever uses the default value (which is then applied correctly).
Oddly enough the function calls to set the scalar parameter value and get it seem to work, and the value does actually update since I can display the scalar parameter value on-screen as a debug message. The problem is that MID then doesn’t seem to use that value and only whatever the default is set to.
In my header file I have:
UPROPERTY(BlueprintReadOnly, Category = Material)
UMaterialInstanceDynamic* MIDFade;
UPROPERTY(BlueprintReadOnly, Category = Material)
UMaterial* MFade;
And then inside the constructor:
//Find the shader material and create a reference to it
static ConstructorHelpers::FObjectFinder<UMaterial> TempFadeShader(TEXT("/Game/Materials/FadeShader"));
if (TempFadeShader.Succeeded())
{
MFade = TempFadeShader.Object;
}
And finally in the PostInitializeComponents function:
//Create a material instance dynamic from the Material
MIDFade = UMaterialInstanceDynamic::Create(MFade, this);
//Set the AlphaBlend value to something to test that it works
MIDFade->SetScalarParameterValue(FName(TEXT("AlphaBlend")), 1.0f);
float outBlendValue;
FString BlendValue;
//Find the post-process volume in the scene, there is only one in the scene.
for (TActorIterator<APostProcessVolume> ActorItr(GetWorld()); ActorItr; ++ActorItr)
{
ActorItr->bEnabled = true;
//Add the MID as a blendable
ActorItr->AddOrUpdateBlendable(MIDFade);
}
//Get the AlphaBlend value in the MID and display it on-screen
if (MIDFade->GetScalarParameterValue(FName(TEXT("AlphaBlend")), outBlendValue))
{
BlendValue = FString::SanitizeFloat(outBlendValue);
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "AlphaBlend Value: " + BlendValue);
}
And for completeness sake here is the FadeShader Material itself:
This PostProcess material should just simply fade the screen to a specific colour, and the strength of the fade is based on the AlphaBlend parameter. Any help with this will be appreciated, thanks a lot!