M_Ch_DefaultLut

Texture

Texture Example Usage Comment
BaseMap Pasted image 20261119192439.png 基础色
NormalMap Pasted image 20261119192450.png 法线图
MaskMap Pasted image 20261119192504.png
LightRamp Pasted image 20261119192635.png
ViewRamp Pasted image 20261119192700.png
SpecularLut M_Ch_DefaultLut_SpecularLut.png

光照计算

构造Lighting Context

首先,构造共用的灯光上下文。

struct FEFSurfaceLightingContext
{
    float4 LightRamp;
    float4 ViewRamp;
    float3 SpecularRamp;
    float NoV;
    float SpecularFactor;
    float3 F0;
    float3 IndirectSpecularF0;
};
IndirectSpecularF0
    // ViewBlend 同时控制环境反射是否使用 LUT 色调,这是原公式的耦合关系。
    Context.IndirectSpecularF0 = Context.F0 * lerp(1.0.xxx, SpecularRampColor, ViewBlend);

M_Ch_DefaultLut_IndirectSpecularF09.png

风格化GGX

首先,终末地构造了一个虚拟光源和虚拟HalfVector,它的作用是将高光尽量集中到面向相机的表面。

    float3 VirtualLightDirectionWS = normalize(float3(Shading.CameraBackwardWS.xy, lerp(0.5, MainLight.DirectionWS.z, MainLightVisibility)));
    float3 VirutalHalfVectorWS = normalize(MainLight.DirectionWS * MainLightVisibility + VirtualLightDirectionWS * 2.0 + Shading.ViewDirectionWS * (2.0 + MainLightVisibility));
    float NoH = dot(Surface.NormalWS, VirutalHalfVectorWS);
Image compare

M_Ch_DefaultLut_GGXNormal.png

M_Ch_DefaultLut_VirutalLightGGX.png

左图名称:HalfVector

右图名称:VirtualHalfVector

HalfVectorVirtualHalfVector

然后是终末地风格化了GGX公式:

FEFSurfaceGGXTerms EFSurfaceEvaluateStylizedGGX(float PerceptualRoughness, float NoH, float NoV)
{
    FEFSurfaceGGXTerms Result;
    Result.Alpha = max(PerceptualRoughness * PerceptualRoughness, 0.0078125); // 1/128
    float AlphaSquared = Result.Alpha * Result.Alpha;
    float Denominator = NoH * NoH * (AlphaSquared - 1.0) + 1.0;
    Denominator *= Denominator;
    // 保留原相等分支和运算顺序;此量是 GGX 形状因子,不是标准归一化 NDF。
    Result.Distribution = AlphaSquared / Denominator;
    // 这是风格化可见度,不是完整 Smith G / (4*NoL*NoV)。
    float Visibility = 0.5 / (2.0 * NoV + Result.Alpha + 0.0001);
    // DG/4(Nol)(NoV)
    Result.SpecularFactor = clamp(Result.Distribution * Visibility - 0.00006103515625, 0.0, 20.0);
    return Result;
}

这段代码保留了 GGX 的高光形状,但改写了可见度和强度控制。

普通 Cook–Torrance 镜面反射公式为:

𝑓𝑠=𝐷GGX⋅𝐹⋅𝐺4𝑁𝑜𝐿𝑁𝑜𝑉

其中,GGX 法线分布函数为:

𝐷GGX=𝛼2𝜋[𝑁𝑜𝐻2(𝛼2−1)+1]2

通常使用:

𝛼=𝑟2

这里 𝑟 是感知粗糙度,𝑁𝑜𝐿 =𝑁 ⋅𝐿、𝑁𝑜𝑉 =𝑁 ⋅𝑉、𝑁𝑜𝐻 =𝑁 ⋅𝐻。

1. 给粗糙度设置下限

𝛼=max(𝑟2,1128)

避免粗糙度趋近零时,高光峰值过于尖锐。

2. 分布项去掉了 𝜋

𝐷style=𝛼2[𝑁𝑜𝐻2(𝛼2−1)+1]2

因此,在使用相同 𝛼 时:

𝐷style=𝜋𝐷GGX

高光分布形状保持一致,但这个分布项的数值变成标准值的 𝜋 倍。这不代表最终高光也一定恰好增强 𝜋 倍,因为后续计算也发生了变化。

3. 可见度项换成简化公式
普通微表面模型中的可见度项为:

𝑉Smith=𝐺4𝑁𝑜𝐿𝑁𝑜𝑉

终末地的代码将它替换为:

𝑉style=0.52𝑁𝑜𝑉+𝛼+0.0001

标准 Smith 可见度同时考虑光线和视线方向;这里仅根据 𝑁𝑜𝑉 和粗糙度调节,不再完整模拟微表面遮蔽。

4. 对高光系数进行限幅
最终返回的 SpecularFactor 为:

𝑆style=clamp⁡(𝐷style𝑉style−116384,0,20)

减去很小的偏移,再将结果限制在 0 ∼20,用于控制极亮高光。

这个函数还没有乘 Fresnel 项 𝐹,所以返回的是风格化高光系数,不是完整的镜面反射 BRDF。

构造SurfaceShadingTerms

然后,构造共享着色计算项。

struct FEFSurfaceShadingTerms
{
    FEFSurfaceRampWeights Weights;
    float3 DiffuseAlbedo; // BaseColor * 0.96 * (1 - Metallic)。
    float3 DiffuseTint;   // Ramp 染色后的材质响应,主光与附加 Regular 光复用。
    float3 SpecularResponse;    // F0 * SpecularTint * SpecularFactor。
    float HighlightScale; // HighlightBlend * 0.5 + 0.5。
    float HighlightShadowResponse;
    float MainLightVisibility;  // GI 的亮部/暗部倍率仍由主光可见度控制。
    float StylizedSpecularScale;    // 沿用主光 SpecularScale,同时影响两路风格高光。
};

先计算3个权重:

struct FEFSurfaceRampWeights
{
    float View;   // View 可见度
    float Shadow; // 阴影
    float DeepShadow; // Deep阴影程度
};

FEFSurfaceRampWeights EFSurfaceComputeRampWeights(
FEFSurfaceData Surface, FEFSurfaceMainLightInputs MainLight, FEFSurfaceLightingContext Context)
{
    float CharacterVisibility = MainLight.Visibility.y;
    float OcclusionVisibility = Surface.RampOcclusion * CharacterVisibility;
    FEFSurfaceRampWeights Weights;
    Weights.View = Context.ViewRamp.a * OcclusionVisibility; // 角色View可见度
    Weights.Shadow = min(min(CharacterVisibility, Surface.RampOcclusion), Context.LightRamp.a);   // 角色阴影
    Weights.DeepShadow = saturate(Weights.View + Context.LightRamp.a);  // 角色Shadow向lighter色偏移程度
    return Weights;
}

终末地分了两种阴影色,Shadow和DeepShadow,用View和LightRamp来lerp。

M_Ch_DefaultLut_DeepShadowMask.png

DiffuseAlbedo
    float DiffuseEnergy = 0.96 * (1.0 - Surface.Metallic);
    Terms.DiffuseAlbedo = Surface.BaseColor * DiffuseEnergy;

M_Ch_DefaultLut_DiffuseAlbedo.png

DiffuseTint
float3 EFSurfaceComputeDiffuseTint(
float3 DiffuseAlbedo, float3 DarkDiffuseAlbedo,
float ShadowResponseScale, float MainLightVisibility,
FEFSurfaceLightingContext Context, FEFSurfaceRampWeights Weights)
{
    float3 ShadowAlbedo = DarkDiffuseAlbedo * ShadowResponseScale;
    float3 DeepShadowAlbedo = EFSurfaceSaturation(ShadowAlbedo * 0.65, 1.2);
    float3 SaturatedDiffuseAlbedo = EFSurfaceSaturation(DiffuseAlbedo, 1.2);
    float3 Albedo = lerp(lerp(DeepShadowAlbedo, ShadowAlbedo, Weights.DeepShadow), DiffuseAlbedo, Weights.Shadow);

    // 对Albedo进行Ramp图染色
    // 灰色 Ramp 的 Chroma=0,不直接通过 RGB 压暗颜色;Alpha 仍控制明暗权重。
    float3 RampColor = Context.LightRamp.rgb;
    float RampChroma = max(max(RampColor.r, RampColor.g), RampColor.b) - min(min(RampColor.r, RampColor.g), RampColor.b); // Ramp有色程度
    float3 RampTint = lerp(1.0.xxx, RampColor, RampChroma);
    float3 RampTone = Albedo * RampTint;
    // 染色后的亮度补偿有 1.5 上限,不是无条件严格保亮度。
    RampTone *= clamp(EFSurfaceLuminance(Albedo) / max(EFSurfaceLuminance(RampTone), 0.001), 0.0, 1.5);
    float3 ShadowTone = lerp(ShadowAlbedo, SaturatedDiffuseAlbedo, Weights.View);

    return lerp(ShadowTone, RampTone, MainLightVisibility);
}

    float3 DarkDiffuseAlbedo = Surface.DarkColor * DiffuseEnergy;
    Terms.DiffuseTint = EFSurfaceComputeDiffuseTint(Terms.DiffuseAlbedo, DarkDiffuseAlbedo,
        ShadowResponseScale, Terms.MainLightVisibility, Context, Terms.Weights);
Image compare

M_Ch_DefaultLut_DiffuseAlbedo-2.png

M_Ch_DefaultLut_RampTone.png

左图名称:DiffuseAlbedo

右图名称:DiffuseRamp

DiffuseAlbedoDiffuseRamp

Main Lighting

Diffuse

M_Ch_DefaultLut_MainLightDiffuse.png

Specular

M_Ch_DefaultLut_MainLightSpecular.png

Emission

Indirect Lighting

补光

高光

风格化GGX

代码附件

搜索博客