Changed normal of the ground to GL, Now the normal mapping is correct.

This commit is contained in:
2026-01-04 16:11:18 +01:00
parent 296aad3f84
commit b417f016af
20 changed files with 985 additions and 254 deletions

View File

@@ -150,7 +150,7 @@ vec3 fixCubemapLookup(vec3 v, float lod) {
vec3 evaluateSpecularIBL(vec3 r, float roughness) {
float lod = 5.0 * roughness;
float lod = clamp(5.0 * roughness, 0.0, 8.0);
r = fixCubemapLookup(r, lod);
@@ -165,91 +165,81 @@ vec3 getSpecularDominantDirection(vec3 n, vec3 r, float roughness) {
float s = 1.0 - roughness;
return mix(n, r, s * (sqrt(s) + roughness));
}
vec3 indirectLight( deferredMaterialData materialData ) { //, deferredLightData materialData
vec3 indirectLight( deferredMaterialData materialData ) {
vec3 normal = materialData.normal;
vec3 vertex = materialData.vertex;
float roughness = materialData.roughness;
float NoV = max( dot(normal, vertex), 0.0 );
vec3 vertex = materialData.vertex;
float roughness = clamp( materialData.roughness, 0.0, 1.0 );
float NoV = max( dot( normal, vertex ), 0.0 );
#if ANISOTROPY == 1
vec3 t = normalize( materialData.tangent );
vec3 b = normalize( cross(t, normal));
vec3 t = normalize( materialData.tangent );
vec3 b = normalize( cross( t, normal ) );
vec3 anisotropicTangent = cross(-vertex, b);
vec3 anisotropicNormal = cross(anisotropicTangent, b);
vec3 bentNormal = normalize(mix(normal, anisotropicNormal, anisotropy));
vec3 anisotropicTangent = cross( -vertex, b );
vec3 anisotropicNormal = cross( anisotropicTangent, b );
vec3 bentNormal = normalize( mix( normal, anisotropicNormal, anisotropy ) );
vec3 r = reflect( vertex, bentNormal );
vec3 r = reflect( vertex, bentNormal );
#else
vec3 r = reflect( -vertex, normal );
r = getSpecularDominantDirection(normal, r, roughness * roughness);
vec3 r = reflect( -vertex, normal );
// Use perceptual roughness here (not squared)
r = getSpecularDominantDirection( normal, r, roughness );
#endif
float NoR = max( dot(r, normal), 0.0);
float NoR = max( dot( r, normal ), 0.0 );
// specular indirect
vec3 indirectSpecular = evaluateSpecularIBL( reflect(vertex, normal), roughness);
// Specular indirect (USE r, not reflect(-vertex, normal) again)
vec3 indirectSpecular = evaluateSpecularIBL( r, roughness );
// horizon occlusion, can be removed for performance
//float horizon = min(1.0 + NoR, 1.0);
//indirectSpecular *= horizon * horizon;
// Optional but usually helps reduce “edge glitter” on grazing angles
indirectSpecular *= clamp( 1.0 + NoR, 0.0, 1.0 );
vec2 env = prefilteredDFGKaris( NoV, materialData.roughness );
// we should multiply env.y by f90 for more accurate results
vec3 specularColor = materialData.specularReflectance * env.x + env.y * (1.0 - materialData.clearCoat) *
clamp(dot(materialData.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0);
vec2 env = prefilteredDFGKaris( NoV, roughness );
vec3 specularColor =
materialData.specularReflectance * env.x +
env.y * ( 1.0 - materialData.clearCoat ) *
clamp( dot( materialData.specularReflectance, vec3( 50.0 * 0.33 ) ), 0.0, 1.0 );
vec3 indirectDiffuse = max( irradianceSH( normal ), 0.0 ) * Fd_Lambert();
float ambientOcclusionFade = clamp( dot( normalize( normal ), vertex ), 0.0, 1.0 );
float ambientOcclusion = mix( 1.0, materialData.ambientOcclusion, ambientOcclusionFade );
// diffuse indirect
vec3 indirectDiffuse = max(irradianceSH(normal), 0.0) * Fd_Lambert();
// ambient occlusion
float ambientOcclusionFade = clamp(dot(normalize(normal), vertex), 0.0, 1.0);
float ambientOcclusion = mix(1.0, materialData.ambientOcclusion, ambientOcclusionFade);
indirectDiffuse *= ambientOcclusion;
//indirectDiffuse *= 4.0;
// TODO: Not really useful without SSambientOcclusion/HBambientOcclusion/etc.
indirectSpecular *= computeSpecularambientOcclusion(NoV, ambientOcclusion, materialData.roughness);
// clear coat
float Fcc = F_Schlick_Scalar(NoV, 0.04, 1.0) * 0.2;
#if ANISOTROPY == 1
// We used the bent normal for the base layer
r = reflect(-vertex, normal);
#endif
indirectSpecular *= computeSpecularambientOcclusion( NoV, ambientOcclusion, roughness );
float Fcc = F_Schlick_Scalar( NoV, 0.04, 1.0 ) * 0.2;
vec3 indirectClearCoatSpecular = evaluateSpecularIBL(reflect(vertex, normal), materialData.clearCoatRoughness);
vec3 indirectClearCoatSpecular =
evaluateSpecularIBL( reflect( vertex, normal ), clamp( materialData.clearCoatRoughness, 0.0, 1.0 ) );
vec3 clearCoatAbsorption = mix(vec3(1.0),
beerLambert(materialData.refracted_NoV, materialData.refracted_NoV, materialData.clearCoatColor, materialData.clearCoatThickness),
materialData.clearCoat);
vec3 clearCoatAbsorption = mix(
vec3( 1.0 ),
beerLambert(
materialData.refracted_NoV,
materialData.refracted_NoV,
materialData.clearCoatColor,
materialData.clearCoatThickness
),
materialData.clearCoat
);
// indirect contribution (clear coat)
vec3 color =
(materialData.diffuse * indirectDiffuse + indirectSpecular * specularColor)//kaj materialData.diffuse * indirectDiffuse
* (1.0 - Fcc) * clearCoatAbsorption +
indirectClearCoatSpecular * Fcc;
vec3 color =
( materialData.diffuse * indirectDiffuse + indirectSpecular * specularColor ) *
( 1.0 - Fcc ) * clearCoatAbsorption +
indirectClearCoatSpecular * Fcc;
#if TRANSLUCENT_MATERIAL == 1
indirectDiffuse = max(irradianceSH(-vertex), 0.0) * Fd_Lambert();
vec3 tL = -vertex + normal * translucencyDistortion;
float tD = pow(clamp(dot(vertex, -tL), 0.0, 1.0), translucencyPower) * translucencyScale;
vec3 tT = (tD + translucencyAmbient) * texture(translucencyThicknessMap, outUV).r;
color.rgb += materialData.diffuse * indirectDiffuse * tT;
#endif
return color;
return color;
}
vec3 getNormal(vec3 n) {
return normalize(n);
@@ -271,6 +261,9 @@ vec3 directLight( deferredMaterialData materialData, deferredLightData lightData
vec3 lightDir = normalize(lightData.direction);
float linearRoughness = materialData.roughness * materialData.roughness;
vec3 r = reflect(-v, n);
if (lightType == 1.0) {
@@ -327,6 +320,7 @@ vec3 directLight( deferredMaterialData materialData, deferredLightData lightData
float D = D_GGX(NoH, linearRoughness);
#endif
vec3 F = F_Schlick(LoH, materialData.specularReflectance, clamp(dot(materialData.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0));
float V = V_SmithGGXCorrelated(NoV, NoL, linearRoughness);
vec3 Fr = (D * V) * F;