Skip to content

Commit

Permalink
Fix normal banding
Browse files Browse the repository at this point in the history
This fixes the visible issues in specular due to gbuffer encoding; of
course, if normal maps are encoded using block compression, those
artifacts tend to dominate.
  • Loading branch information
zeux committed Dec 11, 2024
1 parent 7f71e8b commit 0ca4a58
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/shaders/mesh.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ void main()
MeshDraw meshDraw = draws[drawId];
Material material = materials[meshDraw.materialIndex];

float deband = gradientNoise(gl_FragCoord.xy) * 2 - 1;

vec4 albedo = material.diffuseFactor;
if (material.albedoTexture > 0)
albedo *= fromsrgb(texture(SAMP(material.albedoTexture), uv));
Expand All @@ -81,7 +83,7 @@ void main()

// TODO: reconstruct metalness from specular texture
gbuffer[0] = vec4(tosrgb(albedo).rgb, log2(1 + emissivef) / 5);
gbuffer[1] = vec4(encodeOct(nrm) * 0.5 + 0.5, specgloss.a, 0.0);
gbuffer[1] = vec4(encodeOct(nrm) * 0.5 + 0.5 + deband * (0.5 / 1023), specgloss.a, 0.0);

if (POST > 0 && albedo.a < 0.5)
discard;
Expand Down
4 changes: 2 additions & 2 deletions src/shaders/shade.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ void main()

vec3 outputColor = albedo.rgb * (ndotl * min(shadow + shadowAmbient, 1.0) * sunIntensity + ambient) + vec3(specular * shadow) + emissive;

float deband = gradientNoise(vec2(pos));
imageStore(outImage, ivec2(pos), vec4(tosrgb(outputColor) + (deband * 2 - 1) * (0.5 / 255), 1.0));
float deband = gradientNoise(vec2(pos)) * 2 - 1;
imageStore(outImage, ivec2(pos), vec4(tosrgb(outputColor) + deband * (0.5 / 255), 1.0));
}

0 comments on commit 0ca4a58

Please sign in to comment.