3D engine that AI agents drive
Cull the water mirror draw on the CPU: parts under the plane, coarser levels, sub-pixel parts (part of #316)
Nguyen Xuan An0e1b8652ffc0
diff --git a/src/render-culling.js b/src/render-culling.jsindex e238f125..8a38f731 100644--- a/src/render-culling.js+++ b/src/render-culling.js@@ -98,9 +98,10 @@ export class InstanceSelection {* Splits a batch's instances into the ones that touch the frustum, by level. `bounds` is the geometry's bounding* sphere, `levelErrors` the error of each level in geometry units (level 0 is 0), and `pixelsPerUnit` the pixels* that one world unit covers at depth 1 (half the canvas height times projection[5]). `margin` widens every sphere by that many- * world units, for vertices that move away from their geometry (wind sway).+ * world units, for vertices that move away from their geometry (wind sway). `belowHeight` drops an instance whose sphere lies wholly under+ * that world height, and `minimumPixelRadius` one whose sphere covers a smaller radius than that on screen (the water mirror uses both).*/-export function selectInstances(selection, { instanceData, instanceCount, stride, bounds, margin = 0, levelErrors, planes, viewProjection, pixelsPerUnit, pixelError = DEFAULT_LOD_PIXEL_ERROR }) {+export function selectInstances(selection, { instanceData, instanceCount, stride, bounds, margin = 0, levelErrors, planes, viewProjection, pixelsPerUnit, pixelError = DEFAULT_LOD_PIXEL_ERROR, belowHeight = -Infinity, minimumPixelRadius = 0 }) {selection.counts.fill(0);selection.visibleCount = 0;const hasLevels = levelErrors.length > 1;@@ -111,11 +112,13 @@ export function selectInstances(selection, { instanceData, instanceCount, strideconst y = instanceData[offset + 1] * centerX + instanceData[offset + 5] * centerY + instanceData[offset + 9] * centerZ + instanceData[offset + 13];const z = instanceData[offset + 2] * centerX + instanceData[offset + 6] * centerY + instanceData[offset + 10] * centerZ + instanceData[offset + 14];const worldScale = largestColumnLength(instanceData, offset);- if (isOutsideFrustum(planes, x, y, z, bounds.radius * worldScale + margin)) continue;+ const reach = bounds.radius * worldScale + margin;+ if (y + reach < belowHeight || isOutsideFrustum(planes, x, y, z, reach)) continue;let level = 0;- if (hasLevels) {+ if (hasLevels || minimumPixelRadius > 0) {const depth = viewProjection[3] * x + viewProjection[7] * y + viewProjection[11] * z + viewProjection[15];- level = depth > MINIMUM_DEPTH ? chooseLevel(levelErrors, worldScale, pixelsPerUnit / depth, pixelError) : 0;+ if (depth > MINIMUM_DEPTH && reach * pixelsPerUnit / depth < minimumPixelRadius) continue;+ level = hasLevels && depth > MINIMUM_DEPTH ? chooseLevel(levelErrors, worldScale, pixelsPerUnit / depth, pixelError) : 0;}selection.indices[level][selection.counts[level]++] = index;selection.visibleCount++;diff --git a/src/renderer.js b/src/renderer.jsindex 8ec77ca2..c7175fe1 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -970,7 +970,8 @@ export class Renderer67 {return selectInstances(batch.selection, {instanceData: batch.instanceData, instanceCount: batch.instanceCount, stride: INSTANCE_STRIDE, bounds: batch.bounds, margin: batch.swayReach * Math.abs(this.windState.strength),levelErrors: batch.levelErrors, planes: this.frustumPlanes, viewProjection: this.viewProjection,- pixelsPerUnit: this.canvas.height / 2 * this.projection[5], pixelError: this.lodPixelError,+ pixelsPerUnit: this.canvas.height / 2 * this.projection[5], pixelError: this.lodPixelError * (this.mirrorCulling?.lodScale ?? 1),+ belowHeight: this.mirrorCulling?.belowHeight, minimumPixelRadius: this.mirrorCulling?.minimumPixelRadius,});}diff --git a/src/water-reflection.js b/src/water-reflection.jsindex 82d99026..a11abdb5 100644--- a/src/water-reflection.js+++ b/src/water-reflection.js@@ -14,6 +14,11 @@ export const WATER_MAXIMUM_THICKNESS_RATIO = 0.02;export const WATER_MINIMUM_SCENE_SHARE = 0.002;/** The reflection target is this share of the canvas in each direction. */export const WATER_REFLECTION_SCALE = 0.75;+// The mirror draw is blurred and rippled, so it can use coarser levels and drop parts that cover almost nothing.+/** The mirror draw accepts this many times the level error that the lit frame accepts (`lodPixelError`). */+export const WATER_MIRROR_LOD_SCALE = 3;+/** The mirror draw skips a part whose bounding sphere covers a smaller radius than this on screen, in pixels. */+export const WATER_MIRROR_MINIMUM_PIXEL_RADIUS = 1.5;// How the picture is treated before it reaches the surface (see afterEmission). Fresnel starts at the reflectance of water.const FRESNEL_BASE = '0.02';const REFLECTION_GAIN = '1.0';@@ -189,9 +194,12 @@ function drawMirrorImage(renderer, state, frame, renderFrame) {renderer.context.bindTexture(renderer.context.TEXTURE_2D, null);renderer.context.activeTexture(renderer.context.TEXTURE0);state.drawing = true;+ // Parts wholly under the plane are discarded by the shader anyway, so the renderer leaves them out of the draw.+ renderer.mirrorCulling = { belowHeight: state.height + CLIP_MARGIN, lodScale: WATER_MIRROR_LOD_SCALE, minimumPixelRadius: WATER_MIRROR_MINIMUM_PIXEL_RADIUS };try {renderFrame(mirroredFrame(frame, state.height, state.target));} finally {+ renderer.mirrorCulling = null;state.drawing = false;}state.matrix.set(renderer.viewProjection);diff --git a/tests/render-culling.test.js b/tests/render-culling.test.jsindex f76e4a00..0da56d25 100644--- a/tests/render-culling.test.js+++ b/tests/render-culling.test.js@@ -24,12 +24,12 @@ function instanceData(positions, scale = 1) {return data;}-function selectionFor(positions, { levelErrors = [0], bounds = { center: [0, 0, 0], radius: 1 }, eye, scale } = {}) {+function selectionFor(positions, { levelErrors = [0], bounds = { center: [0, 0, 0], radius: 1 }, eye, scale, ...extra } = {}) {const { projection, viewProjection, planes } = cameraMatrices(eye);const selection = new InstanceSelection(positions.length, levelErrors.length);return selectInstances(selection, {instanceData: instanceData(positions, scale), instanceCount: positions.length, stride: STRIDE, bounds, levelErrors, planes, viewProjection,- pixelsPerUnit: CANVAS_HEIGHT / 2 * projection[5],+ pixelsPerUnit: CANVAS_HEIGHT / 2 * projection[5], ...extra,});}@@ -212,3 +212,35 @@ test('swaying foliage is culled and given shadow levels, with a bound that growsassert.equal(select(0), 0);assert.equal(select(4), 1);});++test('an instance wholly under belowHeight is culled and one that reaches above it stays', () => {+ const positions = [[0, -3, 0], [1, 0.5, 0], [-1, -0.5, 0]];+ assert.equal(selectionFor(positions).visibleCount, 3);+ const selection = selectionFor(positions, { belowHeight: 0 });+ assert.deepEqual([...selection.indices[0].subarray(0, selection.counts[0])], [1, 2]);+ assert.equal(selectionFor(positions, { belowHeight: 0, scale: 4 }).visibleCount, 3, 'a larger sphere reaches the plane');+});++test('an instance smaller than minimumPixelRadius on screen is culled, and a near one stays', () => {+ const positions = [[0, 0, 0], [0, 0, -80]];+ assert.equal(selectionFor(positions).visibleCount, 2);+ const selection = selectionFor(positions, { minimumPixelRadius: 10 });+ assert.deepEqual([...selection.indices[0].subarray(0, selection.counts[0])], [0]);+});++test('the renderer lets the water mirror widen the level error, and a normal frame is unchanged', () => {+ const { projection, viewProjection, planes } = cameraMatrices();+ const renderer = Object.create(Renderer67.prototype);+ Object.assign(renderer, { culling: true, canvas: { height: CANVAS_HEIGHT }, projection, viewProjection, frustumPlanes: planes, lodPixelError: 1, windState: { strength: 0 } });+ const batch = { cullable: true, selection: new InstanceSelection(2, 2), instanceData: instanceData([[0, -5, 0], [0, 1, 0]]), instanceCount: 2, bounds: { center: [0, 0, 0], radius: 1 }, swayReach: 0, levelErrors: [0, 0.02] };+ const levelOfSecond = () => {+ const selection = renderer.selectBatchInstances(batch);+ return selection.indices[1][0] === 1 ? 1 : 0;+ };+ assert.equal(renderer.selectBatchInstances(batch).visibleCount, 2);+ assert.equal(levelOfSecond(), 0);+ renderer.mirrorCulling = { belowHeight: 0, lodScale: 100, minimumPixelRadius: 0 };+ const mirrored = renderer.selectBatchInstances(batch);+ assert.equal(mirrored.visibleCount, 1);+ assert.equal(levelOfSecond(), 1);+});