Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Path tracer reads the pixels of decoded glTF textures (issue #635)

Nguyen Xuan An1e098bb98086
diff --git a/changes/635.md b/changes/635.md
new file mode 100644
index 00000000..6ccf53ba
--- /dev/null
+++ b/changes/635.md
@@ -0,0 +1 @@
+- The path tracer draws the textures and cutouts of loaded glTF files, which it used to trace untextured (#635)
diff --git a/src/path-tracer-scene.js b/src/path-tracer-scene.js
index 3544e5b8..4259f572 100644
--- a/src/path-tracer-scene.js
+++ b/src/path-tracer-scene.js
@@ -69,13 +69,44 @@ function srgbToLinearTable() {
const SRGB_TO_LINEAR = srgbToLinearTable();
+// Pixels read back from browser images, kept per image so parts that share a texture read it once.
+const drawablePixels = new WeakMap();
+
+/**
+ * RGBA8 pixels of a texture image: its own `data`, or, for a decoded glTF texture (an ImageBitmap, an image element or a
+ * canvas), the pixels read back through a 2D canvas. GPU-compressed images and runtimes without a canvas give null.
+ */
+export function texturePixels(image, createCanvas = defaultCanvas) {
+ if (!image?.width || !image.height) return null;
+ if (image.data) return image.data;
+ if (image.compressed || typeof image !== 'object') return null;
+ if (drawablePixels.has(image)) return drawablePixels.get(image);
+ const canvas = createCanvas(image.width, image.height);
+ const context = canvas?.getContext('2d', { willReadFrequently: true });
+ if (!context) return null;
+ context.drawImage(image, 0, 0);
+ const pixels = context.getImageData(0, 0, image.width, image.height).data;
+ drawablePixels.set(image, pixels);
+ return pixels;
+}
+
+function defaultCanvas(width, height) {
+ if (typeof OffscreenCanvas === 'function') return new OffscreenCanvas(width, height);
+ const canvas = globalThis.document?.createElement('canvas');
+ if (!canvas) return null;
+ canvas.width = width;
+ canvas.height = height;
+ return canvas;
+}
+
function resolveTexture(descriptor) {
const image = descriptor?.image;
- if (!image?.data || !image.width || !image.height) return null;
+ const data = texturePixels(image);
+ if (!data) return null;
return {
width: image.width,
height: image.height,
- data: image.data,
+ data,
colorLookup: descriptor.colorSpace === 'linear' ? null : SRGB_TO_LINEAR,
repeat: descriptor.wrapS === REPEAT_WRAP,
};
diff --git a/tests/path-tracer-texture-pixels.test.js b/tests/path-tracer-texture-pixels.test.js
new file mode 100644
index 00000000..76cb101f
--- /dev/null
+++ b/tests/path-tracer-texture-pixels.test.js
@@ -0,0 +1,41 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+import { texturePixels } from '../src/path-tracer-scene.js';
+
+function fakeCanvasFactory(readCounter) {
+ return (width, height) => ({
+ getContext: () => ({
+ drawImage: () => {},
+ getImageData: () => {
+ readCounter.reads++;
+ return { data: new Uint8ClampedArray(width * height * 4).fill(200) };
+ },
+ }),
+ });
+}
+
+test('an image with RGBA data is used as it is', () => {
+ const data = new Uint8Array(4).fill(7);
+ assert.equal(texturePixels({ width: 1, height: 1, data }), data);
+});
+
+test('a decoded bitmap without data is read back through a canvas, once per image', () => {
+ const readCounter = { reads: 0 };
+ const bitmap = { width: 2, height: 1 };
+ const first = texturePixels(bitmap, fakeCanvasFactory(readCounter));
+ const second = texturePixels(bitmap, fakeCanvasFactory(readCounter));
+ assert.equal(first.length, 8);
+ assert.equal(first[0], 200);
+ assert.equal(second, first);
+ assert.equal(readCounter.reads, 1);
+});
+
+test('compressed images, images without a size and runtimes without a canvas give no pixels', () => {
+ const readCounter = { reads: 0 };
+ assert.equal(texturePixels({ width: 4, height: 4, compressed: true }, fakeCanvasFactory(readCounter)), null);
+ assert.equal(texturePixels({ width: 0, height: 4 }, fakeCanvasFactory(readCounter)), null);
+ assert.equal(texturePixels({ width: 4, height: 4 }, () => null), null);
+ assert.equal(texturePixels(undefined), null);
+ assert.equal(readCounter.reads, 0);
+});