Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Cut worldBounds and per-texture anisotropy cost on large scenes (part of #406)

Nguyen Xuan Anee9e18f45f96
diff --git a/changes/406-raster.md b/changes/406-raster.md
new file mode 100644
index 00000000..7f8a4085
--- /dev/null
+++ b/changes/406-raster.md
@@ -0,0 +1 @@
+- `worldBounds` (used by `applyWaterReflection`, `findWaterSurface` and the probe code) keeps its minimum and maximum in scalars instead of arrays indexed by axis, and the renderer reads the anisotropy limit once instead of once per texture (issue #406). On the AIS port film, `applyWaterReflection` falls from about 160 ms to 80 ms and a 24-megapixel texture upload from about 340 ms to 300 ms in total; the bounds are bit-equal and the frame is pixel-identical.
diff --git a/docs/terrain.md b/docs/terrain.md
index 1f9d5dbe..9607b7ca 100644
--- a/docs/terrain.md
+++ b/docs/terrain.md
@@ -117,6 +117,8 @@ const geometry = createHeightfieldGeometry({ ...lines, heights: Float32Array.fro
Load images as `createImageBitmap(await (await fetch(url)).blob(), { colorSpaceConversion: 'none', premultiplyAlpha: 'none' })` and pass the bitmap as a texture `image`. The renderer accepts it, it decodes off the main thread, and `texImage2D` from a bitmap takes about 0.2 of the time it takes from an `Image` (measured on 24-megapixel JPEGs: 64 ms against 300 ms), with identical pixels. The renderer also starts every program before it uploads textures and buffers, so the driver links them while the upload runs.
+Where the rest of the port's load goes (marks in a scratch copy, ms from navigation, quiet run of 2.7 to 3.4 s against 1.1 to 1.2 s for three.js): images and heights decoded by 0.65 s, four mask readbacks done by 0.94 s (each 24-megapixel image costs about 20 ms to draw, 37 ms to read and 19 ms to copy the channel, all in the browser), meshes by 1.5 s (the two raster passes are at about 50 ms per 24-megapixel mask in plain JavaScript, so a summed-area table would not help: the bilinear weights differ per vertex), buildings by 1.97 s, `applyWaterReflection` by 2.06 s, the renderer constructor by 2.9 s. The port runs these one after the other: reading each mask as its image arrives, starting the building fetches with the images, and building the three tiers while the next tier's mask reads are the steps still open, and they live in the scene's code.
+
## Navigation
`addons/navmesh` finds walking routes over any triangle ground, so a character can go around holes, steep slopes and obstacles. It is pure JavaScript and gives the same path for the same input.
diff --git a/src/probe-trace.js b/src/probe-trace.js
index e8e8697a..a59afcc0 100644
--- a/src/probe-trace.js
+++ b/src/probe-trace.js
@@ -172,16 +172,29 @@ export function normalizeProbeList(list) {
export function worldBounds(part) {
const { positions } = part.geometry;
const { matrix } = part;
- const minimum = [Infinity, Infinity, Infinity];
- const maximum = [-Infinity, -Infinity, -Infinity];
+ const [xx, xy, xz, , yx, yy, yz, , zx, zy, zz, , tx, ty, tz] = matrix;
+ let minimumX = Infinity;
+ let minimumY = Infinity;
+ let minimumZ = Infinity;
+ let maximumX = -Infinity;
+ let maximumY = -Infinity;
+ let maximumZ = -Infinity;
+ // Scalars, not arrays indexed by axis: this runs over every vertex of every part, millions of them in a terrain scene.
for (let vertex = 0; vertex < positions.length; vertex += 3) {
- for (let axis = 0; axis < 3; axis++) {
- const value = matrix[axis] * positions[vertex] + matrix[4 + axis] * positions[vertex + 1] + matrix[8 + axis] * positions[vertex + 2] + matrix[12 + axis];
- minimum[axis] = Math.min(minimum[axis], value);
- maximum[axis] = Math.max(maximum[axis], value);
- }
+ const x = positions[vertex];
+ const y = positions[vertex + 1];
+ const z = positions[vertex + 2];
+ const worldX = xx * x + yx * y + zx * z + tx;
+ const worldY = xy * x + yy * y + zy * z + ty;
+ const worldZ = xz * x + yz * y + zz * z + tz;
+ minimumX = Math.min(minimumX, worldX);
+ minimumY = Math.min(minimumY, worldY);
+ minimumZ = Math.min(minimumZ, worldZ);
+ maximumX = Math.max(maximumX, worldX);
+ maximumY = Math.max(maximumY, worldY);
+ maximumZ = Math.max(maximumZ, worldZ);
}
- return { minimum, maximum };
+ return { minimum: [minimumX, minimumY, minimumZ], maximum: [maximumX, maximumY, maximumZ] };
}
export function boundsCentre({ minimum, maximum }) {
diff --git a/src/renderer.js b/src/renderer.js
index 84f04b88..2dcc6218 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -739,10 +739,14 @@ export class Renderer67 {
// blurred level long before its detail is gone. Applies to the texture bound to `target`.
applyAnisotropy(target) {
const context = this.context;
- const extension = context.getExtension('EXT_texture_filter_anisotropic');
- if (!extension) return;
- const supported = context.getParameter(extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT);
- context.texParameterf(target, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(MAX_TEXTURE_ANISOTROPY, supported));
+ // Asked once: reading the limit makes the browser wait for the GPU process, which would stall behind every texture upload before it.
+ if (!this.anisotropyExtension) {
+ const extension = context.getExtension('EXT_texture_filter_anisotropic');
+ if (!extension) return;
+ this.anisotropyExtension = extension;
+ this.anisotropyLevel = Math.min(MAX_TEXTURE_ANISOTROPY, context.getParameter(extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT));
+ }
+ context.texParameterf(target, this.anisotropyExtension.TEXTURE_MAX_ANISOTROPY_EXT, this.anisotropyLevel);
}
createTexture(map) {
diff --git a/tests/water-reflection.test.js b/tests/water-reflection.test.js
index cb378180..a7609d5c 100644
--- a/tests/water-reflection.test.js
+++ b/tests/water-reflection.test.js
@@ -224,3 +224,25 @@ test('the shader samples a mip level taken from roughness', () => {
assert.match(source, /float mirrorLevel = 5\.0 \* clamp\(\(roughness - 0\.10\) \/ 0\.40, 0\.0, 1\.0\)/);
assert.match(source, /textureLod\(waterReflection,/);
});
+
+test('worldBounds equals the per-axis formula it replaced, under a rotated and moved matrix', async () => {
+ const { worldBounds } = await import('../src/probe-trace.js');
+ const positions = new Float32Array(3 * 500);
+ let state = 11;
+ for (let index = 0; index < positions.length; index += 1) {
+ state = (state * 1664525 + 1013904223) >>> 0;
+ positions[index] = (state / 2 ** 32 - 0.5) * 400;
+ }
+ const angle = 0.7;
+ const matrix = [Math.cos(angle), 0, -Math.sin(angle), 0, 0, 1.5, 0, 0, Math.sin(angle), 0, Math.cos(angle), 0, 120.25, -3.5, 40];
+ const minimum = [Infinity, Infinity, Infinity];
+ const maximum = [-Infinity, -Infinity, -Infinity];
+ for (let vertex = 0; vertex < positions.length; vertex += 3) {
+ for (let axis = 0; axis < 3; axis++) {
+ const value = matrix[axis] * positions[vertex] + matrix[4 + axis] * positions[vertex + 1] + matrix[8 + axis] * positions[vertex + 2] + matrix[12 + axis];
+ minimum[axis] = Math.min(minimum[axis], value);
+ maximum[axis] = Math.max(maximum[axis], value);
+ }
+ }
+ assert.deepEqual(worldBounds({ geometry: { positions }, matrix }), { minimum, maximum });
+});