Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Stop pointing alpha_tex at the base colour map, which multiplied a cutout's alpha by the colour's red (#896)

Nguyen Xuan An1098ba247dad
diff --git a/bench/cycles-parity/texture-scenes.mjs b/bench/cycles-parity/texture-scenes.mjs
index 6c461d13..cb7c0dd9 100644
--- a/bench/cycles-parity/texture-scenes.mjs
+++ b/bench/cycles-parity/texture-scenes.mjs
@@ -86,9 +86,9 @@ function backdrop() {
return part('backdrop', quadGeometry(1), [0, 0, -1.5], [7, 4, 1], { color: [0.55, 0.12, 0.1], roughness: 0.9 });
}
-function textureRepeatScene() {
+function textureRepeatScene(maskColorSpace = 'srgb') {
const colourMap = { image: gradientChecker(false), colorSpace: 'srgb' };
- const maskMap = { image: gradientChecker(true), colorSpace: 'srgb' };
+ const maskMap = { image: gradientChecker(true), colorSpace: maskColorSpace };
return sceneWithParts([
backdrop(),
part('colour', quadGeometry(3), [-2.2, 0, 0], [1.9, 1.9, 1], { color: [1, 1, 1], map: colourMap }),
@@ -119,7 +119,11 @@ function textureWrapsScene() {
]);
}
-export const TEXTURE_SCENES = { 'tex-repeat': textureRepeatScene, 'tex-wraps': textureWrapsScene };
+export const TEXTURE_SCENES = {
+ 'tex-repeat': () => textureRepeatScene('srgb'),
+ 'tex-repeat-linear': () => textureRepeatScene('linear'),
+ 'tex-wraps': textureWrapsScene,
+};
async function writeScenes(outputDirectory) {
const scenesDirectory = join(outputDirectory, 'scenes');
diff --git a/packages/render-native/pack-input-v2.mjs b/packages/render-native/pack-input-v2.mjs
index a6b9fde2..b3941d9c 100644
--- a/packages/render-native/pack-input-v2.mjs
+++ b/packages/render-native/pack-input-v2.mjs
@@ -670,9 +670,10 @@ export function packMaterialRecordV2(material = {}, textureCollector = null) {
? collector.addTexture(material.bumpTexture ?? material.bumpMap, false)
: { index: Number(material.bumpTex ?? -1), meanLinearRgb: [1, 1, 1] };
- const alphaTexIndex = explicitAlphaTex.index >= 0
- ? explicitAlphaTex.index
- : (alphaMode !== ALPHA_MODE_OPAQUE ? baseTex.index : -1);
+ // The kernel reads the alpha of the base colour map itself (its alpha channel). `alpha_tex` is a separate map read through its
+ // red channel, so pointing it at the base colour map would multiply the alpha by the colour's red (and, for an sRGB map, by its
+ // linearised red): a cutout then loses every texel that is not red enough. Only an explicit alpha map sets it.
+ const alphaTexIndex = explicitAlphaTex.index;
let flags = 0;
if (material.multiscatterGgx !== false) {
@@ -2588,7 +2589,7 @@ export function upgradeNativeInputV1ToV2(v1Input, options = {}) {
normalTex: normalTex.index,
metallicRoughnessTex: mrTex.index,
emissionTex: emissionTex.index,
- alphaTex: alphaCutoff > 0 && baseTex.index >= 0 ? baseTex.index : -1,
+ alphaTex: -1,
}, null);
packedMat.meanEmissiveRadiance = [
packedMat.meanEmissiveRadiance[0] * emissionTex.meanLinearRgb[0],
diff --git a/tests/native-pack-v2.test.js b/tests/native-pack-v2.test.js
index ba9e592d..24a54917 100644
--- a/tests/native-pack-v2.test.js
+++ b/tests/native-pack-v2.test.js
@@ -289,7 +289,7 @@ test('packMaterialRecordV2 and texture packing follow Principled v2 flags and de
const mat1 = viewRecord(v2.materials, STRUCT_STRIDES_V2.material, 1);
assert.equal(mat1.u32[49], 1, 'ALPHA_MODE_MASK = 1');
assert.equal(mat1.i32[52], 0, 'shared base_color_tex');
- assert.equal(mat1.i32[62], 0, 'alpha_tex points to base_color_tex');
+ assert.equal(mat1.i32[62], -1, 'the base colour map is not also the alpha map: its alpha channel is read as part of base_color_tex');
// Cycles clamp semantics: multiplied by 3.0.
const kp = viewRecord(v2.kernelParams, STRUCT_STRIDES_V2.kernelParams);
diff --git a/tests/native-texture-sampler.test.js b/tests/native-texture-sampler.test.js
index 1a4b7cb1..1996b6c7 100644
--- a/tests/native-texture-sampler.test.js
+++ b/tests/native-texture-sampler.test.js
@@ -132,3 +132,32 @@ test('a pool of many small textures packs, uploads and renders', { skip: !findNa
const result = traceNative(input, { width: 8, height: 8, samples: 1, kernel: 'cycles' });
assert.ok(result.rgba.every(Number.isFinite));
});
+
+const ALPHA_TEX_WORD = 62;
+const BASE_COLOR_TEX_WORD = 52;
+
+function readMaterialWord(input, materialIndex, wordIndex) {
+ const stride = STRUCT_STRIDES_V2.material;
+ const view = new Int32Array(input.materials.buffer, input.materials.byteOffset + materialIndex * stride, stride / 4);
+ return view[wordIndex];
+}
+
+test('a masked material reads its base map alpha through base_color_tex, not through alpha_tex (whose red channel it would multiply in)', () => {
+ const image = solidImage(4, 4, 200);
+ const camera = createCamera({ eye: [0, 0, 3], target: [0, 0, 0], fov: 40 }, 8, 8);
+ const input = buildNativeInputV2(
+ {
+ materials: [
+ { color: [1, 1, 1], alphaMode: 'mask', alphaCutoff: 0.5, map: { image, colorSpace: 'srgb' } },
+ { color: [1, 1, 1], alphaMode: 'mask', alphaCutoff: 0.5, map: { image, colorSpace: 'srgb' }, alphaMap: { image: solidImage(2, 2, 90), colorSpace: 'linear' } },
+ ],
+ instances: [{ geometry: quadGeometry(0), materialIndex: 0 }, { geometry: quadGeometry(1), materialIndex: 1 }],
+ lights: {},
+ },
+ camera,
+ { width: 8, height: 8, samples: 1 },
+ );
+ assert.equal(readMaterialWord(input, 0, BASE_COLOR_TEX_WORD), 0);
+ assert.equal(readMaterialWord(input, 0, ALPHA_TEX_WORD), -1);
+ assert.equal(readMaterialWord(input, 1, ALPHA_TEX_WORD), 1, 'an explicit alpha map is still bound');
+});