// Constants #define RECIPROCAL_PI2 0.15915494 #define FRESNEL_MAXIMUM_ON_ROUGH 0.25 // PBR CUSTOM CONSTANTS const float kPi = 3.1415926535897932384626433832795; const float kRougnhessToAlphaScale = 0.1; const float kRougnhessToAlphaOffset = 0.29248125; float Square(float value) { return value * value; } float getLuminance(vec3 color) { return clamp(dot(color, vec3(0.2126, 0.7152, 0.0722)), 0., 1.); } float convertRoughnessToAverageSlope(float roughness) { // Calculate AlphaG as square of roughness; add epsilon to avoid numerical issues const float kMinimumVariance = 0.0005; float alphaG = Square(roughness) + kMinimumVariance; return alphaG; } // Based on Beckamm roughness to Blinn exponent + http://casual-effects.blogspot.ca/2011/08/plausible-environment-lighting-in-two.html float getMipMapIndexFromAverageSlope(float maxMipLevel, float alpha) { // do not take in account lower mips hence -1... and wait from proper preprocess. // formula comes from approximation of the mathematical solution. //float mip = maxMipLevel + kRougnhessToAlphaOffset + 0.5 * log2(alpha); // In the mean time // Always [0..1] goes from max mip to min mip in a log2 way. // Change 5 to nummip below. // http://www.wolframalpha.com/input/?i=x+in+0..1+plot+(+5+%2B+0.3+%2B+0.1+*+5+*+log2(+(1+-+x)+*+(1+-+x)+%2B+0.0005)) float mip = kRougnhessToAlphaOffset + maxMipLevel + (maxMipLevel * kRougnhessToAlphaScale * log2(alpha)); return clamp(mip, 0., maxMipLevel); } float getMipMapIndexFromAverageSlopeWithPMREM(float maxMipLevel, float alphaG) { float specularPower = clamp(2. / alphaG - 2., 0.000001, 2048.); // Based on CubeMapGen for cosine power with 2048 spec default and 0.25 dropoff return clamp(- 0.5 * log2(specularPower) + 5.5, 0., maxMipLevel); } // From Microfacet Models for Refraction through Rough Surfaces, Walter et al. 2007 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 smithVisibilityG_TrowbridgeReitzGGX_Walter(float NdotL, float NdotV, float alphaG) { return smithVisibilityG1_TrowbridgeReitzGGX(NdotL, alphaG) * smithVisibilityG1_TrowbridgeReitzGGX(NdotV, alphaG); } // 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 / (kPi * d * d); } vec3 fresnelSchlickGGX(float VdotH, vec3 reflectance0, vec3 reflectance90) { return reflectance0 + (reflectance90 - reflectance0) * pow(clamp(1.0 - VdotH, 0., 1.), 5.0); } vec3 FresnelSchlickEnvironmentGGX(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); } // Cook Torance Specular computation. vec3 computeSpecularTerm(float NdotH, float NdotL, float NdotV, float VdotH, float roughness, vec3 specularColor) { float alphaG = convertRoughnessToAverageSlope(roughness); float distribution = normalDistributionFunction_TrowbridgeReitzGGX(NdotH, alphaG); float visibility = smithVisibilityG_TrowbridgeReitzGGX_Walter(NdotL, NdotV, alphaG); visibility /= (4.0 * NdotL * NdotV); // Cook Torance Denominator integated in viibility to avoid issues when visibility function changes. vec3 fresnel = fresnelSchlickGGX(VdotH, specularColor, vec3(1., 1., 1.)); float specTerm = max(0., visibility * distribution) * NdotL; return fresnel * specTerm * kPi; // TODO: audit pi constants } 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 diffuseFresnelTerm = (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNL) * (1.0 + (diffuseFresnel90 - 1.0) * diffuseFresnelNV); return diffuseFresnelTerm * NdotL; // PI Test // diffuseFresnelTerm /= kPi; } float adjustRoughnessFromLightProperties(float roughness, float lightRadius, float lightDistance) { // At small angle this approximation works. float lightRoughness = lightRadius / lightDistance; // Distribution can sum. float totalRoughness = clamp(lightRoughness + roughness, 0., 1.); return totalRoughness; } float computeDefaultMicroSurface(float microSurface, vec3 reflectivityColor) { float kReflectivityNoAlphaWorkflow_SmoothnessMax = 0.95; float reflectivityLuminance = getLuminance(reflectivityColor); float reflectivityLuma = sqrt(reflectivityLuminance); microSurface = reflectivityLuma * kReflectivityNoAlphaWorkflow_SmoothnessMax; return microSurface; } vec3 toLinearSpace(vec3 color) { return vec3(pow(color.r, 2.2), pow(color.g, 2.2), pow(color.b, 2.2)); } vec3 toGammaSpace(vec3 color) { return vec3(pow(color.r, 1.0 / 2.2), pow(color.g, 1.0 / 2.2), pow(color.b, 1.0 / 2.2)); } float computeLightFalloff(vec3 lightOffset, float lightDistanceSquared, float range) { #ifdef USEPHYSICALLIGHTFALLOFF float lightDistanceFalloff = 1.0 / ((lightDistanceSquared + 0.0001)); return lightDistanceFalloff; #else float lightFalloff = max(0., 1.0 - length(lightOffset) / range); return lightFalloff; #endif } #ifdef CAMERATONEMAP vec3 toneMaps(vec3 color) { color = max(color, 0.0); // TONE MAPPING / EXPOSURE color.rgb = color.rgb * vCameraInfos.x; float tuning = 1.5; // TODO: sync up so e.g. 18% greys are matched to exposure appropriately // PI Test // tuning *= kPi; vec3 tonemapped = 1.0 - exp2(-color.rgb * tuning); // simple local photographic tonemapper color.rgb = mix(color.rgb, tonemapped, 1.0); return color; } #endif #ifdef CAMERACONTRAST vec4 contrasts(vec4 color) { color = clamp(color, 0.0, 1.0); vec3 resultHighContrast = color.rgb * color.rgb * (3.0 - 2.0 * color.rgb); float contrast = vCameraInfos.y; if (contrast < 1.0) { // Decrease contrast: interpolate towards zero-contrast image (flat grey) color.rgb = mix(vec3(0.5, 0.5, 0.5), color.rgb, contrast); } else { // Increase contrast: apply simple shoulder-toe high contrast curve color.rgb = mix(color.rgb, resultHighContrast, contrast - 1.0); } return color; } #endif