#ifndef UNITY_LEFTOVER_INCLUDED #define UNITY_LEFTOVER_INCLUDED //------------------------------------------------------------------------------------- inline half Pow4 (half x) { return x*x*x*x; } inline float2 Pow4 (float2 x) { return x*x*x*x; } inline half3 Pow4 (half3 x) { return x*x*x*x; } inline half4 Pow4 (half4 x) { return x*x*x*x; } // Pow5 uses the same amount of instructions as generic pow(), but has 2 advantages: // 1) better instruction pipelining // 2) no need to worry about NaNs inline half Pow5 (half x) { return x*x * x*x * x; } inline half2 Pow5 (half2 x) { return x*x * x*x * x; } inline half3 Pow5 (half3 x) { return x*x * x*x * x; } inline half4 Pow5 (half4 x) { return x*x * x*x * x; } inline half3 FresnelTerm (half3 F0, half cosA) { half t = Pow5 (1 - cosA); // ala Schlick interpoliation return F0 + (1-F0) * t; } inline half3 FresnelLerp (half3 F0, half3 F90, half cosA) { half t = Pow5 (1 - cosA); // ala Schlick interpoliation return lerp (F0, F90, t); } // approximage Schlick with ^4 instead of ^5 inline half3 FresnelLerpFast (half3 F0, half3 F90, half cosA) { half t = Pow4 (1 - cosA); return lerp (F0, F90, t); } // Note: Disney diffuse must be multiply by diffuseAlbedo / PI. This is done outside of this function. half DisneyDiffuse(half NdotV, half NdotL, half LdotH, half perceptualRoughness) { half fd90 = 0.5 + 2 * LdotH * LdotH * perceptualRoughness; // Two schlick fresnel term half lightScatter = (1 + (fd90 - 1) * Pow5(1 - NdotL)); half viewScatter = (1 + (fd90 - 1) * Pow5(1 - NdotV)); return lightScatter * viewScatter; } // NOTE: Visibility term here is the full form from Torrance-Sparrow model, it includes Geometric term: V = G / (N.L * N.V) // This way it is easier to swap Geometric terms and more room for optimizations (except maybe in case of CookTorrance geom term) // Generic Smith-Schlick visibility term inline half SmithVisibilityTerm (half NdotL, half NdotV, half k) { half gL = NdotL * (1-k) + k; half gV = NdotV * (1-k) + k; return 1.0 / (gL * gV + 1e-5f); // This function is not intended to be running on Mobile, // therefore epsilon is smaller than can be represented by half } // Smith-Schlick derived for Beckmann inline half SmithBeckmannVisibilityTerm (half NdotL, half NdotV, half roughness) { half c = 0.797884560802865h; // c = sqrt(2 / Pi) half k = roughness * c; return SmithVisibilityTerm (NdotL, NdotV, k) * 0.25f; // * 0.25 is the 1/4 of the visibility term } // Ref: http://jcgt.org/published/0003/02/03/paper.pdf inline float SmithJointGGXVisibilityTerm (float NdotL, float NdotV, float roughness) { #if 0 // Original formulation: // lambda_v = (-1 + sqrt(a2 * (1 - NdotL2) / NdotL2 + 1)) * 0.5f; // lambda_l = (-1 + sqrt(a2 * (1 - NdotV2) / NdotV2 + 1)) * 0.5f; // G = 1 / (1 + lambda_v + lambda_l); // Reorder code to be more optimal half a = roughness; half a2 = a * a; half lambdaV = NdotL * sqrt((-NdotV * a2 + NdotV) * NdotV + a2); half lambdaL = NdotV * sqrt((-NdotL * a2 + NdotL) * NdotL + a2); // Simplify visibility term: (2.0f * NdotL * NdotV) / ((4.0f * NdotL * NdotV) * (lambda_v + lambda_l + 1e-5f)); return 0.5f / (lambdaV + lambdaL + 1e-5f); // This function is not intended to be running on Mobile, // therefore epsilon is smaller than can be represented by half #else // Approximation of the above formulation (simplify the sqrt, not mathematically correct but close enough) float a = roughness; float lambdaV = NdotL * (NdotV * (1 - a) + a); float lambdaL = NdotV * (NdotL * (1 - a) + a); #if defined(SHADER_API_SWITCH) return 0.5f / (lambdaV + lambdaL + 1e-4f); // work-around against hlslcc rounding error #else return 0.5f / (lambdaV + lambdaL + 1e-5f); #endif #endif } inline float GGXTerm (float NdotH, float roughness) { float a2 = roughness * roughness; float d = (NdotH * a2 - NdotH) * NdotH + 1.0f; // 2 mad return UNITY_INV_PI * a2 / (d * d + 1e-7f); // This function is not intended to be running on Mobile, // therefore epsilon is smaller than what can be represented by half } inline half PerceptualRoughnessToSpecPower (half perceptualRoughness) { half m = PerceptualRoughnessToRoughness(perceptualRoughness); // m is the true academic roughness. half sq = max(1e-4f, m*m); half n = (2.0 / sq) - 2.0; // https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf n = max(n, 1e-4f); // prevent possible cases of pow(0,0), which could happen when roughness is 1.0 and NdotH is zero return n; } // BlinnPhong normalized as normal distribution function (NDF) // for use in micro-facet model: spec=D*G*F // eq. 19 in https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf inline half NDFBlinnPhongNormalizedTerm (half NdotH, half n) { // norm = (n+2)/(2*pi) half normTerm = (n + 2.0) * (0.5/UNITY_PI); half specTerm = pow (NdotH, n); return specTerm * normTerm; } //------------------------------------------------------------------------------------- #endif // UNITY_LEFTOVER_INCLUDED