/** * Bakes the light maps of the landing's materials into `ui/maps/`. Run after changing a profile or * the light, `node ./scripts/landing-maps.ts` (the grain needs `cwebp`), and commit what it writes. * The profiles, lights and grain are those of `poster/build.py` in fumadocs-promotion. */ import { execFileSync } from 'node:child_process'; import { mkdirSync, rmSync, statSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { crc32, deflateSync } from 'node:zlib'; const out = join(import.meta.dirname, '../app/[lang]/(landing)/ui/maps'); mkdirSync(out, { recursive: true }); type Vec = [x: number, y: number, z: number]; const dot = (a: Vec, b: Vec) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; function norm([x, y, z]: Vec): Vec { const l = Math.hypot(x, y, z); return [x / l, y / l, z / l]; } const clamp = (v: number, min = 0, max = 1) => Math.min(max, Math.max(min, v)); const toLinear = (c: number) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4); const toSrgb = (c: number) => (c <= 0.0031308 ? c * 12.92 : 1.055 * clamp(c) ** (1 / 2.4) - 0.055); /** * x right, y down, z toward the camera; `power` is the energy over the distance squared, `size` the * angular radius. */ const lights = [ { dir: norm([-120, -150, 220]), power: 700000 / 85300, size: 50 / 292 }, { dir: norm([90, 150, 180]), power: 80000 / 63000, size: 120 / 251 }, { dir: [0, 0, 1] as Vec, power: 110000 / 90000, size: 150 / 300 }, ]; /** the grey world, and the white body's light thrown back onto walls that face it */ const ambient = 4; const bounce = 7; const up: Vec = [0, 0, 1]; function irradiance(n: Vec): number { let e = (ambient * (1 + n[2])) / 2 + (bounce * (1 - n[2])) / 2; for (const l of lights) e += l.power * Math.max(0, dot(n, l.dir)); return e; } const flat = irradiance(up); /** GGX with Fresnel at 4%, each lobe widened by its light's size, in units of flat white diffuse */ function specular(n: Vec, roughness: number): number { let s = (ambient / flat) * (0.04 + 0.96 * (1 - n[2]) ** 5); for (const l of lights) { const h = norm([l.dir[0], l.dir[1], l.dir[2] + 1]); const a2 = roughness ** 4 + (l.size / 2) ** 2; const c = Math.max(0, dot(n, h)); const d = a2 / (Math.PI * (c * c * (a2 - 1) + 1) ** 2); const fresnel = 0.04 + 0.96 * (1 - h[2]) ** 5; if (dot(n, l.dir) > 0) s += (fresnel * d * l.power * Math.PI) / (4 * Math.max(n[2], 0.2) * flat); } return s; } /** * A `hard-light` map darkens by what a white cap loses and lightens by what a graphite cap gains, * as white clips before it gains any, so the black edition's rims catch the light from one map. */ const white = toLinear(0xf4 / 255); const graphite = toLinear(0x33 / 255); function hardLight(diffuse: number, reflection: number): number { const dark = toSrgb(graphite * diffuse + reflection); if (dark >= toSrgb(graphite)) return 0.5 + (dark - toSrgb(graphite)) / (1 - toSrgb(graphite)) / 2; return toSrgb(white * diffuse + reflection) / toSrgb(white) / 2; } function png(name: string, w: number, h: number, value: (x: number, y: number) => number) { const raw = Buffer.alloc((w + 1) * h); for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) raw[y * (w + 1) + 1 + x] = Math.round(clamp(value(x, y)) * 255); const chunk = (type: string, data: Buffer) => { const b = Buffer.alloc(12 + data.length); b.writeUInt32BE(data.length, 0); b.write(type, 4, 'ascii'); data.copy(b, 8); b.writeUInt32BE(crc32(b.subarray(4, 8 + data.length)), 8 + data.length); return b; }; // 8-bit grayscale const header = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0]); header.writeUInt32BE(w, 0); header.writeUInt32BE(h, 4); const file = Buffer.concat([ Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', header), chunk('IDAT', deflateSync(raw, { level: 9 })), chunk('IEND', Buffer.alloc(0)), ]); writeFileSync(join(out, name), file); console.log(`${name}: ${w}x${h}, ${file.length} B`); } function profile(points: [inset: number, height: number][]) { return (i: number) => { if (i <= points[0][0]) return points[0][1]; for (let k = 1; k < points.length; k++) { const [i1, z1] = points[k]; if (i <= i1) { const [i0, z0] = points[k - 1]; return z0 + ((z1 - z0) * (i - i0)) / (i1 - i0 || 1); } } return points[points.length - 1][1]; }; } /** `fillet()` of build.py */ function fillet(top: number, f: number): [number, number][] { const points: [number, number][] = []; for (let i = 0; i <= 32; i++) { const a = (Math.PI / 2) * (i / 32); points.push([f * (1 - Math.cos(a)), top - f * (1 - Math.sin(a))]); } return points; } /** `soft()` of build.py */ function soft(half: number, rise: number, f: number, dish: number) { const points = fillet(rise, f); for (let i = 1; i <= 24; i++) { const t = i / 24; points.push([f + (half - f - 0.2) * t, rise - dish * (1 - (1 - t) ** 2)]); } return profile(points); } /** outlines as the distance in from them, in mm from the centre */ const disc = (d: number) => (x: number, y: number) => d / 2 - Math.hypot(x, y); function rounded(w: number, h: number, r: number) { return (x: number, y: number) => { const qx = Math.abs(x) - w / 2 + r; const qy = Math.abs(y) - h / 2 + r; return r - Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) - Math.min(Math.max(qx, qy), 0); }; } /** maps hold 12 pixels a millimetre: 1.7 device pixels for each of the densest screen's */ const PX = 12; function shaded( name: string, w: number, h: number, inset: (x: number, y: number) => number, z: (inset: number) => number, shade: (n: Vec) => number, ) { const e = 0.01; const height = (x: number, y: number) => z(Math.max(0, inset(x, y))); png(name, Math.round(w * PX), Math.round(h * PX), (px, py) => { let sum = 0; for (let sy = 0; sy < 3; sy++) for (let sx = 0; sx < 3; sx++) { const x = (px + (sx + 0.5) / 3) / PX - w / 2; const y = (py + (sy + 0.5) / 3) / PX - h / 2; const dx = (height(x + e, y) - height(x - e, y)) / (2 * e); const dy = (height(x, y + e) - height(x, y - e)) / (2 * e); sum += shade(norm([-dx, -dy, 1])); } return sum / 9; }); } /** build.py's `cap`, `orange` and `graphite` materials */ const smooth = (roughness: number) => (n: Vec) => hardLight(irradiance(n) / flat, specular(n, roughness) - specular(up, roughness)); const key = soft(4.2, 1, 0.95, 0.28); shaded('cap.png', 8.4, 8.4, disc(8.4), key, smooth(0.48)); shaded('pill.png', 19.4, 8.4, rounded(19.4, 8.4, 4.2), key, smooth(0.48)); // fillets are millimetres whatever the size, so each size has its map for (const [name, d, f] of [ ['knob', 12.4, 0.6], ['knob-cap', 6.2, 0.45], ['collar', 12.8, 0.3], ['dial', 9, 0.45], ['encoder-collar', 6.6, 0.3], ['encoder', 4.6, 0.45], ] as const) shaded(`${name}.png`, d, d, disc(d), profile(fillet(1, f)), smooth(0.48)); shaded('fader.png', 3.4, 8.4, rounded(3.4, 8.4, 1.1), profile(fillet(2, 0.6)), smooth(0.48)); shaded('thumb.png', 4.4, 2.2, rounded(4.4, 2.2, 1), profile(fillet(1, 0.45)), smooth(0.48)); shaded('switch.png', 6, 2.8, rounded(6, 2.8, 1.3), profile(fillet(1, 0.8)), smooth(0.5)); /** * `_grained()` of build.py, each pixel averaging 36 samples as the poster's render does. An * `overlay` layer, so its mean leaves the colour as it is; tuned for white, it is stronger on * graphite, as a dark blasted surface shows its grain in its reflections. */ { const G = 6.4; const size = 40 * G; let seed = 1; const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647; function hash(x: number, y: number, z: number) { let h = Math.imul(x, 0x27d4eb2d) ^ Math.imul(y, 0x165667b1) ^ Math.imul(z, 0x61c88647); h = Math.imul(h ^ (h >>> 15), 0x85ebca6b); h = Math.imul(h ^ (h >>> 13), 0xc2b2ae35); return (h ^ (h >>> 16)) & 15; } /** Perlin's improved noise: the 12 edges of a cube, 4 twice */ const gradients: Vec[] = [ [1, 1, 0], [-1, 1, 0], [1, -1, 0], [-1, -1, 0], [1, 0, 1], [-1, 0, 1], [1, 0, -1], [-1, 0, -1], [0, 1, 1], [0, -1, 1], [0, 1, -1], [0, -1, -1], [1, 1, 0], [0, -1, 1], [-1, 1, 0], [0, -1, -1], ]; const fade = (t: number) => t * t * t * (t * (t * 6 - 15) + 10); const slope = (t: number) => 30 * t * t * (t - 1) * (t - 1); /** Blender's 3D Perlin noise, sliced: blotchier than 2D noise, as the poster's is */ function perlin(cells: number, z = 0.37) { const wrap = (i: number) => ((i % cells) + cells) % cells; return (x: number, y: number, d: number[]) => { const xi = Math.floor(x); const yi = Math.floor(y); const fx = x - xi; const fy = y - yi; const fz = z - Math.floor(z); const u = fade(fx); const v = fade(fy); let n = 0; d[0] = 0; d[1] = 0; for (let k = 0; k < 2; k++) for (let j = 0; j < 2; j++) for (let i = 0; i < 2; i++) { const g = gradients[hash(wrap(xi + i), wrap(yi + j), Math.floor(z) + k)]; const value = g[0] * (fx - i) + g[1] * (fy - j) + g[2] * (fz - k); const wx = i ? u : 1 - u; const wy = j ? v : 1 - v; const wz = k ? fade(fz) : 1 - fade(fz); n += wz * wx * wy * value; d[0] += wz * wy * ((i ? 1 : -1) * slope(fx) * value + wx * g[0]); d[1] += wz * wx * ((j ? 1 : -1) * slope(fy) * value + wy * g[1]); } d[0] *= 0.982; d[1] *= 0.982; return 0.982 * n; }; } /** Blender's normalised fractal noise */ function fractal(perMm: number, detail: number, roughness: number) { const octaves = Array.from({ length: detail + 1 }, (_, i) => ({ noise: perlin(perMm * 40 * 2 ** i), scale: perMm * 2 ** i, amp: roughness ** i, })); let total = 0; for (const o of octaves) total += o.amp; const d = [0, 0]; return (x: number, y: number, grad: number[]) => { let v = 0; grad[0] = 0; grad[1] = 0; for (const o of octaves) { v += o.amp * o.noise(x * o.scale, y * o.scale, d); grad[0] += (o.amp * o.scale * d[0]) / total / 2; grad[1] += (o.amp * o.scale * d[1]) / total / 2; } return 0.5 + v / total / 2; }; } const coarse = fractal(2.6, 3, 0.62); const fine = fractal(12, 2, 0.5); const dark = 0.2126 * toLinear(0xe6 / 255) + 0.7152 * toLinear(0xe4 / 255) + 0.0722 * toLinear(0xde / 255); const light = 0.2126 * toLinear(0xf4 / 255) + 0.7152 * toLinear(0xf3 / 255) + 0.0722 * toLinear(0xef / 255); const g1 = [0, 0]; const g2 = [0, 0]; const linear = new Float64Array(size * size); let mean = 0; for (let py = 0; py < size; py++) for (let px = 0; px < size; px++) { let sum = 0; for (let s = 0; s < 36; s++) { const x = (px + ((s % 6) + random()) / 6) / G; const y = (py + (Math.floor(s / 6) + random()) / 6) / G; const c = coarse(x, y, g1); const f = fine(x, y, g2); const bumped = norm([-0.042 * (g1[0] + 0.45 * g2[0]), -0.042 * (g1[1] + 0.45 * g2[1]), 1]); const n = norm([0.55 * bumped[0], 0.55 * bumped[1], 0.55 * bumped[2] + 0.45]); const albedo = dark + (light - dark) * clamp((c - 0.34) / 0.32); sum += (albedo * irradiance(n)) / flat + specular(n, 0.4 + 0.14 * f); } linear[py * size + px] = sum / 36; mean += sum / 36 / (size * size); } // over the body's colour, `overlay` moves it by 2 (1 - body) (map - 0.5) const body = 0.941; const gain = body / (2 * (1 - body)); png('grain.png', size, size, (x, y) => { const ratio = toSrgb(toLinear(body) * (linear[y * size + x] / mean)) / body; return 0.5 + gain * (ratio - 1); }); // noise doesn't deflate: a lossy WebP keeps its mean at a fraction of the PNG execFileSync('cwebp', [ '-quiet', '-q', '70', join(out, 'grain.png'), '-o', join(out, 'grain.webp'), ]); rmSync(join(out, 'grain.png')); console.log(`grain.webp: ${size}x${size}, ${statSync(join(out, 'grain.webp')).size} B`); }