Description

Material layer instances do not generate a valid UMaterialFunctionInterface::StateId.
For this reason, materials and material instances that use material layer instances will not recompile their shaders even if the material layer has changed.

 

Here's a workaround

 
MaterialExpressions.cpp

#if WITH_EDITOR
//workaround begin
void UMaterialFunctionInstance::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
	StateId = Parent ? Parent->StateId : FGuid(); 
	Super::PostEditChangeProperty(PropertyChangedEvent);
}
//workaround end

#endif // WITH_EDITOR
void UMaterialFunctionInstance::PostLoad()
{
	Super::PostLoad();

	if (Parent)
	{
		Parent->ConditionalPostLoad();
		StateId = Parent->StateId;		//workaround
	}
}

MaterialFunctionInstance.h

class UMaterialFunctionInstance : public UMaterialFunctionInterface
{
...
	virtual bool ValidateFunctionUsage(class FMaterialCompiler* Compiler, const FFunctionExpressionOutput& Output) override;

#if WITH_EDITOR	//workaround begin
	virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif // WITH_EDITOR //workaround end
	virtual void PostLoad() override;

MaterialEditingLibrary.cpp

void UMaterialEditingLibrary::UpdateMaterialFunction(UMaterialFunctionInterface* MaterialFunction, UMaterial* PreviewMaterial)
{
...
			// Go through all function instances in memory and update them if they are children
			for (TObjectIterator<UMaterialFunctionInstance> It; It; ++It)
			{
				UMaterialFunctionInstance* FunctionInstance = *It;

				TArray<UMaterialFunctionInterface*> Functions;
				FunctionInstance->GetDependentFunctions(Functions);
				if (Functions.Contains(MaterialFunction))
				{
					FunctionInstance->UpdateParameterSet();
					FunctionInstance->PostEditChange();	//workaround
					FunctionInstance->MarkPackageDirty();
				}
			}


 

 The workaround copies StateId from parent material layer.

 

Steps to Reproduce
  1. Open the repro project on UE5.0 [Link Removed]
  2. See the red box in the level
  3. Open /Game/NewMaterialLayer
  4. Modify the Constat3Vector color
  5. Press the Apply button in the material layer editor
  6. See the box color has not changed

 

Have Comments or More Details?

There's no existing public thread on this issue, so head over to Questions & Answers just mention UE-138666 in the post.

1
Login to Vote

Backlogged
CreatedJan 7, 2022
UpdatedSep 22, 2022