All files / engine/Source/Shaders BrdfLutGeneratorFS.js

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103                                                                                                                                                                                                             
//This file is automatically rebuilt by the Cesium build process.
export default "in vec2 v_textureCoordinates;\n\
const float M_PI = 3.141592653589793;\n\
\n\
float vdcRadicalInverse(int i)\n\
{\n\
    float r;\n\
    float base = 2.0;\n\
    float value = 0.0;\n\
    float invBase = 1.0 / base;\n\
    float invBi = invBase;\n\
    for (int x = 0; x < 100; x++)\n\
    {\n\
        if (i <= 0)\n\
        {\n\
            break;\n\
        }\n\
        r = mod(float(i), base);\n\
        value += r * invBi;\n\
        invBi *= invBase;\n\
        i = int(float(i) * invBase);\n\
    }\n\
    return value;\n\
}\n\
\n\
vec2 hammersley2D(int i, int N)\n\
{\n\
    return vec2(float(i) / float(N), vdcRadicalInverse(i));\n\
}\n\
\n\
vec3 importanceSampleGGX(vec2 xi, float alphaRoughness, vec3 N)\n\
{\n\
    float alphaRoughnessSquared = alphaRoughness * alphaRoughness;\n\
    float phi = 2.0 * M_PI * xi.x;\n\
    float cosTheta = sqrt((1.0 - xi.y) / (1.0 + (alphaRoughnessSquared - 1.0) * xi.y));\n\
    float sinTheta = sqrt(1.0 - cosTheta * cosTheta);\n\
    vec3 H = vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta);\n\
    vec3 upVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);\n\
    vec3 tangentX = normalize(cross(upVector, N));\n\
    vec3 tangentY = cross(N, tangentX);\n\
    return tangentX * H.x + tangentY * H.y + N * H.z;\n\
}\n\
\n\
/**\n\
 * Estimate the geometric self-shadowing of the microfacets in a surface,\n\
 * using the Smith Joint GGX visibility function.\n\
 * Note: Vis = G / (4 * NdotL * NdotV)\n\
 * see Eric Heitz. 2014. Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs. Journal of Computer Graphics Techniques, 3\n\
 * see Real-Time Rendering. Page 331 to 336.\n\
 * see https://google.github.io/filament/Filament.md.html#materialsystem/specularbrdf/geometricshadowing(specularg)\n\
 *\n\
 * @param {float} alphaRoughness The roughness of the material, expressed as the square of perceptual roughness.\n\
 * @param {float} NdotL The cosine of the angle between the surface normal and the direction to the light source.\n\
 * @param {float} NdotV The cosine of the angle between the surface normal and the direction to the camera.\n\
 */\n\
float smithVisibilityGGX(float alphaRoughness, float NdotL, float NdotV)\n\
{\n\
    float alphaRoughnessSq = alphaRoughness * alphaRoughness;\n\
\n\
    float GGXV = NdotL * sqrt(NdotV * NdotV * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);\n\
    float GGXL = NdotV * sqrt(NdotL * NdotL * (1.0 - alphaRoughnessSq) + alphaRoughnessSq);\n\
\n\
    float GGX = GGXV + GGXL; // 2.0 if NdotL = NdotV = 1.0\n\
    if (GGX > 0.0)\n\
    {\n\
        return 0.5 / GGX; // 1/4 if NdotL = NdotV = 1.0\n\
    }\n\
    return 0.0;\n\
}\n\
\n\
vec2 integrateBrdf(float roughness, float NdotV)\n\
{\n\
    vec3 V = vec3(sqrt(1.0 - NdotV * NdotV), 0.0, NdotV);\n\
    float A = 0.0;\n\
    float B = 0.0;\n\
    const int NumSamples = 1024;\n\
    float alphaRoughness = roughness * roughness;\n\
    for (int i = 0; i < NumSamples; i++)\n\
    {\n\
        vec2 xi = hammersley2D(i, NumSamples);\n\
        vec3 H = importanceSampleGGX(xi, alphaRoughness, vec3(0.0, 0.0, 1.0));\n\
        vec3 L = 2.0 * dot(V, H) * H - V;\n\
        float NdotL = clamp(L.z, 0.0, 1.0);\n\
        float NdotH = clamp(H.z, 0.0, 1.0);\n\
        float VdotH = clamp(dot(V, H), 0.0, 1.0);\n\
        if (NdotL > 0.0)\n\
        {\n\
            float G = smithVisibilityGGX(alphaRoughness, NdotL, NdotV);\n\
            float G_Vis = 4.0 * G * VdotH * NdotL / NdotH;\n\
            float Fc = pow(1.0 - VdotH, 5.0);\n\
            A += (1.0 - Fc) * G_Vis;\n\
            B += Fc * G_Vis;\n\
        }\n\
    }\n\
    return vec2(A, B) / float(NumSamples);\n\
}\n\
\n\
void main()\n\
{\n\
    out_FragColor = vec4(integrateBrdf(v_textureCoordinates.y, v_textureCoordinates.x), 0.0, 1.0);\n\
}\n\
";