Per vertex ambient occlusion

Hello!

I was wondering if anyone had a method of achieving dynamic ambient occlusion using vertex colors, rather than something screen space or complicated.

I believe Minecraft does something similar, maybe…?

Minecraft is a special case since it’s voxel based, so the vertex occlusion comes down to a simple table based on the nearest block neighborhood.

Arbitrary meshes don’t have any of that, unless you build some kind of structure to represent it.

You can do something simple like labelling the edges with the occlusion of its shared faces.. but it would likely be pretty coarse.

Check out Inigo Quilez articles on this kind of stuff though:

(And really.. check out all his articles.. the dude is a master of his craft.)

If your geometry is a voxel grid or a watertight architectural/procedural mesh where storing an extra UV2 lightmap texture is overkill, you can bake per-vertex ambient occlusion directly into a custom ao buffer attribute (or color attribute) and multiply diffuseColor.rgb / reflectedLight.indirectDiffuse inside onBeforeCompile (or a MeshStandardNodeMaterial TSL aoNode in WebGPU):

// 1. Attach per-vertex AO float32 array [0.0 .. 1.0] to BufferGeometry
geometry.setAttribute('aVertexAO', new THREE.BufferAttribute(aoFloat32Array, 1));

// 2. Inject into MeshStandardMaterial without requiring a UV2 texture
material.onBeforeCompile = (shader) => {
  shader.vertexShader = 'attribute float aVertexAO;\nvarying float vVertexAO;\n' + shader.vertexShader
    .replace('#include <begin_vertex>', '#include <begin_vertex>\nvVertexAO = aVertexAO;');
  shader.fragmentShader = 'varying float vVertexAO;\n' + shader.fragmentShader
    .replace('#include <aomap_fragment>', 'reflectedLight.indirectDiffuse *= vVertexAO;');
};

To avoid diagonal interpolation artifacts on quad faces where one corner has dark AO (0.2) and the opposite three have 1.0, flip the quad triangulation diagonal so the shared edge connects the two vertices with the closest AO values.