您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
326 行
12 KiB
326 行
12 KiB
#ifndef UNITY_BSDF_INCLUDED
|
|
#define UNITY_BSDF_INCLUDED
|
|
|
|
// Note: All NDF and diffuse term have a version with and without divide by PI.
|
|
// Version with divide by PI are use for direct lighting.
|
|
// Version without divide by PI are use for image based lighting where often the PI cancel during importance sampling
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Fresnel term
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float F_Schlick(float f0, float f90, float u)
|
|
{
|
|
float x = 1.0 - u;
|
|
float x2 = x * x;
|
|
float x5 = x * x2 * x2;
|
|
return (f90 - f0) * x5 + f0; // sub mul mul mul sub mad
|
|
}
|
|
|
|
float F_Schlick(float f0, float u)
|
|
{
|
|
return F_Schlick(f0, 1.0, u); // sub mul mul mul sub mad
|
|
}
|
|
|
|
float3 F_Schlick(float3 f0, float f90, float u)
|
|
{
|
|
float x = 1.0 - u;
|
|
float x2 = x * x;
|
|
float x5 = x * x2 * x2;
|
|
return f0 * (1.0 - x5) + (f90 * x5); // sub mul mul mul sub mul mad*3
|
|
}
|
|
|
|
float3 F_Schlick(float3 f0, float u)
|
|
{
|
|
return F_Schlick(f0, 1.0, u); // sub mul mul mul sub mad*3
|
|
}
|
|
|
|
// Does not handle TIR.
|
|
float F_Transm_Schlick(float f0, float f90, float u)
|
|
{
|
|
float x = 1.0 - u;
|
|
float x2 = x * x;
|
|
float x5 = x * x2 * x2;
|
|
return (1.0 - f90 * x5) - f0 * (1.0 - x5); // sub mul mul mul mad sub mad
|
|
}
|
|
|
|
// Does not handle TIR.
|
|
float F_Transm_Schlick(float f0, float u)
|
|
{
|
|
return F_Transm_Schlick(f0, 1.0, u); // sub mul mul mad mad
|
|
}
|
|
|
|
// Does not handle TIR.
|
|
float3 F_Transm_Schlick(float3 f0, float f90, float u)
|
|
{
|
|
float x = 1.0 - u;
|
|
float x2 = x * x;
|
|
float x5 = x * x2 * x2;
|
|
return (1.0 - f90 * x5) - f0 * (1.0 - x5); // sub mul mul mul mad sub mad*3
|
|
}
|
|
|
|
// Does not handle TIR.
|
|
float3 F_Transm_Schlick(float3 f0, float u)
|
|
{
|
|
return F_Transm_Schlick(f0, 1.0, u); // sub mul mul mad mad*3
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Specular BRDF
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// With analytical light (not image based light) we clamp the minimun roughness in the NDF to avoid numerical instability.
|
|
#define UNITY_MIN_ROUGHNESS 0.002
|
|
|
|
float ClampRoughnessForAnalyticalLights(float roughness)
|
|
{
|
|
return max(roughness, UNITY_MIN_ROUGHNESS);
|
|
}
|
|
|
|
float D_GGXNoPI(float NdotH, float roughness)
|
|
{
|
|
float a2 = roughness * roughness;
|
|
float f = (NdotH * a2 - NdotH) * NdotH + 1.0;
|
|
return a2 / (f * f);
|
|
}
|
|
|
|
float D_GGX(float NdotH, float roughness)
|
|
{
|
|
return INV_PI * D_GGXNoPI(NdotH, roughness);
|
|
}
|
|
|
|
// Ref: Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs, p. 19, 29.
|
|
float G_MaskingSmithGGX(float NdotV, float roughness)
|
|
{
|
|
// G1(V, H) = HeavisideStep(VdotH) / (1 + Λ(V)).
|
|
// Λ(V) = -0.5 + 0.5 * sqrt(1 + 1 / a²).
|
|
// a = 1 / (roughness * tan(theta)).
|
|
// 1 + Λ(V) = 0.5 + 0.5 * sqrt(1 + roughness² * tan²(theta)).
|
|
// tan²(theta) = (1 - cos²(theta)) / cos²(theta) = 1 / cos²(theta) - 1.
|
|
// Assume that (VdotH > 0), e.i. (acos(LdotV) < Pi).
|
|
|
|
float a2 = roughness * roughness;
|
|
float z2 = NdotV * NdotV;
|
|
|
|
return 1 / (0.5 + 0.5 * sqrt(1.0 + a2 * (1.0 / z2 - 1.0)));
|
|
}
|
|
|
|
// Ref: Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs, p. 12.
|
|
float D_GGX_Visible(float NdotH, float NdotV, float VdotH, float roughness)
|
|
{
|
|
return D_GGX(NdotH, roughness) * G_MaskingSmithGGX(NdotV, roughness) * VdotH / NdotV;
|
|
}
|
|
|
|
// Precompute part of lambdaV
|
|
float GetSmithJointGGXPreLambdaV(float NdotV, float roughness)
|
|
{
|
|
float a2 = roughness * roughness;
|
|
return sqrt((-NdotV * a2 + NdotV) * NdotV + a2);
|
|
}
|
|
|
|
// Ref: http://jcgt.org/published/0003/02/03/paper.pdf
|
|
float V_SmithJointGGX(float NdotL, float NdotV, float roughness, float preLambdaV)
|
|
{
|
|
float a2 = roughness * roughness;
|
|
|
|
// Original formulation:
|
|
// lambda_v = (-1 + sqrt(a2 * (1 - NdotL2) / NdotL2 + 1)) * 0.5
|
|
// lambda_l = (-1 + sqrt(a2 * (1 - NdotV2) / NdotV2 + 1)) * 0.5
|
|
// G = 1 / (1 + lambda_v + lambda_l);
|
|
|
|
// Reorder code to be more optimal:
|
|
float lambdaV = NdotL * preLambdaV;
|
|
float lambdaL = NdotV * sqrt((-NdotL * a2 + NdotL) * NdotL + a2);
|
|
|
|
// Simplify visibility term: (2.0 * NdotL * NdotV) / ((4.0 * NdotL * NdotV) * (lambda_v + lambda_l));
|
|
return 0.5 / (lambdaV + lambdaL);
|
|
}
|
|
|
|
float V_SmithJointGGX(float NdotL, float NdotV, float roughness)
|
|
{
|
|
float preLambdaV = GetSmithJointGGXPreLambdaV(NdotV, roughness);
|
|
return V_SmithJointGGX(NdotL, NdotV, roughness, preLambdaV);
|
|
}
|
|
|
|
// Inline D_GGX() * V_SmithJointGGX() together for better code generation.
|
|
float DV_SmithJointGGX(float NdotH, float NdotL, float NdotV, float roughness, float preLambdaV)
|
|
{
|
|
float a2 = roughness * roughness;
|
|
float f = (NdotH * a2 - NdotH) * NdotH + 1.0;
|
|
float2 D = float2(a2, f * f); // Fraction without the constant (1/Pi)
|
|
|
|
float lambdaV = NdotL * preLambdaV;
|
|
float lambdaL = NdotV * sqrt((-NdotL * a2 + NdotL) * NdotL + a2);
|
|
|
|
float2 G = float2(1, lambdaV + lambdaL); // Fraction without the constant (0.5)
|
|
|
|
return (INV_PI * 0.5) * (D.x * G.x) / (D.y * G.y);
|
|
}
|
|
|
|
float DV_SmithJointGGX(float NdotH, float NdotL, float NdotV, float roughness)
|
|
{
|
|
float preLambdaV = GetSmithJointGGXPreLambdaV(NdotV, roughness);
|
|
return DV_SmithJointGGX(NdotH, NdotL, NdotV, roughness, preLambdaV);
|
|
}
|
|
|
|
// Precompute a part of LambdaV.
|
|
// Note on this linear approximation.
|
|
// Exact for roughness values of 0 and 1. Also, exact when the cosine is 0 or 1.
|
|
// Otherwise, the worst case relative error is around 10%.
|
|
// https://www.desmos.com/calculator/wtp8lnjutx
|
|
float GetSmithJointGGXPreLambdaVApprox(float NdotV, float roughness)
|
|
{
|
|
float a = roughness;
|
|
return NdotV * (1 - a) + a;
|
|
}
|
|
|
|
float V_SmithJointGGXApprox(float NdotL, float NdotV, float roughness, float preLambdaV)
|
|
{
|
|
float a = roughness;
|
|
|
|
float lambdaV = NdotL * preLambdaV;
|
|
float lambdaL = NdotV * (NdotL * (1 - a) + a);
|
|
|
|
return 0.5 / (lambdaV + lambdaL);
|
|
}
|
|
|
|
float V_SmithJointGGXApprox(float NdotL, float NdotV, float roughness)
|
|
{
|
|
float preLambdaV = GetSmithJointGGXPreLambdaVApprox(NdotV, roughness);
|
|
return V_SmithJointGGXApprox(NdotL, NdotV, roughness, preLambdaV);
|
|
}
|
|
|
|
// roughnessT -> roughness in tangent direction
|
|
// roughnessB -> roughness in bitangent direction
|
|
float D_GGXAnisoNoPI(float TdotH, float BdotH, float NdotH, float roughnessT, float roughnessB)
|
|
{
|
|
float aT2 = roughnessT * roughnessT;
|
|
float aB2 = roughnessB * roughnessB;
|
|
|
|
float f = TdotH * TdotH / aT2 + BdotH * BdotH / aB2 + NdotH * NdotH;
|
|
return 1.0 / (roughnessT * roughnessB * f * f);
|
|
}
|
|
|
|
float D_GGXAniso(float TdotH, float BdotH, float NdotH, float roughnessT, float roughnessB)
|
|
{
|
|
return INV_PI * D_GGXAnisoNoPI(TdotH, BdotH, NdotH, roughnessT, roughnessB);
|
|
}
|
|
|
|
float GetSmithJointGGXAnisoPreLambdaV(float TdotV, float BdotV, float NdotV, float roughnessT, float roughnessB)
|
|
{
|
|
float aT2 = roughnessT * roughnessT;
|
|
float aB2 = roughnessB * roughnessB;
|
|
|
|
return sqrt(aT2 * TdotV * TdotV + aB2 * BdotV * BdotV + NdotV * NdotV);
|
|
}
|
|
|
|
// Ref: https://cedec.cesa.or.jp/2015/session/ENG/14698.html The Rendering Materials of Far Cry 4
|
|
float V_SmithJointGGXAniso(float TdotV, float BdotV, float NdotV, float TdotL, float BdotL, float NdotL, float roughnessT, float roughnessB, float preLambdaV)
|
|
{
|
|
float aT2 = roughnessT * roughnessT;
|
|
float aB2 = roughnessB * roughnessB;
|
|
|
|
float lambdaV = NdotL * preLambdaV;
|
|
float lambdaL = NdotV * sqrt(aT2 * TdotL * TdotL + aB2 * BdotL * BdotL + NdotL * NdotL);
|
|
|
|
return 0.5 / (lambdaV + lambdaL);
|
|
}
|
|
|
|
float V_SmithJointGGXAniso(float TdotV, float BdotV, float NdotV, float TdotL, float BdotL, float NdotL, float roughnessT, float roughnessB)
|
|
{
|
|
float preLambdaV = GetSmithJointGGXAnisoPreLambdaV(TdotV, BdotV, NdotV, roughnessT, roughnessB);
|
|
return V_SmithJointGGXAniso(TdotV, BdotV, NdotV, TdotL, BdotL, NdotL, roughnessT, roughnessB, preLambdaV);
|
|
}
|
|
|
|
// Inline D_GGXAniso() * V_SmithJointGGXAniso() together for better code generation.
|
|
float DV_SmithJointGGXAniso(float TdotH, float BdotH, float NdotH,
|
|
float TdotV, float BdotV, float NdotV,
|
|
float TdotL, float BdotL, float NdotL,
|
|
float roughnessT, float roughnessB, float preLambdaV)
|
|
{
|
|
float aT2 = roughnessT * roughnessT;
|
|
float aB2 = roughnessB * roughnessB;
|
|
|
|
float f = TdotH * TdotH / aT2 + BdotH * BdotH / aB2 + NdotH * NdotH;
|
|
float2 D = float2(1, roughnessT * roughnessB * f * f); // Fraction without the constant (1/Pi)
|
|
|
|
float lambdaV = NdotL * preLambdaV;
|
|
float lambdaL = NdotV * sqrt(aT2 * TdotL * TdotL + aB2 * BdotL * BdotL + NdotL * NdotL);
|
|
|
|
float2 G = float2(1, lambdaV + lambdaL); // Fraction without the constant (0.5)
|
|
|
|
return (INV_PI * 0.5) * (D.x * G.x) / (D.y * G.y);
|
|
}
|
|
|
|
float DV_SmithJointGGXAniso(float TdotH, float BdotH, float NdotH,
|
|
float TdotV, float BdotV, float NdotV,
|
|
float TdotL, float BdotL, float NdotL,
|
|
float roughnessT, float roughnessB)
|
|
{
|
|
float preLambdaV = GetSmithJointGGXAnisoPreLambdaV(TdotV, BdotV, NdotV, roughnessT, roughnessB);
|
|
return DV_SmithJointGGXAniso(TdotH, BdotH, NdotH,
|
|
TdotV, BdotV, NdotV,
|
|
TdotL, BdotL, NdotL,
|
|
roughnessT, roughnessB, preLambdaV);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Diffuse BRDF - diffuseColor is expected to be multiply by the caller
|
|
//-----------------------------------------------------------------------------
|
|
|
|
float LambertNoPI()
|
|
{
|
|
return 1.0;
|
|
}
|
|
|
|
float Lambert()
|
|
{
|
|
return INV_PI;
|
|
}
|
|
|
|
float DisneyDiffuseNoPI(float NdotV, float NdotL, float LdotV, float perceptualRoughness)
|
|
{
|
|
// (2 * LdotH * LdotH) = 1 + LdotV
|
|
// float fd90 = 0.5 + 2 * LdotH * LdotH * perceptualRoughness;
|
|
float fd90 = 0.5 + (perceptualRoughness + perceptualRoughness * LdotV);
|
|
// Two schlick fresnel term
|
|
float lightScatter = F_Schlick(1.0, fd90, NdotL);
|
|
float viewScatter = F_Schlick(1.0, fd90, NdotV);
|
|
|
|
// Normalize the BRDF for polar view angles of up to (Pi/4).
|
|
// We use the worst case of (roughness = albedo = 1), and, for each view angle,
|
|
// integrate (brdf * cos(theta_light)) over all light directions.
|
|
// The resulting value is for (theta_view = 0), which is actually a little bit larger
|
|
// than the value of the integral for (theta_view = Pi/4).
|
|
// Hopefully, the compiler folds the constant together with (1/Pi).
|
|
return rcp(1.03571) * (lightScatter * viewScatter);
|
|
}
|
|
|
|
float DisneyDiffuse(float NdotV, float NdotL, float LdotV, float perceptualRoughness)
|
|
{
|
|
return INV_PI * DisneyDiffuseNoPI(NdotV, NdotL, LdotV, perceptualRoughness);
|
|
}
|
|
|
|
// Ref: Diffuse Lighting for GGX + Smith Microsurfaces, p. 113.
|
|
float3 DiffuseGGXNoPI(float3 albedo, float NdotV, float NdotL, float NdotH, float LdotV, float perceptualRoughness)
|
|
{
|
|
float facing = 0.5 + 0.5 * LdotV; // (LdotH)^2
|
|
float rough = facing * (0.9 - 0.4 * facing) * ((0.5 + NdotH) / NdotH);
|
|
float transmitL = F_Transm_Schlick(0, NdotL);
|
|
float transmitV = F_Transm_Schlick(0, NdotV);
|
|
float smooth = transmitL * transmitV * 1.05; // Normalize F_t over the hemisphere
|
|
float single = lerp(smooth, rough, perceptualRoughness); // Rescaled by PI
|
|
// This constant is picked s.t. setting perceptualRoughness, albedo and all cosines to 1
|
|
// allows us to match the Lambertian and the Disney Diffuse models. Original value: 0.1159.
|
|
float multiple = perceptualRoughness * (0.079577 * PI); // Rescaled by PI
|
|
|
|
return single + albedo * multiple;
|
|
}
|
|
|
|
float3 DiffuseGGX(float3 albedo, float NdotV, float NdotL, float NdotH, float LdotV, float perceptualRoughness)
|
|
{
|
|
// Note that we could save 2 cycles by inlining the multiplication by INV_PI.
|
|
return INV_PI * DiffuseGGXNoPI(albedo, NdotV, NdotL, NdotH, LdotV, perceptualRoughness);
|
|
}
|
|
|
|
#endif // UNITY_BSDF_INCLUDED
|