Skip to content

Commit

Permalink
Clean up anisotropy code in GltfLoader and pbrLighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeshurun Hembd committed May 17, 2024
1 parent 5cd73a7 commit df61c61
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
14 changes: 5 additions & 9 deletions packages/engine/Source/Scene/GltfLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1543,9 +1543,9 @@ function loadMetallicRoughness(loader, metallicRoughnessInfo, frameState) {

function loadSpecular(loader, specularInfo, frameState) {
const {
specularFactor,
specularFactor = Specular.DEFAULT_SPECULAR_FACTOR,
specularTexture,
specularColorFactor,
specularColorFactor = Specular.DEFAULT_SPECULAR_COLOR_FACTOR,
specularColorTexture,
} = specularInfo;

Expand All @@ -1560,7 +1560,6 @@ function loadSpecular(loader, specularInfo, frameState) {
frameState
);
}
// TODO: if not defined, should we leave the default values?
specular.specularFactor = specularFactor;
specular.specularColorFactor = fromArray(Cartesian3, specularColorFactor);

Expand Down Expand Up @@ -1617,21 +1616,18 @@ function loadMaterial(loader, gltfMaterial, frameState) {
pbrSpecularGlossiness,
frameState
);
} else {
// TODO: should this branch exclude materials.unlit?
} else if (material.unlit === false) {
if (defined(pbrMetallicRoughness)) {
material.metallicRoughness = loadMetallicRoughness(
loader,
pbrMetallicRoughness,
frameState
);
}
if (defined(pbrSpecular)) {
// TODO: not compatible with materials.unlit
if (defined(pbrSpecular && material.unlit === false)) {
material.specular = loadSpecular(loader, pbrSpecular, frameState);
}
if (defined(pbrAnisotropy)) {
// TODO: not compatible with materials.unlit
if (defined(pbrAnisotropy) && material.unlit === false) {
material.anisotropy = loadAnisotropy(loader, pbrAnisotropy, frameState);
}
}
Expand Down
33 changes: 12 additions & 21 deletions packages/engine/Source/Shaders/Builtin/Functions/pbrLighting.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ vec3 fresnelSchlick2(vec3 f0, vec3 f90, float VdotH)
return f0 + (f90 - f0) * versineSquared * versineSquared * versine;
}

// TODO: add ifdef for GGX anisotropy methods
#ifdef USE_ANISOTROPY
/**
* @param {float} roughness Material roughness (along the anisotropy bitangent)
* @param {float} tangentialRoughness Anisotropic roughness (along the anisotropy tangent)
Expand Down Expand Up @@ -39,7 +39,7 @@ float GGX_anisotropic(float roughness, float tangentialRoughness, vec3 halfwayDi
float w2 = roughnessSquared / dot(f, f);
return roughnessSquared * w2 * w2 / czm_pi;
}

#else
float smithVisibilityG1(float NdotV, float roughness)
{
// this is the k value for direct lighting.
Expand All @@ -62,6 +62,7 @@ float GGX(float roughness, float NdotH)
float f = (NdotH * roughnessSquared - NdotH) * NdotH + 1.0;
return roughnessSquared / (czm_pi * f * f);
}
#endif

/**
* Compute the diffuse and specular contributions using physically based
Expand Down Expand Up @@ -106,23 +107,8 @@ vec3 czm_pbrLighting(
vec3 l = normalize(lightDirectionEC);
vec3 h = normalize(v + l);
vec3 n = normalEC;
float NdotL = clamp(dot(n, l), 0.001, 1.0);
float NdotV = abs(dot(n, v)) + 0.001;
float NdotH = clamp(dot(n, h), 0.0, 1.0);
float VdotH = clamp(dot(v, h), 0.0, 1.0);

#ifdef USE_ANISOTROPY
vec3 anisotropicT = pbrParameters.anisotropicT;
vec3 anisotropicB = pbrParameters.anisotropicB;
// TODO: multiply with TBN matrix to get anisotropic view, light, and half directions
float TdotV = dot(anisotropicT, v);
float BdotV = dot(anisotropicB, v);
float TdotH = dot(anisotropicT, h);
float BdotH = dot(anisotropicB, h);
float TdotL = dot(anisotropicT, l);
float BdotL = dot(anisotropicB, l);
#endif

vec3 f0 = pbrParameters.f0;
float reflectance = max(max(f0.r, f0.g), f0.b);
// Typical dielectrics will have reflectance 0.04, so f90 will be 1.0.
Expand All @@ -131,19 +117,24 @@ vec3 czm_pbrLighting(
vec3 F = fresnelSchlick2(f0, f90, VdotH);

#if defined(USE_SPECULAR)
F *= pbrParameters.specularWeight;
F *= pbrParameters.specularWeight;
#endif

float alpha = pbrParameters.roughness;
#ifdef USE_ANISOTROPY
mat3 tbn = mat3(pbrParameters.anisotropicT, pbrParameters.anisotropicB, n);
vec3 lightDirection = l * tbn;
vec3 viewDirection = v * tbn;
vec3 halfwayDirection = h * tbn;
float anisotropyStrength = pbrParameters.anisotropyStrength;
float tangentialRoughness = mix(alpha, 1.0, anisotropyStrength * anisotropyStrength);
vec3 lightDirection = vec3(TdotL, BdotL, NdotL);
vec3 viewDirection = vec3(TdotV, BdotV, NdotV);
float G = smithVisibilityGGX_anisotropic(alpha, tangentialRoughness, lightDirection, viewDirection);
vec3 halfwayDirection = vec3(TdotH, BdotH, NdotH);
float D = GGX_anisotropic(alpha, tangentialRoughness, halfwayDirection);
float NdotL = clamp(lightDirection.z, 0.001, 1.0);
#else
float NdotL = clamp(dot(n, l), 0.001, 1.0);
float NdotV = abs(dot(n, v)) + 0.001;
float NdotH = clamp(dot(n, h), 0.0, 1.0);
float G = smithVisibilityGGX(alpha, NdotL, NdotV);
float D = GGX(alpha, NdotH);
#endif
Expand Down

0 comments on commit df61c61

Please sign in to comment.