Browse Source

Merge pull request #5988 from sebavan/master

Refactor shader PBR
David Catuhe 6 years ago
parent
commit
4e33180f20

+ 14 - 11
src/Shaders/ShadersInclude/helperFunctions.fx

@@ -4,6 +4,14 @@ const float LinearEncodePowerApprox = 2.2;
 const float GammaEncodePowerApprox = 1.0 / LinearEncodePowerApprox;
 const vec3 LuminanceEncodeApprox = vec3(0.2126, 0.7152, 0.0722);
 
+const float Epsilon = 0.0000001;
+
+#define saturate(x)         clamp(x, 0.0, 1.0)
+
+#define absEps(x)           abs(x) + Epsilon
+#define maxEps(x)           max(x, Epsilon)
+#define saturateEps(x)      clamp(x, Epsilon, 1.0)
+
 mat3 transposeMat3(mat3 inMatrix) {
     vec3 i0 = inMatrix[0];
     vec3 i1 = inMatrix[1];
@@ -35,16 +43,6 @@ mat3 inverseMat3(mat3 inMatrix) {
               b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
 }
 
-float computeFallOff(float value, vec2 clipSpace, float frustumEdgeFalloff)
-{
-    float mask = smoothstep(1.0 - frustumEdgeFalloff, 1.0, clamp(dot(clipSpace, clipSpace), 0., 1.));
-    return mix(value, 1.0, mask);
-}
-
-vec3 applyEaseInOut(vec3 x){
-    return x * x * (3.0 - 2.0 * x);
-}
-
 vec3 toLinearSpace(vec3 color)
 {
     return pow(color, vec3(LinearEncodePowerApprox));
@@ -60,6 +58,11 @@ float square(float value)
     return value * value;
 }
 
+float pow5(float value) {
+    float sq = value * value;
+    return sq * sq * value;
+}
+
 float getLuminance(vec3 color)
 {
     return clamp(dot(color, LuminanceEncodeApprox), 0., 1.);
@@ -81,7 +84,7 @@ float dither(vec2 seed, float varianceAmount) {
 const float rgbdMaxRange = 255.0;
 
 vec4 toRGBD(vec3 color) {
-    float maxRGB = max(0.0000001, max(color.r, max(color.g, color.b)));
+    float maxRGB = maxEps(max(color.r, max(color.g, color.b)));
     float D      = max(rgbdMaxRange / maxRGB, 1.);
     D            = clamp(floor(D) / 255.0, 0., 1.);
     // vec3 rgb = color.rgb * (D * (255.0 / rgbdMaxRange));

+ 7 - 7
src/Shaders/ShadersInclude/imageProcessingFunctions.fx

@@ -28,13 +28,13 @@
 		sliceUV.x *= sliceSize;
 		sliceUV.x += sliceInteger * sliceSize;
 
-		sliceUV = clamp(sliceUV, 0., 1.);
+		sliceUV = saturate(sliceUV);
 
 		vec4 slice0Color = texture2D(colorTransform, sliceUV);
 
 		sliceUV.x += sliceSize;
 		
-		sliceUV = clamp(sliceUV, 0., 1.);
+		sliceUV = saturate(sliceUV);
 		vec4 slice1Color = texture2D(colorTransform, sliceUV);
 
 		vec3 result = mix(slice0Color.rgb, slice1Color.rgb, sliceFraction);
@@ -86,7 +86,7 @@
 		color = ACESOutputMat * color;
 
 		// Clamp to [0, 1]
-		color = clamp(color, 0.0, 1.0);
+		color = saturate(color);
 
 		return color;
 	}
@@ -130,12 +130,12 @@ vec4 applyImageProcessing(vec4 result) {
 
 	// Going back to gamma space
 	result.rgb = toGammaSpace(result.rgb);
-	result.rgb = clamp(result.rgb, 0.0, 1.0);
+	result.rgb = saturate(result.rgb);
 
 #ifdef CONTRAST
-	// Contrast
-	vec3 resultHighContrast = applyEaseInOut(result.rgb);
-
+	// Contrast EaseInOut
+	vec3 resultHighContrast = result.rgb * result.rgb * (3.0 - 2.0 * result.rgb);
+	
 	if (contrast < 1.0) {
 		// Decrease contrast: interpolate towards zero-contrast image (flat grey)
 		result.rgb = mix(vec3(0.5, 0.5, 0.5), result.rgb, contrast);

+ 287 - 0
src/Shaders/ShadersInclude/pbrBRDFFunctions.fx

@@ -0,0 +1,287 @@
+// Constants
+#define FRESNEL_MAXIMUM_ON_ROUGH 0.25
+
+// ______________________________________________________________________
+//
+//                              BRDF LOOKUP
+// ______________________________________________________________________
+
+#ifdef MS_BRDF_ENERGY_CONSERVATION
+    // http://www.jcgt.org/published/0008/01/03/
+    // http://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf
+    vec3 getEnergyConservationFactor(const vec3 specularEnvironmentR0, vec2 environmentBrdf) {
+        return 1.0 + specularEnvironmentR0 * (1.0 / environmentBrdf.y - 1.0);
+    }
+#endif
+
+#ifdef ENVIRONMENTBRDF
+    vec2 getBRDFLookup(float NdotV, float perceptualRoughness, sampler2D brdfSampler) {
+        // Indexed on cos(theta) and roughness
+        vec2 UV = vec2(NdotV, perceptualRoughness);
+        
+        // We can find the scale and offset to apply to the specular value.
+        vec2 brdfLookup = texture2D(brdfSampler, UV).xy;
+
+        return brdfLookup;
+    }
+
+    vec3 getReflectanceFromBRDFLookup(const vec3 specularEnvironmentR0, vec2 environmentBrdf) {
+        #ifdef BRDF_V_HEIGHT_CORRELATED
+            vec3 reflectance = mix(environmentBrdf.xxx, environmentBrdf.yyy, specularEnvironmentR0);
+        #else
+            vec3 reflectance = specularEnvironmentR0 * environmentBrdf.x + environmentBrdf.y;
+        #endif
+        return reflectance;
+    }
+#else
+    vec3 getReflectanceFromAnalyticalBRDFLookup_Jones(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
+    {
+        // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
+        float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
+        return reflectance0 + weight * (reflectance90 - reflectance0) * pow5(saturate(1.0 - VdotN));
+    }
+#endif
+
+#if defined(SHEEN) && defined(REFLECTION)
+    /**
+    * Special thanks to @romainguy for all the support :-)
+    * Analytical approximation of the pre-filtered DFG terms for the cloth shading
+    * model. This approximation is based on the Estevez & Kulla distribution term
+    * ("Charlie" sheen) and the Neubelt visibility term. See brdf.fs for more
+    * details.
+    */
+    vec2 getCharlieSheenAnalyticalBRDFLookup_RomainGuy(float NoV, float roughness) {
+        const vec3 c0 = vec3(0.95, 1250.0, 0.0095);
+        const vec4 c1 = vec4(0.04, 0.2, 0.3, 0.2);
+
+        float a = 1.0 - NoV;
+        float b = 1.0 - roughness;
+
+        float n = pow(c1.x + a, 64.0);
+        float e = b - c0.x;
+        float g = exp2(-(e * e) * c0.y);
+        float f = b + c1.y;
+        float a2 = a * a;
+        float a3 = a2 * a;
+        float c = n * g + c1.z * (a + c1.w) * roughness + f * f * a3 * a3 * a2;
+        float r = min(c, 18.0);
+
+        return vec2(r, r * c0.z);
+    }
+
+    vec3 getSheenReflectanceFromBRDFLookup(const vec3 reflectance0, float NdotV, float sheenAlphaG) {
+        vec2 environmentSheenBrdf = getCharlieSheenAnalyticalBRDFLookup_RomainGuy(NdotV, sheenAlphaG);
+        vec3 reflectance = reflectance0 * environmentSheenBrdf.x + environmentSheenBrdf.y;
+
+        return reflectance;
+    }
+#endif
+
+// ______________________________________________________________________
+//
+//                              Schlick/Fresnel
+// ______________________________________________________________________
+
+// Schlick's approximation for R0 (Fresnel Reflectance Values)
+// Keep for references
+// vec3 getR0fromAirToSurfaceIOR(vec3 ior1) {
+//     return getR0fromIOR(ior1, vec3(1.0));
+// }
+
+// vec3 getR0fromIOR(vec3 ior1, vec3 ior2) {
+//     vec3 t = (ior1 - ior2) / (ior1 + ior2);
+//     return t * t;
+// }
+
+// vec3 getIORfromAirToSurfaceR0(vec3 f0) {
+//     vec3 s = sqrt(f0);
+//     return (1.0 + s) / (1.0 - s);
+// }
+
+// f0 Remapping due to layers
+// vec3 getR0RemappedForClearCoat(vec3 f0, vec3 clearCoatF0) {
+//     vec3 iorBase = getIORfromAirToSurfaceR0(f0);
+//     vec3 clearCoatIor = getIORfromAirToSurfaceR0(clearCoatF0);
+//     return getR0fromIOR(iorBase, clearCoatIor);
+// }
+
+vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
+{
+    return reflectance0 + (reflectance90 - reflectance0) * pow5(1.0 - VdotH);
+}
+
+float fresnelSchlickGGX(float VdotH, float reflectance0, float reflectance90)
+{
+    return reflectance0 + (reflectance90 - reflectance0) * pow5(1.0 - VdotH);
+}
+
+#ifdef CLEARCOAT
+    // Knowing ior clear coat is fix for the material
+    // Solving iorbase = 1 + sqrt(fo) / (1 - sqrt(fo)) and f0base = square((iorbase - iorclearcoat) / (iorbase - iorclearcoat))
+    // provide f0base = square(A + B * sqrt(fo)) / (B + A * sqrt(fo))
+    // where A = 1 - iorclearcoat
+    // and   B = 1 + iorclearcoat
+    vec3 getR0RemappedForClearCoat(vec3 f0) {
+        #ifdef CLEARCOAT_DEFAULTIOR
+            #ifdef MOBILE
+                return saturate(f0 * (f0 * 0.526868 + 0.529324) - 0.0482256);
+            #else
+                return saturate(f0 * (f0 * (0.941892 - 0.263008 * f0) + 0.346479) - 0.0285998);
+            #endif
+        #else
+            vec3 s = sqrt(f0);
+            vec3 t = (vClearCoatRefractionParams.z + vClearCoatRefractionParams.w * s) / (vClearCoatRefractionParams.w + vClearCoatRefractionParams.z * s);
+            return t * t;
+        #endif
+    }
+#endif
+
+// ______________________________________________________________________
+//
+//                              Distribution
+// ______________________________________________________________________
+
+// Trowbridge-Reitz (GGX)
+// Generalised Trowbridge-Reitz with gamma power=2.0
+float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
+{
+    // Note: alphaG is average slope (gradient) of the normals in slope-space.
+    // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
+    // a tangent (gradient) closer to the macrosurface than this slope.
+    float a2 = square(alphaG);
+    float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
+    return a2 / (PI * d * d);
+}
+
+#ifdef SHEEN
+    // https://knarkowicz.wordpress.com/2018/01/04/cloth-shading/
+    float normalDistributionFunction_CharlieSheen(float NdotH, float alphaG)
+    {
+        float invR = 1. / alphaG;
+        float cos2h = NdotH * NdotH;
+        float sin2h = 1. - cos2h;
+        return (2. + invR) * pow(sin2h, invR * .5) / (2. * PI);
+    }
+#endif
+
+#ifdef ANISOTROPIC
+    // GGX Distribution Anisotropic
+    // https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf Addenda
+    float normalDistributionFunction_BurleyGGX_Anisotropic(float NdotH, float TdotH, float BdotH, const vec2 alphaTB) {
+        float a2 = alphaTB.x * alphaTB.y;
+        vec3 v = vec3(alphaTB.y * TdotH, alphaTB.x  * BdotH, a2 * NdotH);
+        float v2 = dot(v, v);
+        float w2 = a2 / v2;
+        return a2 * w2 * w2 * RECIPROCAL_PI;
+    }
+#endif
+
+// ______________________________________________________________________
+//
+//                              Visibility/Geometry
+// ______________________________________________________________________
+
+#ifdef BRDF_V_HEIGHT_CORRELATED
+    // GGX Mask/Shadowing Isotropic 
+    // Heitz http://jcgt.org/published/0003/02/03/paper.pdf
+    // https://twvideo01.ubm-us.net/o1/vault/gdc2017/Presentations/Hammon_Earl_PBR_Diffuse_Lighting.pdf
+    float smithVisibility_GGXCorrelated(float NdotL, float NdotV, float alphaG) {
+        #ifdef MOBILE
+            // Appply simplification as all squared root terms are below 1 and squared
+            float GGXV = NdotL * (NdotV * (1.0 - alphaG) + alphaG);
+            float GGXL = NdotV * (NdotL * (1.0 - alphaG) + alphaG);
+            return 0.5 / (GGXV + GGXL);
+        #else
+            float a2 = alphaG * alphaG;
+            float GGXV = NdotL * sqrt(NdotV * (NdotV - a2 * NdotV) + a2);
+            float GGXL = NdotV * sqrt(NdotL * (NdotL - a2 * NdotL) + a2);
+            return 0.5 / (GGXV + GGXL);
+        #endif
+    }
+#else
+    // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
+    // Keep for references
+    // float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
+    // {
+    //     float tanSquared = (1.0 - dot * dot) / (dot * dot);
+    //     return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
+    // }
+
+    // float smithVisibility_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
+    // {
+    //     float visibility = smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
+    //     visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator  integrated in visibility to avoid issues when visibility function changes.
+    //     return visibility;
+    // }
+
+    // From smithVisibilityG1_TrowbridgeReitzGGX * dot / dot to cancel the cook
+    // torrance denominator :-)
+    float smithVisibilityG1_TrowbridgeReitzGGXFast(float dot, float alphaG)
+    {
+        #ifdef MOBILE
+            // Appply simplification as all squared root terms are below 1 and squared
+            return 1.0 / (dot + alphaG + (1.0 - alphaG) * dot ));
+        #else
+            float alphaSquared = alphaG * alphaG;
+            return 1.0 / (dot + sqrt(alphaSquared + (1.0 - alphaSquared) * dot * dot));
+        #endif
+    }
+
+    float smithVisibility_TrowbridgeReitzGGXFast(float NdotL, float NdotV, float alphaG)
+    {
+        float visibility = smithVisibilityG1_TrowbridgeReitzGGXFast(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGXFast(NdotV, alphaG);
+        // No Cook Torance Denominator as it is canceled out in the previous form
+        return visibility;
+    }
+#endif
+
+#ifdef ANISOTROPIC
+    // GGX Mask/Shadowing Anisotropic 
+    // Heitz http://jcgt.org/published/0003/02/03/paper.pdf
+    float smithVisibility_GGXCorrelated_Anisotropic(float NdotL, float NdotV, float TdotV, float BdotV, float TdotL, float BdotL, const vec2 alphaTB) {
+        float lambdaV = NdotL * length(vec3(alphaTB.x * TdotV, alphaTB.y * BdotV, NdotV));
+        float lambdaL = NdotV * length(vec3(alphaTB.x * TdotL, alphaTB.y * BdotL, NdotL));
+        float v = 0.5 / (lambdaV + lambdaL);
+        return v;
+    }
+#endif
+
+#ifdef CLEARCOAT
+    float visibility_Kelemen(float VdotH) {
+        // Simplified form integration the cook torrance denminator.
+        // Expanded is nl * nv / vh2 which factor with 1 / (4 * nl * nv)
+        // giving 1 / (4 * vh2))
+        return 0.25 / (VdotH * VdotH); 
+    }
+#endif
+
+#ifdef SHEEN
+    // https://knarkowicz.wordpress.com/2018/01/04/cloth-shading/
+    // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_sheen.pdf
+    // http://www.cs.utah.edu/~premoze/dbrdf/dBRDF.pdf
+    float visibility_Ashikhmin(float NdotL, float NdotV)
+    {
+        return 1. / (4. * (NdotL + NdotV - NdotL * NdotV));
+    }
+#endif
+
+// ______________________________________________________________________
+//
+//                              DiffuseBRDF
+// ______________________________________________________________________
+
+// Disney diffuse term
+// https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
+// Page 14
+float diffuseBRDF_Burley(float NdotL, float NdotV, float VdotH, float roughness) {
+    // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
+    // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
+    float diffuseFresnelNV = pow5(saturateEps(1.0 - NdotL));
+    float diffuseFresnelNL = pow5(saturateEps(1.0 - NdotV));
+    float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
+    float fresnel =
+        (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
+        (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
+
+    return fresnel / PI;
+}

+ 5 - 6
src/Shaders/ShadersInclude/pbrFalloffLightingFunctions.fx

@@ -5,16 +5,15 @@ float computeDistanceLightFalloff_Standard(vec3 lightOffset, float range)
 
 float computeDistanceLightFalloff_Physical(float lightDistanceSquared)
 {
-    return 1.0 / ((lightDistanceSquared + 0.001));
+    return 1.0 / maxEps(lightDistanceSquared);
 }
 
 float computeDistanceLightFalloff_GLTF(float lightDistanceSquared, float inverseSquaredRange)
 {
-    const float minDistanceSquared = 0.01*0.01;
-    float lightDistanceFalloff = 1.0 / (max(lightDistanceSquared, minDistanceSquared));
+    float lightDistanceFalloff = 1.0 / maxEps(lightDistanceSquared);
 
     float factor = lightDistanceSquared * inverseSquaredRange;
-    float attenuation = clamp(1.0 - factor * factor, 0., 1.);
+    float attenuation = saturate(1.0 - factor * factor);
     attenuation *= attenuation;
 
     // Smooth attenuation of the falloff defined by the range.
@@ -38,7 +37,7 @@ float computeDirectionalLightFalloff_Standard(vec3 lightDirection, vec3 directio
 {
     float falloff = 0.0;
 
-    float cosAngle = max(0.000000000000001, dot(-lightDirection, directionToLightCenterW));
+    float cosAngle = maxEps(dot(-lightDirection, directionToLightCenterW));
     if (cosAngle >= cosHalfAngle)
     {
         falloff = max(0., pow(cosAngle, exponent));
@@ -72,7 +71,7 @@ float computeDirectionalLightFalloff_GLTF(vec3 lightDirection, vec3 directionToL
     // float lightAngleOffset = -cosOuter * angleScale;
 
     float cd = dot(-lightDirection, directionToLightCenterW);
-    float falloff = clamp(cd * lightAngleScale + lightAngleOffset, 0., 1.);
+    float falloff = saturate(cd * lightAngleScale + lightAngleOffset);
     // smooth the transition
     falloff *= falloff;
     return falloff;

+ 134 - 0
src/Shaders/ShadersInclude/pbrDirectLightingFunctions.fx

@@ -0,0 +1,134 @@
+#define CLEARCOATREFLECTANCE90 1.0
+
+// Light Results
+struct lightingInfo
+{
+    vec3 diffuse;
+    #ifdef SPECULARTERM
+        vec3 specular;
+    #endif
+    #ifdef CLEARCOAT
+        // xyz contains the clearcoat color.
+        // w contains the 1 - clearcoat fresnel to ease the energy conservation computation.
+        vec4 clearCoat;
+    #endif
+    #ifdef SHEEN
+        vec3 sheen;
+    #endif
+};
+
+// Simulate area (small) lights by increasing roughness
+float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance) {
+    #if defined(USEPHYSICALLIGHTFALLOFF) || defined(USEGLTFLIGHTFALLOFF)
+        // At small angle this approximation works. 
+        float lightRoughness = lightRadius / lightDistance;
+        // Distribution can sum.
+        float totalRoughness = saturate(lightRoughness + roughness);
+        return totalRoughness;
+    #else
+        return roughness;
+    #endif
+}
+
+vec3 computeHemisphericDiffuseLighting(preLightingInfo info, vec3 lightColor, vec3 groundColor) {
+    return mix(groundColor, lightColor, info.NdotL);
+}
+
+vec3 computeDiffuseLighting(preLightingInfo info, vec3 lightColor) {
+    float diffuseTerm = diffuseBRDF_Burley(info.NdotL, info.NdotV, info.VdotH, info.roughness);
+    return diffuseTerm * info.attenuation * info.NdotL * lightColor;
+}
+
+vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler, mat4 textureProjectionMatrix){
+    vec4 strq = textureProjectionMatrix * vec4(vPositionW, 1.0);
+    strq /= strq.w;
+    vec3 textureColor = texture2D(projectionLightSampler, strq.xy).rgb;
+    return toLinearSpace(textureColor);
+}
+
+#ifdef SPECULARTERM
+    vec3 computeSpecularLighting(preLightingInfo info, vec3 N, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor, vec3 lightColor) {
+        float NdotH = saturateEps(dot(N, info.H));
+        float roughness = max(info.roughness, geometricRoughnessFactor);
+        float alphaG = convertRoughnessToAverageSlope(roughness);
+
+        vec3 fresnel = fresnelSchlickGGX(info.VdotH, reflectance0, reflectance90);
+        float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
+
+        #ifdef BRDF_V_HEIGHT_CORRELATED
+            float visibility = smithVisibility_GGXCorrelated(info.NdotL, info.NdotV, alphaG);
+        #else
+            float visibility = smithVisibility_TrowbridgeReitzGGXFast(info.NdotL, info.NdotV, alphaG);
+        #endif
+
+        vec3 specTerm = fresnel * distribution * visibility;
+        return specTerm * info.attenuation * info.NdotL * lightColor;
+    }
+#endif
+
+#ifdef ANISOTROPIC
+    vec3 computeAnisotropicSpecularLighting(preLightingInfo info, vec3 V, vec3 N, vec3 T, vec3 B, float anisotropy, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor, vec3 lightColor) {
+        float NdotH = saturateEps(dot(N, info.H));
+        float TdotH = dot(T, info.H);
+        float BdotH = dot(B, info.H);
+        float TdotV = dot(T, V);
+        float BdotV = dot(B, V);
+        float TdotL = dot(T, info.L);
+        float BdotL = dot(B, info.L);
+        float alphaG = convertRoughnessToAverageSlope(info.roughness);
+        vec2 alphaTB = getAnisotropicRoughness(alphaG, anisotropy);
+        alphaTB = max(alphaTB, square(geometricRoughnessFactor));
+
+        vec3 fresnel = fresnelSchlickGGX(info.VdotH, reflectance0, reflectance90);
+        float distribution = normalDistributionFunction_BurleyGGX_Anisotropic(NdotH, TdotH, BdotH, alphaTB);
+        float visibility = smithVisibility_GGXCorrelated_Anisotropic(info.NdotL, info.NdotV, TdotV, BdotV, TdotL, BdotL, alphaTB);
+
+        vec3 specTerm = fresnel * distribution * visibility;
+        return specTerm * info.attenuation * info.NdotL * lightColor;
+    }
+#endif
+
+#ifdef CLEARCOAT
+    vec4 computeClearCoatLighting(preLightingInfo info, vec3 Ncc, float geometricRoughnessFactor, float clearCoatIntensity, vec3 lightColor) {
+        float NccdotL = saturateEps(dot(Ncc, info.L));
+        float NccdotH = saturateEps(dot(Ncc, info.H));
+        float clearCoatRoughness = max(info.roughness, geometricRoughnessFactor);
+        float alphaG = convertRoughnessToAverageSlope(clearCoatRoughness);
+
+        float fresnel = fresnelSchlickGGX(info.VdotH, vClearCoatRefractionParams.x, CLEARCOATREFLECTANCE90);
+        fresnel *= clearCoatIntensity;
+        float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NccdotH, alphaG);
+        float visibility = visibility_Kelemen(info.VdotH);
+
+        float clearCoatTerm = fresnel * distribution * visibility;
+
+        return vec4(
+            clearCoatTerm * info.attenuation * NccdotL * lightColor,
+            1.0 - fresnel
+        );
+    }
+
+    vec3 computeClearCoatLightingAbsorption(float NdotVRefract, vec3 L, vec3 Ncc, vec3 clearCoatColor, float clearCoatThickness, float clearCoatIntensity) {
+        vec3 LRefract = -refract(L, Ncc, vClearCoatRefractionParams.y);
+        float NdotLRefract = saturateEps(dot(Ncc, LRefract));
+
+        vec3 absorption = computeClearCoatAbsorption(NdotVRefract, NdotLRefract, clearCoatColor, clearCoatThickness, clearCoatIntensity);
+        return absorption;
+    }
+#endif
+
+#ifdef SHEEN
+    vec3 computeSheenLighting(preLightingInfo info, vec3 N, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor, vec3 lightColor) {
+        float NdotH = saturateEps(dot(N, info.H));
+        float roughness = max(info.roughness, geometricRoughnessFactor);
+        float alphaG = convertRoughnessToAverageSlope(roughness);
+
+        // No Fresnel with sheen
+        // vec3 fresnel = fresnelSchlickGGX(info.VdotH, reflectance0, reflectance90);
+        float distribution = normalDistributionFunction_CharlieSheen(NdotH, alphaG);
+        float visibility = visibility_Ashikhmin(info.NdotL, info.NdotV);
+
+        float sheenTerm = distribution * visibility;
+        return sheenTerm * info.attenuation * info.NdotL * lightColor;
+    }
+#endif

+ 6 - 6
src/Shaders/ShadersInclude/pbrPreLightingFunctions.fx

@@ -31,8 +31,8 @@ preLightingInfo computePointAndSpotPreLightingInfo(vec4 lightData, vec3 V, vec3
     // Geometry Data.
     result.L = normalize(result.lightOffset);
     result.H = normalize(V + result.L);
-    result.NdotL = clamp(dot(N, result.L), 0.000000000001, 1.0);
-    result.VdotH = clamp(dot(V, result.H), 0.0, 1.0);
+    result.NdotL = saturateEps(dot(N, result.L));
+    result.VdotH = saturate(dot(V, result.H));
 
     return result;
 }
@@ -46,8 +46,8 @@ preLightingInfo computeDirectionalPreLightingInfo(vec4 lightData, vec3 V, vec3 N
     // Geometry Data.
     result.L = normalize(-lightData.xyz);
     result.H = normalize(V + result.L);
-    result.NdotL = clamp(dot(N, result.L), 0.00000000001, 1.0);
-    result.VdotH = clamp(dot(V, result.H), 0.0, 1.0);
+    result.NdotL = saturateEps(dot(N, result.L));
+    result.VdotH = saturate(dot(V, result.H));
 
     return result;
 }
@@ -57,12 +57,12 @@ preLightingInfo computeHemisphericPreLightingInfo(vec4 lightData, vec3 V, vec3 N
 
     // Geometry Data.
     result.NdotL = dot(N, lightData.xyz) * 0.5 + 0.5;
-    result.NdotL = clamp(result.NdotL, 0.000000000001, 1.0);
+    result.NdotL = saturateEps(result.NdotL);
 
     #ifdef SPECULARTERM
         result.L = normalize(lightData.xyz);
         result.H = normalize(V + result.L);
-        result.VdotH = clamp(dot(V, result.H), 0.0, 1.0);
+        result.VdotH = saturate(dot(V, result.H));
     #endif
 
     return result;

+ 30 - 0
src/Shaders/ShadersInclude/pbrFragmentExtraDeclaration.fx

@@ -0,0 +1,30 @@
+uniform vec4 vEyePosition;
+uniform vec3 vAmbientColor;
+uniform vec4 vCameraInfos;
+
+// Input
+varying vec3 vPositionW;
+
+#if DEBUGMODE > 0
+    uniform vec2 vDebugMode;
+    varying vec4 vClipSpacePosition;
+#endif
+
+#ifdef MAINUV1
+    varying vec2 vMainUV1;
+#endif 
+
+#ifdef MAINUV2 
+    varying vec2 vMainUV2;
+#endif 
+
+#ifdef NORMAL
+    varying vec3 vNormalW;
+    #if defined(USESPHERICALFROMREFLECTIONMAP) && defined(USESPHERICALINVERTEX)
+        varying vec3 vEnvironmentIrradiance;
+    #endif
+#endif
+
+#ifdef VERTEXCOLOR
+    varying vec4 vColor;
+#endif

+ 203 - 0
src/Shaders/ShadersInclude/pbrFragmentSamplersDeclaration.fx

@@ -0,0 +1,203 @@
+#ifdef ALBEDO
+    #if ALBEDODIRECTUV == 1
+        #define vAlbedoUV vMainUV1
+    #elif ALBEDODIRECTUV == 2
+        #define vAlbedoUV vMainUV2
+    #else
+        varying vec2 vAlbedoUV;
+    #endif
+    uniform sampler2D albedoSampler;
+#endif
+
+#ifdef AMBIENT
+    #if AMBIENTDIRECTUV == 1
+        #define vAmbientUV vMainUV1
+    #elif AMBIENTDIRECTUV == 2
+        #define vAmbientUV vMainUV2
+    #else
+        varying vec2 vAmbientUV;
+    #endif
+    uniform sampler2D ambientSampler;
+#endif
+
+#ifdef OPACITY
+    #if OPACITYDIRECTUV == 1
+        #define vOpacityUV vMainUV1
+    #elif OPACITYDIRECTUV == 2
+        #define vOpacityUV vMainUV2
+    #else
+        varying vec2 vOpacityUV;
+    #endif
+    uniform sampler2D opacitySampler;
+#endif
+
+#ifdef EMISSIVE
+    #if EMISSIVEDIRECTUV == 1
+        #define vEmissiveUV vMainUV1
+    #elif EMISSIVEDIRECTUV == 2
+        #define vEmissiveUV vMainUV2
+    #else
+        varying vec2 vEmissiveUV;
+    #endif
+    uniform sampler2D emissiveSampler;
+#endif
+
+#ifdef LIGHTMAP
+    #if LIGHTMAPDIRECTUV == 1
+        #define vLightmapUV vMainUV1
+    #elif LIGHTMAPDIRECTUV == 2
+        #define vLightmapUV vMainUV2
+    #else
+        varying vec2 vLightmapUV;
+    #endif
+    uniform sampler2D lightmapSampler;
+#endif
+
+#ifdef REFLECTIVITY
+    #if REFLECTIVITYDIRECTUV == 1
+        #define vReflectivityUV vMainUV1
+    #elif REFLECTIVITYDIRECTUV == 2
+        #define vReflectivityUV vMainUV2
+    #else
+        varying vec2 vReflectivityUV;
+    #endif
+    uniform sampler2D reflectivitySampler;
+#endif
+
+#ifdef MICROSURFACEMAP
+    #if MICROSURFACEMAPDIRECTUV == 1
+        #define vMicroSurfaceSamplerUV vMainUV1
+    #elif MICROSURFACEMAPDIRECTUV == 2
+        #define vMicroSurfaceSamplerUV vMainUV2
+    #else
+        varying vec2 vMicroSurfaceSamplerUV;
+    #endif
+    uniform sampler2D microSurfaceSampler;
+#endif
+
+#ifdef CLEARCOAT
+    #ifdef CLEARCOAT_TEXTURE
+        #if CLEARCOAT_TEXTUREDIRECTUV == 1
+            #define vClearCoatUV vMainUV1
+        #elif CLEARCOAT_TEXTUREDIRECTUV == 2
+            #define vClearCoatUV vMainUV2
+        #else
+            varying vec2 vClearCoatUV;
+        #endif
+        uniform sampler2D clearCoatSampler;
+    #endif
+
+    #ifdef CLEARCOAT_BUMP
+        #if CLEARCOAT_BUMPDIRECTUV == 1
+            #define vClearCoatBumpUV vMainUV1
+        #elif CLEARCOAT_BUMPDIRECTUV == 2
+            #define vClearCoatBumpUV vMainUV2
+        #else
+            varying vec2 vClearCoatBumpUV;
+        #endif
+        uniform sampler2D clearCoatBumpSampler;
+    #endif
+
+    #ifdef CLEARCOAT_TINT_TEXTURE
+        #if CLEARCOAT_TINT_TEXTUREDIRECTUV == 1
+            #define vClearCoatTintUV vMainUV1
+        #elif CLEARCOAT_TINT_TEXTUREDIRECTUV == 2
+            #define vClearCoatTintUV vMainUV2
+        #else
+            varying vec2 vClearCoatTintUV;
+        #endif
+        uniform sampler2D clearCoatTintSampler;
+    #endif
+#endif
+
+#ifdef SHEEN
+    #ifdef SHEEN_TEXTURE
+        #if SHEEN_TEXTUREDIRECTUV == 1
+            #define vSheenUV vMainUV1
+        #elif SHEEN_TEXTUREDIRECTUV == 2
+            #define vSheenUV vMainUV2
+        #else
+            varying vec2 vSheenUV;
+        #endif
+        uniform sampler2D sheenSampler;
+    #endif
+#endif
+
+#ifdef ANISOTROPIC
+    #ifdef ANISOTROPIC_TEXTURE
+        #if ANISOTROPIC_TEXTUREDIRECTUV == 1
+            #define vAnisotropyUV vMainUV1
+        #elif ANISOTROPIC_TEXTUREDIRECTUV == 2
+            #define vAnisotropyUV vMainUV2
+        #else
+            varying vec2 vAnisotropyUV;
+        #endif
+        uniform sampler2D anisotropySampler;
+    #endif
+#endif
+
+// Refraction
+#ifdef REFRACTION
+    #ifdef REFRACTIONMAP_3D
+        #define sampleRefraction(s, c) textureCube(s, c)
+        
+        uniform samplerCube refractionSampler;
+
+        #ifdef LODBASEDMICROSFURACE
+            #define sampleRefractionLod(s, c, l) textureCubeLodEXT(s, c, l)
+        #else
+            uniform samplerCube refractionSamplerLow;
+            uniform samplerCube refractionSamplerHigh;
+        #endif
+    #else
+        #define sampleRefraction(s, c) texture2D(s, c)
+        
+        uniform sampler2D refractionSampler;
+
+        #ifdef LODBASEDMICROSFURACE
+            #define sampleRefractionLod(s, c, l) texture2DLodEXT(s, c, l)
+        #else
+            uniform samplerCube refractionSamplerLow;
+            uniform samplerCube refractionSamplerHigh;
+        #endif
+    #endif
+#endif
+
+// Reflection
+#ifdef REFLECTION
+    #ifdef REFLECTIONMAP_3D
+        #define sampleReflection(s, c) textureCube(s, c)
+
+        uniform samplerCube reflectionSampler;
+        
+        #ifdef LODBASEDMICROSFURACE
+            #define sampleReflectionLod(s, c, l) textureCubeLodEXT(s, c, l)
+        #else
+            uniform samplerCube reflectionSamplerLow;
+            uniform samplerCube reflectionSamplerHigh;
+        #endif
+    #else
+        #define sampleReflection(s, c) texture2D(s, c)
+
+        uniform sampler2D reflectionSampler;
+
+        #ifdef LODBASEDMICROSFURACE
+            #define sampleReflectionLod(s, c, l) texture2DLodEXT(s, c, l)
+        #else
+            uniform samplerCube reflectionSamplerLow;
+            uniform samplerCube reflectionSamplerHigh;
+        #endif
+    #endif
+
+    #ifdef REFLECTIONMAP_SKYBOX
+        varying vec3 vPositionUVW;
+    #else
+        #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
+            varying vec3 vDirectionW;
+        #endif
+    #endif
+#endif
+
+#ifdef ENVIRONMENTBRDF
+    uniform sampler2D environmentBrdfSampler;
+#endif

+ 0 - 448
src/Shaders/ShadersInclude/pbrFunctions.fx

@@ -1,448 +0,0 @@
-// Constants
-#define RECIPROCAL_PI2 0.15915494
-#define RECIPROCAL_PI 0.31830988618
-#define FRESNEL_MAXIMUM_ON_ROUGH 0.25
-
-// AlphaG epsilon to avoid numerical issues
-#define MINIMUMVARIANCE 0.0005
-
-#define CLEARCOATREFLECTANCE90 1.0
-
-float convertRoughnessToAverageSlope(float roughness)
-{
-    // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues
-    return square(roughness) + MINIMUMVARIANCE;
-}
-
-vec2 getAARoughnessFactors(vec3 normalVector) {
-    #ifdef SPECULARAA
-        vec3 nDfdx = dFdx(normalVector.xyz);
-        vec3 nDfdy = dFdy(normalVector.xyz);
-        float slopeSquare = max(dot(nDfdx, nDfdx), dot(nDfdy, nDfdy));
-
-        // Vive analytical lights roughness factor.
-        float geometricRoughnessFactor = pow(clamp(slopeSquare , 0., 1.), 0.333);
-
-        // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
-        float geometricAlphaGFactor = sqrt(slopeSquare);
-        // BJS factor.
-        geometricAlphaGFactor *= 0.75;
-
-        return vec2(geometricRoughnessFactor, geometricAlphaGFactor);
-    #else
-        return vec2(0.);
-    #endif
-}
-
-
-#ifdef MS_BRDF_ENERGY_CONSERVATION
-    // http://www.jcgt.org/published/0008/01/03/
-    // http://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf
-    vec3 getEnergyConservationFactor(const vec3 specularEnvironmentR0, vec2 environmentBrdf) {
-        return 1.0 + specularEnvironmentR0 * (1.0 / environmentBrdf.y - 1.0);
-    }
-#endif
-
-vec2 getBRDFLookup(float NdotV, float perceptualRoughness, sampler2D brdfSampler) {
-    // Indexed on cos(theta) and roughness
-    vec2 UV = vec2(NdotV, perceptualRoughness);
-    
-    // We can find the scale and offset to apply to the specular value.
-    vec2 brdfLookup = texture2D(brdfSampler, UV).xy;
-
-    return brdfLookup;
-}
-
-/**
- * Special thanks to @romainguy for all the support :-)
- * Analytical approximation of the pre-filtered DFG terms for the cloth shading
- * model. This approximation is based on the Estevez & Kulla distribution term
- * ("Charlie" sheen) and the Neubelt visibility term. See brdf.fs for more
- * details.
- */
-vec2 getCharlieSheenAnalyticalBRDFLookup_RomainGuy(float NoV, float roughness) {
-    const vec3 c0 = vec3(0.95, 1250.0, 0.0095);
-    const vec4 c1 = vec4(0.04, 0.2, 0.3, 0.2);
-
-    float a = 1.0 - NoV;
-    float b = 1.0 - roughness;
-
-    float n = pow(c1.x + a, 64.0);
-    float e = b - c0.x;
-    float g = exp2(-(e * e) * c0.y);
-    float f = b + c1.y;
-    float a2 = a * a;
-    float a3 = a2 * a;
-    float c = n * g + c1.z * (a + c1.w) * roughness + f * f * a3 * a3 * a2;
-    float r = min(c, 18.0);
-
-    return vec2(r, r * c0.z);
-}
-
-vec3 getReflectanceFromBRDFLookup(const vec3 specularEnvironmentR0, vec2 environmentBrdf) {
-    #ifdef BRDF_V_HEIGHT_CORRELATED
-        vec3 reflectance = mix(environmentBrdf.xxx, environmentBrdf.yyy, specularEnvironmentR0);
-    #else
-        vec3 reflectance = specularEnvironmentR0 * environmentBrdf.x + environmentBrdf.y;
-    #endif
-    return reflectance;
-}
-
-vec3 getReflectanceFromAnalyticalBRDFLookup_Jones(float VdotN, vec3 reflectance0, vec3 reflectance90, float smoothness)
-{
-    // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
-    float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
-    return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
-}
-
-vec3 getSheenReflectanceFromBRDFLookup(const vec3 reflectance0, float NdotV, float sheenAlphaG) {
-    vec2 environmentSheenBrdf = getCharlieSheenAnalyticalBRDFLookup_RomainGuy(NdotV, sheenAlphaG);
-    vec3 reflectance = reflectance0 * environmentSheenBrdf.x + environmentSheenBrdf.y;
-
-    return reflectance;
-}
-
-// Schlick's approximation for R0 (Fresnel Reflectance Values)
-// Keep for references
-// vec3 getR0fromAirToSurfaceIOR(vec3 ior1) {
-//     return getR0fromIOR(ior1, vec3(1.0));
-// }
-
-// vec3 getR0fromIOR(vec3 ior1, vec3 ior2) {
-//     vec3 t = (ior1 - ior2) / (ior1 + ior2);
-//     return t * t;
-// }
-
-// vec3 getIORfromAirToSurfaceR0(vec3 f0) {
-//     vec3 s = sqrt(f0);
-//     return (1.0 + s) / (1.0 - s);
-// }
-
-// f0 Remapping due to layers
-// vec3 getR0RemappedForClearCoat(vec3 f0, vec3 clearCoatF0) {
-//     vec3 iorBase = getIORfromAirToSurfaceR0(f0);
-//     vec3 clearCoatIor = getIORfromAirToSurfaceR0(clearCoatF0);
-//     return getR0fromIOR(iorBase, clearCoatIor);
-// }
-
-#ifdef CLEARCOAT
-    // Knowing ior clear coat is fix for the material
-    // Solving iorbase = 1 + sqrt(fo) / (1 - sqrt(fo)) and f0base = square((iorbase - iorclearcoat) / (iorbase - iorclearcoat))
-    // provide f0base = square(A + B * sqrt(fo)) / (B + A * sqrt(fo))
-    // where A = 1 - iorclearcoat
-    // and   B = 1 + iorclearcoat
-    vec3 getR0RemappedForClearCoat(vec3 f0) {
-        #ifdef CLEARCOAT_DEFAULTIOR
-            #ifdef MOBILE
-                return clamp(f0 * (f0 * 0.526868 + 0.529324) - 0.0482256, 0., 1.);
-            #else
-                return clamp(f0 * (f0 * (0.941892 - 0.263008 * f0) + 0.346479) - 0.0285998, 0., 1.);
-            #endif
-        #else
-            vec3 s = sqrt(f0);
-            vec3 t = (vClearCoatRefractionParams.z + vClearCoatRefractionParams.w * s) / (vClearCoatRefractionParams.w + vClearCoatRefractionParams.z * s);
-            return t * t;
-        #endif
-    }
-#endif
-
-// From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007
-// Keep for references
-// float smithVisibilityG1_TrowbridgeReitzGGX(float dot, float alphaG)
-// {
-//     float tanSquared = (1.0 - dot * dot) / (dot * dot);
-//     return 2.0 / (1.0 + sqrt(1.0 + alphaG * alphaG * tanSquared));
-// }
-
-// float smithVisibility_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG)
-// {
-//     float visibility = smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG);
-//     visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator  integrated in visibility to avoid issues when visibility function changes.
-//     return visibility;
-// }
-
-// From smithVisibilityG1_TrowbridgeReitzGGX * dot / dot to cancel the cook
-// torrance denominator :-)
-float smithVisibilityG1_TrowbridgeReitzGGXFast(float dot, float alphaG)
-{
-    #ifdef MOBILE
-        // Appply simplification as all squared root terms are below 1 and squared
-        return 1.0 / (dot + alphaG + (1.0 - alphaG) * dot ));
-    #else
-        float alphaSquared = alphaG * alphaG;
-        return 1.0 / (dot + sqrt(alphaSquared + (1.0 - alphaSquared) * dot * dot));
-    #endif
-}
-
-float smithVisibility_TrowbridgeReitzGGXFast(float NdotL, float NdotV, float alphaG)
-{
-    float visibility = smithVisibilityG1_TrowbridgeReitzGGXFast(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGXFast(NdotV, alphaG);
-    // No Cook Torance Denominator as it is canceled out in the previous form
-    return visibility;
-}
-
-float visibility_Kelemen(float VdotH) {
-    // Simplified form integration the cook torrance denminator.
-    // Expanded is nl * nv / vh2 which factor with 1 / (4 * nl * nv)
-    // giving 1 / (4 * vh2))
-    return 0.25 / (VdotH * VdotH); 
-}
-
-// https://knarkowicz.wordpress.com/2018/01/04/cloth-shading/
-// https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_sheen.pdf
-// http://www.cs.utah.edu/~premoze/dbrdf/dBRDF.pdf
-float visibility_Ashikhmin(float NdotL, float NdotV)
-{
-    return 1. / (4. * (NdotL + NdotV - NdotL * NdotV));
-}
-
-float normalDistributionFunction_CharlieSheen(float NdotH, float alphaG)
-{
-    float invR = 1. / alphaG;
-    float cos2h = NdotH * NdotH;
-    float sin2h = 1. - cos2h;
-    return (2. + invR) * pow(sin2h, invR * .5) / (2. * PI);
-}
-
-// Trowbridge-Reitz (GGX)
-// Generalised Trowbridge-Reitz with gamma power=2.0
-float normalDistributionFunction_TrowbridgeReitzGGX(float NdotH, float alphaG)
-{
-    // Note: alphaG is average slope (gradient) of the normals in slope-space.
-    // It is also the (trigonometric) tangent of the median distribution value, i.e. 50% of normals have
-    // a tangent (gradient) closer to the macrosurface than this slope.
-    float a2 = square(alphaG);
-    float d = NdotH * NdotH * (a2 - 1.0) + 1.0;
-    return a2 / (PI * d * d);
-}
-
-// Aniso parameter remapping
-// https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf page 24
-vec2 getAnisotropicRoughness(float alphaG, float anisotropy) {
-    float alphaT = max(alphaG * (1.0 + anisotropy), MINIMUMVARIANCE);
-    float alphaB = max(alphaG * (1.0 - anisotropy), MINIMUMVARIANCE);
-    return vec2(alphaT, alphaB);
-}
-
-// Aniso Bent Normals
-// Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far 
-vec3 getAnisotropicBentNormals(const vec3 T, const vec3 B, const vec3 N, const vec3 V, float anisotropy) {
-    vec3 anisotropicFrameDirection = anisotropy >= 0.0 ? B : T;
-    vec3 anisotropicFrameTangent = cross(normalize(anisotropicFrameDirection), V);
-    vec3 anisotropicFrameNormal = cross(anisotropicFrameTangent, anisotropicFrameDirection);
-    vec3 anisotropicNormal = normalize(mix(N, anisotropicFrameNormal, abs(anisotropy)));
-    return anisotropicNormal;
-
-    // should we also do http://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf page 80 ?
-}
-
-// GGX Distribution Anisotropic
-// https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf Addenda
-float normalDistributionFunction_BurleyGGX_Anisotropic(float NdotH, float TdotH, float BdotH, const vec2 alphaTB) {
-    float a2 = alphaTB.x * alphaTB.y;
-    vec3 v = vec3(alphaTB.y * TdotH, alphaTB.x  * BdotH, a2 * NdotH);
-    float v2 = dot(v, v);
-    float w2 = a2 / v2;
-    return a2 * w2 * w2 * RECIPROCAL_PI;
-}
-
-// GGX Mask/Shadowing Isotropic 
-// Heitz http://jcgt.org/published/0003/02/03/paper.pdf
-// https://twvideo01.ubm-us.net/o1/vault/gdc2017/Presentations/Hammon_Earl_PBR_Diffuse_Lighting.pdf
-float smithVisibility_GGXCorrelated(float NdotV, float NdotL, float alphaG) {
-    #ifdef MOBILE
-        // Appply simplification as all squared root terms are below 1 and squared
-        float GGXV = NdotL * (NdotV * (1.0 - alphaG) + alphaG);
-        float GGXL = NdotV * (NdotL * (1.0 - alphaG) + alphaG);
-        return 0.5 / (GGXV + GGXL);
-    #else
-        float a2 = alphaG * alphaG;
-        float GGXV = NdotL * sqrt(NdotV * NdotV * (1.0 - a2) + a2);
-        float GGXL = NdotV * sqrt(NdotL * NdotL * (1.0 - a2) + a2);
-        return 0.5 / (GGXV + GGXL);
-    #endif
-}
-
-// GGX Mask/Shadowing Anisotropic 
-// Heitz http://jcgt.org/published/0003/02/03/paper.pdf
-float smithVisibility_GGXCorrelated_Anisotropic(float NdotV, float NdotL, float TdotV, float BdotV, float TdotL, float BdotL, const vec2 alphaTB) {
-    float lambdaV = NdotL * length(vec3(alphaTB.x * TdotV, alphaTB.y * BdotV, NdotV));
-    float lambdaL = NdotV * length(vec3(alphaTB.x * TdotL, alphaTB.y * BdotL, NdotL));
-    float v = 0.5 / (lambdaV + lambdaL);
-    return v;
-}
-
-vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90)
-{
-    return reflectance0 + (reflectance90 - reflectance0) * pow(1.0 - VdotH, 5.0);
-}
-
-float fresnelSchlickGGX(float VdotH, float reflectance0, float reflectance90)
-{
-    return reflectance0 + (reflectance90 - reflectance0) * pow(1.0 - VdotH, 5.0);
-}
-
-// From beer lambert law I1/I0 = e −α′lc
-// c is considered included in alpha
-// https://blog.selfshadow.com/publications/s2017-shading-course/drobot/s2017_pbs_multilayered.pdf page 47
-// where L on a thin constant size layer can be (d * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))
-vec3 cocaLambert(float NdotVRefract, float NdotLRefract, vec3 alpha, float thickness) {
-    return exp(alpha * -(thickness * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))));
-}
-// From beerLambert Solves what alpha should be for a given resutlt at a known distance.
-vec3 computeColorAtDistanceInMedia(vec3 color, float distance) {
-    return -log(color) / distance;
-}
-
-// Disney diffuse term
-// https://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
-// Page 14
-float computeDiffuseTerm(float NdotL, float NdotV, float VdotH, float roughness) {
-    // Diffuse fresnel falloff as per Disney principled BRDF, and in the spirit of
-    // of general coupled diffuse/specular models e.g. Ashikhmin Shirley.
-    float diffuseFresnelNV = pow(clamp(1.0 - NdotL, 0.000001, 1.), 5.0);
-    float diffuseFresnelNL = pow(clamp(1.0 - NdotV, 0.000001, 1.), 5.0);
-    float diffuseFresnel90 = 0.5 + 2.0 * VdotH * VdotH * roughness;
-    float fresnel =
-        (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) *
-        (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV);
-
-    return fresnel / PI;
-}
-
-// Cook Torance Specular computation.
-vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor) {
-    roughness = max(roughness, geometricRoughnessFactor);
-    float alphaG = convertRoughnessToAverageSlope(roughness);
-
-    float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
-
-    #ifdef BRDF_V_HEIGHT_CORRELATED
-        float visibility = smithVisibility_GGXCorrelated(NdotL, NdotV, alphaG);
-    #else
-        float visibility = smithVisibility_TrowbridgeReitzGGXFast(NdotL, NdotV, alphaG);
-    #endif
-
-    float specTerm = max(0., visibility * distribution);
-
-    vec3 fresnel = fresnelSchlickGGX(VdotH, reflectance0, reflectance90);
-    return fresnel * specTerm;
-}
-
-#ifdef SHEEN
-    vec3 computeSheenTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor) {
-        roughness = max(roughness, geometricRoughnessFactor);
-        float alphaG = convertRoughnessToAverageSlope(roughness);
-
-        float distribution = normalDistributionFunction_CharlieSheen(NdotH, alphaG);
-        float visibility = visibility_Ashikhmin(NdotL, NdotV);
-
-        float specTerm = max(0., visibility * distribution);
-
-        vec3 fresnel = fresnelSchlickGGX(VdotH, reflectance0, reflectance90);
-        return vec3(specTerm);
-    }
-#endif
-
-#ifdef ANISOTROPIC
-    vec3 computeAnisotropicSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float TdotH, float BdotH, float TdotV, float BdotV, float TdotL, float BdotL, float roughness, float anisotropy, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor) {
-        float alphaG = convertRoughnessToAverageSlope(roughness);
-        vec2 alphaTB = getAnisotropicRoughness(alphaG, anisotropy);
-        alphaTB = max(alphaTB, geometricRoughnessFactor * geometricRoughnessFactor);
-
-        float distribution = normalDistributionFunction_BurleyGGX_Anisotropic(NdotH, TdotH, BdotH, alphaTB);
-        float visibility = smithVisibility_GGXCorrelated_Anisotropic(NdotV, NdotL, TdotV, BdotV, TdotL, BdotL, alphaTB);
-        float specTerm = max(0., visibility * distribution);
-
-        vec3 fresnel = fresnelSchlickGGX(VdotH, reflectance0, reflectance90);
-        return fresnel * specTerm;
-    }
-#endif
-
-#ifdef CLEARCOAT
-    vec2 computeClearCoatTerm(float NdotH, float VdotH, float clearCoatRoughness, float geometricRoughnessFactor, float clearCoatIntensity) {
-        clearCoatRoughness = max(clearCoatRoughness, geometricRoughnessFactor);
-        float alphaG = convertRoughnessToAverageSlope(clearCoatRoughness);
-
-        float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG);
-        float visibility = visibility_Kelemen(VdotH);
-        float clearCoatTerm = max(0., visibility * distribution);
-
-        float fresnel = fresnelSchlickGGX(VdotH, vClearCoatRefractionParams.x, CLEARCOATREFLECTANCE90);
-        fresnel *= clearCoatIntensity;
-        
-        return vec2(fresnel * clearCoatTerm, 1.0 - fresnel);
-    }
-
-    vec3 computeClearCoatAbsorption(float NdotVRefract, float NdotLRefract, vec3 clearCoatColor, float clearCoatThickness, float clearCoatIntensity) {
-        vec3 clearCoatAbsorption = mix(vec3(1.0),
-            cocaLambert(NdotVRefract, NdotLRefract, clearCoatColor, clearCoatThickness),
-            clearCoatIntensity);
-        return clearCoatAbsorption;
-    }
-#endif
-
-float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance)
-{
-    #if defined(USEPHYSICALLIGHTFALLOFF) || defined(USEGLTFLIGHTFALLOFF)
-        // At small angle this approximation works. 
-        float lightRoughness = lightRadius / lightDistance;
-        // Distribution can sum.
-        float totalRoughness = clamp(lightRoughness + roughness, 0., 1.);
-        return totalRoughness;
-    #else
-        return roughness;
-    #endif
-}
-
-float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
-{
-    const float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
-
-    float reflectivityLuminance = getLuminance(reflectivityColor);
-    float reflectivityLuma = sqrt(reflectivityLuminance);
-    microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
-
-    return microSurface;
-}
-
-// For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
-// For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
-float fresnelGrazingReflectance(float reflectance0) {
-    float reflectance90 = clamp(reflectance0 * 25.0, 0.0, 1.0);
-    return reflectance90;
-}
-
-// To enable 8 bit textures to be used we need to pack and unpack the LOD
-//inverse alpha is used to work around low-alpha bugs in Edge and Firefox
-#define UNPACK_LOD(x) (1.0 - x) * 255.0
-
-float getLodFromAlphaG(float cubeMapDimensionPixels, float alphaG, float NdotV) {
-    float microsurfaceAverageSlope = alphaG;
-
-    // Compensate for solid angle change between half-vector measure (Blinn-Phong) and reflected-vector measure (Phong):
-    //  dWr = 4*cos(theta)*dWh,
-    // where dWr = solid angle (delta omega) in environment incident radiance (reflection-vector) measure;
-    // where dWh = solid angle (delta omega) in microfacet normal (half-vector) measure;
-    // so the relationship is proportional to cosine theta = NdotV.
-    // The constant factor of four is handled elsewhere as part of the scale/offset filter parameters.
-    microsurfaceAverageSlope *= sqrt(abs(NdotV));
-
-    float microsurfaceAverageSlopeTexels = microsurfaceAverageSlope * cubeMapDimensionPixels;
-    float lod = log2(microsurfaceAverageSlopeTexels);
-    return lod;
-}
-
-float environmentRadianceOcclusion(float ambientOcclusion, float NdotVUnclamped) {
-    // Best balanced (implementation time vs result vs perf) analytical environment specular occlusion found.
-    // http://research.tri-ace.com/Data/cedec2011_RealtimePBR_Implementation_e.pptx
-    float temp = NdotVUnclamped + ambientOcclusion;
-    return clamp(square(temp) - 1.0 + ambientOcclusion, 0.0, 1.0);
-}
-
-float environmentHorizonOcclusion(vec3 view, vec3 normal) {
-    // http://marmosetco.tumblr.com/post/81245981087
-    vec3 reflection = reflect(view, normal);
-    float temp = clamp( 1.0 + 1.1 * dot(reflection, normal), 0.0, 1.0);
-    return square(temp);
-}

+ 101 - 0
src/Shaders/ShadersInclude/pbrHelperFunctions.fx

@@ -0,0 +1,101 @@
+// Constants
+#define RECIPROCAL_PI2 0.15915494
+#define RECIPROCAL_PI 0.31830988618
+
+// AlphaG epsilon to avoid numerical issues
+#define MINIMUMVARIANCE 0.0005
+
+float convertRoughnessToAverageSlope(float roughness)
+{
+    // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues
+    return square(roughness) + MINIMUMVARIANCE;
+}
+
+float fresnelGrazingReflectance(float reflectance0) {
+    // For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
+    // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
+    float reflectance90 = saturate(reflectance0 * 25.0);
+    return reflectance90;
+}
+
+vec2 getAARoughnessFactors(vec3 normalVector) {
+    #ifdef SPECULARAA
+        vec3 nDfdx = dFdx(normalVector.xyz);
+        vec3 nDfdy = dFdy(normalVector.xyz);
+        float slopeSquare = max(dot(nDfdx, nDfdx), dot(nDfdy, nDfdy));
+
+        // Vive analytical lights roughness factor.
+        float geometricRoughnessFactor = pow(saturate(slopeSquare), 0.333);
+
+        // Adapt linear roughness (alphaG) to geometric curvature of the current pixel.
+        float geometricAlphaGFactor = sqrt(slopeSquare);
+        // BJS factor.
+        geometricAlphaGFactor *= 0.75;
+
+        return vec2(geometricRoughnessFactor, geometricAlphaGFactor);
+    #else
+        return vec2(0.);
+    #endif
+}
+
+#ifdef ANISOTROPIC
+    // Aniso parameter remapping
+    // https://blog.selfshadow.com/publications/s2017-shading-course/imageworks/s2017_pbs_imageworks_slides_v2.pdf page 24
+    vec2 getAnisotropicRoughness(float alphaG, float anisotropy) {
+        float alphaT = max(alphaG * (1.0 + anisotropy), MINIMUMVARIANCE);
+        float alphaB = max(alphaG * (1.0 - anisotropy), MINIMUMVARIANCE);
+        return vec2(alphaT, alphaB);
+    }
+
+    // Aniso Bent Normals
+    // Mc Alley https://www.gdcvault.com/play/1022235/Rendering-the-World-of-Far 
+    vec3 getAnisotropicBentNormals(const vec3 T, const vec3 B, const vec3 N, const vec3 V, float anisotropy) {
+        vec3 anisotropicFrameDirection = anisotropy >= 0.0 ? B : T;
+        vec3 anisotropicFrameTangent = cross(normalize(anisotropicFrameDirection), V);
+        vec3 anisotropicFrameNormal = cross(anisotropicFrameTangent, anisotropicFrameDirection);
+        vec3 anisotropicNormal = normalize(mix(N, anisotropicFrameNormal, abs(anisotropy)));
+        return anisotropicNormal;
+
+        // should we also do http://advances.realtimerendering.com/s2018/Siggraph%202018%20HDRP%20talk_with%20notes.pdf page 80 ?
+    }
+#endif
+
+#ifdef CLEARCOAT
+    // From beer lambert law I1/I0 = e −α′lc
+    // c is considered included in alpha
+    // https://blog.selfshadow.com/publications/s2017-shading-course/drobot/s2017_pbs_multilayered.pdf page 47
+    // where L on a thin constant size layer can be (d * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))
+    vec3 cocaLambert(float NdotVRefract, float NdotLRefract, vec3 alpha, float thickness) {
+        return exp(alpha * -(thickness * ((NdotLRefract + NdotVRefract) / (NdotLRefract * NdotVRefract))));
+    }
+
+    // From beerLambert Solves what alpha should be for a given resutlt at a known distance.
+    vec3 computeColorAtDistanceInMedia(vec3 color, float distance) {
+        return -log(color) / distance;
+    }
+
+    vec3 computeClearCoatAbsorption(float NdotVRefract, float NdotLRefract, vec3 clearCoatColor, float clearCoatThickness, float clearCoatIntensity) {
+        vec3 clearCoatAbsorption = mix(vec3(1.0),
+            cocaLambert(NdotVRefract, NdotLRefract, clearCoatColor, clearCoatThickness),
+            clearCoatIntensity);
+        return clearCoatAbsorption;
+    }
+#endif
+
+// ___________________________________________________________________________________
+//
+// LEGACY
+// ___________________________________________________________________________________
+
+#ifdef MICROSURFACEAUTOMATIC
+    float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor)
+    {
+        const float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95;
+
+        float reflectivityLuminance = getLuminance(reflectivityColor);
+        float reflectivityLuma = sqrt(reflectivityLuminance);
+        microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax;
+
+        return microSurface;
+    }
+#endif

+ 50 - 0
src/Shaders/ShadersInclude/pbrIBLFunctions.fx

@@ -0,0 +1,50 @@
+#if defined(REFLECTION) || defined(REFRACTION)
+    float getLodFromAlphaG(float cubeMapDimensionPixels, float microsurfaceAverageSlope) {
+        float microsurfaceAverageSlopeTexels = microsurfaceAverageSlope * cubeMapDimensionPixels;
+        float lod = log2(microsurfaceAverageSlopeTexels);
+        return lod;
+    }
+#endif
+
+#if defined(ENVIRONMENTBRDF) && defined(RADIANCEOCCLUSION)
+    float environmentRadianceOcclusion(float ambientOcclusion, float NdotVUnclamped) {
+        // Best balanced (implementation time vs result vs perf) analytical environment specular occlusion found.
+        // http://research.tri-ace.com/Data/cedec2011_RealtimePBR_Implementation_e.pptx
+        float temp = NdotVUnclamped + ambientOcclusion;
+        return saturate(square(temp) - 1.0 + ambientOcclusion);
+    }
+#endif
+
+#if defined(ENVIRONMENTBRDF) && defined(HORIZONOCCLUSION)
+    float environmentHorizonOcclusion(vec3 view, vec3 normal) {
+        // http://marmosetco.tumblr.com/post/81245981087
+        vec3 reflection = reflect(view, normal);
+        float temp = saturate(1.0 + 1.1 * dot(reflection, normal));
+        return square(temp);
+    }
+#endif
+
+// ___________________________________________________________________________________
+//
+// LEGACY
+// ___________________________________________________________________________________
+
+#if defined(LODINREFLECTIONALPHA) || defined(LODINREFRACTIONALPHA)
+    // To enable 8 bit textures to be used we need to pack and unpack the LOD
+    //inverse alpha is used to work around low-alpha bugs in Edge and Firefox
+    #define UNPACK_LOD(x) (1.0 - x) * 255.0
+
+    float getLodFromAlphaG(float cubeMapDimensionPixels, float alphaG, float NdotV) {
+        float microsurfaceAverageSlope = alphaG;
+
+        // Compensate for solid angle change between half-vector measure (Blinn-Phong) and reflected-vector measure (Phong):
+        //  dWr = 4*cos(theta)*dWh,
+        // where dWr = solid angle (delta omega) in environment incident radiance (reflection-vector) measure;
+        // where dWh = solid angle (delta omega) in microfacet normal (half-vector) measure;
+        // so the relationship is proportional to cosine theta = NdotV.
+        // The constant factor of four is handled elsewhere as part of the scale/offset filter parameters.
+        microsurfaceAverageSlope *= sqrt(abs(NdotV));
+
+        return getLodFromAlphaG(cubeMapDimensionPixels, microsurfaceAverageSlope);
+    }
+#endif

+ 0 - 87
src/Shaders/ShadersInclude/pbrLightingFunctions.fx

@@ -1,87 +0,0 @@
-// Light Results
-struct lightingInfo
-{
-    vec3 diffuse;
-    #ifdef SPECULARTERM
-        vec3 specular;
-    #endif
-    #ifdef CLEARCOAT
-        // xyz contains the clearcoat color.
-        // w contains the 1 - clearcoat fresnel to ease the energy conservation computation.
-        vec4 clearCoat;
-    #endif
-    #ifdef SHEEN
-        vec3 sheen;
-    #endif
-};
-
-vec3 computeHemisphericDiffuseLighting(preLightingInfo info, vec3 lightColor, vec3 groundColor) {
-    return mix(groundColor, lightColor, info.NdotL);
-}
-
-vec3 computeDiffuseLighting(preLightingInfo info, vec3 lightColor) {
-    float diffuseTerm = computeDiffuseTerm(info.NdotL, info.NdotV, info.VdotH, info.roughness);
-    return diffuseTerm * info.attenuation * info.NdotL * lightColor;
-}
-
-vec3 computeSpecularLighting(preLightingInfo info, vec3 N, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor, vec3 lightColor) {
-    float NdotH = clamp(dot(N, info.H), 0.000000000001, 1.0);
-
-    vec3 specTerm = computeSpecularTerm(NdotH, info.NdotL, info.NdotV, info.VdotH, info.roughness, reflectance0, reflectance90, geometricRoughnessFactor);
-    return specTerm * info.attenuation * info.NdotL * lightColor;
-}
-
-#ifdef ANISOTROPIC
-    vec3 computeAnisotropicSpecularLighting(preLightingInfo info, vec3 V, vec3 N, vec3 T, vec3 B, float anisotropy, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor, vec3 lightColor) {
-        float NdotH = clamp(dot(N, info.H), 0.000000000001, 1.0);
-
-        float TdotH = dot(T, info.H);
-        float BdotH = dot(B, info.H);
-
-        float TdotV = dot(T, V);
-        float BdotV = dot(B, V);
-        float TdotL = dot(T, info.L);
-        float BdotL = dot(B, info.L);
-
-        vec3 specTerm = computeAnisotropicSpecularTerm(NdotH, info.NdotL, info.NdotV, info.VdotH, TdotH, BdotH, TdotV, BdotV, TdotL, BdotL, info.roughness, anisotropy, reflectance0, reflectance90, geometricRoughnessFactor);
-        return specTerm * info.attenuation * info.NdotL * lightColor;
-    }
-#endif
-
-#ifdef CLEARCOAT
-    vec4 computeClearCoatLighting(preLightingInfo info, vec3 Ncc, float geometricRoughnessFactor, float clearCoatIntensity, vec3 lightColor) {
-        float NccdotL = clamp(dot(Ncc, info.L), 0.00000000001, 1.0);
-        float NccdotH = clamp(dot(Ncc, info.H), 0.000000000001, 1.0);
-
-        vec2 clearCoatTerm = computeClearCoatTerm(NccdotH, info.VdotH, info.roughness, geometricRoughnessFactor, clearCoatIntensity);
-
-        vec4 result = vec4(0.);
-        result.rgb = clearCoatTerm.x * info.attenuation * NccdotL * lightColor;
-        result.a = clearCoatTerm.y;
-        return result;
-    }
-
-    vec3 computeClearCoatLightingAbsorption(float NdotVRefract, vec3 L, vec3 Ncc, vec3 clearCoatColor, float clearCoatThickness, float clearCoatIntensity) {
-        vec3 LRefract = -refract(L, Ncc, vClearCoatRefractionParams.y);
-        float NdotLRefract = clamp(dot(Ncc, LRefract), 0.00000000001, 1.0);
-
-        vec3 absorption = computeClearCoatAbsorption(NdotVRefract, NdotLRefract, clearCoatColor, clearCoatThickness, clearCoatIntensity);
-        return absorption;
-    }
-#endif
-
-#ifdef SHEEN
-    vec3 computeSheenLighting(preLightingInfo info, vec3 N, vec3 reflectance0, vec3 reflectance90, float geometricRoughnessFactor, vec3 lightColor) {
-        float NdotH = clamp(dot(N, info.H), 0.000000000001, 1.0);
-
-        vec3 specTerm = computeSheenTerm(NdotH, info.NdotL, info.NdotV, info.VdotH, info.roughness, reflectance0, reflectance90, geometricRoughnessFactor);
-        return specTerm * info.attenuation * info.NdotL * lightColor;
-    }
-#endif
-
-vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler, mat4 textureProjectionMatrix){
-	vec4 strq = textureProjectionMatrix * vec4(vPositionW, 1.0);
-	strq /= strq.w;
-	vec3 textureColor = texture2D(projectionLightSampler, strq.xy).rgb;
-	return toLinearSpace(textureColor);
-}

+ 6 - 0
src/Shaders/ShadersInclude/shadowsFragmentFunctions.fx

@@ -7,6 +7,12 @@
         }
     #endif
 
+    float computeFallOff(float value, vec2 clipSpace, float frustumEdgeFalloff)
+    {
+        float mask = smoothstep(1.0 - frustumEdgeFalloff, 1.0, clamp(dot(clipSpace, clipSpace), 0., 1.));
+        return mix(value, 1.0, mask);
+    }
+
     float computeShadowCube(vec3 lightPosition, samplerCube shadowSampler, float darkness, vec2 depthValues)
     {
         vec3 directionToLight = vPositionW - lightPosition;

+ 6 - 6
src/Shaders/background.fragment.fx

@@ -107,7 +107,7 @@ varying vec3 vNormalW;
     {
         // Schlick fresnel approximation, extended with basic smoothness term so that rough surfaces do not approach reflectance90 at grazing angle
         float weight = mix(FRESNEL_MAXIMUM_ON_ROUGH, 1.0, smoothness);
-        return reflectance0 + weight * (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotN, 0., 1.), 5.0);
+        return reflectance0 + weight * (reflectance90 - reflectance0) * pow5(saturate(1.0 - VdotN));
     }
 #endif
 
@@ -163,7 +163,7 @@ vec4 reflectionColor = vec4(1., 1., 1., 1.);
             reflectionLOD = reflectionLOD * log2(vReflectionMicrosurfaceInfos.x) * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
             reflectionColor = sampleReflectionLod(reflectionSampler, reflectionCoords, reflectionLOD);
         #else
-            float lodReflectionNormalized = clamp(reflectionLOD, 0., 1.);
+            float lodReflectionNormalized = saturate(reflectionLOD);
             float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
 
             vec4 reflectionSpecularMid = sampleReflection(reflectionSampler, reflectionCoords);
@@ -249,16 +249,16 @@ float finalAlpha = alpha;
     vec3 reflectionReflectance90 = vReflectionControl.zzz;
     float VdotN = dot(normalize(vEyePosition), normalW);
 
-    vec3 planarReflectionFresnel = fresnelSchlickEnvironmentGGX(clamp(VdotN, 0.0, 1.0), reflectionReflectance0, reflectionReflectance90, 1.0);
+    vec3 planarReflectionFresnel = fresnelSchlickEnvironmentGGX(saturate(VdotN), reflectionReflectance0, reflectionReflectance90, 1.0);
     reflectionAmount *= planarReflectionFresnel;
 
     #ifdef REFLECTIONFALLOFF
-        float reflectionDistanceFalloff = 1.0 - clamp(length(vPositionW.xyz - vBackgroundCenter) * vReflectionControl.w, 0.0, 1.0);
+        float reflectionDistanceFalloff = 1.0 - saturate(length(vPositionW.xyz - vBackgroundCenter) * vReflectionControl.w);
         reflectionDistanceFalloff *= reflectionDistanceFalloff;
         reflectionAmount *= reflectionDistanceFalloff;
     #endif
 
-    finalColor = mix(finalColor, reflectionColor.rgb, clamp(reflectionAmount, 0., 1.));
+    finalColor = mix(finalColor, reflectionColor.rgb, saturate(reflectionAmount));
 #endif
 
 #ifdef OPACITYFRESNEL
@@ -266,7 +266,7 @@ float finalAlpha = alpha;
 
     // Fade out the floor plane as the angle between the floor and the camera tends to 0 (starting from startAngle)
     const float startAngle = 0.1;
-    float fadeFactor = clamp(viewAngleToFloor/startAngle, 0.0, 1.0);
+    float fadeFactor = saturate(viewAngleToFloor/startAngle);
 
     finalAlpha *= fadeFactor * fadeFactor;
 #endif

+ 40 - 273
src/Shaders/pbr.fragment.fx

@@ -12,279 +12,43 @@
 
 precision highp float;
 
-#include<__decl__pbrFragment>
-
-uniform vec4 vEyePosition;
-uniform vec3 vAmbientColor;
-uniform vec4 vCameraInfos;
-
-// Input
-varying vec3 vPositionW;
-
-#if DEBUGMODE > 0
-    uniform vec2 vDebugMode;
-    varying vec4 vClipSpacePosition;
-#endif
-
-#ifdef MAINUV1
-    varying vec2 vMainUV1;
-#endif 
-
-#ifdef MAINUV2 
-    varying vec2 vMainUV2;
-#endif 
-
-#ifdef NORMAL
-    varying vec3 vNormalW;
-    #if defined(USESPHERICALFROMREFLECTIONMAP) && defined(USESPHERICALINVERTEX)
-        varying vec3 vEnvironmentIrradiance;
-    #endif
-#endif
-
-#ifdef VERTEXCOLOR
-varying vec4 vColor;
-#endif
-
-// Lights
-#include<__decl__lightFragment>[0..maxSimultaneousLights]
-
-// Samplers
-#ifdef ALBEDO
-    #if ALBEDODIRECTUV == 1
-        #define vAlbedoUV vMainUV1
-    #elif ALBEDODIRECTUV == 2
-        #define vAlbedoUV vMainUV2
-    #else
-        varying vec2 vAlbedoUV;
-    #endif
-    uniform sampler2D albedoSampler;
-#endif
-
-#ifdef AMBIENT
-    #if AMBIENTDIRECTUV == 1
-        #define vAmbientUV vMainUV1
-    #elif AMBIENTDIRECTUV == 2
-        #define vAmbientUV vMainUV2
-    #else
-        varying vec2 vAmbientUV;
-    #endif
-    uniform sampler2D ambientSampler;
-#endif
-
-#ifdef OPACITY
-    #if OPACITYDIRECTUV == 1
-        #define vOpacityUV vMainUV1
-    #elif OPACITYDIRECTUV == 2
-        #define vOpacityUV vMainUV2
-    #else
-        varying vec2 vOpacityUV;
-    #endif
-    uniform sampler2D opacitySampler;
-#endif
-
-#ifdef EMISSIVE
-    #if EMISSIVEDIRECTUV == 1
-        #define vEmissiveUV vMainUV1
-    #elif EMISSIVEDIRECTUV == 2
-        #define vEmissiveUV vMainUV2
-    #else
-        varying vec2 vEmissiveUV;
-    #endif
-    uniform sampler2D emissiveSampler;
-#endif
-
-#ifdef LIGHTMAP
-    #if LIGHTMAPDIRECTUV == 1
-        #define vLightmapUV vMainUV1
-    #elif LIGHTMAPDIRECTUV == 2
-        #define vLightmapUV vMainUV2
-    #else
-        varying vec2 vLightmapUV;
-    #endif
-    uniform sampler2D lightmapSampler;
-#endif
-
-#ifdef REFLECTIVITY
-    #if REFLECTIVITYDIRECTUV == 1
-        #define vReflectivityUV vMainUV1
-    #elif REFLECTIVITYDIRECTUV == 2
-        #define vReflectivityUV vMainUV2
-    #else
-        varying vec2 vReflectivityUV;
-    #endif
-    uniform sampler2D reflectivitySampler;
-#endif
-
-#ifdef MICROSURFACEMAP
-    #if MICROSURFACEMAPDIRECTUV == 1
-        #define vMicroSurfaceSamplerUV vMainUV1
-    #elif MICROSURFACEMAPDIRECTUV == 2
-        #define vMicroSurfaceSamplerUV vMainUV2
-    #else
-        varying vec2 vMicroSurfaceSamplerUV;
-    #endif
-    uniform sampler2D microSurfaceSampler;
-#endif
-
-#ifdef CLEARCOAT
-    #ifdef CLEARCOAT_TEXTURE
-        #if CLEARCOAT_TEXTUREDIRECTUV == 1
-            #define vClearCoatUV vMainUV1
-        #elif CLEARCOAT_TEXTUREDIRECTUV == 2
-            #define vClearCoatUV vMainUV2
-        #else
-            varying vec2 vClearCoatUV;
-        #endif
-        uniform sampler2D clearCoatSampler;
-    #endif
-
-    #ifdef CLEARCOAT_BUMP
-        #if CLEARCOAT_BUMPDIRECTUV == 1
-            #define vClearCoatBumpUV vMainUV1
-        #elif CLEARCOAT_BUMPDIRECTUV == 2
-            #define vClearCoatBumpUV vMainUV2
-        #else
-            varying vec2 vClearCoatBumpUV;
-        #endif
-        uniform sampler2D clearCoatBumpSampler;
-    #endif
-
-    #ifdef CLEARCOAT_TINT_TEXTURE
-        #if CLEARCOAT_TINT_TEXTUREDIRECTUV == 1
-            #define vClearCoatTintUV vMainUV1
-        #elif CLEARCOAT_TINT_TEXTUREDIRECTUV == 2
-            #define vClearCoatTintUV vMainUV2
-        #else
-            varying vec2 vClearCoatTintUV;
-        #endif
-        uniform sampler2D clearCoatTintSampler;
-    #endif
-#endif
-
-#ifdef SHEEN
-    #ifdef SHEEN_TEXTURE
-        #if SHEEN_TEXTUREDIRECTUV == 1
-            #define vSheenUV vMainUV1
-        #elif SHEEN_TEXTUREDIRECTUV == 2
-            #define vSheenUV vMainUV2
-        #else
-            varying vec2 vSheenUV;
-        #endif
-        uniform sampler2D sheenSampler;
-    #endif
-#endif
-
-#ifdef ANISOTROPIC
-    #ifdef ANISOTROPIC_TEXTURE
-        #if ANISOTROPIC_TEXTUREDIRECTUV == 1
-            #define vAnisotropyUV vMainUV1
-        #elif ANISOTROPIC_TEXTUREDIRECTUV == 2
-            #define vAnisotropyUV vMainUV2
-        #else
-            varying vec2 vAnisotropyUV;
-        #endif
-        uniform sampler2D anisotropySampler;
-    #endif
-#endif
-
-// Refraction
-#ifdef REFRACTION
-    #ifdef REFRACTIONMAP_3D
-        #define sampleRefraction(s, c) textureCube(s, c)
-        
-        uniform samplerCube refractionSampler;
-
-        #ifdef LODBASEDMICROSFURACE
-            #define sampleRefractionLod(s, c, l) textureCubeLodEXT(s, c, l)
-        #else
-            uniform samplerCube refractionSamplerLow;
-            uniform samplerCube refractionSamplerHigh;
-        #endif
-    #else
-        #define sampleRefraction(s, c) texture2D(s, c)
-        
-        uniform sampler2D refractionSampler;
-
-        #ifdef LODBASEDMICROSFURACE
-            #define sampleRefractionLod(s, c, l) texture2DLodEXT(s, c, l)
-        #else
-            uniform samplerCube refractionSamplerLow;
-            uniform samplerCube refractionSamplerHigh;
-        #endif
-    #endif
-#endif
-
-// Reflection
-#ifdef REFLECTION
-    #ifdef REFLECTIONMAP_3D
-        #define sampleReflection(s, c) textureCube(s, c)
-
-        uniform samplerCube reflectionSampler;
-        
-        #ifdef LODBASEDMICROSFURACE
-            #define sampleReflectionLod(s, c, l) textureCubeLodEXT(s, c, l)
-        #else
-            uniform samplerCube reflectionSamplerLow;
-            uniform samplerCube reflectionSamplerHigh;
-        #endif
-    #else
-        #define sampleReflection(s, c) texture2D(s, c)
-
-        uniform sampler2D reflectionSampler;
-
-        #ifdef LODBASEDMICROSFURACE
-            #define sampleReflectionLod(s, c, l) texture2DLodEXT(s, c, l)
-        #else
-            uniform samplerCube reflectionSamplerLow;
-            uniform samplerCube reflectionSamplerHigh;
-        #endif
-    #endif
-
-    #ifdef REFLECTIONMAP_SKYBOX
-        varying vec3 vPositionUVW;
-    #else
-        #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
-            varying vec3 vDirectionW;
-        #endif
-    #endif
-
-    #include<reflectionFunction>
-#endif
-
-#ifdef ENVIRONMENTBRDF
-    uniform sampler2D environmentBrdfSampler;
-#endif
-
 // Forces linear space for image processing
 #ifndef FROMLINEARSPACE
     #define FROMLINEARSPACE;
 #endif
 
+// Declaration
+#include<__decl__pbrFragment>
+#include<pbrFragmentExtraDeclaration>
+#include<__decl__lightFragment>[0..maxSimultaneousLights]
+#include<pbrFragmentSamplersDeclaration>
 #include<imageProcessingDeclaration>
+#include<clipPlaneFragmentDeclaration>
+#include<logDepthDeclaration>
+#include<fogFragmentDeclaration>
 
+// Helper Functions
 #include<helperFunctions>
-
+#include<pbrHelperFunctions>
 #include<imageProcessingFunctions>
-
-// PBR
 #include<shadowsFragmentFunctions>
-#include<pbrFunctions>
 #include<harmonicsFunctions>
-#include<pbrPreLightingFunctions>
-#include<pbrFalloffLightingFunctions>
-#include<pbrLightingFunctions>
-
+#include<pbrDirectLightingSetupFunctions>
+#include<pbrDirectLightingFalloffFunctions>
+#include<pbrBRDFFunctions>
+#include<pbrDirectLightingFunctions>
+#include<pbrIBLFunctions>
 #include<bumpFragmentFunctions>
-#include<clipPlaneFragmentDeclaration>
-#include<logDepthDeclaration>
 
-// Fog
-#include<fogFragmentDeclaration>
+#ifdef REFLECTION
+    #include<reflectionFunction>
+#endif
 
+// _____________________________ MAIN FUNCTION ____________________________
 void main(void) {
-#include<clipPlaneFragment>
 
-// _______________________________________________________________________________
+    #include<clipPlaneFragment>
+
 // _____________________________ Geometry Information ____________________________
     vec3 viewDirectionW = normalize(vEyePosition.xyz - vPositionW);
 
@@ -468,7 +232,7 @@ void main(void) {
     #endif
 
     // Adapt microSurface.
-    microSurface = clamp(microSurface, 0., 1.);
+    microSurface = saturate(microSurface);
     // Compute roughness.
     float roughness = 1. - microSurface;
 
@@ -490,7 +254,7 @@ void main(void) {
             vec3 normalForward = faceforward(normalW, -viewDirectionW, normalW);
 
             // Calculate the appropriate linear opacity for the current viewing angle (formally, this quantity is the "directional absorptance").
-            alpha = getReflectanceFromAnalyticalBRDFLookup_Jones(clamp(dot(viewDirectionW, normalForward), 0.0, 1.0), vec3(opacity0), vec3(opacity90), sqrt(microSurface)).x;
+            alpha = getReflectanceFromAnalyticalBRDFLookup_Jones(saturate(dot(viewDirectionW, normalForward)), vec3(opacity0), vec3(opacity90), sqrt(microSurface)).x;
 
             #ifdef ALPHATEST
                 if (alpha < ALPHATESTVALUE)
@@ -506,7 +270,8 @@ void main(void) {
 
     // _____________________________ Compute Geometry info _________________________________
     float NdotVUnclamped = dot(normalW, viewDirectionW);
-    float NdotV = clamp(NdotVUnclamped,0., 1.) + 0.00001;
+    // The order 1886 page 3.
+    float NdotV = absEps(NdotVUnclamped);
     float alphaG = convertRoughnessToAverageSlope(roughness);
     vec2 AARoughnessFactors = getAARoughnessFactors(normalW.xyz);
 
@@ -560,7 +325,7 @@ void main(void) {
         #ifdef LODINREFRACTIONALPHA
             float refractionLOD = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, alphaG, NdotVUnclamped);
         #else
-            float refractionLOD = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, alphaG, 1.0);
+            float refractionLOD = getLodFromAlphaG(vRefractionMicrosurfaceInfos.x, alphaG);
         #endif
 
         #ifdef LODBASEDMICROSFURACE
@@ -586,7 +351,7 @@ void main(void) {
 
             environmentRefraction = sampleRefractionLod(refractionSampler, refractionCoords, requestedRefractionLOD);
         #else
-            float lodRefractionNormalized = clamp(refractionLOD / log2(vRefractionMicrosurfaceInfos.x), 0., 1.);
+            float lodRefractionNormalized = saturate(refractionLOD / log2(vRefractionMicrosurfaceInfos.x));
             float lodRefractionNormalizedDoubled = lodRefractionNormalized * 2.0;
 
             vec4 environmentRefractionMid = sampleRefraction(refractionSampler, refractionCoords);
@@ -646,7 +411,7 @@ void main(void) {
         #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
             float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG, NdotVUnclamped);
         #else
-            float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG, 1.);
+            float reflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, alphaG);
         #endif
 
         #ifdef LODBASEDMICROSFURACE
@@ -672,7 +437,7 @@ void main(void) {
 
             environmentRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, requestedReflectionLOD);
         #else
-            float lodReflectionNormalized = clamp(reflectionLOD / log2(vReflectionMicrosurfaceInfos.x), 0., 1.);
+            float lodReflectionNormalized = saturate(reflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
             float lodReflectionNormalizedDoubled = lodReflectionNormalized * 2.0;
 
             vec4 environmentSpecularMid = sampleReflection(reflectionSampler, reflectionCoords);
@@ -740,7 +505,7 @@ void main(void) {
         #endif
 
         #ifdef SHEEN_LINKWITHALBEDO
-            float sheenFactor = pow(1.0-sheenIntensity, 5.0);
+            float sheenFactor = pow5(1.0-sheenIntensity);
             vec3 sheenColor = baseColor.rgb*(1.0-sheenFactor);
             float sheenRoughness = sheenIntensity;
             // remap albedo.
@@ -771,7 +536,7 @@ void main(void) {
             #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
                 float sheenReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, sheenAlphaG, NdotVUnclamped);
             #else
-                float sheenReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, sheenAlphaG, 1.);
+                float sheenReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, sheenAlphaG);
             #endif
 
             #ifdef LODBASEDMICROSFURACE
@@ -779,7 +544,7 @@ void main(void) {
                 sheenReflectionLOD = sheenReflectionLOD * vReflectionMicrosurfaceInfos.y + vReflectionMicrosurfaceInfos.z;
                 environmentSheenRadiance = sampleReflectionLod(reflectionSampler, reflectionCoords, sheenReflectionLOD);
             #else
-                float lodSheenReflectionNormalized = clamp(sheenReflectionLOD / log2(vReflectionMicrosurfaceInfos.x), 0., 1.);
+                float lodSheenReflectionNormalized = saturate(sheenReflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
                 float lodSheenReflectionNormalizedDoubled = lodSheenReflectionNormalized * 2.0;
 
                 vec4 environmentSheenMid = sampleReflection(reflectionSampler, reflectionCoords);
@@ -879,7 +644,8 @@ void main(void) {
 
         // Compute N dot V.
         float clearCoatNdotVUnclamped = dot(clearCoatNormalW, viewDirectionW);
-        float clearCoatNdotV = clamp(clearCoatNdotVUnclamped,0., 1.) + 0.00001;
+        // The order 1886 page 3.
+        float clearCoatNdotV = absEps(clearCoatNdotVUnclamped);
 
         // Clear Coat Reflection
         #if defined(REFLECTION)
@@ -911,7 +677,7 @@ void main(void) {
             #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
                 float clearCoatReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, clearCoatAlphaG, clearCoatNdotVUnclamped);
             #else
-                float clearCoatReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, clearCoatAlphaG, 1.);
+                float clearCoatReflectionLOD = getLodFromAlphaG(vReflectionMicrosurfaceInfos.x, clearCoatAlphaG);
             #endif
 
             #ifdef LODBASEDMICROSFURACE
@@ -921,7 +687,7 @@ void main(void) {
 
                 environmentClearCoatRadiance = sampleReflectionLod(reflectionSampler, clearCoatReflectionCoords, requestedClearCoatReflectionLOD);
             #else
-                float lodClearCoatReflectionNormalized = clamp(clearCoatReflectionLOD / log2(vReflectionMicrosurfaceInfos.x), 0., 1.);
+                float lodClearCoatReflectionNormalized = saturate(clearCoatReflectionLOD / log2(vReflectionMicrosurfaceInfos.x));
                 float lodClearCoatReflectionNormalizedDoubled = lodClearCoatReflectionNormalized * 2.0;
 
                 vec4 environmentClearCoatMid = sampleReflection(reflectionSampler, reflectionCoords);
@@ -951,7 +717,8 @@ void main(void) {
             #ifdef CLEARCOAT_TINT
                 // Used later on in the light fragment and ibl.
                 vec3 clearCoatVRefract = -refract(vPositionW, clearCoatNormalW, vClearCoatRefractionParams.y);
-                float clearCoatNdotVRefract = clamp(dot(clearCoatNormalW, clearCoatVRefract), 0.00000000001, 1.0);
+                // The order 1886 page 3.
+                float clearCoatNdotVRefract = absEps(dot(clearCoatNormalW, clearCoatVRefract));
                 vec3 absorption = vec3(0.);
             #endif
 
@@ -1115,7 +882,7 @@ void main(void) {
             // TODO. PBR Tinting.
             vec3 mixedAlbedo = surfaceAlbedo;
             float maxChannel = max(max(mixedAlbedo.r, mixedAlbedo.g), mixedAlbedo.b);
-            vec3 tint = clamp(maxChannel * mixedAlbedo, 0.0, 1.0);
+            vec3 tint = saturate(maxChannel * mixedAlbedo);
 
             // Decrease Albedo Contribution
             surfaceAlbedo *= alpha;
@@ -1252,7 +1019,7 @@ void main(void) {
         #endif
 
         #if defined(RADIANCEOVERALPHA) || defined(SPECULAROVERALPHA)
-            alpha = clamp(alpha + luminanceOverAlpha * luminanceOverAlpha, 0., 1.);
+            alpha = saturate(alpha + luminanceOverAlpha * luminanceOverAlpha);
         #endif
     #endif
 #endif