Skip to content

Instantly share code, notes, and snippets.

float D_GGX_Anisotropic(float at, float ab, float ToH, float BoH, float NoH) {
// Burley 2012, "Physically-Based Shading at Disney"
float a2 = at * ab;
vec3 d = vec3(ab * ToH, at * BoH, a2 * NoH);
return saturateMediump(a2 * sq(a2 / dot(d, d)) * (1.0 / PI));
}
float V_SmithGGXCorrelated_Anisotropic(float at, float ab, float ToV, float BoV,
float ToL, float BoL, float NoV, float NoL) {
// Heitz 2014, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs"
@romainguy
romainguy / anisotropic_ibl.glsl
Last active February 9, 2023 02:48
Anisotropic IBL
vec3 anisotropyDirection = pixel.anisotropy >= 0.0 ? pixel.anisotropicB : pixel.anisotropicT;
vec3 anisotropicTangent = cross(anisotropyDirection, shading_view);
vec3 anisotropicNormal = cross(anisotropicTangent, anisotropyDirection);
float bendFactor = abs(pixel.anisotropy) * saturate(5.0 * pixel.roughness);
vec3 bentNormal = normalize(mix(shading_normal, anisotropicNormal, bendFactor));
vec3 r = reflect(-shading_view, bentNormal);
@romainguy
romainguy / d_ggx.glsl
Last active October 26, 2023 06:18
D_GGX in mediump/half float
// From https://github.com/google/filament
float D_GGX(float linearRoughness, float NoH, const vec3 h) {
// Walter et al. 2007, "Microfacet Models for Refraction through Rough Surfaces"
// In mediump, there are two problems computing 1.0 - NoH^2
// 1) 1.0 - NoH^2 suffers floating point cancellation when NoH^2 is close to 1 (highlights)
// 2) NoH doesn't have enough precision around 1.0
// Both problem can be fixed by computing 1-NoH^2 in highp and providing NoH in highp as well
// However, we can do better using Lagrange's identity:
@romainguy
romainguy / sh.glsl
Last active April 22, 2024 10:36
Spherical Harmonics lighting
// Uniform array of 4 vec3 for SH environment lighting
// Computed from "Ditch River" (http://www.hdrlabs.com/sibl/archive.html)
static const vec3 sphericalHarmonics[4] = {
{ 0.754554516862612, 0.748542953903366, 0.790921515418539 },
{ -0.083856548007422, 0.092533500963210, 0.322764661032516 },
{ 0.308152705331738, 0.366796330467391, 0.466698181299906 },
{ -0.188884931542396, -0.277402551592231, -0.377844212327557 }
};
// Fragment shader, "n" is the normal at the shading point