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.
There's no existing public thread on this issue, so head over to Questions & Answers just mention UE-138666 in the post.
| 2 |
| Component | UE - Rendering Architecture - Materials |
|---|---|
| Affects Versions | 4.26, 5.0-early access 2 |
| Created | Jan 7, 2022 |
|---|---|
| Updated | Sep 22, 2022 |