3D engine that AI agents drive
Add glare.count and glare.angle and make the streaks thin lines (issue #602)
Nguyen Xuan Anc4a08fcb925e
diff --git a/bench/post/post-steps.js b/bench/post/post-steps.jsindex 67887226..4a6d10d5 100644--- a/bench/post/post-steps.js+++ b/bench/post/post-steps.js@@ -32,6 +32,8 @@ const CASES = {defocus: { depthOfField: { blur: 0.8, focusDistance: 4.5 } },'color-balance': { colorBalance: { lift: [0.06, 0, -0.06], gamma: [1, 1, 1.2], gain: [1.2, 1, 0.8] } },'glare-streaks': { glare: { streaks: 0.8, threshold: 1, size: 0.2 } },+ 'glare-streaks-6': { glare: { streaks: 0.8, count: 6, angle: 30, threshold: 1, size: 0.2 } },+ 'glare-anamorphic': { glare: { streaks: 0.8, count: 2, angle: 0, threshold: 1, size: 0.35 } },'glare-fog': { glare: { fogGlow: 0.6, threshold: 1, size: 0.12 } },'lens-distortion': { lensDistortion: { distortion: 0.8 } },'lens-dispersion': { lensDistortion: { dispersion: 1 } },diff --git a/changes/602-post-steps.md b/changes/602-post-steps.mdindex 64d38eae..2341ba33 100644--- a/changes/602-post-steps.md+++ b/changes/602-post-steps.md@@ -1 +1 @@-- Three named post steps join defocus (`effects.depthOfField`) in the look: `effects.colorBalance` (`lift`, `gamma`, `gain`, each three numbers), `effects.glare` (`streaks`, `fogGlow`, `threshold`, `size`) and `effects.lensDistortion` (`distortion`, `dispersion`). Each is off until a recipe names it and draws nothing at its neutral values, so a recipe without them renders the same pixels as before. Their code is a lazy module that loads only for a recipe that names one; `installPostSteps(renderer)` from `src/post-steps.js` adds the stages (#602)+- Three named post steps join defocus (`effects.depthOfField`) in the look: `effects.colorBalance` (`lift`, `gamma`, `gain`, each three numbers), `effects.glare` (`streaks`, `count`, `angle`, `fogGlow`, `threshold`, `size`) and `effects.lensDistortion` (`distortion`, `dispersion`). Each is off until a recipe names it and draws nothing at its neutral values, so a recipe without them renders the same pixels as before. Their code is a lazy module that loads only for a recipe that names one; `installPostSteps(renderer)` from `src/post-steps.js` adds the stages (#602)diff --git a/docs/agent-look.md b/docs/agent-look.mdindex 3f6cbb87..2d93bf1c 100644--- a/docs/agent-look.md+++ b/docs/agent-look.md@@ -261,7 +261,9 @@ Ambient occlusion reads the depth texture, so it needs the HDR depth target that| `colorBalance.lift` | `[0, 0, 0]` | Three numbers, `-1` to `1`. Raises the shadows per channel and fades out toward white || `colorBalance.gamma` | `[1, 1, 1]` | Three numbers, `0.1` to `4`. Bends the midtones; above `1` is brighter || `colorBalance.gain` | `[1, 1, 1]` | Three numbers, `0` to `4`. Scales the highlights |-| `glare.streaks` | `0` | `0` to `1`. Four diagonal streaks from every pixel above the threshold |+| `glare.streaks` | `0` | `0` to `1`. Thin star streaks from every pixel above the threshold, bright at the core and fading to the tip |+| `glare.count` | `4` | Whole number, `1` to `8`. Arms spaced evenly around the circle: `2` is one straight line (a horizontal anamorphic streak at angle `0`), `4` a plus, `6` a six-point star |+| `glare.angle` | `0` | `-360` to `360` degrees. Turns the first arm from the horizontal; `45` turns a plus into an X || `glare.fogGlow` | `0` | `0` to `1`. A soft halo around the same bright pixels || `glare.threshold` | `1` | `0` to `10`. Peak scene brightness (linear, before tone mapping) from which a pixel glares || `glare.size` | `0.15` | `0.01` to `1`. Streak length and halo radius as a fraction of the image height |diff --git a/skills/build-67js-scene/references/look.md b/skills/build-67js-scene/references/look.mdindex ebe57f05..bb01c151 100644--- a/skills/build-67js-scene/references/look.md+++ b/skills/build-67js-scene/references/look.md@@ -93,7 +93,7 @@ Point lights do not scatter in volume fog; the sun does. A sunlit closed room wi`effects` holds `exposure`, `saturation`, `contrast`, `temperature`, `vignette`, `grain`, `ambientOcclusion` (0 = off),`bloom` and `depthOfField` (see `camera.md`). Nested `bloom` fields merge.Named post steps, each off until set: `colorBalance` (`lift`, `gamma`, `gain`, three numbers each), `glare` (`streaks`,-`fogGlow`, `threshold`, `size`) and `lensDistortion` (`distortion`, `dispersion`); ranges in `docs/agent-look.md`.+`count`, `angle`, `fogGlow`, `threshold`, `size`) and `lensDistortion` (`distortion`, `dispersion`); ranges in `docs/agent-look.md`.Tool `scene_apply`:diff --git a/src/post-steps.js b/src/post-steps.jsindex c77c414c..70f5a1b2 100644--- a/src/post-steps.js+++ b/src/post-steps.js@@ -8,8 +8,8 @@ import { createStage, sharedFragmentHeader } from './camera-effects.js';//// All three are deterministic: fixed sample patterns and no history, so a seek repeats the same pixels.-const STREAK_DIRECTIONS = 4;-const STREAK_STEPS = 12;+const MAXIMUM_STREAK_ARMS = 8;+const STREAK_STEPS = 24;const FOG_SAMPLES = 32;// Longest lateral color offset at the image corner at full dispersion, as a fraction of the distance from the center.const MAXIMUM_DISPERSION = 0.03;@@ -51,9 +51,13 @@ function resolveColorBalance(input) {function resolveGlare(input) {const group = groupOf(input, 'glare');- onlyKeys(group, ['streaks', 'fogGlow', 'threshold', 'size'], 'glare');+ onlyKeys(group, ['streaks', 'count', 'angle', 'fogGlow', 'threshold', 'size'], 'glare');+ const count = numberInRange(group.count, 4, 1, MAXIMUM_STREAK_ARMS, 'glare.count');+ if (!Number.isInteger(count)) throw new RangeError('glare.count must be a whole number.');return {streaks: numberInRange(group.streaks, 0, 0, 1, 'glare.streaks'),+ count,+ angle: numberInRange(group.angle, 0, -360, 360, 'glare.angle'),fogGlow: numberInRange(group.fogGlow, 0, 0, 1, 'glare.fogGlow'),threshold: numberInRange(group.threshold, 1, 0, 10, 'glare.threshold'),size: numberInRange(group.size, 0.15, 0.01, 1, 'glare.size'),@@ -95,6 +99,8 @@ void main() {export const glareFragment = `${sharedFragmentHeader}uniform float streakAmount;+uniform int streakCount;+uniform float streakAngle;uniform float fogAmount;uniform float threshold;uniform float sizePixels;@@ -110,16 +116,19 @@ void main() {float jitter = pixelRotation() / 6.2831853;if (streakAmount > 0.0) {vec3 streaks = vec3(0.0);- for (int direction = 0; direction < ${STREAK_DIRECTIONS}; direction++) {- float angle = 0.7853982 + float(direction) * 1.5707963;+ // Each arm is one thin line of taps from the pixel outward, evenly spaced around the circle; a pair of arms is one streak.+ // Weights fall off cubically, so a streak is bright at its core and thins to nothing at its tip.+ for (int arm = 0; arm < ${MAXIMUM_STREAK_ARMS}; arm++) {+ if (arm >= streakCount) break;+ float angle = streakAngle + float(arm) * 6.2831853 / float(streakCount);vec2 step = vec2(cos(angle), sin(angle)) * sizePixels / ${STREAK_STEPS}.0 / resolution;for (int index = 1; index <= ${STREAK_STEPS}; index++) {float along = float(index) - jitter;float weight = 1.0 - along / (${STREAK_STEPS}.0 + 1.0);- streaks += (brightPart(textureCoordinate + step * along, 0.0) + brightPart(textureCoordinate - step * along, 0.0)) * weight * weight;+ streaks += brightPart(textureCoordinate + step * along, 0.0) * weight * weight * weight;}}- glow += streaks * streakAmount / ${STREAK_STEPS}.0;+ glow += streaks * streakAmount * 4.0 / ${STREAK_STEPS}.0;}if (fogAmount > 0.0) {// Taps read a blurred level of the frame, so the fixed spiral of taps merges into one smooth halo with no per-pixel noise.@@ -187,7 +196,7 @@ function createGlareStage() {const stage = createStage({name: 'Glare',fragment: glareFragment,- uniformNames: ['streakAmount', 'fogAmount', 'threshold', 'sizePixels', 'fogLod'],+ uniformNames: ['streakAmount', 'streakCount', 'streakAngle', 'fogAmount', 'threshold', 'sizePixels', 'fogLod'],isActive: (effects, frame) => {const { glare } = stepsOf(frame);return Boolean(glare) && (glare.streaks > 0 || glare.fogGlow > 0);@@ -195,6 +204,8 @@ function createGlareStage() {setUniforms(context, uniforms, renderer, frame) {const { glare } = stepsOf(frame);context.uniform1f(uniforms.streakAmount, glare.streaks);+ context.uniform1i(uniforms.streakCount, glare.count);+ context.uniform1f(uniforms.streakAngle, glare.angle * Math.PI / 180);context.uniform1f(uniforms.fogAmount, glare.fogGlow);context.uniform1f(uniforms.threshold, glare.threshold);const sizePixels = glare.size * renderer.canvas.height;diff --git a/tests/post-steps.test.js b/tests/post-steps.test.jsindex fdfbec14..b60504b7 100644--- a/tests/post-steps.test.js+++ b/tests/post-steps.test.js@@ -44,7 +44,11 @@ test('a look that names no step resolves exactly as before', async () => {test('each named step resolves with defaults and refuses bad input', () => {assert.deepEqual(resolvePostSteps({ colorBalance: {} }).colorBalance, { lift: [0, 0, 0], gamma: [1, 1, 1], gain: [1, 1, 1] });- assert.deepEqual(resolvePostSteps({ glare: { streaks: 0.5 } }).glare, { streaks: 0.5, fogGlow: 0, threshold: 1, size: 0.15 });+ assert.deepEqual(resolvePostSteps({ glare: { streaks: 0.5 } }).glare, { streaks: 0.5, count: 4, angle: 0, fogGlow: 0, threshold: 1, size: 0.15 });+ assert.deepEqual(resolvePostSteps({ glare: { streaks: 1, count: 2, angle: 30 } }).glare, { streaks: 1, count: 2, angle: 30, fogGlow: 0, threshold: 1, size: 0.15 });+ assert.throws(() => resolvePostSteps({ glare: { count: 2.5 } }), /whole number/);+ assert.throws(() => resolvePostSteps({ glare: { count: 9 } }), /glare.count must be between/);+ assert.throws(() => resolvePostSteps({ glare: { angle: 400 } }), /glare.angle must be between/);assert.deepEqual(resolvePostSteps({ lensDistortion: { dispersion: 1 } }).lensDistortion, { distortion: 0, dispersion: 1 });assert.deepEqual(resolvePostSteps({ saturation: 1 }), {});assert.throws(() => resolvePostSteps({ colorBalance: { lift: [0, 0] } }), /three numbers/);@@ -114,7 +118,7 @@ test('the shaders keep their fixed sample patterns and read no history', () => {// RENDER_TESTS=1 node --test tests/post-steps.test.js (on a shared GPU, start it through the machine GPU lock)const chromePath = process.env.CHROME_PATH ?? 'C:/Program Files/Google/Chrome/Application/chrome.exe';const rendersEnabled = process.env.RENDER_TESTS === '1' && existsSync(chromePath);-const STEP_CASES = ['defocus', 'color-balance', 'glare-streaks', 'glare-fog', 'lens-distortion', 'lens-dispersion'];+const STEP_CASES = ['defocus', 'color-balance', 'glare-streaks', 'glare-streaks-6', 'glare-anamorphic', 'glare-fog', 'lens-distortion', 'lens-dispersion'];test('each named step changes the picture and the default still keeps its hash', { skip: !rendersEnabled, timeout: 300000 }, () => {const output = execFileSync('node', ['bench/post/post-steps.mjs', '--rounds=1', `--out=${join(tmpdir(), 'post-steps-test')}`], { encoding: 'utf8' });