ue4.26 通过材质开关控制mesh pass的blend function
一,mesh pass中blend function的设置方法
在mesh pass中设置blend function有如下几种方式:
1,在CreateXXXProcessor (返回FXXXProcessor)中:
2,FXXXProcessor::AddMeshBatch中:
3,FXXXProcessor::Process中:
4,RenderXXX中:
二,材质开关访问途径
我们知道,访问材质开关有以下几种途径:
1,通过FMaterialRelevance
((StaticMeshSceneProxy)PrimitiveSceneProxy).MaterialRelevance.bXXX;
2,通过UMaterial
Material.bXXX
3,通过FMaterialResource (public FMaterial):
MaterialResource.IsEnableXXX()
三,在mesh pass中访问材质开关
以Distortion pass为例,其SetBlendState是写在CreateDistortionPassProcessor中的,为Add one one:
现在假设我们已经添加了材质开关bDistortionBlendFuncOverride,下面看如何通过这个开关去控制把blend func切换为Add one zero。
由于CreateDistortionPassProcessor中不方便访问到材质开关,而且CreateDistortionPassProcessor为一个创建函数,其中设置blendState只是初始化,只会在特定时刻调用,意味着即使人们在这里面加了条件判断,那么当我们切换开关状态时因为不会再重新创建processor,所以也不会生效。所以还是应该在后面频繁调用的函数中去设置。
比如可以选择在FDistortionMeshProcessor::Process中去设置:
void FDistortionMeshProcessor::Process(
const FMeshBatch& MeshBatch,
uint64 BatchElementMask,
const FPrimitiveSceneProxy* RESTRICT PrimitiveSceneProxy,
int32 StaticMeshId,
const FMaterialRenderProxy& RESTRICT MaterialRenderProxy,
const FMaterial& RESTRICT MaterialResource,
ERasterizerFillMode MeshFillMode,
ERasterizerCullMode MeshCullMode)
{
const FVertexFactory* VertexFactory = MeshBatch.VertexFactory;
TMeshProcessorShaders<
FDistortionMeshVS,
FDistortionMeshHS,
FDistortionMeshDS,
FDistortionMeshPS> DistortionPassShaders;
GetDistortionPassShaders(
MaterialResource,
VertexFactory->GetType(),
FeatureLevel,
DistortionPassShaders.HullShader,
DistortionPassShaders.DomainShader,
DistortionPassShaders.VertexShader,
DistortionPassShaders.PixelShader
);
FMeshMaterialShaderElementData ShaderElementData;
ShaderElementData.InitializeMeshMaterialData(ViewIfDynamicMeshCommand, PrimitiveSceneProxy, MeshBatch, StaticMeshId, false);
const FMeshDrawCommandSortKey SortKey = CalculateMeshStaticSortKey(DistortionPassShaders.VertexShader, DistortionPassShaders.PixelShader);
//yang chao begin
if (MaterialResource.IsEnableDistortionBlendFuncOverride() == true) {
PassDrawRenderState.SetBlendState(TStaticBlendState<CW_RGBA, BO_Add, BF_One, BF_Zero, BO_Add, BF_Zero, BF_One>::GetRHI());
}else{
PassDrawRenderState.SetBlendState(TStaticBlendState<CW_RGBA, BO_Add, BF_One, BF_One, BO_Add, BF_One, BF_One>::GetRHI());
}
//yang chao end
BuildMeshDrawCommands(
MeshBatch,
BatchElementMask,
PrimitiveSceneProxy,
MaterialRenderProxy,
MaterialResource,
PassDrawRenderState,
DistortionPassShaders,
MeshFillMode,
MeshCullMode,
SortKey,
EMeshPassFeatures::Default,
ShaderElementData);
}