Skip to content

Instantly share code, notes, and snippets.

@BlurryLight
Created June 11, 2023 07:21
Show Gist options
  • Save BlurryLight/ec74297784e614e2cf005e86a9c64460 to your computer and use it in GitHub Desktop.
Save BlurryLight/ec74297784e614e2cf005e86a9c64460 to your computer and use it in GitHub Desktop.
Simple Logic about replacing Material nodes in UE
void AlterMaterialParameter_Simple(UMaterial* Mat)
{
UTexture2DArrayFactory* Texture2DArrayFactory = NewObject<UTexture2DArrayFactory>();
UTexture2DArray* DummyTex = Cast<UTexture2DArray>(Texture2DArrayFactory->FactoryCreateNew(UTexture2DArray::StaticClass(),Mat,NAME_None,RF_Standalone | RF_Transactional,nullptr,GWarn));
TArray<UMaterialExpressionTextureSampleParameter* > ParamsTextureSamples;
for (int32 ExpressionIndex = 0; ExpressionIndex < Mat->Expressions.Num(); ExpressionIndex++)
{
auto* ExpressionPtr = Cast<UMaterialExpressionTextureSampleParameter>(Mat->Expressions[ExpressionIndex]);
if (ExpressionPtr)
{
ParamsTextureSamples.Add(ExpressionPtr);
}
}
if(ParamsTextureSamples.Num() == 0) {return;}
for(int i = 0 ; i < ParamsTextureSamples.Num();i++)
{
auto TargetExpression = ParamsTextureSamples[i];
if (!Mat->MaterialGraph)
{
Mat->MaterialGraph = CastChecked<UMaterialGraph>(FBlueprintEditorUtils::CreateNewGraph(Mat, NAME_None, UMaterialGraph::StaticClass(), UMaterialGraphSchema::StaticClass()));
Mat->MaterialGraph->Material = Mat;
Mat->MaterialGraph->RebuildGraph();
}
UMaterialGraphNode* GraphNode = Cast<UMaterialGraphNode>(TargetExpression->GraphNode);
if(!GraphNode)
{
UE_LOG(LogTemp, Error, TEXT("No Graph Node"));
return ;
}
UAssetEditorSubsystem* AssetEditorSubsystem = GEditor->GetEditorSubsystem<UAssetEditorSubsystem>();
AssetEditorSubsystem->CloseAllEditorsForAsset( Mat );
UMaterialExpression* TexCoordExpression = TargetExpression->Coordinates.Expression;
UMaterialExpressionTextureSampleParameter2DArray* Tex2DArrayExpression=
NewObject<UMaterialExpressionTextureSampleParameter2DArray>(Mat);
Tex2DArrayExpression->ParameterName = *FString::Printf(TEXT("%s_Arr"),*TargetExpression->ParameterName.ToString());
Tex2DArrayExpression->SamplerType = SAMPLERTYPE_Color;
Tex2DArrayExpression->Group = TargetExpression->Group;
Tex2DArrayExpression->Texture = DummyTex;
Tex2DArrayExpression->MaterialExpressionEditorX = TargetExpression->MaterialExpressionEditorX;
Tex2DArrayExpression->MaterialExpressionEditorY = TargetExpression->MaterialExpressionEditorY - 96;
Mat->MaterialGraph->AddExpression(Tex2DArrayExpression,/*bUserInvoke*/ false);
Mat->Expressions.Add(Tex2DArrayExpression);
auto Tex2DArrayGraphNode = Cast<UMaterialGraphNode>(Tex2DArrayExpression->GraphNode);
Tex2DArrayGraphNode->ReplaceNode(GraphNode);
TArray<UEdGraphPin*> Tex2DArrayGraphInputPins;
Tex2DArrayGraphNode->GetInputPins(Tex2DArrayGraphInputPins);
GraphNode->GetGraph()->GetSchema()->BreakPinLinks(*Tex2DArrayGraphInputPins[0],true);
GraphNode->GetGraph()->GetSchema()->BreakNodeLinks(*GraphNode);
Mat->MaterialGraph->LinkMaterialExpressionsFromGraph();
auto * LayerExpression= Cast<UMaterialExpressionConstant>(UMaterialEditingLibrary::CreateMaterialExpression(Mat,UMaterialExpressionConstant::StaticClass()));
LayerExpression->R = 0.0;
FString FunctionString = TEXT("/Engine/Functions/Engine_MaterialFunctions02/Utility/BreakOutFloat2Components.BreakOutFloat2Components");
UMaterialFunction* BreakOutFloat2 = LoadObject<UMaterialFunction>(nullptr, *FunctionString, nullptr, LOAD_None, nullptr);
UMaterialExpressionMaterialFunctionCall* BreakOutFloat2Expression=
Cast<UMaterialExpressionMaterialFunctionCall>(UMaterialEditingLibrary::CreateMaterialExpression(Mat,UMaterialExpressionMaterialFunctionCall::StaticClass()));
BreakOutFloat2Expression->MaterialFunction = BreakOutFloat2;
BreakOutFloat2Expression->UpdateFromFunctionResource();
TexCoordExpression->ConnectExpression(BreakOutFloat2Expression->GetInput(0),0);
FunctionString = TEXT("/Engine/Functions/Engine_MaterialFunctions02/Utility/MakeFloat3.MakeFloat3");
UMaterialFunction* MakeFloat3= LoadObject<UMaterialFunction>(nullptr, *FunctionString, nullptr, LOAD_None, nullptr);
UMaterialExpressionMaterialFunctionCall* MakeFloat3Expression =
Cast<UMaterialExpressionMaterialFunctionCall>(UMaterialEditingLibrary::CreateMaterialExpression(Mat,UMaterialExpressionMaterialFunctionCall::StaticClass()));
MakeFloat3Expression->MaterialFunction = MakeFloat3;
MakeFloat3Expression->UpdateFromFunctionResource();
BreakOutFloat2Expression->ConnectExpression(MakeFloat3Expression->GetInput(0),0); // u
BreakOutFloat2Expression->ConnectExpression(MakeFloat3Expression->GetInput(1),1); // v
LayerExpression->ConnectExpression(MakeFloat3Expression->GetInput(2),0); // w
MakeFloat3Expression->ConnectExpression(&(Tex2DArrayExpression->Coordinates),0); // uvw
Mat->Expressions.Remove(GraphNode->MaterialExpression);
Mat->RemoveExpressionParameter(GraphNode->MaterialExpression);
Mat->MaterialGraph->RebuildGraph();
}
Mat->MarkPackageDirty();
Mat->PostEditChange();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment