Lighting.hlsl 1.1 KB
Newer Older
timchen1002's avatar
update  
timchen1002 已提交
1 2
// 光照计算相关库
#ifndef CUSTOM_LIGHTING_INCLUDED
timchen1002's avatar
update  
timchen1002 已提交
3 4
#define CUSTOM_LIGHTING_INCLUDED

timchen1002's avatar
update  
timchen1002 已提交
5 6 7 8
// 计算入射光照
float3 IncomingLight(Surface surface, Light light)
{
    return saturate(dot(surface.normal, light.direction) * light.attenuation) * light.color;
timchen1002's avatar
update  
timchen1002 已提交
9 10
}

timchen1002's avatar
update  
timchen1002 已提交
11 12 13 14
// 入射光乘以光照照射到表面的直接照明颜色,得到最终的照明颜色
float3 GetLighting(Surface surface, BRDF brdf, Light light)
{
    return IncomingLight(surface, light) * DirectBRDF(surface, brdf, light);
timchen1002's avatar
update  
timchen1002 已提交
15 16
}

timchen1002's avatar
update  
timchen1002 已提交
17 18 19 20 21 22 23 24
// 根据物体的表面信息和灯光属性获取最终光照结果
float3 GetLighting(Surface surfaceWS, BRDF brdf, GI gi)
{
    // 得到表面阴影数据
    ShadowData shadowData = GetShadowData(surfaceWS);
    shadowData.shadowMask = gi.shadowMask;

    // 可见光的光照结果进行累加得到最终光照结果
timchen1002's avatar
update  
timchen1002 已提交
25
    float3 color = IndirectBRDF(surfaceWS, brdf, gi.diffuse, gi.specular);
timchen1002's avatar
update  
timchen1002 已提交
26 27 28 29 30 31
    for (int i = 0; i < GetDirectionalLightCount(); i++)
    {
        Light light = GetDirectionalLight(i, surfaceWS, shadowData);
        color += GetLighting(surfaceWS, brdf, light);
    }
    return color;
timchen1002's avatar
update  
timchen1002 已提交
32 33 34
}

#endif