/** * @file class1/deferred/deferredUtil.glsl * * $LicenseInfo:firstyear=2007&license=viewerlgpl$ * Second Life Viewer Source Code * Copyright (C) 2007, Linden Research, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License only. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA * $/LicenseInfo$ */ /* Parts of this file are taken from Sascha Willem's Vulkan GLTF refernce implementation MIT License Copyright (c) 2018 Sascha Willems Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ uniform sampler2DRect normalMap; uniform sampler2DRect depthMap; uniform sampler2D projectionMap; // rgba uniform sampler2D brdfLut; // projected lighted params uniform mat4 proj_mat; //screen space to light space projector uniform vec3 proj_n; // projector normal uniform vec3 proj_p; //plane projection is emitting from (in screen space) uniform float proj_focus; // distance from plane to begin blurring uniform float proj_lod ; // (number of mips in proj map) uniform float proj_range; // range between near clip and far clip plane of projection uniform float proj_ambiance; // light params uniform vec3 color; // light_color uniform float size; // light_size uniform mat4 inv_proj; uniform vec2 screen_res; const float M_PI = 3.14159265; const float ONE_OVER_PI = 0.3183098861; vec3 srgb_to_linear(vec3 cs); float calcLegacyDistanceAttenuation(float distance, float falloff) { float dist_atten = 1.0 - clamp((distance + falloff)/(1.0 + falloff), 0.0, 1.0); dist_atten *= dist_atten; // Tweak falloff slightly to match pre-EEP attenuation // NOTE: this magic number also shows up in a great many other places, search for dist_atten *= to audit dist_atten *= 2.0; return dist_atten; } // In: // lv unnormalized surface to light vector // n normal of the surface // pos unnormalized camera to surface vector // Out: // l normalized surace to light vector // nl diffuse angle // nh specular angle void calcHalfVectors(vec3 lv, vec3 n, vec3 v, out vec3 h, out vec3 l, out float nh, out float nl, out float nv, out float vh, out float lightDist) { l = normalize(lv); h = normalize(l + v); nh = clamp(dot(n, h), 0.0, 1.0); nl = clamp(dot(n, l), 0.0, 1.0); nv = clamp(dot(n, v), 0.0, 1.0); vh = clamp(dot(v, h), 0.0, 1.0); lightDist = length(lv); } // In: // light_center // pos // Out: // dist // l_dist // lv // proj_tc Projector Textue Coordinates bool clipProjectedLightVars(vec3 light_center, vec3 pos, out float dist, out float l_dist, out vec3 lv, out vec4 proj_tc ) { lv = light_center - pos.xyz; dist = length(lv); bool clipped = (dist >= size); if ( !clipped ) { dist /= size; l_dist = -dot(lv, proj_n); vec4 projected_point = (proj_mat * vec4(pos.xyz, 1.0)); clipped = (projected_point.z < 0.0); projected_point.xyz /= projected_point.w; proj_tc = projected_point; } return clipped; } vec2 getScreenCoordinate(vec2 screenpos) { vec2 sc = screenpos.xy * 2.0; if (screen_res.x > 0 && screen_res.y > 0) { sc /= screen_res; } return sc - vec2(1.0, 1.0); } // See: https://aras-p.info/texts/CompactNormalStorage.html // Method #4: Spheremap Transform, Lambert Azimuthal Equal-Area projection vec3 getNorm(vec2 screenpos) { vec2 enc = texture2DRect(normalMap, screenpos.xy).xy; vec2 fenc = enc*4-2; float f = dot(fenc,fenc); float g = sqrt(1-f/4); vec3 n; n.xy = fenc*g; n.z = 1-f/2; return n; } vec3 getNormalFromPacked(vec4 packedNormalEnvIntensityFlags) { vec2 enc = packedNormalEnvIntensityFlags.xy; vec2 fenc = enc*4-2; float f = dot(fenc,fenc); float g = sqrt(1-f/4); vec3 n; n.xy = fenc*g; n.z = 1-f/2; return normalize(n); // TODO: Is this normalize redundant? } // return packedNormalEnvIntensityFlags since GBUFFER_FLAG_HAS_PBR needs .w // See: C++: addDeferredAttachments(), GLSL: softenLightF vec4 getNormalEnvIntensityFlags(vec2 screenpos, out vec3 n, out float envIntensity) { vec4 packedNormalEnvIntensityFlags = texture2DRect(normalMap, screenpos.xy); n = getNormalFromPacked( packedNormalEnvIntensityFlags ); envIntensity = packedNormalEnvIntensityFlags.z; return packedNormalEnvIntensityFlags; } float getDepth(vec2 pos_screen) { float depth = texture2DRect(depthMap, pos_screen).r; return depth; } vec4 getTexture2DLodAmbient(vec2 tc, float lod) { #ifndef FXAA_GLSL_120 vec4 ret = textureLod(projectionMap, tc, lod); #else vec4 ret = texture2D(projectionMap, tc); #endif ret.rgb = srgb_to_linear(ret.rgb); vec2 dist = tc-vec2(0.5); float d = dot(dist,dist); ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0), 1.0); return ret; } vec4 getTexture2DLodDiffuse(vec2 tc, float lod) { #ifndef FXAA_GLSL_120 vec4 ret = textureLod(projectionMap, tc, lod); #else vec4 ret = texture2D(projectionMap, tc); #endif ret.rgb = srgb_to_linear(ret.rgb); vec2 dist = vec2(0.5) - abs(tc-vec2(0.5)); float det = min(lod/(proj_lod*0.5), 1.0); float d = min(dist.x, dist.y); float edge = 0.25*det; ret *= clamp(d/edge, 0.0, 1.0); return ret; } // lit This is set by the caller: if (nl > 0.0) { lit = attenuation * nl * noise; } // Uses: // color Projected spotlight color vec3 getProjectedLightAmbiance(float amb_da, float attenuation, float lit, float nl, float noise, vec2 projected_uv) { vec4 amb_plcol = getTexture2DLodAmbient(projected_uv, proj_lod); vec3 amb_rgb = amb_plcol.rgb * amb_plcol.a; amb_da += proj_ambiance; amb_da += (nl*nl*0.5+0.5) * proj_ambiance; amb_da *= attenuation * noise; amb_da = min(amb_da, 1.0-lit); return (amb_da * color.rgb * amb_rgb); } // Returns projected light in Linear // Uses global spotlight color: // color // NOTE: projected.a will be pre-multiplied with projected.rgb vec3 getProjectedLightDiffuseColor(float light_distance, vec2 projected_uv) { float diff = clamp((light_distance - proj_focus)/proj_range, 0.0, 1.0); float lod = diff * proj_lod; vec4 plcol = getTexture2DLodDiffuse(projected_uv.xy, lod); return color.rgb * plcol.rgb * plcol.a; } vec4 texture2DLodSpecular(vec2 tc, float lod) { #ifndef FXAA_GLSL_120 vec4 ret = textureLod(projectionMap, tc, lod); #else vec4 ret = texture2D(projectionMap, tc); #endif ret.rgb = srgb_to_linear(ret.rgb); vec2 dist = vec2(0.5) - abs(tc-vec2(0.5)); float det = min(lod/(proj_lod*0.5), 1.0); float d = min(dist.x, dist.y); d *= min(1, d * (proj_lod - lod)); // BUG? extra factor compared to diffuse causes N repeats float edge = 0.25*det; ret *= clamp(d/edge, 0.0, 1.0); return ret; } // See: clipProjectedLightVars() vec3 getProjectedLightSpecularColor(vec3 pos, vec3 n ) { vec3 slit = vec3(0); vec3 ref = reflect(normalize(pos), n); //project from point pos in direction ref to plane proj_p, proj_n vec3 pdelta = proj_p-pos; float l_dist = length(pdelta); float ds = dot(ref, proj_n); if (ds < 0.0) { vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds; vec4 stc = (proj_mat * vec4(pfinal.xyz, 1.0)); if (stc.z > 0.0) { stc /= stc.w; slit = getProjectedLightDiffuseColor( l_dist, stc.xy ); // NOTE: Using diffuse due to texture2DLodSpecular() has extra: d *= min(1, d * (proj_lod - lod)); } } return slit; // specular light } vec3 getProjectedLightSpecularColor(float light_distance, vec2 projected_uv) { float diff = clamp((light_distance - proj_focus)/proj_range, 0.0, 1.0); float lod = diff * proj_lod; vec4 plcol = getTexture2DLodDiffuse(projected_uv.xy, lod); // NOTE: Using diffuse due to texture2DLodSpecular() has extra: d *= min(1, d * (proj_lod - lod)); return color.rgb * plcol.rgb * plcol.a; } vec4 getPosition(vec2 pos_screen) { float depth = getDepth(pos_screen); vec2 sc = getScreenCoordinate(pos_screen); vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0); vec4 pos = inv_proj * ndc; pos /= pos.w; pos.w = 1.0; return pos; } vec4 getPositionWithDepth(vec2 pos_screen, float depth) { vec2 sc = getScreenCoordinate(pos_screen); vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0); vec4 pos = inv_proj * ndc; pos /= pos.w; pos.w = 1.0; return pos; } vec2 getScreenXY(vec4 clip) { vec4 ndc = clip; ndc.xyz /= clip.w; vec2 screen = vec2( ndc.xy * 0.5 ); screen += 0.5; screen *= screen_res; return screen; } // Color utils vec3 colorize_dot(float x) { if (x > 0.0) return vec3( 0, x, 0 ); if (x < 0.0) return vec3(-x, 0, 0 ); return vec3( 0, 0, 1 ); } vec3 hue_to_rgb(float hue) { if (hue > 1.0) return vec3(0.5); vec3 rgb = abs(hue * 6. - vec3(3, 2, 4)) * vec3(1, -1, -1) + vec3(-1, 2, 2); return clamp(rgb, 0.0, 1.0); } // PBR Utils // ior Index of Refraction, normally 1.5 // returns reflect0 float calcF0(float ior) { float f0 = (1.0 - ior) / (1.0 + ior); return f0 * f0; } vec3 fresnel(float vh, vec3 f0, vec3 f90 ) { float x = 1.0 - abs(vh); float x2 = x*x; float x5 = x2*x2*x; vec3 fr = f0 + (f90 - f0)*x5; return fr; } vec3 fresnelSchlick( vec3 reflect0, vec3 reflect90, float vh) { return reflect0 + (reflect90 - reflect0) * pow(clamp(1.0 - vh, 0.0, 1.0), 5.0); } // Approximate Environment BRDF vec2 getGGXApprox( vec2 uv ) { // Reference: Physically Based Shading on Mobile // https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile // EnvBRDFApprox( vec3 SpecularColor, float Roughness, float NoV ) float nv = uv.x; float roughness = uv.y; const vec4 c0 = vec4( -1, -0.0275, -0.572, 0.022 ); const vec4 c1 = vec4( 1, 0.0425, 1.04 , -0.04 ); vec4 r = roughness * c0 + c1; float a004 = min( r.x * r.x, exp2( -9.28 * nv ) ) * r.x + r.y; vec2 ScaleBias = vec2( -1.04, 1.04 ) * a004 + r.zw; return ScaleBias; } #define PBR_USE_GGX_APPROX 1 vec2 getGGX( vec2 brdfPoint ) { #if PBR_USE_GGX_APPROX return getGGXApprox( brdfPoint); #else return texture2D(GGXLUT, brdfPoint).rg; // TODO: use GGXLUT #endif } // Reference: float getRangeAttenuation(float range, float distance) float getLightAttenuationPointSpot(float range, float distance) { #if 1 return distance; #else float range2 = pow(range, 2.0); // support negative range as unlimited if (range <= 0.0) { return 1.0 / range2; } return max(min(1.0 - pow(distance / range, 4.0), 1.0), 0.0) / range2; #endif } vec3 getLightIntensityPoint(vec3 lightColor, float lightRange, float lightDistance) { float rangeAttenuation = getLightAttenuationPointSpot(lightRange, lightDistance); return rangeAttenuation * lightColor; } float getLightAttenuationSpot(vec3 spotDirection) { return 1.0; } vec3 getLightIntensitySpot(vec3 lightColor, float lightRange, float lightDistance, vec3 v) { float spotAttenuation = getLightAttenuationSpot(-v); return spotAttenuation * getLightIntensityPoint( lightColor, lightRange, lightDistance ); } // NOTE: This is different from the GGX texture float D_GGX( float nh, float alphaRough ) { float rough2 = alphaRough * alphaRough; float f = (nh * nh) * (rough2 - 1.0) + 1.0; return rough2 / (M_PI * f * f); } // NOTE: This is different from the GGX texture // See: // Real Time Rendering, 4th Edition // Page 341 // Equation 9.43 // Also see: // https://google.github.io/filament/Filament.md.html#materialsystem/specularbrdf/geometricshadowing(specularg) // 4.4.2 Geometric Shadowing (specular G) float V_GGX( float nl, float nv, float alphaRough ) { #if 1 // Note: When roughness is zero, has discontuinity in the bottom hemisphere float rough2 = alphaRough * alphaRough; float ggxv = nl * sqrt(nv * nv * (1.0 - rough2) + rough2); float ggxl = nv * sqrt(nl * nl * (1.0 - rough2) + rough2); float ggx = ggxv + ggxl; if (ggx > 0.0) { return 0.5 / ggx; } return 0.0; #else // See: smithVisibility_GGXCorrelated, V_SmithCorrelated, etc. float rough2 = alphaRough * alphaRough; float ggxv = nl * sqrt(nv * (nv - rough2 * nv) + rough2); float ggxl = nv * sqrt(nl * (nl - rough2 * nl) + rough2); return 0.5 / (ggxv + ggxl); #endif } // NOTE: Assumes a hard-coded IOR = 1.5 void initMaterial( vec3 diffuse, vec3 packedORM, out float alphaRough, out vec3 c_diff, out vec3 reflect0, out vec3 reflect90, out float specWeight ) { float metal = packedORM.b; c_diff = mix(diffuse, vec3(0), metal); float IOR = 1.5; // default Index Of Refraction 1.5 (dielectrics) reflect0 = vec3(0.04); // -> incidence reflectance 0.04 // reflect0 = vec3(calcF0(IOR)); reflect0 = mix(reflect0, diffuse, metal); // reflect at 0 degrees reflect90 = vec3(1); // reflect at 90 degrees specWeight = 1.0; // When roughness is zero blender shows a tiny specular float perceptualRough = max(packedORM.g, 0.1); alphaRough = perceptualRough * perceptualRough; } vec3 BRDFDiffuse(vec3 color) { return color * ONE_OVER_PI; } vec3 BRDFLambertian( vec3 reflect0, vec3 reflect90, vec3 c_diff, float specWeight, float vh ) { return (1.0 - fresnelSchlick( reflect0, reflect90, vh)) * BRDFDiffuse(c_diff); } vec3 BRDFSpecularGGX( vec3 reflect0, vec3 reflect90, float alphaRough, float specWeight, float vh, float nl, float nv, float nh ) { vec3 fresnel = fresnelSchlick( reflect0, reflect90, vh ); // Fresnel float vis = V_GGX( nl, nv, alphaRough ); // Visibility float d = D_GGX( nh, alphaRough ); // Distribution return fresnel * vis * d; } // Based omn http://byteblacksmith.com/improvements-to-the-canonical-one-liner-glsl-rand-for-opengl-es-2-0/ float random(vec2 co) { float a = 12.9898; float b = 78.233; float c = 43758.5453; float dt= dot(co.xy ,vec2(a,b)); float sn= mod(dt,3.14); return fract(sin(sn) * c); } vec2 hammersley2d(uint i, uint N) { // Radical inverse based on http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html uint bits = (i << 16u) | (i >> 16u); bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); float rdi = float(bits) * 2.3283064365386963e-10; return vec2(float(i) /float(N), rdi); } // Based on http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_slides.pdf vec3 importanceSample_GGX(vec2 Xi, float roughness, vec3 normal) { // Maps a 2D point to a hemisphere with spread based on roughness float alpha = roughness * roughness; float phi = 2.0 * M_PI * Xi.x + random(normal.xz) * 0.1; float cosTheta = sqrt((1.0 - Xi.y) / (1.0 + (alpha*alpha - 1.0) * Xi.y)); float sinTheta = sqrt(1.0 - cosTheta * cosTheta); vec3 H = vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta); // Tangent space vec3 up = abs(normal.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0); vec3 tangentX = normalize(cross(up, normal)); vec3 tangentY = normalize(cross(normal, tangentX)); // Convert to world Space return normalize(tangentX * H.x + tangentY * H.y + normal * H.z); } // Geometric Shadowing function float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness) { float k = (roughness * roughness) / 2.0; float GL = dotNL / (dotNL * (1.0 - k) + k); float GV = dotNV / (dotNV * (1.0 - k) + k); return GL * GV; } #define NUM_SAMPLES 16 vec2 BRDF(float NoV, float roughness) { #if 0 // Normal always points along z-axis for the 2D lookup const vec3 N = vec3(0.0, 0.0, 1.0); vec3 V = vec3(sqrt(1.0 - NoV*NoV), 0.0, NoV); vec2 LUT = vec2(0.0); for(uint i = 0u; i < NUM_SAMPLES; i++) { vec2 Xi = hammersley2d(i, NUM_SAMPLES); vec3 H = importanceSample_GGX(Xi, roughness, N); vec3 L = 2.0 * dot(V, H) * H - V; float dotNL = max(dot(N, L), 0.0); float dotNV = max(dot(N, V), 0.0); float dotVH = max(dot(V, H), 0.0); float dotNH = max(dot(H, N), 0.0); if (dotNL > 0.0) { float G = G_SchlicksmithGGX(dotNL, dotNV, roughness); float G_Vis = (G * dotVH) / (dotNH * dotNV); float Fc = pow(1.0 - dotVH, 5.0); LUT += vec2((1.0 - Fc) * G_Vis, Fc * G_Vis); } } return LUT / float(NUM_SAMPLES); #else return texture(brdfLut, vec2(NoV, roughness)).rg; #endif } // set colorDiffuse and colorSpec to the results of GLTF PBR style IBL vec3 pbrIbl(vec3 diffuseColor, vec3 specularColor, vec3 radiance, // radiance map sample vec3 irradiance, // irradiance map sample float ao, // ambient occlusion factor float nv, // normal dot view vector float perceptualRough) { // retrieve a scale and bias to F0. See [1], Figure 3 vec2 brdf = BRDF(clamp(nv, 0, 1), 1.0-perceptualRough); vec3 diffuseLight = irradiance; vec3 specularLight = radiance; vec3 diffuse = diffuseLight * diffuseColor; vec3 specular = specularLight * (specularColor * brdf.x + brdf.y); return (diffuse + specular) * ao; } // Encapsulate the various inputs used by the various functions in the shading equation // We store values in this struct to simplify the integration of alternative implementations // of the shading terms, outlined in the Readme.MD Appendix. struct PBRInfo { float NdotL; // cos angle between normal and light direction float NdotV; // cos angle between normal and view direction float NdotH; // cos angle between normal and half vector float LdotH; // cos angle between light direction and half vector float VdotH; // cos angle between view direction and half vector float perceptualRoughness; // roughness value, as authored by the model creator (input to shader) float metalness; // metallic value at the surface vec3 reflectance0; // full reflectance color (normal incidence angle) vec3 reflectance90; // reflectance color at grazing angle float alphaRoughness; // roughness mapped to a more linear change in the roughness (proposed by [2]) vec3 diffuseColor; // color contribution from diffuse lighting vec3 specularColor; // color contribution from specular lighting }; // Basic Lambertian diffuse // Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog // See also [1], Equation 1 vec3 diffuse(PBRInfo pbrInputs) { return pbrInputs.diffuseColor / M_PI; } // The following equation models the Fresnel reflectance term of the spec equation (aka F()) // Implementation of fresnel from [4], Equation 15 vec3 specularReflection(PBRInfo pbrInputs) { return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0); } // This calculates the specular geometric attenuation (aka G()), // where rougher material will reflect less light back to the viewer. // This implementation is based on [1] Equation 4, and we adopt their modifications to // alphaRoughness as input as originally proposed in [2]. float geometricOcclusion(PBRInfo pbrInputs) { float NdotL = pbrInputs.NdotL; float NdotV = pbrInputs.NdotV; float r = pbrInputs.alphaRoughness; float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL))); float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV))); return attenuationL * attenuationV; } // The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D()) // Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz // Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3. float microfacetDistribution(PBRInfo pbrInputs) { float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness; float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0; return roughnessSq / (M_PI * f * f); } vec3 pbrPunctual(vec3 diffuseColor, vec3 specularColor, float perceptualRoughness, float metallic, vec3 n, // normal vec3 v, // surface point to camera vec3 l) //surface point to light { float alphaRoughness = perceptualRoughness * perceptualRoughness; // Compute reflectance. float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b); // 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 = clamp(reflectance * 25.0, 0.0, 1.0); vec3 specularEnvironmentR0 = specularColor.rgb; vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90; vec3 h = normalize(l+v); // Half vector between both l and v vec3 reflection = -normalize(reflect(v, n)); reflection.y *= -1.0f; float NdotL = clamp(dot(n, l), 0.001, 1.0); float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0); float NdotH = clamp(dot(n, h), 0.0, 1.0); float LdotH = clamp(dot(l, h), 0.0, 1.0); float VdotH = clamp(dot(v, h), 0.0, 1.0); PBRInfo pbrInputs = PBRInfo( NdotL, NdotV, NdotH, LdotH, VdotH, perceptualRoughness, metallic, specularEnvironmentR0, specularEnvironmentR90, alphaRoughness, diffuseColor, specularColor ); // Calculate the shading terms for the microfacet specular shading model vec3 F = specularReflection(pbrInputs); float G = geometricOcclusion(pbrInputs); float D = microfacetDistribution(pbrInputs); const vec3 u_LightColor = vec3(1.0); // Calculation of analytical lighting contribution vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs); vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV); // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law) vec3 color = NdotL * u_LightColor * (diffuseContrib + specContrib); return color; } void pbrDirectionalLight(inout vec3 colorDiffuse, inout vec3 colorSpec, vec3 sunlit, float scol, vec3 reflect0, vec3 reflect90, vec3 c_diff, float alphaRough, float vh, float nl, float nv, float nh) { float scale = 32.0; vec3 sunColor = sunlit * scale; // scol = sun shadow vec3 intensity = sunColor * nl * scol; vec3 sunDiffuse = intensity * BRDFLambertian (reflect0, reflect90, c_diff , 1.0, vh); vec3 sunSpec = intensity * BRDFSpecularGGX(reflect0, reflect90, alphaRough, 1.0, vh, nl, nv, nh); colorDiffuse += sunDiffuse; colorSpec += sunSpec; }