Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Filter material textures anisotropically

Nguyen Xuan An1293a1d0abd2
diff --git a/bench/size/budgets.json b/bench/size/budgets.json
index f8305fe0..6adf3744 100644
--- a/bench/size/budgets.json
+++ b/bench/size/budgets.json
@@ -6,7 +6,7 @@
"agent-scene": 100000
},
"budgets": {
- "entry:core": 86800,
+ "entry:core": 86840,
"entry:model": 14400,
"entry:nature": 13200,
"entry:motion": 13600,
@@ -45,10 +45,10 @@
"entry:material-layer-shaders": 21360,
"entry:light-linking": 19680,
"entry:cascaded-shadows": 28480,
- "scene:lit-box": 64170,
- "scene:gltf-box": 85190,
- "scene:gltf-box+lazy": 88520,
- "scene:agent-scene": 105980,
+ "scene:lit-box": 64210,
+ "scene:gltf-box": 85240,
+ "scene:gltf-box+lazy": 88570,
+ "scene:agent-scene": 106030,
"scene:agent-scene+lazy": 288460
}
}
diff --git a/changes/texture-anisotropy.md b/changes/texture-anisotropy.md
new file mode 100644
index 00000000..0a62ccda
--- /dev/null
+++ b/changes/texture-anisotropy.md
@@ -0,0 +1 @@
+- Filter mipmapped material textures anisotropically (8x, or less where the GPU caps it). Only terrain texture arrays had it, so glTF floors, roads and aprons seen at a grazing angle fell to a blurred mip level. The far apron joints of the airport showcase read sharper; core grows 40 bytes gzip.
diff --git a/src/renderer.js b/src/renderer.js
index 0017be05..18492824 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -31,6 +31,8 @@ const MATERIAL_MAPS = ['map', 'normalMap', 'metallicRoughnessMap', 'occlusionMap
const MAP_UNIFORMS = ['baseMap', 'normalMap', 'metallicRoughnessMap', 'occlusionMap', 'emissiveMap'];
const MAP_FLAGS = ['hasBaseMap', 'hasNormalMap', 'hasMetallicRoughnessMap', 'hasOcclusionMap', 'hasEmissiveMap'];
const DEFAULT_NORMAL_SCALE = [1, 1];
+// The most texture taps a mipmapped texture may take along a grazing axis (16 is the usual hardware ceiling, 8 costs less).
+const MAX_TEXTURE_ANISOTROPY = 8;
const PROCEDURAL_MATERIAL_TAGS = new Set(['water', 'foliage', 'grass', 'terrain', 'floor', 'fire', 'smoke', 'liquid', 'foam']);
// Tags whose vertices sway in the wind; the shadow pass must be redrawn when time moves.
@@ -649,6 +651,16 @@ export class Renderer67 {
return buffer;
}
+ // Without anisotropic filtering a mipmapped texture seen at a grazing angle (a floor, a road, an apron) falls to a
+ // 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));
+ }
+
createTexture(map) {
if (this.textures.has(map)) return this.textures.get(map);
const context = this.context;
@@ -666,7 +678,10 @@ export class Renderer67 {
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, info.wrapT);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, info.minFilter);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, info.magFilter);
- if (info.mipmaps) context.generateMipmap(context.TEXTURE_2D);
+ if (info.mipmaps) {
+ context.generateMipmap(context.TEXTURE_2D);
+ this.applyAnisotropy(context.TEXTURE_2D);
+ }
} else {
context.texImage2D(context.TEXTURE_2D, 0, context.RGBA8, 1, 1, 0, context.RGBA, context.UNSIGNED_BYTE, new Uint8Array([255, 255, 255, 255]));
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.LINEAR);
@@ -718,8 +733,7 @@ export class Renderer67 {
context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_WRAP_T, context.REPEAT);
context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MIN_FILTER, context.LINEAR_MIPMAP_LINEAR);
context.texParameteri(context.TEXTURE_2D_ARRAY, context.TEXTURE_MAG_FILTER, context.LINEAR);
- const anisotropy = context.getExtension('EXT_texture_filter_anisotropic');
- if (anisotropy) context.texParameterf(context.TEXTURE_2D_ARRAY, anisotropy.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(8, context.getParameter(anisotropy.MAX_TEXTURE_MAX_ANISOTROPY_EXT)));
+ this.applyAnisotropy(context.TEXTURE_2D_ARRAY);
return texture;
}
diff --git a/tests/renderer.test.js b/tests/renderer.test.js
index 5b490a52..c87e5fd1 100644
--- a/tests/renderer.test.js
+++ b/tests/renderer.test.js
@@ -297,10 +297,12 @@ test('texture budgets count every channel and charge shared descriptors once bef
assert.throws(() => textureDescriptorInfo({ ...shared, rotation: 1 }), /transforms/);
});
-test('GPU uploads use descriptor color spaces, cache shared maps, and dispose every cached texture', () => {
+test('GPU uploads use descriptor color spaces, filter anisotropically, cache shared maps, and dispose every cached texture', () => {
const renderer = Object.create(Renderer67.prototype);
const uploaded = [];
const deleted = [];
+ const anisotropyLevels = [];
+ const anisotropyExtension = { TEXTURE_MAX_ANISOTROPY_EXT: 34046, MAX_TEXTURE_MAX_ANISOTROPY_EXT: 34047 };
renderer.textures = new Map();
renderer.buffers = [];
renderer.batches = [];
@@ -308,7 +310,13 @@ test('GPU uploads use descriptor color spaces, cache shared maps, and dispose ev
renderer.releaseFrameTargets = () => {};
renderer.context = {
RGBA8: 32856, SRGB8_ALPHA8: 35907, MAX_TEXTURE_SIZE: 3379,
- getParameter(parameter) { return parameter === this.MAX_TEXTURE_SIZE ? 8192 : 0; },
+ getParameter(parameter) {
+ if (parameter === this.MAX_TEXTURE_SIZE) return 8192;
+ if (parameter === anisotropyExtension.MAX_TEXTURE_MAX_ANISOTROPY_EXT) return 16;
+ return 0;
+ },
+ getExtension(name) { return name === 'EXT_texture_filter_anisotropic' ? anisotropyExtension : null; },
+ texParameterf(target, name, value) { if (name === anisotropyExtension.TEXTURE_MAX_ANISOTROPY_EXT) anisotropyLevels.push(value); },
createTexture() { return {}; }, bindTexture() {}, pixelStorei() {}, texParameteri() {}, generateMipmap() {},
texImage2D(...arguments_) { uploaded.push(arguments_[2]); },
deleteTexture(texture) { if (texture) deleted.push(texture); }, deleteFramebuffer() {}, deleteProgram() {},
@@ -319,6 +327,7 @@ test('GPU uploads use descriptor color spaces, cache shared maps, and dispose ev
const second = renderer.createTexture(data);
assert.equal(renderer.createTexture(data), second);
assert.deepEqual(uploaded, [35907, 32856]);
+ assert.deepEqual(anisotropyLevels, [8, 8], 'Mipmapped maps take 8x anisotropic filtering, capped below the hardware limit');
renderer.dispose();
assert.deepEqual(deleted, [first, second]);
assert.equal(renderer.textures.size, 0);