Ssao 2 has different results each time i add it to the scene ( video showing it )

SSAO won’t really work with a number of samples = 1 as you set in the PG.

The bug occurs when the sample generated by the SSAO class has a very low Z component.

For eg, setting explicitely a (0.707, 0.707, 0) value (see line 64):

Setting more samples will improve things. For eg, with 15 samples (and changing a bit the SSAO properties so that the effect is visible):

However, generating low Z components can still happen for some samples, and even if the artefacts are less visible because of the number of samples I’m not sure it is what we should expect.

Looking at this code in the SSAO2RenderingPipeline class:

private _hemisphereSample_uniform(u: number, v: number): Vector3 {
    const phi = v * 2.0 * Math.PI;
    // rejecting samples that are close to tangent plane to avoid z-fighting artifacts
    const cosTheta = 1.0 - (u * 0.85 + 0.15);
    const sinTheta = Math.sqrt(1.0 - cosTheta * cosTheta);
    return new Vector3(Math.cos(phi) * sinTheta, Math.sin(phi) * sinTheta, cosTheta);
}

Given the comment in the code and my findings above, I would compute cosTheta as cosTheta = 1.0 - u * 0.85 instead, to avoid being too close to 0.

@CraigFeldspar what do you think?

3 Likes