yzt
2023-05-26 de4278af2fd46705a40bac58ec01122db6b7f3d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
vec3 LINEARtoSRGB(vec3 linearIn) 
{
    #ifndef HDR 
    return pow(linearIn, vec3(1.0/2.2));
    #else 
    return linearIn;
    #endif 
}
 
#ifdef LIGHTING_PBR
vec3 applyTonemapping(vec3 linearIn) 
{
    #ifndef HDR 
    return czm_acesTonemapping(linearIn);
    #else 
    return linearIn;
    #endif 
}
 
vec3 computePbrLighting(czm_modelMaterial inputMaterial)
{
    czm_pbrParameters pbrParameters;
    pbrParameters.diffuseColor = inputMaterial.diffuse;
    pbrParameters.f0 = inputMaterial.specular;
    pbrParameters.roughness = inputMaterial.roughness;
    
    vec3 lightColorHdr = czm_lightColorHdr;
 
    vec3 color = inputMaterial.diffuse;
    #ifdef HAS_NORMALS
    color = czm_pbrLighting(
        v_positionEC,
        inputMaterial.normalEC,
        czm_lightDirectionEC,
        lightColorHdr,
        pbrParameters
    );
    #endif
 
    color *= inputMaterial.occlusion;
    color += inputMaterial.emissive;
 
    // Convert high-dynamic range to low-dynamic range in HDR mode
    color = applyTonemapping(color);
    return color;
}
#endif
 
void lightingStage(inout czm_modelMaterial material)
{
    // Even though the lighting will only set the diffuse color,
    // pass all other properties so further stages have access to them.
    vec3 color = vec3(0.0);
 
    #ifdef LIGHTING_PBR
    color = computePbrLighting(material);
    #else // unlit
    color = material.diffuse;
    #endif
 
    color = LINEARtoSRGB(color);
 
    material.diffuse = color;
}