UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MaterialInstanceUpdateParameterSet.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5// HEADER_UNIT_SKIP - Not included directly
6
11#include "Misc/App.h"
12
13#if WITH_EDITOR
15{
17 template<typename ExpressionType>
19 {
21 {
25
26 if (ExpressionPtr && ExpressionPtr->GetParameterExpressionId() == InGUID)
27 {
28 check(ExpressionPtr->bIsParameterExpression);
30 {
31 // UE-57086, workaround - To deal with duplicated parameters with matching GUIDs we walk
32 // through every parameter rather than taking the first. Either we return the first matching GUID
33 // we encounter (as before), or if we find another with the same name that can take precedence.
34 // Only taking the first parameter means we can incorrectly treat the parameter as a rename and
35 // lose/move data when we encounter an illegal GUID duplicate.
36 // Properly fixing duplicate GUIDs is beyond the scope of a hotfix, see UE-47863 for more info.
37 // NOTE: The case where a parameter in a function is renamed but another function in the material
38 // contains a duplicate GUID is still broken and may lose the data. This still leaves us in a
39 // more consistent state than 4.18 and should minimize the impact to a rarer occurrence.
40 if (!OutExpression || InName == ParamExpression->ParameterName)
41 {
43 }
44 }
45 }
46 else if (MaterialFunctionCall && MaterialFunctionCall->MaterialFunction)
47 {
49 }
50 else if (MaterialLayers)
51 {
52 const TArray<UMaterialFunctionInterface*>& Layers = MaterialLayers->GetLayers();
53 const TArray<UMaterialFunctionInterface*>& Blends = MaterialLayers->GetBlends();
54
55 for (const auto* Layer : Layers)
56 {
57 if (Layer)
58 {
60 }
61 }
62
63 for (const auto* Blend : Blends)
64 {
65 if (Blend)
66 {
68 }
69 }
70 }
71 }
72 }
73
74 template <typename ParameterType, typename ExpressionType>
75 bool UpdateParameter_FullTraversal(ParameterType& Parameter, UMaterial* ParentMaterial)
76 {
78 {
79 if (Expression->IsA<ExpressionType>())
80 {
82 if (ParameterExpression->ParameterName == Parameter.ParameterInfo.Name)
83 {
84 Parameter.ExpressionGUID = ParameterExpression->ExpressionGUID;
85 return true;
86 }
87 }
89 {
90 if (FunctionCall->MaterialFunction && FunctionCall->MaterialFunction->UpdateParameterSet<ParameterType, ExpressionType>(Parameter))
91 {
92 return true;
93 }
94 }
96 {
97 const TArray<UMaterialFunctionInterface*> Layers = LayersExpression->GetLayers();
98 const TArray<UMaterialFunctionInterface*> Blends = LayersExpression->GetBlends();
99
100 for (auto* Layer : Layers)
101 {
102 if (Layer && Layer->UpdateParameterSet<ParameterType, ExpressionType>(Parameter))
103 {
104 return true;
105 }
106 }
107
108 for (auto* Blend : Blends)
109 {
110 if (Blend && Blend->UpdateParameterSet<ParameterType, ExpressionType>(Parameter))
111 {
112 return true;
113 }
114 }
115 }
116 }
117 return false;
118 }
119
120 template <typename ParameterType, typename ExpressionType>
122 {
123 bool bChanged = false;
124
125 // Loop through all of the parameters and try to either establish a reference to the
126 // expression the parameter represents, or check to see if the parameter's name has changed.
127 for (int32 ParameterIdx = 0; ParameterIdx < Parameters.Num(); ParameterIdx++)
128 {
129 bool bTryToFindByName = true;
130
131 ParameterType& Parameter = Parameters[ParameterIdx];
132
133 if (Parameter.ExpressionGUID.IsValid())
134 {
135 ExpressionType* Expression = nullptr;
136 FindClosestExpressionByGUIDRecursive<ExpressionType>(Parameter.ParameterInfo.Name, Parameter.ExpressionGUID, ParentMaterial->GetExpressions(), Expression);
137
138 // Check to see if the parameter name was changed.
139 if (Expression)
140 {
141 bTryToFindByName = false;
142
143 if (Parameter.ParameterInfo.Name != Expression->ParameterName)
144 {
145 Parameter.ParameterInfo.Name = Expression->ParameterName;
146 bChanged = true;
147 }
148 }
149 }
150
151 // No reference to the material expression exists, so try to find one in the material expression's array if we are in the editor.
153 {
155 {
156 bChanged = true;
157 }
158 }
159 }
160
161 return bChanged;
162 }
163
164
165 template <typename ParameterType, typename ExpressionType>
167 {
168 bool bChanged = false;
169
175
176 // Loop through all of the parameters and try to either establish a reference to the
177 // expression the parameter represents, or check to see if the parameter's name has changed.
178 for (int32 ParameterIdx = 0; ParameterIdx < Parameters.Num(); ParameterIdx++)
179 {
180 bool bTryToFindByName = true;
181
182 ParameterType& Parameter = Parameters[ParameterIdx];
183
184 if (Parameter.ExpressionGUID.IsValid())
185 {
188 {
189 if (CachedParamGuids[CachedParamIdx] == Parameter.ExpressionGUID)
190 {
191 // UE-57086, workaround - To deal with duplicated parameters with matching GUIDs we walk
192 // through every parameter rather than taking the first. Either we return the first matching GUID
193 // we encounter (as before), or if we find another with the same name that can take precedence.
194 // Only taking the first parameter means we can incorrectly treat the parameter as a rename and
195 // lose/move data when we encounter an illegal GUID duplicate.
196 // Properly fixing duplicate GUIDs is beyond the scope of a hotfix, see UE-47863 for more info.
197 // NOTE: The case where a parameter in a function is renamed but another function in the material
198 // contains a duplicate GUID is still broken and may lose the data. This still leaves us in a
199 // more consistent state than 4.18 and should minimize the impact to a rarer occurrence.
200 if ((CachedParamCandidate == INDEX_NONE) || Parameter.ParameterInfo.Name == CachedParamInfos[CachedParamIdx].Name)
201 {
203 }
204 }
205 }
206
207 // Check to see if the parameter name was changed.
209 {
211 bTryToFindByName = false;
212
213 if (Parameter.ParameterInfo.Name != CandidateParamInfo.Name)
214 {
215 Parameter.ParameterInfo.Name = CandidateParamInfo.Name;
216 bChanged = true;
217 }
218 }
219 }
220
221 // No reference to the material expression exists, so try to find one in the material expression's array if we are in the editor.
223 {
225 {
226 bChanged = true;
227 }
228 }
229 }
230
231 return bChanged;
232 }
233}
234
244template <typename ParameterType, typename ExpressionType>
245bool UpdateParameterSet(TArray<ParameterType>& Parameters, UMaterial* ParentMaterial) { return MaterialInstance_Private::UpdateParameterSet_FullTraversal<ParameterType, ExpressionType>(Parameters, ParentMaterial); }
246
250template <typename ParameterType, typename ExpressionType>
252{
253 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FScalarParameterValue, ExpressionType>(EMaterialParameterType::Scalar, Parameters, ParentMaterial);
254}
255template <typename ParameterType, typename ExpressionType>
257{
258 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FVectorParameterValue, ExpressionType>(EMaterialParameterType::Vector, Parameters, ParentMaterial);
259}
260template <typename ParameterType, typename ExpressionType>
262{
263 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FDoubleVectorParameterValue, ExpressionType>(EMaterialParameterType::DoubleVector, Parameters, ParentMaterial);
264}
265template <typename ParameterType, typename ExpressionType>
267{
268 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FTextureParameterValue, ExpressionType>(EMaterialParameterType::Texture, Parameters, ParentMaterial);
269}
270template <typename ParameterType, typename ExpressionType>
272{
273 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FTextureCollectionParameterValue, ExpressionType>(EMaterialParameterType::TextureCollection, Parameters, ParentMaterial);
274}
275template <typename ParameterType, typename ExpressionType>
277{
278 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FFontParameterValue, ExpressionType>(EMaterialParameterType::Font, Parameters, ParentMaterial);
279}
280template <typename ParameterType, typename ExpressionType>
282{
283 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FRuntimeVirtualTextureParameterValue, ExpressionType>(EMaterialParameterType::RuntimeVirtualTexture, Parameters, ParentMaterial);
284}
285template <typename ParameterType, typename ExpressionType>
287{
288 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FSparseVolumeTextureParameterValue, ExpressionType>(EMaterialParameterType::SparseVolumeTexture, Parameters, ParentMaterial);
289}
290template <typename ParameterType, typename ExpressionType>
292{
293 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FStaticSwitchParameter, ExpressionType>(EMaterialParameterType::StaticSwitch, Parameters, ParentMaterial);
294}
295template <typename ParameterType, typename ExpressionType>
297{
298 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FStaticComponentMaskParameter, ExpressionType>(EMaterialParameterType::StaticComponentMask, Parameters, ParentMaterial);
299}
300template <typename ParameterType, typename ExpressionType>
302{
303 return MaterialInstance_Private::UpdateParameterSet_WithCachedData<FParameterCollectionParameterValue, ExpressionType>(EMaterialParameterType::ParameterCollection, Parameters, ParentMaterial);
304}
305
306#endif // WITH_EDITOR
#define check(expr)
Definition AssertionMacros.h:314
#define GIsEditor
Definition CoreGlobals.h:233
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EMaterialParameterType
Definition MaterialParameters.h:187
static FORCEINLINE bool IsGame()
Definition App.h:175
Definition NameTypes.h:617
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition MaterialExpressionMaterialAttributeLayers.h:27
Definition MaterialExpressionMaterialFunctionCall.h:81
Definition MaterialExpression.h:150
Definition Material.h:432
Definition Guid.h:109
Definition MaterialParameters.h:33
Definition ObjectPtr.h:488