Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

fix(ao): unproject ambient occlusion samples in view space (#713) (#729)

Nguyen Xuan An4e066c94c840
diff --git a/changes/713.md b/changes/713.md
new file mode 100644
index 00000000..8b3b2927
--- /dev/null
+++ b/changes/713.md
@@ -0,0 +1 @@
+- Unprojected ambient occlusion samples in view space rather than world space so cameras far from the origin do not quantize distant wall normals into vertical stripes (#713).
diff --git a/src/ambient-occlusion.js b/src/ambient-occlusion.js
index 9d0679e8..401fc564 100644
--- a/src/ambient-occlusion.js
+++ b/src/ambient-occlusion.js
@@ -10,7 +10,6 @@ precision highp float;
in vec2 textureCoordinate;
uniform sampler2D sceneDepth;
uniform mat4 inverseViewProjection;
-uniform vec3 eye;
uniform float radius;
uniform float projectionScale;
uniform float aspect;
@@ -34,9 +33,8 @@ void main() {
}
vec3 position = worldPosition(textureCoordinate, depth);
vec3 normal = normalize(cross(dFdx(position), dFdy(position)));
- if (orthographic < 0.5 && dot(normal, eye - position) < 0.0) normal = -normal;
- float distanceToEye = orthographic > 0.5 ? 1.0 : length(eye - position);
- float screenRadius = min(radius * projectionScale / distanceToEye, 0.25);
+ if (orthographic < 0.5 && dot(normal, position) > 0.0) normal = -normal;
+ float screenRadius = min(radius * projectionScale / (orthographic > 0.5 ? 1.0 : length(position)), 0.25);
float rotation = interleavedNoise(gl_FragCoord.xy) * 6.2831853;
float total = 0.0;
for (int index = 0; index < ${AMBIENT_OCCLUSION_SAMPLES}; index++) {
diff --git a/src/renderer.js b/src/renderer.js
index debd3b20..3e6a7e6e 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -1594,7 +1594,7 @@ export class Renderer67 {
const context = this.context;
if (!this.occlusionProgram) {
this.occlusionProgram = compileProgram(context, fullscreenVertex, ambientOcclusionFragment);
- const names = ['sceneDepth', 'inverseViewProjection', 'eye', 'radius', 'projectionScale', 'aspect', 'orthographic'];
+ const names = ['sceneDepth', 'inverseViewProjection', 'radius', 'projectionScale', 'aspect', 'orthographic'];
this.occlusionUniforms = Object.fromEntries(names.map(name => [name, context.getUniformLocation(this.occlusionProgram, name)]));
}
if (this.occlusionTarget) return;
@@ -1627,8 +1627,7 @@ export class Renderer67 {
context.activeTexture(context.TEXTURE1);
context.bindTexture(context.TEXTURE_2D, this.depthTexture);
context.uniform1i(uniforms.sceneDepth, 1);
- context.uniformMatrix4fv(uniforms.inverseViewProjection, false, this.inverseViewProjection);
- context.uniform3fv(uniforms.eye, frame.eye);
+ context.uniformMatrix4fv(uniforms.inverseViewProjection, false, mat4.invert([], this.projection));
context.uniform1f(uniforms.radius, effects.ambientOcclusionRadius);
context.uniform1f(uniforms.projectionScale, 0.5 * this.projection[5]);
context.uniform1f(uniforms.aspect, this.canvas.width / this.canvas.height);
diff --git a/tests/renderer.test.js b/tests/renderer.test.js
index 6d6bdd9e..453ff86c 100644
--- a/tests/renderer.test.js
+++ b/tests/renderer.test.js
@@ -627,3 +627,41 @@ test('resize keeps the projection centered at 699 px and 1440 px', () => {
assert.equal(renderer.projection[8], 0);
}
});
+
+test('drawAmbientOcclusion unprojects in view space so far camera coordinates do not quantize distant normals', () => {
+ const uploaded = {};
+ const renderer = Object.create(Renderer67.prototype);
+ renderer.canvas = { width: 540, height: 960 };
+ renderer.depthTexture = 'depth';
+ renderer.projection = mat4.perspective(mat4.create(), (52 * Math.PI) / 180, 540 / 960, 0.05, 400);
+ renderer.view = mat4.lookAt(mat4.create(), [6000.3, 1.0, 9.2], [6000.0, 1.4, -20.0], [0, 1, 0]);
+ renderer.viewProjection = mat4.multiply(mat4.create(), renderer.projection, renderer.view);
+ renderer.inverseViewProjection = mat4.invert(mat4.create(), renderer.viewProjection);
+ renderer.occlusionTarget = { texture: 'ao', framebuffer: 'fbo', width: 270, height: 480 };
+ renderer.occlusionProgram = 'program';
+ renderer.occlusionUniforms = {
+ sceneDepth: 'sceneDepth', inverseViewProjection: 'inverseViewProjection',
+ radius: 'radius', projectionScale: 'projectionScale', aspect: 'aspect', orthographic: 'orthographic',
+ };
+ renderer.context = {
+ FRAMEBUFFER: 1, TEXTURE_2D: 2, TEXTURE1: 3, DEPTH_TEST: 4, CULL_FACE: 5, TRIANGLES: 6,
+ bindFramebuffer() {}, viewport() {}, disable() {}, bindVertexArray() {}, useProgram() {}, activeTexture() {}, bindTexture() {},
+ uniform1i(name, value) { uploaded[name] = value; },
+ uniform1f(name, value) { uploaded[name] = value; },
+ uniformMatrix4fv(name, transpose, value) { uploaded[name] = new Float32Array(value); },
+ drawArrays() {},
+ };
+ renderer.drawAmbientOcclusion({ eye: [6000.3, 1.0, 9.2], projection: 'perspective' }, { ambientOcclusionRadius: 0.5 });
+ const matrix = uploaded.inverseViewProjection;
+ const clipZ = (-renderer.projection[10] * 129.2 + renderer.projection[14]) / 129.2;
+ const unprojectX = (column) => {
+ const clipX = ((column + 0.5) / 270) * 2 - 1;
+ const x = Math.fround(Math.fround(Math.fround(matrix[0] * clipX) + Math.fround(matrix[8] * clipZ)) + matrix[12]);
+ const w = Math.fround(Math.fround(Math.fround(matrix[3] * clipX) + Math.fround(matrix[11] * clipZ)) + matrix[15]);
+ return Math.fround(x / w);
+ };
+ for (let column = 0; column < 269; column++) {
+ const step = unprojectX(column + 1) - unprojectX(column);
+ assert.ok(step > 0.25 && step < 0.27, `column ${column} step ${step} must not quantize to zero`);
+ }
+});