web: give the grain a size of its own, and read the count off the print

MONOCHROME GRAIN was one integer knob 0..10 with one meaning, how much. It is now
a strip of three: AMOUNT — the same knob, in half steps — SIZE, a percentage of
the stock's own grain cell (50..200%, so the same number means the same texture
relative to the picture on both platforms), and an inert readout of N/INCH, the
clump count the two knobs and the stock add up to in the print's own terms (300
dpi = 300px of the 1080-wide reference the knob was tuned at).

Emulsion is not one grain size across the frame: the coating settles unevenly.
The field now prints that — the same hash read slowly (ZONE_FREQ = 1/96 cells,
turned off the axes, smoothed so a border between two patches is a slope and not
a seam) swings each patch's own cell by half of ZONE_SWING either way, ±20%.
Nothing in it moves the field's mean: a coarser patch prints bigger clumps, not a
brighter one, which is why the strip can read out one number while the frame
carries a range.

A patch may not swing a cell under the pixel the target can print, or the clumps
are sub-pixel and print as static — aliasing, not a finer emulsion. The shader
takes that floor as a `mincell` uniform beside the cell (u, mincell, seed.xy, in
declaration order): an export passes one output pixel, a preview one device pixel
(1 / PixelRatio), which is the floor the phone's preview already needed.

SIZE is stored as an integer percent so no float noise reaches the recipe JSON,
and it is read by the same two engines that read grain: the web's
grainCell(width, stock, sizePct) and the phone's grainCell(width, minCell,
sizePct). The chip above the strip carries the amount in half steps the way TEMP
carries the kelvin, and the readout moves with SIZE, not with AMOUNT.

Measured:
  grain-controls-test.cjs 20/0 — the strip carries grp-grain, grain:amount,
    grain:size and grain-inch; the AMOUNT ruler is 0..10 step 0.5, and 3 -> 3.5
    moves the frame (sigma 18.53 -> 21.91, new hash) without moving the readout;
    10 -> sigma 60.43, 0 -> sigma 0; SIZE 200% -> 130/INCH (sigma 40.06), 50% ->
    522/INCH (66.56: under the preview's pixel floor what is printed is static,
    not finer grain); the region claim on an 8x8 grid of the flat frame gives
    tile sd 51.0..66.1, max/min 1.295, and a tile mean spread of 1.14 — a coarser
    patch is not a brighter one.
  grain-size-test.cjs 17/0 (was 12/0) — the SIZE rule and the readout on the
    module itself: 200% doubles the cell, 50% halves it, the output pixel still
    floors the smaller one. 4000px file cell 3.704 against 1.083 device px on a 3x
    preview, the old one-dp floor 2.77x coarser, rho1 0.627 against 0.074. The
    harness built its own 3-uniform array; it now passes [u, mincell, seed.xy]
    like every other caller.
  _grain-zone-ck.cjs — the zone's own contribution, at preview scale (cell 1.70,
    1600px, 8x8 tiles of 200px): zone on, tile sd 23.22..24.82 (ratio 1.069);
    zone off, 24.42..24.79 (ratio 1.015). Nothing else differs.
  _grain-ck.cjs — the clump field is otherwise what it was: rho1 0.62
    classic-neg / 0.12 velvia, residual autocorr 0.035 against 0.036 with the
    swing forced to 0, peak/median 32.3 against 27.6.
  _grain-spectrum.cjs (app, 1600px render) — residual autocorr 0.017..0.018,
    spectral peak/median 4.6..6.1: the slow lattice adds no peak of its own.
  grain-stock-test 53/0, sims-test 31/0, fx-mono-test 15/0, wb-preset-test 33/0,
    temp-swatch-test 33/0, wm-font-test 38/0, grain-analog-test 7/0 (its grain
    selectors moved to the strip).
  tsc: web clean; the phone's scoped config reports exactly the pre-change
    baseline (Viewfinder.tsx's own errors, none new).

ponytail: the amount is fractional now, so the two recipe-create forms read grain
through their own half() instead of the int() that would truncate the half the
ruler just spent — every other knob there is still whole. The SIZE knob is one
number for the whole strip: no way to dial a single patch, and no seed control.
The readout is the DESIGN count the field is built on, never a per-patch
measurement.
This commit is contained in:
2026-09-23 15:26:23 +07:00
parent 64f1598076
commit 0e9f78bd5e
13 changed files with 268 additions and 28 deletions
+8 -1
View File
@@ -565,7 +565,14 @@ export async function processAndExportPhoto(
// finer than the grain the user had just tuned at 1080px.
const noiseEffect = own(Skia.RuntimeEffect.Make(GRAIN_SKSL));
const noiseShader = noiseEffect
? own(noiseEffect.makeShader([grainCell(width), GRAIN_SEED[0], GRAIN_SEED[1]]))
? own(
noiseEffect.makeShader([
grainCell(width, 1, adjustments.grainSize ?? 100),
1,
GRAIN_SEED[0],
GRAIN_SEED[1],
])
)
: null;
if (noiseShader) {
+44 -3
View File
@@ -25,8 +25,21 @@ export const GRAIN_REF = 1080;
// The web port (docker/frontend/shared/utils/grainShader.ts) carries the same
// field with the stock's weights, cell and gain as uniforms; the 0.55/0.30/0.15
// and 2.95 below are its classic-chrome row, so the two print the same texture.
//
// Emulsion is not ONE grain size across the frame: the coating settles unevenly,
// so the clumps run coarser in patches and tighter in others. The same hash read
// as a SLOW value noise is what draws those patches — one every 1/ZONE_FREQ
// cells, turned off the axes and smoothed, because a step at a patch border
// would print as a seam — and each patch swings its own cell size by half of
// ZONE_SWING either way. Nothing here moves the field's mean: a coarser patch
// prints bigger clumps, not a brighter one, which is why the panel can read out
// one number while the frame carries a range.
export const ZONE_FREQ = 1 / 96;
export const ZONE_SWING = 0.4;
export const GRAIN_SKSL = `
uniform float u;
uniform float mincell;
uniform vec2 seed;
float grainHash(vec2 q) {
vec3 p3 = fract(vec3(q.x, q.y, q.x) * 0.1031);
@@ -49,8 +62,22 @@ export const GRAIN_SKSL = `
}
return 1.0 - min(sqrt(near), 1.0);
}
// The patches of grain size: the same hash read slowly, smoothed so a border
// between two patches is a slope and not a step.
float grainZone(vec2 p) {
vec2 i = floor(p);
vec2 f = p - i;
f = f * f * (3.0 - 2.0 * f);
return mix(mix(grainHash(i), grainHash(i + vec2(1.0, 0.0)), f.x),
mix(grainHash(i + vec2(0.0, 1.0)), grainHash(i + vec2(1.0, 1.0)), f.x), f.y);
}
vec4 main(vec2 pos) {
vec2 q = pos.xy / max(u, 0.0001) + seed;
vec2 p = pos.xy / max(u, 0.0001) + seed;
// This patch's own cell: the design cell swung by its zone, never under the
// floor — under it the clumps are sub-pixel and print as static, which is
// aliasing, not a finer emulsion.
float cell = max(u * (1.0 + (grainZone(mat2(0.9397, -0.3420, 0.3420, 0.9397) * p * ${ZONE_FREQ.toFixed(6)}) - 0.5) * ${ZONE_SWING}), mincell);
vec2 q = pos.xy / cell + seed;
// The grain, then the clumps of grain above it at 0.53x and 0.29x the cell.
// The octaves go COARSER only — a finer one lands under the pixel, the
// clumping is lost (rho(1) 0.275 -> 0, measured) and the field is static
@@ -81,5 +108,19 @@ export const GRAIN_SKSL = `
// canvas units are dp: on a 3x phone the old floor of a whole dp pinned a
// 390dp-wide preview to a 3 device-pixel cell, 2.8x coarser than the file's
// grain, which is the preview-vs-file mismatch all over again, only that way.
export const grainCell = (pictureWidth: number, minCell = 1) =>
Math.max(pictureWidth / GRAIN_REF, minCell);
//
// `sizePct` is the SIZE knob: a percentage of the cell above, which is the
// phone's own classic-chrome cell (see GRAIN_SKSL) — the web port scales its
// stock's own cell by the same number. It is handed to the shader as a floor
// too, so the field's patch-to-patch swing cannot cross it.
export const grainCell = (pictureWidth: number, minCell = 1, sizePct = 100) =>
Math.max((pictureWidth / GRAIN_REF) * (sizePct / 100), minCell);
// The clump count the panel READS OUT: how many clumps the design puts across an
// inch of a 300 dpi print, i.e. 300px of the GRAIN_REF frame. A statement about
// the cell and the SIZE knob, never about one patch of the frame — the field
// swings ±ZONE_SWING/2 patch by patch — and never about the screen, so the same
// cell reads the same number in the preview and in the file.
export const GRAIN_DPI = 300;
export const grainPerInch = (sizePct = 100) => Math.round(GRAIN_DPI / (sizePct / 100));
+17 -1
View File
@@ -169,12 +169,28 @@ export const PARAM_DEFS: {
label: 'MONOCHROME GRAIN',
min: 0,
max: 10,
step: 1,
// Half a step: the grain is a look, not a switch, and the AMOUNT chip in
// the GRAIN strip is what spends the half.
step: 0.5,
defaultValue: 0,
display: String,
get: (a) => a.grain,
set: (v) => ({ grain: v }),
},
{
// The SIZE knob: percent of the stock's own cell (grainShader.ts), so the
// same number means the same texture RELATIVE to the picture on both
// platforms and the readout beside it is the stock's, scaled.
key: 'grainSize',
label: 'GRAIN SIZE',
min: 50,
max: 200,
step: 5,
defaultValue: 100,
display: (v) => `${v}%`,
get: (a) => a.grainSize ?? 100,
set: (v) => ({ grainSize: v }),
},
{
key: 'hdf',
label: 'HDF EFFECT',