I am having a weird issue with MaterialParameterCollections.
I have made a Notify State that updates a MaterialParameterCollection overtime. When logged, it seems that the scalar parameters are properly being animated, but I am not seeing any visual change during the game. When I close the game and return to the editor however, the effects of the MaterialParameterCollection reflects what it should have been in game however.
It’s really hard to explain with just words, so let me show some screenshots.
Note that the Output Log on the left is showing the scalar value of the pupil; at that size, the eye should have been perfectly black, like you can see in the second screenshot.
The code for the notify state in question.
void UPA_AnimState_SetMatParameter::NotifyBegin(USkeletalMeshComponent * MeshComp, class UAnimSequenceBase * Animation, float TotalDuration)
{
if (ValidateParameters(MeshComp) && !bStartedState)
{
AnimDuration = TotalDuration;
ElapsedTime = 0.f;
World = GetWorld();
#if WITH_EDITOR
World = GEditor->GetEditorWorldContext().World();
#endif
if (World == NULL)
return;
StartTime = World->GetTimeSeconds();
bStartedState = true;
UE_LOG(AnyLog, Log, TEXT("StartTime %f"), StartTime);
Received_NotifyBegin(MeshComp, Animation, TotalDuration);
}
}
void UPA_AnimState_SetMatParameter::NotifyTick(USkeletalMeshComponent * MeshComp, class UAnimSequenceBase * Animation, float FrameDeltaTime)
{
if (MeshComp
&& Animation
&& World)
{
ElapsedTime = World->GetTimeSeconds() - StartTime;
float DurationPct = ElapsedTime / (AnimDuration / Animation->RateScale);
float BlendPct;
if (bEaseIn)
{
if (bEaseOut)
{
// EASE IN/OUT
BlendPct = FMath::InterpEaseInOut(0.f, 1.f, DurationPct, CurveIntensity);
}
else
{
// EASE IN
BlendPct = FMath::Lerp(0.f, 1.f, FMath::Pow(DurationPct, CurveIntensity));
}
}
else
{
if (bEaseOut)
{
// EASE OUT
BlendPct = FMath::Lerp(0.f, 1.f, FMath::Pow(DurationPct, 1.f / CurveIntensity));
}
else
{
// LINEAR
BlendPct = FMath::Lerp(0.f, 1.f, DurationPct);
}
}
if (ScalarParameters.Num() != 0)
{
for (int i = 0; i < ScalarParameters.Num(); i++)
{
float AnimFloatValue = FMath::Lerp(ScalarParameters[i].StartValue, ScalarParameters[i].TargetValue, BlendPct);
UKismetMaterialLibrary::SetScalarParameterValue(World, MaterialParameterCollection, ScalarParameters[i].ParameterName, AnimFloatValue);
}
}
if (VectorParameters.Num() != 0)
{
for (int i = 0; i < VectorParameters.Num(); i++)
{
FVector AnimVectorValue = UKismetMathLibrary::LinearColorLerp(VectorParameters[i].StartValue, VectorParameters[i].TargetValue, BlendPct);
UKismetMaterialLibrary::SetVectorParameterValue(World, MaterialParameterCollection, VectorParameters[i].ParameterName, AnimVectorValue);
}
}
}
Received_NotifyTick(MeshComp, Animation, FrameDeltaTime);
}