#9512·engine

Proposal: specular anti-aliasing (normal map filtering and geometric AA)

Author: willeastcottCreated Sep 24, 2026Updated Sep 24, 2026
Labelsenhancementarea: graphics

Glossy materials alias badly in the engine, and there is currently no specular anti-aliasing of any kind. This proposes adding two complementary, opt-in techniques: normal map filtering (a GGX-compatible successor to the Toksvig support that was removed) and geometric specular anti-aliasing. It includes prototype measurements against a supersampled reference.

Supersedes #4260.

Background

Specular aliasing comes from detail that is smaller than a pixel:

  • Normal maps. Mipmapping averages the normals, so at a distance the bumps disappear and the surface turns into a mirror, with a narrow, over-bright glint. At mid distance, individual texels sparkle and crawl as the camera moves.
  • Small or thin curved geometry. Highlights narrower than a pixel are hit or missed from one frame to the next, and pop in and out. In the default LDR path clipping hides much of this, but in HDR pipelines (CameraFrame with bloom and TAA) these fireflies are amplified.

The engine used to have Toksvig filtering for normal maps. It was effectively disabled by #1968 (the normal is normalized before its length is measured) and was removed in v1.55 by #4254, along with the specularAntialias material property. Nothing replaced it.

Prototype results

Both techniques were prototyped with StandardMaterial shader chunk overrides (glossPS and normalMapPS), without engine changes. They were compared against a supersampled reference with 1024 samples per pixel (8x8 supersampling with a 4x4 jitter, radiance averaged before tone mapping), over a short loop of sub-pixel camera motion. The scenes are deliberately hard: metal, gloss 0.9, GGX, one directional light plus an environment atlas, rendered at 480x320 without MSAA.

Metrics:

  • RMSE: error against the reference in 8 bit sRGB units, lower is better.
  • Popping: how much the HDR energy in each 16x16 tile changes from frame to frame. A stable image stays close to the reference's level.
  • Brightness: mean tone mapped luminance relative to the reference.

Normal map filtering

Tiled normal map at a grazing angle, animated: reference, current engine, and normal map filtering

A tiled normal map on a large plane, viewed at a grazing angle into a sun glint. Today, the distance turns into a narrow mirror-like glint and the mid distance sparkles. With normal map filtering, the broad sheen of the reference is restored and the sparkle settles.

RMSE Popping Brightness
Current engine 94.5 55% -59%
Normal map filtering (proposed) 42.6 4.1% -9%
Reference - 3.9% 0%

Geometric specular AA

Small metal spheres at 2x zoom, animated: reference, current engine, and geometric specular AA

Small metal spheres, 1.5 to 6 pixels in radius, at 2x zoom. Today, most highlights are missed and some pop in for a single frame. Geometric AA keeps every highlight present and stable, at the cost of making them larger than in the reference.

Thin wires: reference, current engine, and geometric specular AA at thresholds 0.1 and 0.2

The trade-off on thin geometry. The filter is isotropic, so it also widens highlights along wires, which curve in only one direction. At Filament's default threshold of 0.2 the wires glow along their whole length. A threshold of 0.1 reduces the glow but does not remove it.

RMSE / popping / brightness:

Small spheres Thin cylinders
Current engine 7.7 / 99% / -7% 19.3 / 129% / -26%
Geometric AA, threshold 0.1 13.9 / 14% / +26% 24.3 / 52% / +38%
Geometric AA, threshold 0.2 20.4 / 5.7% / +52% 31.7 / 26% / +83%
Reference - / 6.0% / 0% - / 9.5% / 0%

Geometric AA removes the HDR popping. On these very glossy, very small objects it also increases the error against the reference, because highlights become larger and read brighter after tone mapping. The HDR energy itself is conserved (spheres: +10% today, +0.7% with AA). With environment lighting only, it reduced shimmer by 26%.

Other findings

  • Blinn-Phong. Geometric AA at 0.1 improved every metric on the spheres (RMSE 10.5 to 8.2, popping 88% to 22%, brightness -11% to +4%). Normal map filtering overshoots on Blinn-Phong (+56% brightness), and so does the engine's original Toksvig, restored as it worked before #1968: it removes the popping but renders 78% brighter than the reference. That matches the "better behaved, but also much brighter" observation in #4260.
  • Real normal maps are often not unit length. 7 of 13 normal maps sampled from examples/assets have 25-80% of their texels shorter than 0.99 at full resolution. For example, textures/normal-map.png has a median length of 0.955, and 48% of the MaterialsVariantsShoe JPEG's texels are below 0.99. Normal map filtering reads that shortening as variance, so a gloss 0.9 material would drop to around 0.57-0.75 even up close. 8 bit quantization alone (about 0.5% length error) is enough to pull gloss 0.9 down to about 0.77.

Proposal

Phase 1: normal map filtering

  • For mipmapped XYZ normal maps, derive a normal variance from the length r of the filtered, unnormalized sample using a vMF fit: kappa = (3r - r^3) / (1 - r^2) and variance = 1 / (4 kappa). This is the same as HDRP's TextureNormalVariance and Filament's roughness-prefilter. Scale it by bumpiness^2, then add min(2 * variance, 0.4^2) to the material's alpha^2.
  • Fade the term in by the mip level being sampled, computed from UV derivatives as parallax.js already does, so maps that are not unit length at full resolution don't roughen close-up surfaces. This part has not been prototyped yet.
  • XY, BC5 and basis normal maps are not supported, because their z is reconstructed and the length is always 1. They need offline prefiltering (see below).

Phase 2: geometric specular AA

  • Kaplanyan 2016 and Tokuyoshi and Kaplanyan 2019, as used by Filament and HDRP: variance = 0.15 * (|dn/dx|^2 + |dn/dy|^2) from the screen space derivatives of the interpolated vertex normal, then alpha^2 += min(2 * variance, threshold^2).
  • Default threshold of about 0.1 rather than Filament's 0.2 (Figure 3).
  • Its main value is removing HDR fireflies, which matters for CameraFrame bloom and TAA. In the default LDR path the visible gain is smaller.

Shared design

  • Opt-in per material, off by default, as in Filament, HDRP and Babylon.js. Supported by both StandardMaterial and LitMaterial.
  • A single gloss adjustment in the lit backend, applied before any lighting is evaluated. It works in alpha^2 space and converts back through the active BRDF's gloss mapping (GGX or Blinn-Phong). That way punctual lights, clustered and area lights, environment reflections and clearcoat all see it. The old Toksvig only affected Blinn-Phong punctual lights.
  • New property names. specularAntialias must not be revived: many Editor projects have it saved as true (the engine currently strips it on load), so reusing the name would silently switch the feature on for years-old content.
  • GLSL and WGSL chunks, chunk validation entries, an engine example with an on/off comparison, and Editor UI.
  • Cost: about two vec3 derivatives and 10 ALU operations for the geometric term, and one length and about 6 ALU operations for the normal map term. Both exist only in shader variants that enable them.

Later

  • Offline or load time prefiltering of normal map variance into the gloss map, like Filament's roughness-prefilter tool or Unreal's composite textures. This would also cover XY, BC5 and basis normal maps.
  • An anisotropic geometric filter (the forward variant from Tokuyoshi and Kaplanyan 2019) to avoid widening highlights along thin geometry.

Prerequisites

#9510 and #9511 should be fixed first. The GGX chunks' roughness mappings need to be correct, because the filter maps alpha^2 back through each BRDF.

Open questions

  • API shape: separate flags with variance and threshold parameters, as in Filament, or a single mode enum?
  • Should any of this eventually be on by default, for example for glTF imports?
  • Should Blinn-Phong be supported at all? Normal map filtering overshoots on it, while geometric AA behaves well.
  • Should normal map prefiltering live in the engine (at load time) or in the Editor's asset pipeline?
Prototype chunk overrides (engine 2.23, StandardMaterial, tested on WebGL2 only)
javascript
// Specular anti-aliasing prototype for StandardMaterial (engine 2.23, GLSL, tested on WebGL2 only).
// Replaces the engine's normalMapPS and glossPS chunks. Assumes pc is the engine namespace.
// Usage: enableSpecularAA(material);

const header = `
#define SAA_GEOMETRIC
#define SAA_TEXTURE
`;

// the engine's normalMapPS, which also measures the variance of the mip filtered normal map
const normalMapPS = /* glsl */`
#ifdef STD_NORMAL_TEXTURE
    uniform float material_bumpiness;
#endif

float saaTextureVariance = 0.0;

// vMF fit to the average normal: kappa = (3r - r^3) / (1 - r^2), variance = 1 / (4 kappa)
float saaNormalVariance(vec3 n) {
    float r = length(n);
    if (r >= 1.0) return 0.0;
    float r2 = r * r;
    float kappa = (3.0 * r - r * r2) / (1.0 - r2);
    return 0.25 / kappa;
}

#ifdef STD_NORMALDETAIL_TEXTURE
    uniform float material_normalDetailMapBumpiness;

    vec3 blendNormals(vec3 n1, vec3 n2) {
        n1 += vec3(0, 0, 1);
        n2 *= vec3(-1, -1, 1);
        return n1 * dot(n1, n2) / n1.z - n2;
    }
#endif

void getNormal() {
#ifdef STD_NORMAL_TEXTURE
    vec3 normalMap = {STD_NORMAL_TEXTURE_DECODE}(texture2DBias({STD_NORMAL_TEXTURE_NAME}, {STD_NORMAL_TEXTURE_UV}, {STD_TEXTURE_BIAS}));

    #ifdef SAA_TEXTURE
        saaTextureVariance = saaNormalVariance(normalMap) * material_bumpiness * material_bumpiness;
    #endif

    normalMap = mix(vec3(0.0, 0.0, 1.0), normalMap, material_bumpiness);

    #ifdef STD_NORMALDETAIL_TEXTURE
        vec3 normalDetailMap = {STD_NORMALDETAIL_TEXTURE_DECODE}(texture2DBias({STD_NORMALDETAIL_TEXTURE_NAME}, {STD_NORMALDETAIL_TEXTURE_UV}, {STD_TEXTURE_BIAS}));
        normalDetailMap = mix(vec3(0.0, 0.0, 1.0), normalDetailMap, material_normalDetailMapBumpiness);
        normalMap = blendNormals(normalMap, normalDetailMap);
    #endif

    dNormalW = normalize(dTBN * normalMap);
#else
    dNormalW = dVertexNormalW;
#endif
}
`;

// the engine's glossPS, which then widens the specular lobe by the measured normal variance
const glossPS = /* glsl */`
#ifdef STD_GLOSS_CONSTANT
uniform float material_gloss;
#endif

uniform float saa_variance;
uniform float saa_threshold2;
uniform float saa_texThreshold2;

// gloss to alpha^2 and back, for the active BRDF
float saaGlossToAlpha2(float gloss) {
    #ifdef LIT_GGX_SPECULAR
        float a = (1.0 - gloss) * (1.0 - gloss);
        return a * a;
    #else
        // Blinn-Phong exponent to the equivalent Beckmann alpha^2
        return 2.0 / (exp2(gloss * 11.0) + 2.0);
    #endif
}

float saaAlpha2ToGloss(float a2) {
    #ifdef LIT_GGX_SPECULAR
        return 1.0 - sqrt(sqrt(a2));
    #else
        return log2(max(2.0 / a2 - 2.0, 1.0)) / 11.0;
    #endif
}

void getGlossiness() {
    dGlossiness = 1.0;

    #ifdef STD_GLOSS_CONSTANT
    dGlossiness *= material_gloss;
    #endif

    #ifdef STD_GLOSS_TEXTURE
    dGlossiness *= texture2DBias({STD_GLOSS_TEXTURE_NAME}, {STD_GLOSS_TEXTURE_UV}, {STD_TEXTURE_BIAS}).{STD_GLOSS_TEXTURE_CHANNEL};
    #endif

    #ifdef STD_GLOSS_VERTEX
    dGlossiness *= saturate(vVertexColor.{STD_GLOSS_VERTEX_CHANNEL});
    #endif

    #ifdef STD_GLOSS_INVERT
    dGlossiness = 1.0 - dGlossiness;
    #endif

    dGlossiness += 0.0000001;

    float kernel = 0.0;

    // geometric: screen space variance of the vertex normal (Kaplanyan 2016, Tokuyoshi & Kaplanyan 2019)
    #ifdef SAA_GEOMETRIC
        vec3 du = dFdx(dVertexNormalW);
        vec3 dv = dFdy(dVertexNormalW);
        kernel += min(2.0 * saa_variance * (dot(du, du) + dot(dv, dv)), saa_threshold2);
    #endif

    // normal map: variance of the mip filtered normals
    #ifdef SAA_TEXTURE
        kernel += min(2.0 * saaTextureVariance, saa_texThreshold2);
    #endif

    dGlossiness = saaAlpha2ToGloss(saturate(saaGlossToAlpha2(dGlossiness) + kernel));
}
`;

const enableSpecularAA = (material) => {
    const chunks = material.getShaderChunks(pc.SHADERLANGUAGE_GLSL);
    chunks.set('normalMapPS', header + normalMapPS);
    chunks.set('glossPS', header + glossPS);
    material.shaderChunksVersion = '2.23';
    material.setParameter('saa_variance', 0.15);
    material.setParameter('saa_threshold2', 0.1 * 0.1);
    material.setParameter('saa_texThreshold2', 0.4 * 0.4);
    material.update();
};

References

  • Toksvig 2005, "Mipmapping Normal Maps"
  • Han et al. 2007, "Frequency Domain Normal Map Filtering"
  • Kaplanyan et al. 2016, "Filtering Distributions of Normals for Shading Antialiasing"
  • Tokuyoshi and Kaplanyan 2019, "Improved Geometric Specular Antialiasing"
  • Filament: surface_shading_lit.fs (normalFiltering) and tools/roughness-prefilter
  • Unity HDRP: CommonMaterial.hlsl (GeometricNormalFiltering, TextureNormalFiltering)