4.24之后源码有所改动,网上的方法用不了。我改了一下成功了,在这里记录一下怕之后忘记,请不要吐槽我
1.UnrealEngine\Engine\Source\Runtime\Engine\Classes\Engine\EngineTypes.h
就还是叫这个名字吧MSM_StylizedShadow
2. UnrealEngine\Engine\Source\Runtime\Engine\Private\Materials\Material.cpp
找到 static bool IsPropertyActive_Internal这个函数:
3.UnrealEngine\Engine\Source\Runtime\Engine\Private\Materials\HLSLMaterialTranslator.h
找到Line 1520.
(4.25在HLSLMaterialTranslator.cpp)
4. UnrealEngine\Engine\Shaders\Private\ShadingCommon.ush
5. UnrealEngine\Engine\Shaders\Private\ShadingCommon.ush
找到 float3 GetShadingModelColor(uint ShadingModelID):
6. UnrealEngine\Engine\Shaders\Private\ShadingModelsMaterial.ush
找到 void SetGBufferForShadingModel:
#if MATERIAL_SHADINGMODEL_STYLIZED_SHADOW
GBuffer.ShadingModelID = SHADINGMODELID_STYLIZED_SHADOW;
GBuffer.CustomData.x = GetMaterialCustomData0(MaterialParameters);
#endif
7. UnrealEngine\Engine\Shaders\Private\BasePassCommon.ush
找到 #define WRITES_CUSTOMDATA_TO_GBUFFER
至此准备工作完成
8. UnrealEngine\Engine\Shaders\Private\DeferredLightingCommon.ush
找到GetDynamicLighting函数:
在Line373.左右
float3 AttenuationColor = 0.f;
float NoL = saturate( dot(N, L) );
float DistanceAttenuation = 1;
float LightRadiusMask = 1;
float SpotFalloff = 1;
BRANCH
if(ShadingModelID == SHADINGMODELID_STYLIZED_SHADOW)
{
float Range = GBuffer.CustomData.x * 0.5f;
AttenuationColor = LightColor * ((DistanceAttenuation * LightRadiusMask * SpotFalloff) * smoothstep(0.5f - Range, 0.5f + Range, SurfaceShadow) * 0.1f);
float3 H = normalize(V+L);
float NoH = saturate( dot(N, H));
float3 SurfaceLighting = GBuffer.DiffuseColor + saturate(smoothstep(0.5f - Range, 0.5f + Range, D_GGX(GBuffer.Roughness, NoH)) * GBuffer.SpecularColor);
LightAccumulator_Add( LightAccumulator, SurfaceLighting, Lighting.Diffuse, AttenuationColor * LightMask * Shadow.SurfaceShadow, bNeedsSeparateSubsurfaceLightAccumulation );
}
else
{
float SurfaceAttenuation = (DistanceAttenuation * LightRadiusMask * SpotFalloff) * SurfaceShadow;
AttenuationColor = LightColor * (NoL * SurfaceAttenuation);
Lighting.Specular *= LightData.SpecularScale;
LightAccumulator_Add( LightAccumulator, Lighting.Diffuse + Lighting.Specular, Lighting.Diffuse, AttenuationColor * LightMask * Shadow.SurfaceShadow, bNeedsSeparateSubsurfaceLightAccumulation );
LightAccumulator_Add( LightAccumulator, Lighting.Transmission, Lighting.Transmission, AttenuationColor * LightMask * Shadow.TransmissionShadow, bNeedsSeparateSubsurfaceLightAccumulation );
}
就是,判断一下是否是SHADINGMODELID_STYLIZED_SHADOW,然后在LightAccumulator_Add里替换成我们自己的AttenuationColor与SurfaceLighting
4.25要注意LightAccumulator_Add换成LightAccumulator_AddSplit,并且多了一个参数
最终会是这样
9.编译一下,然后找个场景把里面物体的材质挨个换成SHADINGMODELID_STYLIZED_SHADOW就好啦
10. UnrealEngine\Engine\Shaders\Private\BasePassPixelShader.usf
找到注释// Volume lighting for lit translucency
11.Engine\Source\Editor\PixelInspector\Private\PixelInspectorResult.h
12.Engine\Source\Editor\PixelInspector\Private\PixelInspectorResult.cpp
Line:211.DecodeShadingModel()
13.Engine\Shaders\Private\TiledDeferredLightShaders.usf
Line.345