Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Match Blender 5.2's AgX mid grey and colour ramp (part of #282)

Nguyen Xuan An0c4a47ded4f5
diff --git a/bench/reference/README.md b/bench/reference/README.md
index 13745b30..a4dfe50d 100644
--- a/bench/reference/README.md
+++ b/bench/reference/README.md
@@ -67,19 +67,19 @@ Colour ramp (`scripts/blender-view-transform-golden.py`, rerun on 5.2.2: the out
| Transform | Threshold (max) | Max error | Mean error | Max per channel R, G, B | Within |
| --- | --- | --- | --- | --- | --- |
| `neutral` | 0.010 | 0.0079 | 0.0013 | 0.0079, 0.0052, 0.0079 | yes |
-| `agx` | 0.090 | 0.0885 | 0.0252 | 0.0714, 0.0756, 0.0885 | yes, but loose |
+| `agx` | 0.026 | 0.0247 | 0.0026 | 0.0168, 0.0247, 0.0072 | yes |
-`agx` is within its threshold only because the threshold was set at the measured error. 67js uses the Filament/three.js polynomial for the AgX contrast, and Blender 5.2.2 is darker in the mid-tones: grey 0.18 maps to 0.461 in Blender and 0.501 in 67js (0.09 gives 0.320 against 0.374, 0.03 gives 0.165 against 0.198); the worst patch is the saturated red 0.8, 0.1, 0.05. `neutral` follows Blender to within 2 steps of 255 (the Khronos formula is the same). Closing the `agx` gap needs Blender's own contrast curve; no change was made to `src/` here.
+`agx` was 0.0885 at most (mean 0.0252) before 2026-11-03, measured the same way. Blender 5.2's AgX is the EaryChow version, not the Troy Sobotka one the Filament/three.js polynomial copies: E-Gamut input, log2 over -12.47393 to +4.026 EV (the same range as before), a per-channel sigmoid with pivot at 0.18 grey (output 0.488, slope 2.39, toe and shoulder power about 1.5), a 2.4 power, then a matrix back to Rec.709 applied to the linear value. The differing steps were the sigmoid (the 6th-degree polynomial was too bright in the mid-tones: grey 0.18 gave 0.501 against Blender's 0.461), the matrices (Rec.2020 inset and outset against Blender's gamut-specific LUT) and the output power (2.2 against 2.4 before the matrix). `src/view-transform.js` now holds a fit of `AgX_Base_sRGB.cube`: the sigmoid is fitted to the LUT's grey diagonal (within 0.0013 in the 2.4-encoded value), the two matrices to random colours. Grey 0.18 maps to 0.460 (Blender 0.461); the grey patches are within 0.0012. The rest of the error is saturated colour (worst patch 2.0, 0.8, 0.1, green channel), which a per-channel sigmoid and two matrices cannot follow exactly, so the threshold is 0.026. `neutral` follows Blender to within 2 steps of 255 (the Khronos formula is the same).
Scenes: `npm run reference -- --view-transform agx --scene <id>,<id>` draws 67js with `viewTransform: 'agx'`, renders Cycles with the AgX view transform (same samples, denoising and scene description as the suite; 8-bit sRGB PNG, so both sides are quantised), and scores them with FLIP. Blender's sRGB picture is decoded and encoded with the 2.2 power 67js uses, so the score measures the curve and the lighting, not the transfer function. The standard column is the suite's own score (Cycles with the `Raw` transform, then the ACES fit on both sides, `baseline.json`).
-| Scene | 67js `agx` against Cycles AgX | Standard (ACES fit both sides) | Change |
+| Scene | `agx` before (2026-11-03, #343) | `agx` after (2026-11-03) | Standard (ACES fit both sides) |
| --- | --- | --- | --- |
-| `outdoor-hdri` | 0.0997 | 0.0462 | +0.0535 |
-| `material-spheres` | 0.1467 | 0.1293 | +0.0174 |
-| `character` | 0.1238 | 0.0799 | +0.0439 |
+| `outdoor-hdri` | 0.0997 | 0.0425 | 0.0462 |
+| `material-spheres` | 0.1467 | 0.0903 | 0.1293 |
+| `character` | 0.1238 | 0.0581 | 0.0799 |
-AgX scores worse than the standard row on all three. The mid-tone gap measured on the ramp (67js `agx` is brighter than Blender's) is the likely cause (inferred, not isolated by an experiment). The scores are in `view-transform.json`; Cycles ran on OptiX as in the suite, 67js on the RTX 3060.
+AgX now scores better than the standard row on all three (the mid-tone gap was the cause; the Cycles pictures were reused, only 67js was redrawn). The scores are in `view-transform.json`; Cycles ran on OptiX as in the suite, 67js on the RTX 3060.
## One sun (2026-11-01)
diff --git a/bench/reference/view-transform.json b/bench/reference/view-transform.json
index 4f8cad6a..0cb71e8d 100644
--- a/bench/reference/view-transform.json
+++ b/bench/reference/view-transform.json
@@ -2,8 +2,8 @@
"metric": "FLIP (LDR), mean error, lower is better",
"viewTransform": "agx",
"scores": {
- "outdoor-hdri": 0.0997,
- "material-spheres": 0.1467,
- "character": 0.1238
+ "outdoor-hdri": 0.0425,
+ "material-spheres": 0.0903,
+ "character": 0.0581
}
}
diff --git a/changes/282-agx.md b/changes/282-agx.md
new file mode 100644
index 00000000..94e79eae
--- /dev/null
+++ b/changes/282-agx.md
@@ -0,0 +1 @@
+- Match Blender 5.2's AgX in `environment.viewTransform: 'agx'` (issue #282). Grey 0.18 now maps to 0.460 sRGB against Blender's 0.461 (it was 0.501), and the colour ramp is within 0.0247 at most, 0.0026 on average (it was 0.0885 and 0.0252). The curve is a fit of Blender's AgX LUT: a per-channel sigmoid and two matrices. FLIP against Cycles AgX, `outdoor-hdri` 0.0997 to 0.0425, `material-spheres` 0.1467 to 0.0903, `character` 0.1238 to 0.0581.
diff --git a/docs/parity.md b/docs/parity.md
index fbd47105..4a6f2831 100644
--- a/docs/parity.md
+++ b/docs/parity.md
@@ -207,7 +207,7 @@ To move to a new Blender release, run `node scripts/parity-manual-snapshot.mjs`.
| Rendering > Shader Nodes > Color | partial | Color ramp, mix and invert. There is no hue, curves, gamma or blend modes. | [material-graph](../tests/material-graph.test.js) | #50 |
| Rendering > Shader Nodes > Utilities | partial | Maths on numbers and vectors: add, sub, mul, div, min, max, pow, abs, floor, fract, sqrt, clamp and smoothstep. There is no vector maths, mapping, map range or separate and combine nodes. | [material-graph](../tests/material-graph.test.js) | #50 |
| Rendering > Shader Nodes > Group | missing | No node graph. | | #50 |
-| Rendering > Color Management | partial | View transforms `aces` (default), `agx` and `neutral` (Khronos PBR Neutral) in the rasterizer and the path tracer, and `.cube` look LUTs or four named looks; grading as before. Neutral matches Blender 5.2 within 0.01 on a ramp, AgX within 0.09 (the Filament fit used here is about 0.04 lighter in the mid-tones than Blender 5.2). No choice of working color space or display device; no Filmic or Standard. | [view transforms](../tests/view-transform.test.js), [effects](../tests/effects.test.js) | AgX looks and exact Blender AgX curve |
+| Rendering > Color Management | partial | View transforms `aces` (default), `agx` and `neutral` (Khronos PBR Neutral) in the rasterizer and the path tracer, and `.cube` look LUTs or four named looks; grading as before. Neutral matches Blender 5.2 within 0.01 on a ramp, AgX within 0.026 (a fit of Blender 5.2's AgX LUT; saturated colour is the part that is off). No choice of working color space or display device; no Filmic or Standard. | [view transforms](../tests/view-transform.test.js), [effects](../tests/effects.test.js) | AgX looks |
| Rendering > Freestyle | partial | `environment.lines` draws silhouette, boundary and crease lines in screen space, and `polylines` draws screen-width lines in the world. There are no line sets, styles or strokes along curves. | [line-art](../tests/line-art.test.js), [guide](line-art.md) | |
| Rendering > View Layers & Passes | partial | Render jobs write albedo, normal, emission, depth and object ID passes as PNG files. There are no view layers, no cryptomatte and no lighting passes (diffuse, glossy, shadow). | [render-passes](../tests/render-passes.test.js) | #54 |
| Rendering > Render Output > Output Properties | partial | PNG and MP4, frame rate, duration (to 60 s) and size, and a CPU or NVENC encoder. There is no stereo or metadata. | [agent-render-jobs](../tests/agent-render-jobs.test.js) | #60 |
diff --git a/src/view-transform-shader.js b/src/view-transform-shader.js
index 8c199927..62f7c07f 100644
--- a/src/view-transform-shader.js
+++ b/src/view-transform-shader.js
@@ -19,20 +19,25 @@ const glslNumber = value => (Number.isInteger(value) ? value.toFixed(1) : String
const glslVector = values => `vec3(${values.map(glslNumber).join(', ')})`;
const glslMatrix = columns => `mat3(${columns.map(glslVector).join(', ')})`;
-const AGX_GLSL = `const mat3 agxToRec2020 = ${glslMatrix(AGX_CONSTANTS.toRec2020)};
-const mat3 agxFromRec2020 = ${glslMatrix(AGX_CONSTANTS.fromRec2020)};
-const mat3 agxInset = ${glslMatrix(AGX_CONSTANTS.inset)};
+const agxSide = ({ scale, power }) => ({ scale: glslNumber(scale), power: glslNumber(power), inversePower: glslNumber(1 / power) });
+const agxToe = agxSide(AGX_CONSTANTS.toe);
+const agxShoulder = agxSide(AGX_CONSTANTS.shoulder);
+
+const AGX_GLSL = `const mat3 agxInset = ${glslMatrix(AGX_CONSTANTS.inset)};
const mat3 agxOutset = ${glslMatrix(AGX_CONSTANTS.outset)};
-vec3 agxContrast(vec3 x) {
- vec3 x2 = x * x;
- vec3 x4 = x2 * x2;
- return ${glslNumber(AGX_CONSTANTS.contrast[0])} * x4 * x2 + ${glslNumber(AGX_CONSTANTS.contrast[1])} * x4 * x + ${glslNumber(AGX_CONSTANTS.contrast[2])} * x4 + ${glslNumber(AGX_CONSTANTS.contrast[3])} * x2 * x + ${glslNumber(AGX_CONSTANTS.contrast[4])} * x2 + ${glslNumber(AGX_CONSTANTS.contrast[5])} * x + ${glslNumber(AGX_CONSTANTS.contrast[6])};
+vec3 agxCurve(vec3 x) {
+ vec3 distance = ${glslNumber(AGX_CONSTANTS.slope)} * (x - ${glslNumber(AGX_CONSTANTS.pivot[0])});
+ vec3 above = max(distance, 0.0) / ${agxShoulder.scale};
+ vec3 below = max(-distance, 0.0) / ${agxToe.scale};
+ vec3 rise = ${agxShoulder.scale} * above / pow(1.0 + pow(above, vec3(${agxShoulder.power})), vec3(${agxShoulder.inversePower}));
+ vec3 fall = ${agxToe.scale} * below / pow(1.0 + pow(below, vec3(${agxToe.power})), vec3(${agxToe.inversePower}));
+ return ${glslNumber(AGX_CONSTANTS.pivot[1])} + rise - fall;
}
vec3 selectedViewTransform(vec3 color) {
- color = agxInset * (agxToRec2020 * color);
+ color = agxInset * color;
color = clamp((log2(max(color, vec3(1e-10))) - ${glslNumber(AGX_CONSTANTS.minimumEv)}) / ${glslNumber(AGX_CONSTANTS.maximumEv - AGX_CONSTANTS.minimumEv)}, 0.0, 1.0);
- color = pow(max(vec3(0.0), agxOutset * agxContrast(color)), vec3(2.2));
- return clamp(agxFromRec2020 * color, 0.0, 1.0);
+ color = pow(max(agxCurve(color), vec3(0.0)), vec3(${glslNumber(AGX_CONSTANTS.encodingPower)}));
+ return clamp(agxOutset * color, 0.0, 1.0);
}`;
const NEUTRAL_GLSL = `vec3 selectedViewTransform(vec3 color) {
diff --git a/src/view-transform.js b/src/view-transform.js
index 5bdf16f8..780ab3e7 100644
--- a/src/view-transform.js
+++ b/src/view-transform.js
@@ -3,20 +3,30 @@ import { LOOK_NAMES, VIEW_TRANSFORM_NAMES } from './view-transform-names.js';
// Display transforms and look LUTs as plain functions, shared by the path tracer, the tests and the GLSL in
// view-transform-shader.js (which prints the constants below, so the three cannot drift apart).
// - aces is the Narkowicz fit the rasterizer has always used, applied per channel.
-// - agx follows Blender's AgX through the Filament port that three.js ships: Rec.2020 inset, log2 between -12.47393 and
-// 4.026069 EV, a polynomial for the default contrast, outset, then a 2.2 power back to linear sRGB.
+// - agx is Blender 5.2's AgX (the sRGB view of its OCIO config: E-Gamut log2 between -12.47393 and 4.026069 EV, a per-channel
+// sigmoid, a 2.4 power, a matrix back to Rec.709) as a fit of AgX_Base_sRGB.cube, not the 57-point LUT itself. The
+// older Filament/three.js polynomial put 0.18 grey 0.04 above Blender (0.501 against 0.461).
// - neutral is the Khronos PBR Neutral tone mapper: base colours below 0.76 pass unchanged.
-// All three take linear sRGB and return linear sRGB in 0 to 1; the caller encodes with a power of 1 / 2.2.
+// All three take linear sRGB and return display-linear sRGB in 0 to 1; the caller encodes with a power of 1 / 2.2.
-/** Columns of each matrix, as GLSL writes them. */
+const rowsToColumns = rows => rows[0].map((_, column) => rows.map(row => row[column]));
+
+/**
+ * Matrices as columns, as GLSL writes them. inset takes linear Rec.709 to the space the log and the sigmoid work in; outset
+ * takes the linearised sigmoid output back to Rec.709. Rows sum to 1, so grey stays grey. Both were fitted to the LUT, and
+ * the sigmoid to its grey diagonal. From the pivot (0.18 grey) it rises by scale * v / (1 + v ** power) ** (1 / power)
+ * with v = slope * distance / scale, and falls the same way with the toe's own scale and power.
+ */
export const AGX_CONSTANTS = Object.freeze({
- toRec2020: [[0.6274, 0.0691, 0.0164], [0.3293, 0.9195, 0.0880], [0.0433, 0.0113, 0.8956]],
- fromRec2020: [[1.6605, -0.1246, -0.0182], [-0.5876, 1.1329, -0.1006], [-0.0728, -0.0083, 1.1187]],
- inset: [[0.856627153315983, 0.137318972929847, 0.11189821299995], [0.0951212405381588, 0.761241990602591, 0.0767994186031903], [0.0482516061458583, 0.101439036467562, 0.811302368396859]],
- outset: [[1.1271005818144368, -0.1413297634984383, -0.14132976349843826], [-0.11060664309660323, 1.157823702216272, -0.11060664309660294], [-0.016493938717834573, -0.016493938717834257, 1.2519364065950405]],
+ inset: rowsToColumns([[0.57215, 0.33216, 0.09569], [0.12368, 0.74091, 0.13541], [0.09814, 0.19729, 0.70457]]),
+ outset: rowsToColumns([[1.84762, -0.70741, -0.14021], [-0.2341, 1.3225, -0.0884], [-0.19713, -0.30062, 1.49775]]),
minimumEv: -12.47393,
maximumEv: 4.026069,
- contrast: [15.5, -40.14, 31.96, -6.868, 0.4298, 0.1191, -0.00232],
+ pivot: [0.60608, 0.48834],
+ slope: 2.39013,
+ toe: { scale: 0.56474, power: 1.50159 },
+ shoulder: { scale: 0.70613, power: 1.53357 },
+ encodingPower: 2.4,
});
export const NEUTRAL_CONSTANTS = Object.freeze({ startCompression: 0.8 - 0.04, desaturation: 0.15 });
@@ -36,21 +46,24 @@ export function acesChannel(value) {
return clampUnit((color * (2.51 * color + 0.03)) / (color * (2.43 * color + 0.59) + 0.14));
}
-function agxContrast(value) {
- const [sixth, fifth, fourth, third, second, first, zeroth] = AGX_CONSTANTS.contrast;
- const square = value * value;
- const quartic = square * square;
- return sixth * quartic * square + fifth * quartic * value + fourth * quartic + third * square * value + second * square + first * value + zeroth;
+function agxSigmoidSide(distance, side) {
+ const scaled = distance / side.scale;
+ return side.scale * scaled / (1 + scaled ** side.power) ** (1 / side.power);
+}
+
+function agxCurve(value) {
+ const { pivot, slope, toe, shoulder } = AGX_CONSTANTS;
+ const distance = slope * (value - pivot[0]);
+ return pivot[1] + (distance >= 0 ? agxSigmoidSide(distance, shoulder) : -agxSigmoidSide(-distance, toe));
}
export function agxColor(rgb) {
- const { toRec2020, fromRec2020, inset, outset, minimumEv, maximumEv } = AGX_CONSTANTS;
- const encoded = multiply(inset, multiply(toRec2020, rgb)).map(channel => {
+ const { inset, outset, minimumEv, maximumEv, encodingPower } = AGX_CONSTANTS;
+ const linear = multiply(inset, rgb).map(channel => {
const logged = (Math.log2(Math.max(channel, 1e-10)) - minimumEv) / (maximumEv - minimumEv);
- return agxContrast(clampUnit(logged));
+ return Math.max(0, agxCurve(clampUnit(logged))) ** encodingPower;
});
- const linear = multiply(outset, encoded).map(channel => Math.max(0, channel) ** 2.2);
- return multiply(fromRec2020, linear).map(clampUnit);
+ return multiply(outset, linear).map(clampUnit);
}
export function neutralColor(rgb) {
diff --git a/tests/view-transform.test.js b/tests/view-transform.test.js
index 2ff9758e..7cf2a4f3 100644
--- a/tests/view-transform.test.js
+++ b/tests/view-transform.test.js
@@ -22,11 +22,12 @@ const golden = JSON.parse(readFileSync(new URL('./fixtures/blender/view-transfor
const srgbEncode = value => (value <= 0.0031308 ? value * 12.92 : 1.055 * value ** (1 / 2.4) - 0.055);
// Tolerance per channel in sRGB-encoded units (0 to 1), measured on this ramp against Blender 5.2.2: neutral 0.0079 at most
-// (mean 0.0013), as Blender runs the Khronos formula. agx 0.0885 at most (mean 0.025): it is the Filament/three.js port of
-// AgX (a polynomial for the contrast, 4-digit Rec.2020 matrices), and Blender 5.2 is about 0.04 darker in the mid-tones
-// (0.18 maps to 0.461, the port gives 0.501) and more saturated for strong colours (0.8, 0.1, 0.05 is the worst patch).
-// Closing that gap needs Blender's own AgX curve; the bounds stay at the measured error so a regression shows.
-const TOLERANCE = { agx: 0.09, neutral: 0.01 };
+// (mean 0.0013), as Blender runs the Khronos formula. agx 0.0247 at most (mean 0.0026) since it is a fit of Blender's
+// AgX_Base_sRGB.cube (the Filament polynomial it replaced was 0.0885 at most, 0.501 at grey 0.18 against Blender's 0.461).
+// The grey patches are within 0.0012; what is left is saturated colour (2.0, 0.8, 0.1 is the worst patch), which a
+// per-channel sigmoid and two matrices cannot follow exactly. The bounds sit just above the measured error so a regression shows.
+const TOLERANCE = { agx: 0.026, neutral: 0.01 };
+const MID_GREY_TOLERANCE = 0.005;
for (const name of ['agx', 'neutral']) {
test(`${name} matches Blender ${golden.blenderVersion} on the colour ramp within ${TOLERANCE[name]}`, () => {
@@ -39,6 +40,12 @@ for (const name of ['agx', 'neutral']) {
});
}
+test('agx maps 0.18 mid grey to what Blender does', () => {
+ const grey = golden.patches.find(patch => patch.input.every(value => value === 0.18));
+ const mine = applyViewTransform('agx', grey.input).map(srgbEncode);
+ for (let channel = 0; channel < 3; channel++) assert.ok(Math.abs(mine[channel] - grey.agx[channel]) <= MID_GREY_TOLERANCE, `channel ${channel}: ${mine[channel]}`);
+});
+
test('the ramp covers greys from black to 16 and saturated colours', () => {
assert.ok(golden.patches.length >= 25);
assert.match(golden.blenderVersion, /^5\.2/);
@@ -66,8 +73,10 @@ test('the rasterizer shaders and the path tracer apply the same default curve',
test('the GLSL of agx and neutral prints the constants the path tracer uses', () => {
const agx = viewTransformShader.apply(fragmentShader, 'main', 'agx', false);
- const numbers = [...AGX_CONSTANTS.toRec2020, ...AGX_CONSTANTS.fromRec2020, ...AGX_CONSTANTS.inset, ...AGX_CONSTANTS.outset].flat();
- for (const value of [...numbers, AGX_CONSTANTS.minimumEv, ...AGX_CONSTANTS.contrast]) assert.ok(agx.includes(String(value)) || agx.includes(value.toFixed(1)), `agx GLSL holds ${value}`);
+ const { inset, outset, minimumEv, slope, pivot, toe, shoulder, encodingPower } = AGX_CONSTANTS;
+ const numbers = [...inset, ...outset].flat();
+ const curve = [slope, ...pivot, toe.scale, toe.power, shoulder.scale, shoulder.power, encodingPower];
+ for (const value of [...numbers, minimumEv, ...curve]) assert.ok(agx.includes(String(value)) || agx.includes(value.toFixed(1)), `agx GLSL holds ${value}`);
const neutral = viewTransformShader.apply(fragmentShader, 'main', 'neutral', false);
for (const value of [NEUTRAL_CONSTANTS.startCompression, NEUTRAL_CONSTANTS.desaturation, 1 - NEUTRAL_CONSTANTS.startCompression]) assert.ok(neutral.includes(String(value)), `neutral GLSL holds ${value}`);
});