@sessile/core 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,189 @@
1
+ /**
2
+ * Geometric-optics 1D bezel refraction profile LUT (Snell's law + anti-caustic fold clamping).
3
+ *
4
+ * Precomputes 256-sample `Float32Array` lookup tables for named bezel cross-sections
5
+ * (`squircle`, `circle`, `parabolic`, `lip`) or custom height functions, allowing per-pixel
6
+ * displacement map rasterizers to evaluate physical ray refraction via cheap linear
7
+ * interpolation with zero runtime dependencies or per-frame GC churn.
8
+ *
9
+ * SPDX-License-Identifier: MPL-2.0
10
+ */
11
+ export const DEFAULT_REFRACTION_SAMPLES = 256;
12
+ export const DEFAULT_REFRACTION_IOR = 1.5;
13
+ /** Maximum inward displacement decrease per px (`-d(shift)/du < 1`) to keep mapping injective. */
14
+ export const CAUSTIC_FOLD_LIMIT = 0.85;
15
+ /** Lower bound on blended SDF gradient norm in `smin` necks to avoid 180-degree saddle flips. */
16
+ export const NORMAL_FLOOR = 0.25;
17
+ const MAX_LUT_CACHE_ENTRIES = 64;
18
+ const squircleHeight = (t) => Math.pow(Math.max(0, 1 - Math.pow(Math.max(0, 1 - t), 4)), 0.25);
19
+ /** Built-in physical bezel cross-section height functions (`t = 0` outer rim, `t = 1` plateau). */
20
+ export const BEZEL_HEIGHT_PROFILES = {
21
+ squircle: squircleHeight,
22
+ circle: (t) => Math.sqrt(Math.max(0, 1 - (1 - t) * (1 - t))),
23
+ parabolic: (t) => 1 - (1 - t) * (1 - t),
24
+ lip: (t) => squircleHeight(t) + 0.14 * Math.pow(Math.sin(Math.PI * t), 2),
25
+ };
26
+ const customProfileIds = new WeakMap();
27
+ let nextCustomProfileId = 0;
28
+ /** Returns a stable cache key fragment for a named profile or custom `BezelHeightFn`. */
29
+ export function getRefractionProfileKey(profile) {
30
+ if (profile === undefined)
31
+ return 'squircle';
32
+ if (typeof profile !== 'function')
33
+ return profile;
34
+ let id = customProfileIds.get(profile);
35
+ if (id === undefined) {
36
+ id = nextCustomProfileId++;
37
+ customProfileIds.set(profile, id);
38
+ }
39
+ return `fn${id}`;
40
+ }
41
+ function clamp11(v) {
42
+ return v <= -1 ? -1 : v >= 1 ? 1 : v;
43
+ }
44
+ /**
45
+ * Computes a 1D bezel refraction LUT via geometric-optics Snell ray tracing
46
+ * (`sin(alpha) = n * sin(thetaT)`, `shift = z * tan(alpha - thetaT)`).
47
+ */
48
+ export function computeRefractionProfileLut(options) {
49
+ const rawSamples = options?.samples ?? DEFAULT_REFRACTION_SAMPLES;
50
+ const samples = Math.max(2, Math.floor(Number.isFinite(rawSamples) ? rawSamples : DEFAULT_REFRACTION_SAMPLES));
51
+ const displacementRatio = new Float32Array(samples);
52
+ const normalizedDisplacement = new Float32Array(samples);
53
+ const slope = new Float32Array(samples);
54
+ const height = new Float32Array(samples);
55
+ const step = 1 / (samples - 1);
56
+ const profile = options?.profile ?? 'squircle';
57
+ const fn = typeof profile === 'function'
58
+ ? profile
59
+ : (BEZEL_HEIGHT_PROFILES[profile] ?? BEZEL_HEIGHT_PROFILES.squircle);
60
+ const rawIor = options?.ior;
61
+ const safeIor = Math.max(1, rawIor !== undefined && Number.isFinite(rawIor) ? rawIor : DEFAULT_REFRACTION_IOR);
62
+ const rawRatio = options?.thicknessRatio;
63
+ const safeRatio = Math.max(0, rawRatio !== undefined && Number.isFinite(rawRatio) ? rawRatio : 1.0);
64
+ const half = step * 0.5;
65
+ for (let i = 0; i < samples; i++) {
66
+ const t = i * step;
67
+ const t0 = Math.max(0, t - half);
68
+ const t1 = Math.min(1, t + half);
69
+ const z = safeRatio * fn(t);
70
+ const dzdd = (safeRatio * (fn(t1) - fn(t0))) / (t1 - t0);
71
+ height[i] = z;
72
+ slope[i] = dzdd;
73
+ if (safeIor <= 1 + 1e-6 || safeRatio <= 0) {
74
+ displacementRatio[i] = 0;
75
+ continue;
76
+ }
77
+ const alpha = Math.atan(dzdd);
78
+ const thetaT = Math.asin(clamp11(Math.sin(alpha) / safeIor));
79
+ displacementRatio[i] = z * Math.tan(alpha - thetaT);
80
+ }
81
+ // Enforce d(u + shift(u))/du > 0 when caustics are off so steep rims compress cleanly
82
+ // instead of folding the backdrop twice across neighboring pixels.
83
+ if (!options?.caustics) {
84
+ const limit = CAUSTIC_FOLD_LIMIT * step;
85
+ for (let i = samples - 2; i >= 0; i--) {
86
+ const cap = displacementRatio[i + 1] + limit;
87
+ if (displacementRatio[i] > cap) {
88
+ displacementRatio[i] = cap;
89
+ }
90
+ }
91
+ // Hold the first interior sample at the outline so z(0)=0 does not leave a 1px unrefracted hairline.
92
+ if (displacementRatio[1] > displacementRatio[0]) {
93
+ displacementRatio[0] = displacementRatio[1];
94
+ }
95
+ }
96
+ let maxDisplacementRatio = 0;
97
+ for (let i = 0; i < samples; i++) {
98
+ const absVal = Math.abs(displacementRatio[i]);
99
+ if (absVal > maxDisplacementRatio) {
100
+ maxDisplacementRatio = absVal;
101
+ }
102
+ }
103
+ const invMax = maxDisplacementRatio > 1e-6 ? 1 / maxDisplacementRatio : 0;
104
+ for (let i = 0; i < samples; i++) {
105
+ normalizedDisplacement[i] = displacementRatio[i] * invMax;
106
+ }
107
+ return {
108
+ displacementRatio,
109
+ normalizedDisplacement,
110
+ slope,
111
+ height,
112
+ maxDisplacementRatio,
113
+ samples,
114
+ };
115
+ }
116
+ const lutCache = new Map();
117
+ /**
118
+ * Resolves a cached 1D refraction LUT using quantized optical keys so steady-state
119
+ * 60/120fps frames reuse `Float32Array` tables with zero allocations.
120
+ */
121
+ export function resolveRefractionProfileLut(options) {
122
+ const profileKey = getRefractionProfileKey(options?.profile);
123
+ const samples = Math.max(2, Math.floor(options?.samples !== undefined && Number.isFinite(options.samples)
124
+ ? options.samples
125
+ : DEFAULT_REFRACTION_SAMPLES));
126
+ const rawIor = options?.ior;
127
+ const safeIor = Math.max(1, rawIor !== undefined && Number.isFinite(rawIor) ? rawIor : DEFAULT_REFRACTION_IOR);
128
+ const rawRatio = options?.thicknessRatio;
129
+ const safeRatio = Math.max(0, rawRatio !== undefined && Number.isFinite(rawRatio) ? rawRatio : 1.0);
130
+ const causticsBit = options?.caustics ? 1 : 0;
131
+ const cacheKey = `${profileKey}|${Math.round(safeIor * 1000)}|${Math.round(safeRatio * 1000)}|${causticsBit}|${samples}`;
132
+ const hit = lutCache.get(cacheKey);
133
+ if (hit !== undefined) {
134
+ lutCache.delete(cacheKey);
135
+ lutCache.set(cacheKey, hit);
136
+ return hit;
137
+ }
138
+ const lut = computeRefractionProfileLut(options);
139
+ lutCache.set(cacheKey, lut);
140
+ if (lutCache.size > MAX_LUT_CACHE_ENTRIES) {
141
+ const oldest = lutCache.keys().next().value;
142
+ if (oldest !== undefined) {
143
+ lutCache.delete(oldest);
144
+ }
145
+ }
146
+ return lut;
147
+ }
148
+ /** Linearly interpolates a 1D refraction LUT at normalized bezel coordinate `t` in `[0, 1]`. */
149
+ export function sampleRefractionLut(table, t) {
150
+ const n = table.length;
151
+ if (n === 0)
152
+ return 0;
153
+ if (t <= 0)
154
+ return table[0];
155
+ if (t >= 1)
156
+ return table[n - 1];
157
+ const x = t * (n - 1);
158
+ const i = Math.floor(x);
159
+ const frac = x - i;
160
+ const a = table[i];
161
+ const b = table[Math.min(n - 1, i + 1)];
162
+ return a + (b - a) * frac;
163
+ }
164
+ /** Traces a vertical view ray through the bezel at `d` px inward from the outline. */
165
+ export function traceBezelRay(input, d) {
166
+ const bezel = Math.max(1e-6, input.bezel);
167
+ const thickness = Math.max(0, input.thickness);
168
+ const ior = Math.max(1, input.ior ?? DEFAULT_REFRACTION_IOR);
169
+ const fn = typeof input.profile === 'function'
170
+ ? input.profile
171
+ : (BEZEL_HEIGHT_PROFILES[input.profile ?? 'squircle'] ?? BEZEL_HEIGHT_PROFILES.squircle);
172
+ const t = Math.min(1, Math.max(0, d / bezel));
173
+ const h = 1e-4;
174
+ const t0 = Math.max(0, t - h);
175
+ const t1 = Math.min(1, t + h);
176
+ const height = thickness * fn(t);
177
+ const slope = (thickness * (fn(t1) - fn(t0))) / (t1 - t0) / bezel;
178
+ const incidence = Math.atan(slope);
179
+ const refraction = ior <= 1 + 1e-6 ? incidence : Math.asin(clamp11(Math.sin(incidence) / ior));
180
+ const deviation = incidence - refraction;
181
+ return {
182
+ incidence,
183
+ refraction,
184
+ deviation,
185
+ height,
186
+ slope,
187
+ shift: ior <= 1 + 1e-6 || thickness <= 0 ? 0 : height * Math.tan(deviation),
188
+ };
189
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Shared LRU-cached 9-slice Stored-PNG (`BTYPE=00`) displacement & alpha tile generator
3
+ * for single-primitive rounded-rectangle glass lenses.
4
+ *
5
+ * Encodes only the 4 `tilePx x tilePx` corner patches and 4 1D edge strips (`1 x tilePx`
6
+ * horizontal / `tilePx x 1` vertical) using Snell's Law LUT refraction and exact
7
+ * `cornerBend` (`radius < bezel`) geometry, letting Chromium SVG `<feMerge>` stretch edges
8
+ * and `<feFlood>` fill the plateau interior with zero rasterization during size animations
9
+ * and across shared radii.
10
+ *
11
+ * SPDX-License-Identifier: MPL-2.0
12
+ */
13
+ import { type RefractionProfileSpec } from './refractProfile.ts';
14
+ export declare const MAX_REFRACT_TILE_PX = 128;
15
+ export interface RefractTileParams {
16
+ /** Corner radius in CSS px. */
17
+ radius: number;
18
+ /** Effective lens bezel width in CSS px. */
19
+ bezel: number;
20
+ /** Optional logical CSS corner tile size; defaults to `ceil(max(radius, bezel))`. */
21
+ slice?: number;
22
+ /** Refraction cross-section profile (`'squircle'`, `'circle'`, `'parabolic'`, `'lip'`, or custom height fn). */
23
+ profile?: RefractionProfileSpec;
24
+ /** Index of refraction (`1` = air, `1.5` = glass; defaults to `1.5`). */
25
+ ior?: number;
26
+ /** Glass plateau thickness divided by bezel width (defaults to `1`). */
27
+ thicknessRatio?: number;
28
+ /** Allow steep-edge caustic folds (defaults to `false`). */
29
+ caustics?: boolean;
30
+ }
31
+ export interface RefractTilesResult {
32
+ /** Quantized cache key uniquely identifying the 8 encoded tile data URLs and peak scale. */
33
+ key: string;
34
+ /** Logical CSS px size of each corner tile and thickness of each edge strip. */
35
+ slice: number;
36
+ /** Pixel resolution encoded along each corner tile axis and strip thickness (`<= MAX_REFRACT_TILE_PX`). */
37
+ tilePx: number;
38
+ /** Clamped effective bezel width (`<= slice`) matching the tile displacement encoding. */
39
+ bezel: number;
40
+ /** Peak horizontal displacement in CSS px for `feDisplacementMap` scale calibration. */
41
+ maxDisplacementPx: number;
42
+ /** Top-left `tilePx x tilePx` Stored-PNG data URL. */
43
+ tl: string;
44
+ /** Top-right `tilePx x tilePx` Stored-PNG data URL. */
45
+ tr: string;
46
+ /** Bottom-left `tilePx x tilePx` Stored-PNG data URL. */
47
+ bl: string;
48
+ /** Bottom-right `tilePx x tilePx` Stored-PNG data URL. */
49
+ br: string;
50
+ /** Top `1 x tilePx` Stored-PNG edge strip data URL. */
51
+ top: string;
52
+ /** Bottom `1 x tilePx` Stored-PNG edge strip data URL. */
53
+ bottom: string;
54
+ /** Left `tilePx x 1` Stored-PNG edge strip data URL. */
55
+ left: string;
56
+ /** Right `tilePx x 1` Stored-PNG edge strip data URL. */
57
+ right: string;
58
+ }
59
+ /** Clears the module-level 9-slice Stored-PNG tile LRU cache (primarily for deterministic tests). */
60
+ export declare function clearRefractTilesCache(): void;
61
+ /**
62
+ * Generates (or retrieves from the 64-entry LRU cache) the 8 Stored-PNG 9-slice tiles
63
+ * (`tl`, `tr`, `bl`, `br`, `top`, `bottom`, `left`, `right`) for a single rounded-rect lens.
64
+ */
65
+ export declare function resolveRefractTiles(params: RefractTileParams): RefractTilesResult;
@@ -0,0 +1,234 @@
1
+ /**
2
+ * Shared LRU-cached 9-slice Stored-PNG (`BTYPE=00`) displacement & alpha tile generator
3
+ * for single-primitive rounded-rectangle glass lenses.
4
+ *
5
+ * Encodes only the 4 `tilePx x tilePx` corner patches and 4 1D edge strips (`1 x tilePx`
6
+ * horizontal / `tilePx x 1` vertical) using Snell's Law LUT refraction and exact
7
+ * `cornerBend` (`radius < bezel`) geometry, letting Chromium SVG `<feMerge>` stretch edges
8
+ * and `<feFlood>` fill the plateau interior with zero rasterization during size animations
9
+ * and across shared radii.
10
+ *
11
+ * SPDX-License-Identifier: MPL-2.0
12
+ */
13
+ import { clampAmp, clampChannel, computeCornerGeometry, encodeStoredRgbaPngDataUrl, ensureScanlineCapacity, tmpCornerGeom, } from "./refractMap.js";
14
+ import { DEFAULT_REFRACTION_IOR, getRefractionProfileKey, resolveRefractionProfileLut, sampleRefractionLut, } from "./refractProfile.js";
15
+ export const MAX_REFRACT_TILE_PX = 128;
16
+ const DEFAULT_THICKNESS_RATIO = 1;
17
+ const TILE_CACHE_MAX_ENTRIES = 64;
18
+ const SQRT1_2 = Math.SQRT1_2;
19
+ const tileCache = new Map();
20
+ /** Clears the module-level 9-slice Stored-PNG tile LRU cache (primarily for deterministic tests). */
21
+ export function clearRefractTilesCache() {
22
+ tileCache.clear();
23
+ }
24
+ function encodeCornerTile(slice, tilePx, rad, effBezel, invBezel, interiorGate, bendCorners, normLut, hasDisplacement, sx, sy) {
25
+ const rowBytes = 1 + tilePx * 4;
26
+ const scanlines = ensureScanlineCapacity(tilePx * rowBytes);
27
+ const step = slice / tilePx;
28
+ let offset = 0;
29
+ for (let py = 0; py < tilePx; py++) {
30
+ scanlines[offset++] = 0; // PNG filter type 0 (None)
31
+ const ly = (py + 0.5) * step;
32
+ // Distance inward from outer top (`sy < 0`) or bottom (`sy > 0`) edge.
33
+ const edgeY = sy < 0 ? ly : slice - ly;
34
+ const qy = rad - edgeY;
35
+ const qyPos = qy > 0;
36
+ for (let px = 0; px < tilePx; px++) {
37
+ const lx = (px + 0.5) * step;
38
+ // Distance inward from outer left (`sx < 0`) or right (`sx > 0`) edge.
39
+ const edgeX = sx < 0 ? lx : slice - lx;
40
+ const qx = rad - edgeX;
41
+ let sd;
42
+ let gx;
43
+ let gy;
44
+ if (qx > 0 && qyPos) {
45
+ const outLen = Math.hypot(qx, qy);
46
+ sd = outLen - rad;
47
+ if (outLen > 1e-6) {
48
+ const invLen = 1 / outLen;
49
+ gx = sx * qx * invLen;
50
+ gy = sy * qy * invLen;
51
+ }
52
+ else {
53
+ gx = sx * SQRT1_2;
54
+ gy = sy * SQRT1_2;
55
+ }
56
+ }
57
+ else if (qx > qy) {
58
+ sd = qx - rad;
59
+ gx = sx;
60
+ gy = 0;
61
+ }
62
+ else {
63
+ sd = qy - rad;
64
+ gx = 0;
65
+ gy = sy;
66
+ }
67
+ if (sd >= 0.5) {
68
+ scanlines[offset++] = 128;
69
+ scanlines[offset++] = 128;
70
+ scanlines[offset++] = 128;
71
+ scanlines[offset++] = 0;
72
+ }
73
+ else if (sd <= interiorGate) {
74
+ scanlines[offset++] = 128;
75
+ scanlines[offset++] = 128;
76
+ scanlines[offset++] = 128;
77
+ scanlines[offset++] = 255;
78
+ }
79
+ else {
80
+ const distIn = sd < 0 ? -sd : 0;
81
+ const alpha = sd <= -0.5 ? 255 : Math.round((0.5 - sd) * 255);
82
+ if (!hasDisplacement) {
83
+ scanlines[offset++] = 128;
84
+ scanlines[offset++] = 128;
85
+ scanlines[offset++] = 128;
86
+ scanlines[offset++] = alpha;
87
+ continue;
88
+ }
89
+ if (bendCorners && edgeX < effBezel && edgeY < effBezel) {
90
+ computeCornerGeometry(edgeX > 0 ? edgeX : 0, edgeY > 0 ? edgeY : 0, rad, effBezel);
91
+ const amp = clampAmp(sampleRefractionLut(normLut, tmpCornerGeom[2] * invBezel) * 127);
92
+ scanlines[offset++] = clampChannel(Math.round(128 - sx * tmpCornerGeom[0] * amp));
93
+ scanlines[offset++] = clampChannel(Math.round(128 - sy * tmpCornerGeom[1] * amp));
94
+ scanlines[offset++] = 128;
95
+ scanlines[offset++] = alpha;
96
+ }
97
+ else if (distIn >= effBezel) {
98
+ scanlines[offset++] = 128;
99
+ scanlines[offset++] = 128;
100
+ scanlines[offset++] = 128;
101
+ scanlines[offset++] = alpha;
102
+ }
103
+ else {
104
+ const amp = clampAmp(sampleRefractionLut(normLut, distIn * invBezel) * 127);
105
+ scanlines[offset++] = clampChannel(Math.round(128 - gx * amp));
106
+ scanlines[offset++] = clampChannel(Math.round(128 - gy * amp));
107
+ scanlines[offset++] = 128;
108
+ scanlines[offset++] = alpha;
109
+ }
110
+ }
111
+ }
112
+ }
113
+ return encodeStoredRgbaPngDataUrl(scanlines, tilePx, tilePx);
114
+ }
115
+ function encodeHorizontalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, hasDisplacement, sy) {
116
+ // 1px wide by `tilePx` tall: 1 filter byte + 4 RGBA bytes per scanline.
117
+ const scanlines = ensureScanlineCapacity(tilePx * 5);
118
+ const step = slice / tilePx;
119
+ let offset = 0;
120
+ for (let py = 0; py < tilePx; py++) {
121
+ scanlines[offset++] = 0;
122
+ const ly = (py + 0.5) * step;
123
+ const edgeY = sy < 0 ? ly : slice - ly;
124
+ const sd = -edgeY;
125
+ const alpha = sd <= -0.5 ? 255 : Math.round((0.5 - sd) * 255);
126
+ if (!hasDisplacement || edgeY >= effBezel) {
127
+ scanlines[offset++] = 128;
128
+ scanlines[offset++] = 128;
129
+ scanlines[offset++] = 128;
130
+ scanlines[offset++] = alpha;
131
+ }
132
+ else {
133
+ const amp = clampAmp(sampleRefractionLut(normLut, edgeY * invBezel) * 127);
134
+ scanlines[offset++] = 128;
135
+ scanlines[offset++] = clampChannel(Math.round(128 - sy * amp));
136
+ scanlines[offset++] = 128;
137
+ scanlines[offset++] = alpha;
138
+ }
139
+ }
140
+ return encodeStoredRgbaPngDataUrl(scanlines, 1, tilePx);
141
+ }
142
+ function encodeVerticalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, hasDisplacement, sx) {
143
+ // `tilePx` wide by 1px tall: 1 filter byte + `tilePx * 4` RGBA bytes.
144
+ const scanlines = ensureScanlineCapacity(1 + tilePx * 4);
145
+ const step = slice / tilePx;
146
+ let offset = 0;
147
+ scanlines[offset++] = 0;
148
+ for (let px = 0; px < tilePx; px++) {
149
+ const lx = (px + 0.5) * step;
150
+ const edgeX = sx < 0 ? lx : slice - lx;
151
+ const sd = -edgeX;
152
+ const alpha = sd <= -0.5 ? 255 : Math.round((0.5 - sd) * 255);
153
+ if (!hasDisplacement || edgeX >= effBezel) {
154
+ scanlines[offset++] = 128;
155
+ scanlines[offset++] = 128;
156
+ scanlines[offset++] = 128;
157
+ scanlines[offset++] = alpha;
158
+ }
159
+ else {
160
+ const amp = clampAmp(sampleRefractionLut(normLut, edgeX * invBezel) * 127);
161
+ scanlines[offset++] = clampChannel(Math.round(128 - sx * amp));
162
+ scanlines[offset++] = 128;
163
+ scanlines[offset++] = 128;
164
+ scanlines[offset++] = alpha;
165
+ }
166
+ }
167
+ return encodeStoredRgbaPngDataUrl(scanlines, tilePx, 1);
168
+ }
169
+ /**
170
+ * Generates (or retrieves from the 64-entry LRU cache) the 8 Stored-PNG 9-slice tiles
171
+ * (`tl`, `tr`, `bl`, `br`, `top`, `bottom`, `left`, `right`) for a single rounded-rect lens.
172
+ */
173
+ export function resolveRefractTiles(params) {
174
+ const rawRad = Number.isFinite(params.radius) ? Math.max(0, params.radius) : 0;
175
+ const rawBezel = Number.isFinite(params.bezel) ? Math.max(1e-3, params.bezel) : 1;
176
+ const slice = params.slice !== undefined && Number.isFinite(params.slice)
177
+ ? Math.max(2, params.slice)
178
+ : Math.max(2, Math.ceil(Math.max(rawRad, rawBezel)));
179
+ const rad = Math.min(rawRad, slice);
180
+ const effBezel = Math.min(rawBezel, slice);
181
+ const tilePx = Math.max(2, Math.min(MAX_REFRACT_TILE_PX, Math.ceil(slice)));
182
+ const safeIor = params.ior !== undefined && Number.isFinite(params.ior)
183
+ ? Math.max(1, params.ior)
184
+ : DEFAULT_REFRACTION_IOR;
185
+ const safeRatio = params.thicknessRatio !== undefined && Number.isFinite(params.thicknessRatio)
186
+ ? Math.max(0, params.thicknessRatio)
187
+ : DEFAULT_THICKNESS_RATIO;
188
+ const caustics = Boolean(params.caustics);
189
+ const profileSpec = params.profile ?? 'squircle';
190
+ const profileKey = getRefractionProfileKey(profileSpec);
191
+ const key = `${Math.round(slice * 100)}:${tilePx}:${Math.round(rad * 4)}:${Math.round(effBezel * 4)}:` +
192
+ `${profileKey}:${Math.round(safeIor * 100)}:${Math.round(safeRatio * 100)}:${caustics ? 1 : 0}`;
193
+ const cached = tileCache.get(key);
194
+ if (cached) {
195
+ tileCache.delete(key);
196
+ tileCache.set(key, cached);
197
+ return cached;
198
+ }
199
+ const lut = resolveRefractionProfileLut({
200
+ profile: profileSpec,
201
+ ior: safeIor,
202
+ thicknessRatio: safeRatio,
203
+ caustics,
204
+ });
205
+ const normLut = lut.normalizedDisplacement;
206
+ const hasDisplacement = lut.maxDisplacementRatio > 1e-6;
207
+ const maxDisplacementPx = effBezel * lut.maxDisplacementRatio;
208
+ const invBezel = 1 / effBezel;
209
+ const bendCorners = rad > 0 && rad < effBezel;
210
+ const interiorGate = -Math.max(effBezel, 0.5);
211
+ const result = {
212
+ key,
213
+ slice,
214
+ tilePx,
215
+ bezel: effBezel,
216
+ maxDisplacementPx,
217
+ tl: encodeCornerTile(slice, tilePx, rad, effBezel, invBezel, interiorGate, bendCorners, normLut, hasDisplacement, -1, -1),
218
+ tr: encodeCornerTile(slice, tilePx, rad, effBezel, invBezel, interiorGate, bendCorners, normLut, hasDisplacement, 1, -1),
219
+ bl: encodeCornerTile(slice, tilePx, rad, effBezel, invBezel, interiorGate, bendCorners, normLut, hasDisplacement, -1, 1),
220
+ br: encodeCornerTile(slice, tilePx, rad, effBezel, invBezel, interiorGate, bendCorners, normLut, hasDisplacement, 1, 1),
221
+ top: encodeHorizontalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, hasDisplacement, -1),
222
+ bottom: encodeHorizontalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, hasDisplacement, 1),
223
+ left: encodeVerticalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, hasDisplacement, -1),
224
+ right: encodeVerticalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, hasDisplacement, 1),
225
+ };
226
+ if (tileCache.size >= TILE_CACHE_MAX_ENTRIES) {
227
+ const oldestKey = tileCache.keys().next().value;
228
+ if (oldestKey !== undefined) {
229
+ tileCache.delete(oldestKey);
230
+ }
231
+ }
232
+ tileCache.set(key, result);
233
+ return result;
234
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sessile/core",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "license": "MPL-2.0",
6
6
  "description": "Zero-dependency pure TypeScript kernel for live-DOM continuous glass morphing.",