@sessile/core 0.1.0 → 0.2.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.
- package/dist/contracts.d.ts +11 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/optics.js +13 -0
- package/dist/overlayRenderer.d.ts +1 -1
- package/dist/overlayRenderer.js +84 -15
- package/dist/physics.d.ts +18 -0
- package/dist/physics.js +50 -2
- package/dist/refractMap.d.ts +27 -1
- package/dist/refractMap.js +152 -92
- package/dist/refractProfile.d.ts +84 -0
- package/dist/refractProfile.js +189 -0
- package/dist/refractTiles.d.ts +65 -0
- package/dist/refractTiles.js +234 -0
- package/package.json +1 -1
package/dist/refractMap.js
CHANGED
|
@@ -8,11 +8,12 @@
|
|
|
8
8
|
*
|
|
9
9
|
* SPDX-License-Identifier: MPL-2.0
|
|
10
10
|
*/
|
|
11
|
+
import { DEFAULT_REFRACTION_IOR, NORMAL_FLOOR, getRefractionProfileKey, resolveRefractionProfileLut, sampleRefractionLut, } from "./refractProfile.js";
|
|
11
12
|
const PNG_SIGNATURE = new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
12
13
|
const PNG_IEND = new Uint8Array([0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130]);
|
|
13
14
|
const MAX_DEFLATE_BLOCK = 65535;
|
|
14
15
|
const DEFAULT_MAP_SCALE = 0.5;
|
|
15
|
-
const
|
|
16
|
+
const DEFAULT_THICKNESS_RATIO = 1;
|
|
16
17
|
const MIN_PRIMITIVE_SIZE = 0.01;
|
|
17
18
|
const SQRT1_2 = Math.SQRT1_2;
|
|
18
19
|
// 4-table slicing IEEE 802.3 CRC32 lookup (1024 x uint32).
|
|
@@ -119,11 +120,14 @@ let rowDySigned = new Float64Array(0);
|
|
|
119
120
|
let rowAbsDy = new Float64Array(0);
|
|
120
121
|
let rowQy = new Float64Array(0);
|
|
121
122
|
let rowSy = new Float64Array(0);
|
|
123
|
+
/** @internal Reusable 3-element scratch vector [ux, uy, distIn] for corner-bend geometry without heap allocation. */
|
|
124
|
+
export const tmpCornerGeom = new Float64Array(3);
|
|
122
125
|
// Reusable 2-element scratch vector for corner-bend outward unit gradient without heap allocation.
|
|
123
126
|
const tmpCornerDir = new Float64Array(2);
|
|
124
127
|
// Reusable index buffer for active primitives per build, avoiding per-frame array allocations.
|
|
125
128
|
let activeIndicesBuf = new Int32Array(0);
|
|
126
|
-
|
|
129
|
+
/** @internal */
|
|
130
|
+
export function ensureScanlineCapacity(needed) {
|
|
127
131
|
if (scanlineBuf.length < needed) {
|
|
128
132
|
scanlineBuf = new Uint8Array(Math.max(needed, scanlineBuf.length * 2, 16384));
|
|
129
133
|
}
|
|
@@ -164,7 +168,8 @@ function ensureSoaCapacity(needed) {
|
|
|
164
168
|
rowQy = new Float64Array(nextCap);
|
|
165
169
|
rowSy = new Float64Array(nextCap);
|
|
166
170
|
}
|
|
167
|
-
|
|
171
|
+
/** @internal */
|
|
172
|
+
export function encodeStoredRgbaPngDataUrl(scanlines, pngW, pngH) {
|
|
168
173
|
const rowBytes = 1 + pngW * 4;
|
|
169
174
|
const rawLen = pngH * rowBytes;
|
|
170
175
|
const numBlocks = Math.max(1, Math.ceil(rawLen / MAX_DEFLATE_BLOCK));
|
|
@@ -232,13 +237,26 @@ function encodeStoredRgbaPngDataUrl(scanlines, pngW, pngH) {
|
|
|
232
237
|
function clamp01(v) {
|
|
233
238
|
return v <= 0 ? 0 : v >= 1 ? 1 : v;
|
|
234
239
|
}
|
|
240
|
+
/** @internal */
|
|
241
|
+
export function clampAmp(v) {
|
|
242
|
+
return v <= -127 ? -127 : v >= 127 ? 127 : v;
|
|
243
|
+
}
|
|
244
|
+
/** @internal */
|
|
245
|
+
export function clampChannel(v) {
|
|
246
|
+
return v <= 0 ? 0 : v >= 255 ? 255 : v;
|
|
247
|
+
}
|
|
235
248
|
function quant4(v) {
|
|
236
249
|
return Math.round(v * 4);
|
|
237
250
|
}
|
|
238
251
|
function quant100(v) {
|
|
239
252
|
return Math.round(v * 100);
|
|
240
253
|
}
|
|
241
|
-
|
|
254
|
+
/**
|
|
255
|
+
* @internal
|
|
256
|
+
* Solves corner-bend inward distance and first-quadrant outward unit direction
|
|
257
|
+
* into `tmpCornerGeom` (`[ux, uy, distIn]`) without heap allocations.
|
|
258
|
+
*/
|
|
259
|
+
export function computeCornerGeometry(x, y, radius, bezel) {
|
|
242
260
|
let lo = 0;
|
|
243
261
|
let hi = Math.min(x, y, bezel);
|
|
244
262
|
for (let i = 0; i < 10; i++) {
|
|
@@ -261,27 +279,25 @@ function cornerBend(x, y, radius, bezel, falloff) {
|
|
|
261
279
|
const nx = centerCoord - x > 0 ? centerCoord - x : 0;
|
|
262
280
|
const ny = centerCoord - y > 0 ? centerCoord - y : 0;
|
|
263
281
|
const normal = Math.hypot(nx, ny);
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
282
|
+
if (normal <= 1e-6) {
|
|
283
|
+
tmpCornerGeom[0] = x < y ? 1 : 0;
|
|
284
|
+
tmpCornerGeom[1] = x < y ? 0 : 1;
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
const invN = 1 / normal;
|
|
288
|
+
tmpCornerGeom[0] = nx * invN;
|
|
289
|
+
tmpCornerGeom[1] = ny * invN;
|
|
290
|
+
}
|
|
291
|
+
tmpCornerGeom[2] = lo;
|
|
268
292
|
}
|
|
269
293
|
/**
|
|
270
|
-
* Computes the unit outward gradient matching `
|
|
294
|
+
* Computes the unit outward gradient matching `computeCornerGeometry` inward deflection,
|
|
271
295
|
* stored into `tmpCornerDir` to avoid per-pixel allocations in blend zones.
|
|
272
296
|
*/
|
|
273
|
-
function computeCornerBendOutwardDir(edgeX, edgeY, rad, bezel,
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const invO = 1 / oLen;
|
|
278
|
-
tmpCornerDir[0] = sx * ox * invO;
|
|
279
|
-
tmpCornerDir[1] = sy * oy * invO;
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
tmpCornerDir[0] = fallbackGx;
|
|
283
|
-
tmpCornerDir[1] = fallbackGy;
|
|
284
|
-
}
|
|
297
|
+
function computeCornerBendOutwardDir(edgeX, edgeY, rad, bezel, sx, sy) {
|
|
298
|
+
computeCornerGeometry(edgeX > 0 ? edgeX : 0, edgeY > 0 ? edgeY : 0, rad, bezel);
|
|
299
|
+
tmpCornerDir[0] = sx * tmpCornerGeom[0];
|
|
300
|
+
tmpCornerDir[1] = sy * tmpCornerGeom[1];
|
|
285
301
|
}
|
|
286
302
|
function collectActivePrimitiveIndices(primitives) {
|
|
287
303
|
const len = primitives.length;
|
|
@@ -295,9 +311,10 @@ function collectActivePrimitiveIndices(primitives) {
|
|
|
295
311
|
}
|
|
296
312
|
return count;
|
|
297
313
|
}
|
|
298
|
-
function buildQuantizedKey(cssW, cssH, pngW, pngH, bounds, primitives, activeCount, blendK, bezel,
|
|
314
|
+
function buildQuantizedKey(cssW, cssH, pngW, pngH, bounds, primitives, activeCount, blendK, bezel, profileKey, ior, thicknessRatio, caustics) {
|
|
299
315
|
const base = `${cssW}:${cssH}:${pngW}:${pngH}:${quant4(bounds.x)}:${quant4(bounds.y)}:` +
|
|
300
|
-
`${quant4(bezel)}:${
|
|
316
|
+
`${quant4(bezel)}:${quant4(blendK)}:` +
|
|
317
|
+
`${profileKey}:${quant100(ior)}:${quant100(thicknessRatio)}:${caustics ? 1 : 0}`;
|
|
301
318
|
if (activeCount === 0) {
|
|
302
319
|
return `${base}:EMPTY`;
|
|
303
320
|
}
|
|
@@ -322,7 +339,7 @@ function rasterizeEmptyScanlines(scanlines, pngW, pngH) {
|
|
|
322
339
|
}
|
|
323
340
|
}
|
|
324
341
|
}
|
|
325
|
-
function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, shape, safeBezel,
|
|
342
|
+
function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, shape, safeBezel, lut) {
|
|
326
343
|
const cx = shape.center.x;
|
|
327
344
|
const cy = shape.center.y;
|
|
328
345
|
const halfW = Math.max(0.001, shape.size.width * 0.5);
|
|
@@ -334,6 +351,9 @@ function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
334
351
|
const invBezel = 1 / effBezel;
|
|
335
352
|
const interiorGate = -Math.max(effBezel, 0.5);
|
|
336
353
|
const bendCorners = rad > 0 && rad < effBezel;
|
|
354
|
+
const normLut = lut.normalizedDisplacement;
|
|
355
|
+
const hasDisplacement = lut.maxDisplacementRatio > 1e-6;
|
|
356
|
+
const localScale = effBezel / safeBezel;
|
|
337
357
|
let offset = 0;
|
|
338
358
|
for (let py = 0; py < pngH; py++) {
|
|
339
359
|
scanlines[offset++] = 0; // PNG filter type 0 (None)
|
|
@@ -389,11 +409,20 @@ function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
389
409
|
else {
|
|
390
410
|
const distIn = sd < 0 ? -sd : 0;
|
|
391
411
|
const alpha = sd <= -0.5 ? 255 : Math.round((0.5 - sd) * 255);
|
|
412
|
+
if (!hasDisplacement) {
|
|
413
|
+
scanlines[offset++] = 128;
|
|
414
|
+
scanlines[offset++] = 128;
|
|
415
|
+
scanlines[offset++] = 128;
|
|
416
|
+
scanlines[offset++] = alpha;
|
|
417
|
+
continue;
|
|
418
|
+
}
|
|
392
419
|
const edgeX = halfW - absDx;
|
|
393
420
|
if (bendCorners && edgeX < effBezel && edgeY < effBezel) {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
421
|
+
computeCornerGeometry(edgeX > 0 ? edgeX : 0, edgeY > 0 ? edgeY : 0, rad, effBezel);
|
|
422
|
+
const m = sampleRefractionLut(normLut, tmpCornerGeom[2] * invBezel) * localScale;
|
|
423
|
+
const amp = clampAmp(m * 127);
|
|
424
|
+
scanlines[offset++] = clampChannel(Math.round(128 - sx * tmpCornerGeom[0] * amp));
|
|
425
|
+
scanlines[offset++] = clampChannel(Math.round(128 - sy * tmpCornerGeom[1] * amp));
|
|
397
426
|
scanlines[offset++] = 128;
|
|
398
427
|
scanlines[offset++] = alpha;
|
|
399
428
|
}
|
|
@@ -404,10 +433,10 @@ function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
404
433
|
scanlines[offset++] = alpha;
|
|
405
434
|
}
|
|
406
435
|
else {
|
|
407
|
-
const m =
|
|
408
|
-
const amp = m * 127;
|
|
409
|
-
scanlines[offset++] = Math.round(128 - gx * amp);
|
|
410
|
-
scanlines[offset++] = Math.round(128 - gy * amp);
|
|
436
|
+
const m = sampleRefractionLut(normLut, distIn * invBezel) * localScale;
|
|
437
|
+
const amp = clampAmp(m * 127);
|
|
438
|
+
scanlines[offset++] = clampChannel(Math.round(128 - gx * amp));
|
|
439
|
+
scanlines[offset++] = clampChannel(Math.round(128 - gy * amp));
|
|
411
440
|
scanlines[offset++] = 128;
|
|
412
441
|
scanlines[offset++] = alpha;
|
|
413
442
|
}
|
|
@@ -415,7 +444,7 @@ function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
415
444
|
}
|
|
416
445
|
}
|
|
417
446
|
}
|
|
418
|
-
function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, shapeA, shapeB, blendK, safeBezel,
|
|
447
|
+
function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, shapeA, shapeB, blendK, safeBezel, lut) {
|
|
419
448
|
const pCx = shapeA.center.x;
|
|
420
449
|
const pCy = shapeA.center.y;
|
|
421
450
|
const pHalfW = Math.max(0.001, shapeA.size.width * 0.5);
|
|
@@ -440,6 +469,9 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
440
469
|
const smoothUnion = blendK > 0.001;
|
|
441
470
|
const inv2K = smoothUnion ? 0.5 / blendK : 0;
|
|
442
471
|
const interiorGate = -Math.max(maxBezel, 0.5);
|
|
472
|
+
const normLut = lut.normalizedDisplacement;
|
|
473
|
+
const hasDisplacement = lut.maxDisplacementRatio > 1e-6;
|
|
474
|
+
const invSafeBezel = 1 / safeBezel;
|
|
443
475
|
let offset = 0;
|
|
444
476
|
for (let py = 0; py < pngH; py++) {
|
|
445
477
|
scanlines[offset++] = 0;
|
|
@@ -535,7 +567,7 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
535
567
|
if (pBendCorners && sdA < 0 && -sdA < pBezel) {
|
|
536
568
|
const pEdgeX = pHalfW - pAbsDx;
|
|
537
569
|
if (pEdgeX < pBezel && pEdgeY < pBezel) {
|
|
538
|
-
computeCornerBendOutwardDir(pEdgeX, pEdgeY, pRad, pBezel,
|
|
570
|
+
computeCornerBendOutwardDir(pEdgeX, pEdgeY, pRad, pBezel, pSx, pSy);
|
|
539
571
|
mixAx = tmpCornerDir[0];
|
|
540
572
|
mixAy = tmpCornerDir[1];
|
|
541
573
|
}
|
|
@@ -545,7 +577,7 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
545
577
|
if (dBendCorners && sdB < 0 && -sdB < dBezel) {
|
|
546
578
|
const dEdgeX = dHalfW - dAbsDx;
|
|
547
579
|
if (dEdgeX < dBezel && dEdgeY < dBezel) {
|
|
548
|
-
computeCornerBendOutwardDir(dEdgeX, dEdgeY, dRad, dBezel,
|
|
580
|
+
computeCornerBendOutwardDir(dEdgeX, dEdgeY, dRad, dBezel, dSx, dSy);
|
|
549
581
|
mixBx = tmpCornerDir[0];
|
|
550
582
|
mixBy = tmpCornerDir[1];
|
|
551
583
|
}
|
|
@@ -593,18 +625,28 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
593
625
|
else {
|
|
594
626
|
const distIn = sd < 0 ? -sd : 0;
|
|
595
627
|
const alpha = sd <= -0.5 ? 255 : Math.round((0.5 - sd) * 255);
|
|
628
|
+
if (!hasDisplacement) {
|
|
629
|
+
scanlines[offset++] = 128;
|
|
630
|
+
scanlines[offset++] = 128;
|
|
631
|
+
scanlines[offset++] = 128;
|
|
632
|
+
scanlines[offset++] = alpha;
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
596
635
|
if (!blended) {
|
|
597
636
|
const winBezel = winIsA ? pBezel : dBezel;
|
|
598
637
|
const winInvBezel = winIsA ? pInvBezel : dInvBezel;
|
|
638
|
+
const winScale = winBezel * invSafeBezel;
|
|
599
639
|
const winRad = winIsA ? pRad : dRad;
|
|
600
640
|
const winEdgeX = winIsA ? pHalfW - pAbsDx : dHalfW - dAbsDx;
|
|
601
641
|
const winEdgeY = winIsA ? pEdgeY : dEdgeY;
|
|
602
642
|
const winSx = winIsA ? pSx : dSx;
|
|
603
643
|
const winSy = winIsA ? pSy : dSy;
|
|
604
644
|
if (winRad > 0 && winRad < winBezel && winEdgeX < winBezel && winEdgeY < winBezel) {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
645
|
+
computeCornerGeometry(winEdgeX > 0 ? winEdgeX : 0, winEdgeY > 0 ? winEdgeY : 0, winRad, winBezel);
|
|
646
|
+
const m = sampleRefractionLut(normLut, tmpCornerGeom[2] * winInvBezel) * winScale;
|
|
647
|
+
const amp = clampAmp(m * 127);
|
|
648
|
+
scanlines[offset++] = clampChannel(Math.round(128 - winSx * tmpCornerGeom[0] * amp));
|
|
649
|
+
scanlines[offset++] = clampChannel(Math.round(128 - winSy * tmpCornerGeom[1] * amp));
|
|
608
650
|
scanlines[offset++] = 128;
|
|
609
651
|
scanlines[offset++] = alpha;
|
|
610
652
|
}
|
|
@@ -615,10 +657,10 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
615
657
|
scanlines[offset++] = alpha;
|
|
616
658
|
}
|
|
617
659
|
else {
|
|
618
|
-
const m =
|
|
619
|
-
const amp = m * 127;
|
|
620
|
-
scanlines[offset++] = Math.round(128 - gx * amp);
|
|
621
|
-
scanlines[offset++] = Math.round(128 - gy * amp);
|
|
660
|
+
const m = sampleRefractionLut(normLut, distIn * winInvBezel) * winScale;
|
|
661
|
+
const amp = clampAmp(m * 127);
|
|
662
|
+
scanlines[offset++] = clampChannel(Math.round(128 - gx * amp));
|
|
663
|
+
scanlines[offset++] = clampChannel(Math.round(128 - gy * amp));
|
|
622
664
|
scanlines[offset++] = 128;
|
|
623
665
|
scanlines[offset++] = alpha;
|
|
624
666
|
}
|
|
@@ -633,17 +675,15 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
633
675
|
}
|
|
634
676
|
else {
|
|
635
677
|
const gLen = Math.hypot(gx, gy);
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
scanlines[offset++] = Math.round(128 + nx * amp);
|
|
646
|
-
scanlines[offset++] = Math.round(128 + ny * amp);
|
|
678
|
+
const denom = gLen > NORMAL_FLOOR ? gLen : NORMAL_FLOOR;
|
|
679
|
+
const invG = -1 / denom;
|
|
680
|
+
const nx = gx * invG;
|
|
681
|
+
const ny = gy * invG;
|
|
682
|
+
const blendScale = blendBezel * invSafeBezel;
|
|
683
|
+
const m = sampleRefractionLut(normLut, distIn / blendBezel) * blendScale;
|
|
684
|
+
const amp = clampAmp(m * 127);
|
|
685
|
+
scanlines[offset++] = clampChannel(Math.round(128 + nx * amp));
|
|
686
|
+
scanlines[offset++] = clampChannel(Math.round(128 + ny * amp));
|
|
647
687
|
scanlines[offset++] = 128;
|
|
648
688
|
scanlines[offset++] = alpha;
|
|
649
689
|
}
|
|
@@ -652,7 +692,7 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
652
692
|
}
|
|
653
693
|
}
|
|
654
694
|
}
|
|
655
|
-
function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives, activeCount, blendK, safeBezel,
|
|
695
|
+
function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives, activeCount, blendK, safeBezel, lut) {
|
|
656
696
|
ensureSoaCapacity(activeCount);
|
|
657
697
|
let maxBezel = 1e-3;
|
|
658
698
|
for (let i = 0; i < activeCount; i++) {
|
|
@@ -685,6 +725,9 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
685
725
|
const interiorGate = -Math.max(maxBezel, 0.5);
|
|
686
726
|
const outerThreshold = 0.5 + effectiveBlendK;
|
|
687
727
|
const outerThresholdSq = outerThreshold * outerThreshold;
|
|
728
|
+
const normLut = lut.normalizedDisplacement;
|
|
729
|
+
const hasDisplacement = lut.maxDisplacementRatio > 1e-6;
|
|
730
|
+
const invSafeBezel = 1 / safeBezel;
|
|
688
731
|
let offset = 0;
|
|
689
732
|
for (let py = 0; py < pngH; py++) {
|
|
690
733
|
scanlines[offset++] = 0;
|
|
@@ -833,7 +876,7 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
833
876
|
const wEdgeX = soaHalfW[winIdx] - winAbsDx;
|
|
834
877
|
const wEdgeY = soaHalfH[winIdx] - rowAbsDy[winIdx];
|
|
835
878
|
if (wEdgeX < wBezel && wEdgeY < wBezel) {
|
|
836
|
-
computeCornerBendOutwardDir(wEdgeX, wEdgeY, wRad, wBezel,
|
|
879
|
+
computeCornerBendOutwardDir(wEdgeX, wEdgeY, wRad, wBezel, winSx, rowSy[winIdx]);
|
|
837
880
|
accGx = tmpCornerDir[0];
|
|
838
881
|
accGy = tmpCornerDir[1];
|
|
839
882
|
}
|
|
@@ -845,25 +888,16 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
845
888
|
const bEdgeX = soaHalfW[i] - absDx;
|
|
846
889
|
const bEdgeY = soaHalfH[i] - rowAbsDy[i];
|
|
847
890
|
if (bEdgeX < bBezel && bEdgeY < bBezel) {
|
|
848
|
-
computeCornerBendOutwardDir(bEdgeX, bEdgeY, rad, bBezel,
|
|
891
|
+
computeCornerBendOutwardDir(bEdgeX, bEdgeY, rad, bBezel, sx, sy);
|
|
849
892
|
gBx = tmpCornerDir[0];
|
|
850
893
|
gBy = tmpCornerDir[1];
|
|
851
894
|
}
|
|
852
895
|
}
|
|
853
896
|
accDist = sdB + (accDist - sdB) * h - blendK * h * (1 - h);
|
|
854
897
|
accBezel = bBezel + (accBezel - bBezel) * h;
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
if (mLen > 1e-6) {
|
|
859
|
-
const invM = 1 / mLen;
|
|
860
|
-
accGx = mx * invM;
|
|
861
|
-
accGy = my * invM;
|
|
862
|
-
}
|
|
863
|
-
else {
|
|
864
|
-
accGx = 0;
|
|
865
|
-
accGy = 0;
|
|
866
|
-
}
|
|
898
|
+
// Preserve unnormalized gradient length so opposing saddle normals soften smoothly via NORMAL_FLOOR.
|
|
899
|
+
accGx = gBx + (accGx - gBx) * h;
|
|
900
|
+
accGy = gBy + (accGy - gBy) * h;
|
|
867
901
|
}
|
|
868
902
|
else if (h <= 0) {
|
|
869
903
|
accDist = sdB;
|
|
@@ -901,15 +935,25 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
901
935
|
else {
|
|
902
936
|
const distIn = accDist < 0 ? -accDist : 0;
|
|
903
937
|
const alpha = accDist <= -0.5 ? 255 : Math.round((0.5 - accDist) * 255);
|
|
938
|
+
if (!hasDisplacement) {
|
|
939
|
+
scanlines[offset++] = 128;
|
|
940
|
+
scanlines[offset++] = 128;
|
|
941
|
+
scanlines[offset++] = 128;
|
|
942
|
+
scanlines[offset++] = alpha;
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
904
945
|
if (!blended) {
|
|
905
946
|
const winBezel = soaBezel[winIdx];
|
|
947
|
+
const winScale = winBezel * invSafeBezel;
|
|
906
948
|
const winRad = soaRad[winIdx];
|
|
907
949
|
const winEdgeX = soaHalfW[winIdx] - winAbsDx;
|
|
908
950
|
const winEdgeY = soaHalfH[winIdx] - rowAbsDy[winIdx];
|
|
909
951
|
if (winRad > 0 && winRad < winBezel && winEdgeX < winBezel && winEdgeY < winBezel) {
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
952
|
+
computeCornerGeometry(winEdgeX > 0 ? winEdgeX : 0, winEdgeY > 0 ? winEdgeY : 0, winRad, winBezel);
|
|
953
|
+
const m = sampleRefractionLut(normLut, tmpCornerGeom[2] * soaInvBezel[winIdx]) * winScale;
|
|
954
|
+
const amp = clampAmp(m * 127);
|
|
955
|
+
scanlines[offset++] = clampChannel(Math.round(128 - winSx * tmpCornerGeom[0] * amp));
|
|
956
|
+
scanlines[offset++] = clampChannel(Math.round(128 - rowSy[winIdx] * tmpCornerGeom[1] * amp));
|
|
913
957
|
scanlines[offset++] = 128;
|
|
914
958
|
scanlines[offset++] = alpha;
|
|
915
959
|
}
|
|
@@ -920,10 +964,10 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
920
964
|
scanlines[offset++] = alpha;
|
|
921
965
|
}
|
|
922
966
|
else {
|
|
923
|
-
const m =
|
|
924
|
-
const amp = m * 127;
|
|
925
|
-
scanlines[offset++] = Math.round(128 - accGx * amp);
|
|
926
|
-
scanlines[offset++] = Math.round(128 - accGy * amp);
|
|
967
|
+
const m = sampleRefractionLut(normLut, distIn * soaInvBezel[winIdx]) * winScale;
|
|
968
|
+
const amp = clampAmp(m * 127);
|
|
969
|
+
scanlines[offset++] = clampChannel(Math.round(128 - accGx * amp));
|
|
970
|
+
scanlines[offset++] = clampChannel(Math.round(128 - accGy * amp));
|
|
927
971
|
scanlines[offset++] = 128;
|
|
928
972
|
scanlines[offset++] = alpha;
|
|
929
973
|
}
|
|
@@ -936,17 +980,15 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
936
980
|
}
|
|
937
981
|
else {
|
|
938
982
|
const gLen = Math.hypot(accGx, accGy);
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
scanlines[offset++] = Math.round(128 + nx * amp);
|
|
949
|
-
scanlines[offset++] = Math.round(128 + ny * amp);
|
|
983
|
+
const denom = gLen > NORMAL_FLOOR ? gLen : NORMAL_FLOOR;
|
|
984
|
+
const invG = -1 / denom;
|
|
985
|
+
const nx = accGx * invG;
|
|
986
|
+
const ny = accGy * invG;
|
|
987
|
+
const blendScale = accBezel * invSafeBezel;
|
|
988
|
+
const m = sampleRefractionLut(normLut, distIn / accBezel) * blendScale;
|
|
989
|
+
const amp = clampAmp(m * 127);
|
|
990
|
+
scanlines[offset++] = clampChannel(Math.round(128 + nx * amp));
|
|
991
|
+
scanlines[offset++] = clampChannel(Math.round(128 + ny * amp));
|
|
950
992
|
scanlines[offset++] = 128;
|
|
951
993
|
scanlines[offset++] = alpha;
|
|
952
994
|
}
|
|
@@ -960,12 +1002,19 @@ export function createRefractMapBuilder() {
|
|
|
960
1002
|
let prevDataUrl = '';
|
|
961
1003
|
let prevWidth = 0;
|
|
962
1004
|
let prevHeight = 0;
|
|
1005
|
+
let prevMaxDisplacementPx = 0;
|
|
963
1006
|
return {
|
|
964
1007
|
build(params) {
|
|
965
1008
|
const safeBezel = Math.max(1, params.bezel);
|
|
966
|
-
const
|
|
967
|
-
? params.
|
|
968
|
-
:
|
|
1009
|
+
const safeIor = params.ior !== undefined && Number.isFinite(params.ior)
|
|
1010
|
+
? Math.max(1, params.ior)
|
|
1011
|
+
: DEFAULT_REFRACTION_IOR;
|
|
1012
|
+
const safeRatio = params.thicknessRatio !== undefined && Number.isFinite(params.thicknessRatio)
|
|
1013
|
+
? Math.max(0, params.thicknessRatio)
|
|
1014
|
+
: DEFAULT_THICKNESS_RATIO;
|
|
1015
|
+
const safeCaustics = Boolean(params.caustics);
|
|
1016
|
+
const profileSpec = params.profile ?? 'squircle';
|
|
1017
|
+
const profileKey = getRefractionProfileKey(profileSpec);
|
|
969
1018
|
const mapScale = params.mapScale && params.mapScale > 0 ? params.mapScale : DEFAULT_MAP_SCALE;
|
|
970
1019
|
const cssW = Math.max(1, Math.round(params.bounds.width));
|
|
971
1020
|
const cssH = Math.max(1, Math.round(params.bounds.height));
|
|
@@ -973,15 +1022,23 @@ export function createRefractMapBuilder() {
|
|
|
973
1022
|
const pngH = Math.max(1, Math.round(cssH * mapScale));
|
|
974
1023
|
const { primitives, blendK } = params.field;
|
|
975
1024
|
const activeCount = collectActivePrimitiveIndices(primitives);
|
|
976
|
-
const key = buildQuantizedKey(cssW, cssH, pngW, pngH, params.bounds, primitives, activeCount, blendK, safeBezel,
|
|
1025
|
+
const key = buildQuantizedKey(cssW, cssH, pngW, pngH, params.bounds, primitives, activeCount, blendK, safeBezel, profileKey, safeIor, safeRatio, safeCaustics);
|
|
977
1026
|
if (key === prevKey && prevDataUrl) {
|
|
978
1027
|
return {
|
|
979
1028
|
dataUrl: prevDataUrl,
|
|
980
1029
|
width: prevWidth,
|
|
981
1030
|
height: prevHeight,
|
|
982
1031
|
changed: false,
|
|
1032
|
+
maxDisplacementPx: prevMaxDisplacementPx,
|
|
983
1033
|
};
|
|
984
1034
|
}
|
|
1035
|
+
const lut = resolveRefractionProfileLut({
|
|
1036
|
+
profile: profileSpec,
|
|
1037
|
+
ior: safeIor,
|
|
1038
|
+
thicknessRatio: safeRatio,
|
|
1039
|
+
caustics: safeCaustics,
|
|
1040
|
+
});
|
|
1041
|
+
const maxDisplacementPx = safeBezel * lut.maxDisplacementRatio;
|
|
985
1042
|
const rowBytes = 1 + pngW * 4;
|
|
986
1043
|
const scanlines = ensureScanlineCapacity(pngH * rowBytes);
|
|
987
1044
|
const stepX = cssW / pngW;
|
|
@@ -992,24 +1049,26 @@ export function createRefractMapBuilder() {
|
|
|
992
1049
|
rasterizeEmptyScanlines(scanlines, pngW, pngH);
|
|
993
1050
|
}
|
|
994
1051
|
else if (activeCount === 1) {
|
|
995
|
-
rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives[activeIndicesBuf[0]], safeBezel,
|
|
1052
|
+
rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives[activeIndicesBuf[0]], safeBezel, lut);
|
|
996
1053
|
}
|
|
997
1054
|
else if (activeCount === 2) {
|
|
998
|
-
rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives[activeIndicesBuf[0]], primitives[activeIndicesBuf[1]], blendK, safeBezel,
|
|
1055
|
+
rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives[activeIndicesBuf[0]], primitives[activeIndicesBuf[1]], blendK, safeBezel, lut);
|
|
999
1056
|
}
|
|
1000
1057
|
else {
|
|
1001
|
-
rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives, activeCount, blendK, safeBezel,
|
|
1058
|
+
rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, stepX, stepY, primitives, activeCount, blendK, safeBezel, lut);
|
|
1002
1059
|
}
|
|
1003
1060
|
const dataUrl = encodeStoredRgbaPngDataUrl(scanlines, pngW, pngH);
|
|
1004
1061
|
prevKey = key;
|
|
1005
1062
|
prevDataUrl = dataUrl;
|
|
1006
1063
|
prevWidth = cssW;
|
|
1007
1064
|
prevHeight = cssH;
|
|
1065
|
+
prevMaxDisplacementPx = maxDisplacementPx;
|
|
1008
1066
|
return {
|
|
1009
1067
|
dataUrl,
|
|
1010
1068
|
width: cssW,
|
|
1011
1069
|
height: cssH,
|
|
1012
1070
|
changed: true,
|
|
1071
|
+
maxDisplacementPx,
|
|
1013
1072
|
};
|
|
1014
1073
|
},
|
|
1015
1074
|
reset() {
|
|
@@ -1017,6 +1076,7 @@ export function createRefractMapBuilder() {
|
|
|
1017
1076
|
prevDataUrl = '';
|
|
1018
1077
|
prevWidth = 0;
|
|
1019
1078
|
prevHeight = 0;
|
|
1079
|
+
prevMaxDisplacementPx = 0;
|
|
1020
1080
|
},
|
|
1021
1081
|
};
|
|
1022
1082
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
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 type RefractionProfileName = 'squircle' | 'circle' | 'parabolic' | 'lip';
|
|
12
|
+
export type BezelHeightProfileName = RefractionProfileName;
|
|
13
|
+
/** Normalized bezel height function `f(t)` in `[0, 1]` from outer edge (`t=0`) to plateau (`t=1`). */
|
|
14
|
+
export type BezelHeightFn = (t: number) => number;
|
|
15
|
+
export type RefractionProfileSpec = RefractionProfileName | BezelHeightFn;
|
|
16
|
+
export interface RefractionProfileOptions {
|
|
17
|
+
/** Cross-section profile name or custom height function (defaults to `'squircle'`). */
|
|
18
|
+
profile?: RefractionProfileSpec;
|
|
19
|
+
/** Index of refraction (`1` = air, `1.5` = glass; defaults to `1.5`). */
|
|
20
|
+
ior?: number;
|
|
21
|
+
/** Glass plateau thickness divided by bezel width (`thickness / bezel`; defaults to `1`). */
|
|
22
|
+
thicknessRatio?: number;
|
|
23
|
+
/** Allow steep-edge caustic folds instead of clamping to a one-to-one mapping. */
|
|
24
|
+
caustics?: boolean;
|
|
25
|
+
/** LUT sample count across `[0, 1]` (defaults to `256`). */
|
|
26
|
+
samples?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface RefractionProfileLut {
|
|
29
|
+
/** Signed inward displacement ratio (`shift / bezel`) across `[0, 1]`. */
|
|
30
|
+
displacementRatio: Float32Array;
|
|
31
|
+
/** Normalized displacement (`displacementRatio / maxDisplacementRatio`) in `[-1, 1]`. */
|
|
32
|
+
normalizedDisplacement: Float32Array;
|
|
33
|
+
/** Dimensionless surface slope `d(z / bezel) / d(d / bezel)`. */
|
|
34
|
+
slope: Float32Array;
|
|
35
|
+
/** Normalized surface height `z / bezel`. */
|
|
36
|
+
height: Float32Array;
|
|
37
|
+
/** Peak absolute `displacementRatio` across the bezel. */
|
|
38
|
+
maxDisplacementRatio: number;
|
|
39
|
+
samples: number;
|
|
40
|
+
}
|
|
41
|
+
export interface BezelRayTraceInput {
|
|
42
|
+
bezel: number;
|
|
43
|
+
thickness: number;
|
|
44
|
+
ior?: number;
|
|
45
|
+
profile?: BezelHeightProfileName | BezelHeightFn;
|
|
46
|
+
}
|
|
47
|
+
export interface BezelRayTrace {
|
|
48
|
+
/** Surface tilt incidence angle from vertical, in radians. */
|
|
49
|
+
incidence: number;
|
|
50
|
+
/** Refracted angle from surface normal inside the glass, in radians. */
|
|
51
|
+
refraction: number;
|
|
52
|
+
/** Ray angular deviation from vertical (`incidence - refraction`), in radians. */
|
|
53
|
+
deviation: number;
|
|
54
|
+
/** Surface height above the page, in CSS px. */
|
|
55
|
+
height: number;
|
|
56
|
+
/** Local surface slope `dz / dd`. */
|
|
57
|
+
slope: number;
|
|
58
|
+
/** Inward horizontal displacement where the refracted ray lands, in CSS px. */
|
|
59
|
+
shift: number;
|
|
60
|
+
}
|
|
61
|
+
export declare const DEFAULT_REFRACTION_SAMPLES = 256;
|
|
62
|
+
export declare const DEFAULT_REFRACTION_IOR = 1.5;
|
|
63
|
+
/** Maximum inward displacement decrease per px (`-d(shift)/du < 1`) to keep mapping injective. */
|
|
64
|
+
export declare const CAUSTIC_FOLD_LIMIT = 0.85;
|
|
65
|
+
/** Lower bound on blended SDF gradient norm in `smin` necks to avoid 180-degree saddle flips. */
|
|
66
|
+
export declare const NORMAL_FLOOR = 0.25;
|
|
67
|
+
/** Built-in physical bezel cross-section height functions (`t = 0` outer rim, `t = 1` plateau). */
|
|
68
|
+
export declare const BEZEL_HEIGHT_PROFILES: Readonly<Record<BezelHeightProfileName, BezelHeightFn>>;
|
|
69
|
+
/** Returns a stable cache key fragment for a named profile or custom `BezelHeightFn`. */
|
|
70
|
+
export declare function getRefractionProfileKey(profile?: RefractionProfileSpec): string;
|
|
71
|
+
/**
|
|
72
|
+
* Computes a 1D bezel refraction LUT via geometric-optics Snell ray tracing
|
|
73
|
+
* (`sin(alpha) = n * sin(thetaT)`, `shift = z * tan(alpha - thetaT)`).
|
|
74
|
+
*/
|
|
75
|
+
export declare function computeRefractionProfileLut(options?: RefractionProfileOptions): RefractionProfileLut;
|
|
76
|
+
/**
|
|
77
|
+
* Resolves a cached 1D refraction LUT using quantized optical keys so steady-state
|
|
78
|
+
* 60/120fps frames reuse `Float32Array` tables with zero allocations.
|
|
79
|
+
*/
|
|
80
|
+
export declare function resolveRefractionProfileLut(options?: RefractionProfileOptions): RefractionProfileLut;
|
|
81
|
+
/** Linearly interpolates a 1D refraction LUT at normalized bezel coordinate `t` in `[0, 1]`. */
|
|
82
|
+
export declare function sampleRefractionLut(table: Float32Array, t: number): number;
|
|
83
|
+
/** Traces a vertical view ray through the bezel at `d` px inward from the outline. */
|
|
84
|
+
export declare function traceBezelRay(input: BezelRayTraceInput, d: number): BezelRayTrace;
|