Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Draw opaque texels of blend materials with depth writes before blending (#393)

Nguyen Xuan An16021af151d8
diff --git a/changes/393.md b/changes/393.md
new file mode 100644
index 00000000..af925f9b
--- /dev/null
+++ b/changes/393.md
@@ -0,0 +1 @@
+- Draw the opaque texels of a `blend` material first, with depth writes, then blend only the translucent ones (issue #393). A glTF exported with every material as BLEND (common for Sketchfab converters) no longer lets one mesh paint over another in file order: on the Sea House island a palm in front of the roof was drawn behind it.
diff --git a/changes/393.size.md b/changes/393.size.md
new file mode 100644
index 00000000..ec788ea3
--- /dev/null
+++ b/changes/393.size.md
@@ -0,0 +1,3 @@
+- entry:core: one extra draw per blend batch with opaque texels, 36 bytes, fixes wrong draw order of baked BLEND scenes.
+- scene:agent-scene: +29 bytes gzip for the same fix.
+- goal:agent-scene: the scene is already over its goal by 2797 bytes; the 29 bytes fix a visible draw-order bug.
diff --git a/docs/material-maps.md b/docs/material-maps.md
index a2b7f2e5..5316040e 100644
--- a/docs/material-maps.md
+++ b/docs/material-maps.md
@@ -111,7 +111,7 @@ Renderer disposal deletes every cached GPU texture. Asset disposal closes each s
Final alpha is the base map alpha (when `material.map` is set) times `opacity`. Vertex colors stay RGB.
- `mask` discards below `alphaCutoff` in the color pass and in the shadow pass, so shadows follow the cutout shape. With MSAA the edge uses alpha-to-coverage, sharpened by `fwidth`, so leaf edges stay smooth.
-- `blend` draws after all opaque and masked batches, with depth writes off. Batches sort back to front by their mean distance to the eye, and instances inside one batch sort by translation distance each frame. Intersecting translucent surfaces and large meshes can still show ordering artifacts. Blend parts skip shadows unless `castShadow: true` is set, and then they cast a fully opaque shadow.
+- `blend` draws after all opaque and masked batches. Texels with alpha 1 (opacity 1) are drawn first as a cutout that writes depth, so an opaque baked mesh hides what lies behind it in any order; the translucent texels then blend with depth writes off. Batches sort back to front by their mean distance to the eye, and instances inside one batch sort by translation distance each frame. Intersecting translucent surfaces and large meshes can still show ordering artifacts. Blend parts skip shadows unless `castShadow: true` is set, and then they cast a fully opaque shadow.
- `alphaMode`, `alphaCutoff`, `opacity` and `doubleSided` are batch state like `castShadow`. Parts that differ never share a draw call, and `updateParts` rejects changes after upload. Rebuild the renderer to change them.
- Invalid values (unknown mode, cutoff or opacity outside zero to one, a non-boolean `doubleSided`) throw.
- The benchmark preset ignores alpha.
diff --git a/src/renderer.js b/src/renderer.js
index ef376465..8c175bc0 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -1938,6 +1938,11 @@ export class Renderer67 {
return total / batch.instanceCount;
};
const ordered = blendBatches.map(batch => ({ batch, distance: distance(batch) })).sort((first, second) => second.distance - first.distance);
+ // Fully opaque texels (most of a baked scene exported as BLEND) draw first as a cutout that writes depth, so they hide
+ // whatever lies behind them in any draw order. The blend pass below then shades only the translucent texels (depth LESS).
+ for (const { batch } of ordered) {
+ if (batch.opacity >= 0.999) this.drawAlphaBatch({ ...batch, alphaMode: 'mask', alphaCutoff: 0.999 }, true);
+ }
context.enable(context.BLEND);
context.blendFuncSeparate(context.SRC_ALPHA, context.ONE_MINUS_SRC_ALPHA, context.ONE, context.ONE_MINUS_SRC_ALPHA);
context.depthMask(false);
diff --git a/tests/renderer.test.js b/tests/renderer.test.js
index dff4d0b0..498a5631 100644
--- a/tests/renderer.test.js
+++ b/tests/renderer.test.js
@@ -532,3 +532,34 @@ test('the shadow depth pass draws both faces of a single-sided batch, so the sun
renderer.applyAlphaState(singleSided, true);
assert.equal(culling[0][0], 'enable', 'the colour pass still culls back faces of a single-sided batch');
});
+
+test('blend batches draw their opaque texels with depth writes before the blended pass', () => {
+ const draws = [];
+ const state = { depthMask: true, blend: false };
+ const context = new Proxy({}, {
+ get(target, name) {
+ if (name === 'depthMask') return (value) => { state.depthMask = value; };
+ if (name === 'enable') return (capability) => { if (capability === 'BLEND') state.blend = true; };
+ if (name === 'disable') return (capability) => { if (capability === 'BLEND') state.blend = false; };
+ if (typeof name === 'string' && name === name.toUpperCase()) return name;
+ return () => {};
+ },
+ });
+ const renderer = Object.create(Renderer67.prototype);
+ renderer.context = context;
+ renderer.uniforms = { shadowMap: 'shadowMap' };
+ renderer.canvas = { width: 4, height: 4 };
+ const baked = { alphaMode: 'blend', alphaCutoff: 0, opacity: 1, instanceCount: 1, instanceData: new Float32Array(41) };
+ const glass = { alphaMode: 'blend', alphaCutoff: 0, opacity: 0.3, instanceCount: 1, instanceData: new Float32Array(41) };
+ renderer.batches = [baked, glass];
+ renderer.uploadSortedInstances = () => {};
+ renderer.drawAlphaBatch = (batch) => {
+ draws.push({ opacity: batch.opacity, depthMask: state.depthMask, blend: state.blend, alphaMode: batch.alphaMode, alphaCutoff: batch.alphaCutoff });
+ };
+ renderer.drawBlendBatches({ eye: [0, 0, 0] });
+ assert.equal(draws.length, 3);
+ assert.deepEqual(draws[0], { opacity: 1, depthMask: true, blend: false, alphaMode: 'mask', alphaCutoff: 0.999 });
+ assert.ok(draws.slice(1).every(draw => draw.blend && !draw.depthMask && draw.alphaMode === 'blend'));
+ assert.equal(draws.filter(draw => draw.opacity === 0.3).length, 1, 'a translucent material has no opaque texels to draw first');
+ assert.equal(baked.alphaCutoff, 0);
+});