3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Keep NEAREST glTF samplers hard-edged by skipping anisotropy on them (#2260)
Nguyen Xuan An2e8ef57f4177
diff --git a/changes/2260.md b/changes/2260.mdnew file mode 100644index 00000000..da54e586--- /dev/null+++ b/changes/2260.md@@ -0,0 +1 @@+- A glTF texture with a NEAREST sampler now renders hard-edged instead of blurred by anisotropic filtering (#2260)diff --git a/docs/material-maps.md b/docs/material-maps.mdindex ffec505a..a0fdfb27 100644--- a/docs/material-maps.md+++ b/docs/material-maps.md@@ -50,6 +50,7 @@ A descriptor contains `image`, `uvChannel`, `colorSpace`, `flipY`, `wrapS`, `wraSampler fields use WebGL enum values. Wrapping supports repeat, clamp-to-edge, and mirrored repeat.Magnification supports nearest and linear. Minification also supports all four mipmap filters.+A texture whose magnification or minification is nearest stays hard-edged: anisotropic filtering, which would blur it, is applied only to textures that filter linearly in both directions.Descriptor identity determines GPU texture reuse. Repeated parts can share descriptors without duplicating GPU textures or texture-budget charges.Different descriptors allocate separately even when they reference the same decoded image.diff --git a/src/renderer.js b/src/renderer.jsindex abe2c8c6..81acab92 100644--- a/src/renderer.js+++ b/src/renderer.js@@ -597,10 +597,19 @@ export function configureTexture(renderer, info) {context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, info.magFilter);if (info.mipmaps) {context.generateMipmap(context.TEXTURE_2D);- renderer.applyAnisotropy(context.TEXTURE_2D);+ if (filtersSmoothly(info)) renderer.applyAnisotropy(context.TEXTURE_2D);}}+// Anisotropic filtering replaces both filters with its own smooth one, so it would turn a NEAREST sampler (pixel art, block+// textures) into a blur. Only a texture that already filters linearly in both directions gets it.+function filtersSmoothly({ minFilter, magFilter }) {+ const LINEAR = 9729;+ const LINEAR_MIPMAP_NEAREST = 9985;+ const LINEAR_MIPMAP_LINEAR = 9987;+ return magFilter === LINEAR && (minFilter === LINEAR_MIPMAP_NEAREST || minFilter === LINEAR_MIPMAP_LINEAR);+}+export function textureDescriptorInfo(map, maximumSize = Infinity) {const width = map.image?.naturalWidth ?? map.image?.width;const height = map.image?.naturalHeight ?? map.image?.height;diff --git a/tests/texture-nearest-filter.test.js b/tests/texture-nearest-filter.test.jsnew file mode 100644index 00000000..4f726ad9--- /dev/null+++ b/tests/texture-nearest-filter.test.js@@ -0,0 +1,72 @@+import assert from 'node:assert/strict';+import test from 'node:test';+import { configureTexture } from '../src/renderer.js';+import { withDevServerPage } from './helpers/dev-server-browser.js';++const NEAREST = 9728;+const LINEAR = 9729;+const NEAREST_MIPMAP_NEAREST = 9984;+const LINEAR_MIPMAP_NEAREST = 9985;+const NEAREST_MIPMAP_LINEAR = 9986;+const LINEAR_MIPMAP_LINEAR = 9987;++function recordingRenderer() {+ const anisotropyTargets = [];+ const context = new Proxy({}, {+ get: (target, name) => (name === 'TEXTURE_2D' ? 3553 : () => {}),+ });+ return { renderer: { context, applyAnisotropy: target => anisotropyTargets.push(target) }, anisotropyTargets };+}++const CASES = [+ ['NEAREST magnification with NEAREST_MIPMAP_NEAREST (the placed GLB of #2260)', NEAREST_MIPMAP_NEAREST, NEAREST, false],+ ['NEAREST magnification with LINEAR_MIPMAP_LINEAR', LINEAR_MIPMAP_LINEAR, NEAREST, false],+ ['LINEAR magnification with NEAREST_MIPMAP_LINEAR', NEAREST_MIPMAP_LINEAR, LINEAR, false],+ ['LINEAR magnification with NEAREST_MIPMAP_NEAREST', NEAREST_MIPMAP_NEAREST, LINEAR, false],+ ['the default LINEAR_MIPMAP_LINEAR with LINEAR', LINEAR_MIPMAP_LINEAR, LINEAR, true],+ ['LINEAR_MIPMAP_NEAREST with LINEAR', LINEAR_MIPMAP_NEAREST, LINEAR, true],+];++for (const [name, minFilter, magFilter, expectAnisotropy] of CASES) {+ test(`anisotropic filtering ${expectAnisotropy ? 'is applied' : 'is skipped'} for ${name}`, () => {+ const { renderer, anisotropyTargets } = recordingRenderer();+ configureTexture(renderer, { wrapS: 10497, wrapT: 10497, minFilter, magFilter, mipmaps: true });+ assert.equal(anisotropyTargets.length, expectAnisotropy ? 1 : 0);+ });+}++test('a NEAREST checker stays hard-edged on a real context while a LINEAR one is filtered', async testContext => {+ await withDevServerPage(testContext, async page => {+ const result = await page.evaluate(async () => {+ const { CrucibleRenderer } = await import('/src/renderer.js');+ const { box } = await import('/src/geometry.js');+ const identity = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);+ const material = { tag: 'surface', color: [0.5, 0.5, 0.5], metallic: 0, roughness: 0.5, emission: 0 };+ const canvas = document.createElement('canvas');+ canvas.width = 16;+ canvas.height = 16;+ document.body.append(canvas);+ const renderer = new CrucibleRenderer(canvas, [{ name: 'box', geometry: box(0, 1), matrix: identity, material }], { samples: 1, pixelRatio: 1 });+ const context = renderer.context;+ const extension = context.getExtension('EXT_texture_filter_anisotropic');+ const checker = Object.assign(document.createElement('canvas'), { width: 4, height: 4 });+ const painter = checker.getContext('2d');+ for (let index = 0; index < 16; index++) {+ painter.fillStyle = (index + Math.floor(index / 4)) % 2 ? '#fff' : '#000';+ painter.fillRect(index % 4, Math.floor(index / 4), 1, 1);+ }+ const anisotropyOf = map => {+ context.bindTexture(context.TEXTURE_2D, renderer.createTexture(map));+ return context.getTexParameter(context.TEXTURE_2D, extension.TEXTURE_MAX_ANISOTROPY_EXT);+ };+ return {+ hasExtension: Boolean(extension),+ nearest: anisotropyOf({ image: checker, minFilter: 9984, magFilter: 9728 }),+ linear: anisotropyOf({ image: checker, minFilter: 9987, magFilter: 9729 }),+ };+ });+ if (!result.hasExtension) return testContext.skip('this context has no anisotropic filtering');+ assert.equal(result.nearest, 1);+ assert.ok(result.linear > 1);+ });+});