@sessile/core 0.4.0 → 0.5.1
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 +19 -0
- package/dist/entries/internal.d.ts +1 -1
- package/dist/entries/internal.js +1 -1
- package/dist/legibility.js +6 -0
- package/dist/optics.js +30 -0
- package/dist/overlayRenderer.d.ts +1 -1
- package/dist/overlayRenderer.js +43 -12
- package/dist/refractMap.d.ts +11 -1
- package/dist/refractMap.js +39 -16
- package/dist/refractProfile.d.ts +41 -2
- package/dist/refractProfile.js +69 -1
- package/dist/refractTiles.d.ts +1 -1
- package/dist/refractTiles.js +13 -9
- package/package.json +1 -1
package/dist/contracts.d.ts
CHANGED
|
@@ -87,6 +87,25 @@ export interface OpticalMaterial {
|
|
|
87
87
|
lightElevation?: number;
|
|
88
88
|
/** Optional grazing blue-black rim shade strength in `[0, 1]` for `rimMode === 'physical'` (defaults to `0.32`). */
|
|
89
89
|
rimShade?: number;
|
|
90
|
+
/** Optional opposite-side internal back-bounce reflection strength in `[0, 1.5]` for `rimMode === 'physical'` (defaults to `0.45`). */
|
|
91
|
+
rimBackBounce?: number;
|
|
92
|
+
/** Optional warm bottom/opposite-side internal caustic glow strength in `[0, 1]` for `rimMode === 'physical'` (defaults to `0.28`). */
|
|
93
|
+
rimCaustic?: number;
|
|
94
|
+
/** Optional crisp 1.2px diagonal bezel catchlight strength in `[0, 1]` for `rimMode === 'physical'` (defaults to `0.38`). */
|
|
95
|
+
bezelShine?: number;
|
|
96
|
+
/** Optional ambient colored rim-bounce RGBA `[r, g, b, a]` for `rimMode === 'physical'` (`a` controls tint mix in `[0, 1]`; defaults to `[1, 1, 1, 0]`). */
|
|
97
|
+
rimBounceRgba?: readonly [number, number, number, number];
|
|
98
|
+
/**
|
|
99
|
+
* Optional live surrounding-backdrop rim reflection strength in `[0, 1]` for Chromium Route B3
|
|
100
|
+
* SVG refraction (`0` or `undefined` disables the outward reflection branch).
|
|
101
|
+
*/
|
|
102
|
+
rimReflection?: number;
|
|
103
|
+
/**
|
|
104
|
+
* Optional Abbe number (`V_d`) enabling physical Cauchy RGB dispersion (`n_F - n_C = (n_d - 1) / V_d`).
|
|
105
|
+
* Lower positive values disperse more strongly (e.g. `64` crown glass, `25` flint glass); `0` or `undefined`
|
|
106
|
+
* falls back to the linear pixel `dispersion` offset.
|
|
107
|
+
*/
|
|
108
|
+
abbeNumber?: number;
|
|
90
109
|
}
|
|
91
110
|
export declare const DEFAULT_OPTICAL_MATERIALS: {
|
|
92
111
|
readonly light: {
|
|
@@ -9,6 +9,6 @@
|
|
|
9
9
|
*/
|
|
10
10
|
export { buildRefractMapDataUrl, createRefractMapBuilder, type RefractMapBuilder, type RefractMapParams, type RefractMapResult, } from '../refractMap.ts';
|
|
11
11
|
export { MAX_REFRACT_TILE_PX, clearRefractTilesCache, resolveRefractTiles, type RefractTileParams, type RefractTilesResult, } from '../refractTiles.ts';
|
|
12
|
-
export { BEZEL_HEIGHT_PROFILES, CAUSTIC_FOLD_LIMIT, DEFAULT_REFRACTION_IOR, DEFAULT_REFRACTION_SAMPLES, NORMAL_FLOOR, computeRefractionProfileLut, getRefractionProfileKey, resolveRefractionProfileLut, sampleRefractionLut, traceBezelRay, type BezelHeightFn, type BezelRayTrace, type BezelRayTraceInput, type RefractionProfileLut, type RefractionProfileOptions, type RefractionProfileSpec, } from '../refractProfile.ts';
|
|
12
|
+
export { BEZEL_HEIGHT_PROFILES, CAUSTIC_FOLD_LIMIT, DEFAULT_REFRACTION_IOR, DEFAULT_REFRACTION_SAMPLES, FRAUNHOFER_WAVELENGTHS_UM, NORMAL_FLOOR, computeCauchyDispersionScales, computeCauchyIor, computeRefractionProfileLut, getRefractionProfileKey, resolveRefractionProfileLut, sampleRefractionLut, traceBezelRay, type BezelHeightFn, type BezelRayTrace, type BezelRayTraceInput, type CauchyChannelIor, type CauchyChannelScales, type CauchyDispersionScaleOptions, type RefractionProfileLut, type RefractionProfileOptions, type RefractionProfileSpec, } from '../refractProfile.ts';
|
|
13
13
|
export { OVERLAY_FRAGMENT_SHADER, OVERLAY_VERTEX_SHADER } from '../overlayRenderer.ts';
|
|
14
14
|
export { buildCircularBevelTable, buildColorOpticsMatrix, buildContourAlphaDataUrl, type CircularBevelTable } from '../optics.ts';
|
package/dist/entries/internal.js
CHANGED
|
@@ -9,6 +9,6 @@
|
|
|
9
9
|
*/
|
|
10
10
|
export { buildRefractMapDataUrl, createRefractMapBuilder, } from "../refractMap.js";
|
|
11
11
|
export { MAX_REFRACT_TILE_PX, clearRefractTilesCache, resolveRefractTiles, } from "../refractTiles.js";
|
|
12
|
-
export { BEZEL_HEIGHT_PROFILES, CAUSTIC_FOLD_LIMIT, DEFAULT_REFRACTION_IOR, DEFAULT_REFRACTION_SAMPLES, NORMAL_FLOOR, computeRefractionProfileLut, getRefractionProfileKey, resolveRefractionProfileLut, sampleRefractionLut, traceBezelRay, } from "../refractProfile.js";
|
|
12
|
+
export { BEZEL_HEIGHT_PROFILES, CAUSTIC_FOLD_LIMIT, DEFAULT_REFRACTION_IOR, DEFAULT_REFRACTION_SAMPLES, FRAUNHOFER_WAVELENGTHS_UM, NORMAL_FLOOR, computeCauchyDispersionScales, computeCauchyIor, computeRefractionProfileLut, getRefractionProfileKey, resolveRefractionProfileLut, sampleRefractionLut, traceBezelRay, } from "../refractProfile.js";
|
|
13
13
|
export { OVERLAY_FRAGMENT_SHADER, OVERLAY_VERTEX_SHADER } from "../overlayRenderer.js";
|
|
14
14
|
export { buildCircularBevelTable, buildColorOpticsMatrix, buildContourAlphaDataUrl } from "../optics.js";
|
package/dist/legibility.js
CHANGED
|
@@ -286,6 +286,12 @@ export function compensateLegibilityEdges(material, params, out) {
|
|
|
286
286
|
out.lightAngle = material.lightAngle;
|
|
287
287
|
out.lightElevation = material.lightElevation;
|
|
288
288
|
out.rimShade = material.rimShade;
|
|
289
|
+
out.rimBackBounce = material.rimBackBounce;
|
|
290
|
+
out.rimCaustic = material.rimCaustic;
|
|
291
|
+
out.bezelShine = material.bezelShine;
|
|
292
|
+
out.rimBounceRgba = material.rimBounceRgba;
|
|
293
|
+
out.rimReflection = material.rimReflection;
|
|
294
|
+
out.abbeNumber = material.abbeNumber;
|
|
289
295
|
out.hairlineSideAlpha = clamp(material.hairlineSideAlpha * params.hairlineScale, 0, 1);
|
|
290
296
|
out.hairlineCornerAlpha = clamp(material.hairlineCornerAlpha * params.hairlineScale, 0, 1);
|
|
291
297
|
out.hairlineFlatAlpha = clamp(material.hairlineFlatAlpha * params.hairlineScale, 0, 1);
|
package/dist/optics.js
CHANGED
|
@@ -548,6 +548,36 @@ export function interpolateOpticalMaterial(from, to, t) {
|
|
|
548
548
|
if (from.rimShade !== undefined || to.rimShade !== undefined) {
|
|
549
549
|
result.rimShade = lerp(from.rimShade ?? 0.32, to.rimShade ?? 0.32, u);
|
|
550
550
|
}
|
|
551
|
+
if (from.rimBackBounce !== undefined || to.rimBackBounce !== undefined) {
|
|
552
|
+
result.rimBackBounce = lerp(from.rimBackBounce ?? 0.45, to.rimBackBounce ?? 0.45, u);
|
|
553
|
+
}
|
|
554
|
+
if (from.rimCaustic !== undefined || to.rimCaustic !== undefined) {
|
|
555
|
+
result.rimCaustic = lerp(from.rimCaustic ?? 0.28, to.rimCaustic ?? 0.28, u);
|
|
556
|
+
}
|
|
557
|
+
if (from.bezelShine !== undefined || to.bezelShine !== undefined) {
|
|
558
|
+
result.bezelShine = lerp(from.bezelShine ?? 0.38, to.bezelShine ?? 0.38, u);
|
|
559
|
+
}
|
|
560
|
+
if (from.rimBounceRgba !== undefined || to.rimBounceRgba !== undefined) {
|
|
561
|
+
const a = from.rimBounceRgba ?? [1, 1, 1, 0];
|
|
562
|
+
const b = to.rimBounceRgba ?? [1, 1, 1, 0];
|
|
563
|
+
result.rimBounceRgba = [
|
|
564
|
+
lerp(a[0], b[0], u),
|
|
565
|
+
lerp(a[1], b[1], u),
|
|
566
|
+
lerp(a[2], b[2], u),
|
|
567
|
+
lerp(a[3], b[3], u),
|
|
568
|
+
];
|
|
569
|
+
}
|
|
570
|
+
if (from.rimReflection !== undefined || to.rimReflection !== undefined) {
|
|
571
|
+
result.rimReflection = lerp(from.rimReflection ?? 0, to.rimReflection ?? 0, u);
|
|
572
|
+
}
|
|
573
|
+
if (from.abbeNumber !== undefined || to.abbeNumber !== undefined) {
|
|
574
|
+
// Interpolate in dispersive power (1 / V_d) so transitioning from disabled (0 / undefined)
|
|
575
|
+
// to a finite Abbe number increases dispersion monotonically instead of passing through V_d -> 0.
|
|
576
|
+
const pFrom = from.abbeNumber !== undefined && from.abbeNumber > 0 ? 1 / from.abbeNumber : 0;
|
|
577
|
+
const pTo = to.abbeNumber !== undefined && to.abbeNumber > 0 ? 1 / to.abbeNumber : 0;
|
|
578
|
+
const p = lerp(pFrom, pTo, u);
|
|
579
|
+
result.abbeNumber = p > 1e-6 ? 1 / p : 0;
|
|
580
|
+
}
|
|
551
581
|
return result;
|
|
552
582
|
}
|
|
553
583
|
/** Convenience resolver interpolating between `DEFAULT_OPTICAL_MATERIALS[theme].crisp` and `.frosted` by `styleMix`. */
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import type { IGlassOverlayRenderer } from './contracts.ts';
|
|
17
17
|
export declare const OVERLAY_VERTEX_SHADER = "#version 300 es\nprecision highp float;\n\nout vec2 vUv;\n\nvoid main() {\n vec2 pos = vec2(\n (gl_VertexID == 1) ? 3.0 : -1.0,\n (gl_VertexID == 2) ? 3.0 : -1.0\n );\n vUv = pos * 0.5 + 0.5;\n gl_Position = vec4(pos, 0.0, 1.0);\n}\n";
|
|
18
|
-
export declare const OVERLAY_FRAGMENT_SHADER = "#version 300 es\nprecision highp float;\n\n#define MAX_PRIMITIVES 16\n\nin vec2 vUv;\nout vec4 fragColor;\n\nuniform vec2 uViewport;\nuniform float uDpr;\nuniform int uPrimitiveCount;\nuniform vec4 uPrimitives[MAX_PRIMITIVES]; // (cx, cy, halfW, halfH) in local CSS px\nuniform float uRadii[MAX_PRIMITIVES];\nuniform float uBlendK;\nuniform float uRimOpacity;\nuniform float uStyleMix;\nuniform float uOpacity;\n\nuniform vec4 uRimParams; // (material.rimOpacity, material.rimWidth, material.rimTail, material.rimFocus)\nuniform vec4 uHairline; // (width, sideAlpha, cornerAlpha, flatAlpha)\nuniform vec3 uShadow; // (blur, offsetY, alpha)\nuniform vec4 uBorderColor; // normalized RGBA of optional uniform inner border\nuniform vec2 uBorderParams; // (borderWidthPx, rimBottomRatio)\nuniform vec4 uPhysicalLight; // (lx, ly, lz, enabled)\nuniform vec4 uPhysicalParams; // (bevelPx, specularStrength, shadeStrength, profileCode)\n\nstruct SdfSample {\n float dist;\n vec2 normal;\n};\n\nfloat safePow(float x, float y) {\n return (x <= 1e-6) ? 0.0 : pow(x, y);\n}\n\n// Box-filtered coverage of the band lo <= d <= hi over a pixel footprint of width aa. Lines thinner\n// than a pixel keep the same total alpha at every subpixel offset; a smoothstep around the band\n// centre nearly vanishes or doubles as the edge moves, which reads as shimmer while morphing.\nfloat bandCoverage(float d, float lo, float hi, float aa) {\n return clamp(min(d + 0.5 * aa, hi) - max(d - 0.5 * aa, lo), 0.0, aa) / aa;\n}\n\nfloat computeDirectionalRimWeight(vec2 normal, float focus, float bottomRatio) {\n if (focus < 0.05) {\n return 1.0;\n }\n float topBias = clamp(-normal.y, 0.0, 1.0);\n float botBias = clamp(normal.y, 0.0, 1.0);\n return safePow(max(topBias, botBias * clamp(bottomRatio, 0.0, 1.0)), max(focus, 0.25));\n}\n\nSdfSample sdRoundedBoxRefined(vec2 p, vec2 center, vec2 halfSize, float r) {\n vec2 safeHalf = max(halfSize, vec2(0.001));\n float rad = clamp(r, 0.0, min(safeHalf.x, safeHalf.y));\n vec2 rel = p - center;\n vec2 s = vec2(rel.x >= 0.0 ? 1.0 : -1.0, rel.y >= 0.0 ? 1.0 : -1.0);\n vec2 q = abs(rel) - safeHalf + rad;\n vec2 qPos = max(q, 0.0);\n\n float outside = length(qPos);\n float inside = min(max(q.x, q.y), 0.0);\n float baseDist = outside + inside - rad;\n\n vec2 baseNormal;\n if (q.x > 0.0 && q.y > 0.0) {\n baseNormal = (outside > 1e-5) ? (qPos / outside) * s : normalize(s);\n } else if (q.x > q.y) {\n baseNormal = vec2(s.x, 0.0);\n } else {\n baseNormal = vec2(0.0, s.y);\n }\n\n // Superellipse octant normal refinement within 5 CSS px of the boundary for smooth corner speculars;\n // dist stays exact baseDist for 0px alignment with the Marching Squares contour.\n float rimProximity = smoothstep(5.0, 0.0, abs(baseDist));\n vec2 normal = baseNormal;\n\n if (rimProximity > 0.001 && rad > 1.0 && q.x > 0.0 && q.y > 0.0) {\n const float pExp = 2.55;\n vec2 qn = qPos / rad;\n vec2 grad = vec2(\n safePow(qn.x, pExp - 1.0),\n safePow(qn.y, pExp - 1.0)\n );\n float gl = length(grad);\n if (gl > 1e-5) {\n vec2 superNormal = (grad / gl) * s;\n vec2 mixedN = mix(baseNormal, superNormal, rimProximity);\n float mnl = length(mixedN);\n normal = (mnl > 1e-5) ? (mixedN / mnl) : baseNormal;\n }\n }\n\n return SdfSample(baseDist, normal);\n}\n\nfloat evalBezelHeight(float t, float code) {\n float u = clamp(1.0 - t, 0.0, 1.0);\n if (code > 2.5) {\n float sq = safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);\n float s = sin(3.14159265 * clamp(t, 0.0, 1.0));\n return sq + 0.14 * s * s;\n }\n if (code > 1.5) {\n return 1.0 - u * u;\n }\n if (code > 0.5) {\n return sqrt(max(0.0, 1.0 - u * u));\n }\n return safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);\n}\n\nSdfSample evaluateGlassSdf(vec2 p) {\n if (uPrimitiveCount <= 0) {\n return SdfSample(1e6, vec2(0.0, -1.0));\n }\n\n SdfSample acc = sdRoundedBoxRefined(p, uPrimitives[0].xy, uPrimitives[0].zw, uRadii[0]);\n float d = acc.dist;\n vec2 g = acc.normal;\n for (int i = 1; i < MAX_PRIMITIVES; i++) {\n if (i >= uPrimitiveCount) break;\n SdfSample b = sdRoundedBoxRefined(p, uPrimitives[i].xy, uPrimitives[i].zw, uRadii[i]);\n if (uBlendK <= 0.001) {\n if (b.dist < d) {\n d = b.dist;\n g = b.normal;\n }\n } else {\n float h = clamp(0.5 + 0.5 * (b.dist - d) / uBlendK, 0.0, 1.0);\n d = mix(b.dist, d, h) - uBlendK * h * (1.0 - h);\n g = mix(b.normal, g, h);\n }\n }\n\n // Soften cancelling normals in smin saddle necks (NORMAL_FLOOR = 0.25) so speculars never flip 180 degrees.\n float gl = max(length(g), 0.25);\n return SdfSample(d, g / gl);\n}\n\nvoid main() {\n if (uOpacity <= 0.001 || uPrimitiveCount <= 0) {\n fragColor = vec4(0.0);\n return;\n }\n\n // Convert GL UV (bottom-left origin) to local CSS px (top-left origin).\n vec2 pCss = vec2(vUv.x * uViewport.x, (1.0 - vUv.y) * uViewport.y);\n SdfSample sdf = evaluateGlassSdf(pCss);\n float d = sdf.dist;\n vec2 normal = sdf.normal;\n\n float aa = 1.0 / max(uDpr, 1.0);\n float glassMask = clamp(0.5 - d / aa, 0.0, 1.0);\n float outsideMask = 1.0 - glassMask;\n\n // 1. Outside-only drop shadow (d > 0)\n float shadowA = 0.0;\n if (outsideMask > 0.001 && uShadow.z > 0.001) {\n SdfSample shadowSdf = evaluateGlassSdf(pCss - vec2(0.0, uShadow.y));\n float sigma = max(uShadow.x * 0.5, 1.0);\n float shadowFalloff = 1.0 / (1.0 + exp(clamp(1.65 * shadowSdf.dist / sigma, -12.0, 12.0)));\n shadowA = outsideMask * shadowFalloff * uShadow.z * uOpacity;\n }\n\n // 2. Dark outer hairline: strictly outside when a uniform CSS inner border is present (d >= 0), otherwise straddling the outer edge (d >= -0.25)\n float hw = max(uHairline.x, 0.5 / max(uDpr, 1.0));\n float hasUniformBorder = step(0.001, uBorderParams.x);\n float hairlineLo = mix(-0.25, 0.0, hasUniformBorder);\n float hairlineBand = bandCoverage(d, hairlineLo, hairlineLo + hw, aa);\n float hairA = 0.0;\n if (hairlineBand > 0.001) {\n float ax = abs(normal.x);\n float ay = abs(normal.y);\n float cornerW = safePow(clamp(2.0 * ax * ay, 0.0, 1.0), 1.5);\n float sideVsFlat = mix(uHairline.y, uHairline.w, ay * ay);\n float hairlineProfileAlpha = mix(sideVsFlat, uHairline.z, cornerW);\n hairA = hairlineBand * hairlineProfileAlpha * uOpacity;\n }\n\n // 3. Optional 1px uniform inner border + directional/soft inner rim highlight (inset < 5.0 CSS px)\n float inset = max(-d, 0.0);\n float borderMask = 0.0;\n float borderA = 0.0;\n vec3 borderRgb = vec3(0.0);\n if (hasUniformBorder > 0.5 && d < 0.5 * aa && inset < uBorderParams.x + aa) {\n borderMask = bandCoverage(d, -uBorderParams.x, 0.0, aa);\n borderA = clamp(borderMask * uBorderColor.a * uOpacity, 0.0, 1.0);\n borderRgb = uBorderColor.rgb * borderA;\n }\n\n float rimA = 0.0;\n float physLitA = 0.0;\n float physShadeA = 0.0;\n float isPhysicalRim = step(0.5, uPhysicalLight.w) * (1.0 - hasUniformBorder);\n\n if (glassMask > 0.001 && inset < 5.0 && uRimParams.x > 0.001 && isPhysicalRim < 0.5) {\n float ny = abs(normal.y);\n float wDirectional = 1.0 - clamp(uStyleMix, 0.0, 1.0);\n float wSoft = clamp(uStyleMix, 0.0, 1.0) * clamp(uRimOpacity, 0.0, 1.0);\n\n if (hasUniformBorder > 0.5) {\n // Match CSS inset box-shadow: keep inner rim inside the padding edge so the 1px border is not double-brightened\n float padInset = max(inset - uBorderParams.x, 0.0);\n float padMask = 1.0 - borderMask;\n float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);\n float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, padInset);\n float tail = exp(-padInset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);\n rimA = clamp((core + tail) * dirWeight * uRimParams.x * padMask * glassMask * uOpacity, 0.0, 1.0);\n } else {\n if (wDirectional > 0.001) {\n float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);\n float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, inset);\n float tail = exp(-inset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);\n rimA += (core + tail) * dirWeight * uRimParams.x * wDirectional;\n }\n\n if (wSoft > 0.001) {\n float bandT = clamp(inset / 5.0, 0.0, 1.0);\n float steppedT = min(floor(bandT * 9.0) / 9.0, 1.0);\n float bandCurve = mix(safePow(1.0 - bandT, 2.1), safePow(1.0 - steppedT, 2.1), 0.35);\n float rimCoreBoost = 0.65 + 0.35 * smoothstep(1.2, 0.0, inset);\n float angWeight = mix(0.015, 1.0, safePow(ny, 2.1)) * (normal.y < 0.0 ? 1.0 : 0.85);\n rimA += bandCurve * rimCoreBoost * angWeight * uRimParams.x * wSoft;\n }\n\n // Boost specular luminance to match source-over alpha blending expectations\n rimA = clamp(rimA * 1.35 * glassMask * uOpacity, 0.0, 1.0);\n }\n } else if (glassMask > 0.001 && isPhysicalRim > 0.5 && inset < uPhysicalParams.x) {\n float bezelPx = max(uPhysicalParams.x, 1.0);\n float t = clamp(inset / bezelPx, 0.0, 1.0);\n float eps = 0.015;\n float t0 = max(0.0, t - eps);\n float t1 = min(1.0, t + eps);\n float slope = (evalBezelHeight(t1, uPhysicalParams.w) - evalBezelHeight(t0, uPhysicalParams.w)) / max(t1 - t0, 1e-4);\n vec3 N = normalize(vec3(normal * slope, 1.0));\n vec3 L = uPhysicalLight.xyz;\n vec3 H = vec3(L.xy, L.z + 1.0);\n float hLen = max(length(H), 1e-4);\n const float shininess = 36.0;\n float flatBase = safePow(clamp((L.z + 1.0) / hLen, 0.0, 1.0), shininess);\n float denomBase = max(1.0 - flatBase, 1e-4);\n float hDot = clamp(dot(N, H) / hLen, 0.0, 1.0);\n float highlight = max(safePow(hDot, shininess) - flatBase, 0.0) / denomBase;\n vec3 H_bounce = vec3(-L.xy, L.z + 1.0);\n float bDot = clamp(dot(N, H_bounce) / hLen, 0.0, 1.0);\n float bounce = max(safePow(bDot, shininess) - flatBase, 0.0) / denomBase;\n vec2 planar = L.xy / max(length(L.xy), 1e-4);\n float facing = dot(normal, planar);\n float fresnel = safePow(clamp(1.0 - N.z, 0.0, 1.0), 4.0);\n float rimTerm = fresnel * (0.25 + 0.75 * max(facing, 0.0) + 0.45 * max(-facing, 0.0));\n float shadeTerm = safePow(clamp(1.0 - N.z, 0.0, 1.0), 2.5) * (0.45 + 0.55 * max(-facing, 0.0));\n float lit = clamp((uPhysicalParams.y * (highlight + 0.45 * bounce) + uRimParams.x * rimTerm) * glassMask * uOpacity, 0.0, 1.0);\n float dark = clamp(uPhysicalParams.z * shadeTerm * glassMask * uOpacity, 0.0, 1.0) * (1.0 - lit);\n physLitA = lit;\n physShadeA = dark;\n }\n\n // Keep interior strictly transparent when shadow, hairline, border, and rim are all zero\n if (shadowA <= 0.0001 && hairA <= 0.0001 && borderA <= 0.0001 && rimA <= 0.0001 && physLitA <= 0.0001 && physShadeA <= 0.0001) {\n fragColor = vec4(0.0);\n return;\n }\n\n // Premultiplied alpha composite: uniform border over inner/physical rim over hairline and shadow\n float darkA = clamp(shadowA + hairA * (1.0 - shadowA), 0.0, 1.0);\n vec3 shadeColor = vec3(0.03921569, 0.07058824, 0.10980392);\n vec3 afterRimRgb = vec3(rimA + physLitA) + shadeColor * physShadeA;\n float totalRimA = clamp(rimA + physLitA + physShadeA * (1.0 - rimA), 0.0, 1.0);\n float afterRimA = totalRimA + darkA * (1.0 - totalRimA);\n vec3 outRgb = borderRgb + afterRimRgb * (1.0 - borderA);\n float outA = clamp(borderA + afterRimA * (1.0 - borderA), 0.0, 1.0);\n fragColor = vec4(outRgb, outA);\n}\n";
|
|
18
|
+
export declare const OVERLAY_FRAGMENT_SHADER = "#version 300 es\nprecision highp float;\n\n#define MAX_PRIMITIVES 16\n\nin vec2 vUv;\nout vec4 fragColor;\n\nuniform vec2 uViewport;\nuniform float uDpr;\nuniform int uPrimitiveCount;\nuniform vec4 uPrimitives[MAX_PRIMITIVES]; // (cx, cy, halfW, halfH) in local CSS px\nuniform float uRadii[MAX_PRIMITIVES];\nuniform float uBlendK;\nuniform float uRimOpacity;\nuniform float uStyleMix;\nuniform float uOpacity;\n\nuniform vec4 uRimParams; // (material.rimOpacity, material.rimWidth, material.rimTail, material.rimFocus)\nuniform vec4 uHairline; // (width, sideAlpha, cornerAlpha, flatAlpha)\nuniform vec3 uShadow; // (blur, offsetY, alpha)\nuniform vec4 uBorderColor; // normalized RGBA of optional uniform inner border\nuniform vec2 uBorderParams; // (borderWidthPx, rimBottomRatio)\nuniform vec4 uPhysicalLight; // (lx, ly, lz, enabled)\nuniform vec4 uPhysicalParams; // (bevelPx, specularStrength, shadeStrength, profileCode)\nuniform vec4 uPhysicalOptics; // (backBounceStrength, causticStrength, bezelShineStrength, reserved)\nuniform vec4 uRimBounceColor; // normalized RGBA ambient bounce tint (a = tint mix in [0, 1])\n\nstruct SdfSample {\n float dist;\n vec2 normal;\n};\n\nfloat safePow(float x, float y) {\n return (x <= 1e-6) ? 0.0 : pow(x, y);\n}\n\n// Box-filtered coverage of the band lo <= d <= hi over a pixel footprint of width aa. Lines thinner\n// than a pixel keep the same total alpha at every subpixel offset; a smoothstep around the band\n// centre nearly vanishes or doubles as the edge moves, which reads as shimmer while morphing.\nfloat bandCoverage(float d, float lo, float hi, float aa) {\n return clamp(min(d + 0.5 * aa, hi) - max(d - 0.5 * aa, lo), 0.0, aa) / aa;\n}\n\nfloat computeDirectionalRimWeight(vec2 normal, float focus, float bottomRatio) {\n if (focus < 0.05) {\n return 1.0;\n }\n float topBias = clamp(-normal.y, 0.0, 1.0);\n float botBias = clamp(normal.y, 0.0, 1.0);\n return safePow(max(topBias, botBias * clamp(bottomRatio, 0.0, 1.0)), max(focus, 0.25));\n}\n\nSdfSample sdRoundedBoxRefined(vec2 p, vec2 center, vec2 halfSize, float r) {\n vec2 safeHalf = max(halfSize, vec2(0.001));\n float rad = clamp(r, 0.0, min(safeHalf.x, safeHalf.y));\n vec2 rel = p - center;\n vec2 s = vec2(rel.x >= 0.0 ? 1.0 : -1.0, rel.y >= 0.0 ? 1.0 : -1.0);\n vec2 q = abs(rel) - safeHalf + rad;\n vec2 qPos = max(q, 0.0);\n\n float outside = length(qPos);\n float inside = min(max(q.x, q.y), 0.0);\n float baseDist = outside + inside - rad;\n\n vec2 baseNormal;\n if (q.x > 0.0 && q.y > 0.0) {\n baseNormal = (outside > 1e-5) ? (qPos / outside) * s : normalize(s);\n } else if (q.x > q.y) {\n baseNormal = vec2(s.x, 0.0);\n } else {\n baseNormal = vec2(0.0, s.y);\n }\n\n // Superellipse octant normal refinement within 5 CSS px of the boundary for smooth corner speculars;\n // dist stays exact baseDist for 0px alignment with the Marching Squares contour.\n float rimProximity = smoothstep(5.0, 0.0, abs(baseDist));\n vec2 normal = baseNormal;\n\n if (rimProximity > 0.001 && rad > 1.0 && q.x > 0.0 && q.y > 0.0) {\n const float pExp = 2.55;\n vec2 qn = qPos / rad;\n vec2 grad = vec2(\n safePow(qn.x, pExp - 1.0),\n safePow(qn.y, pExp - 1.0)\n );\n float gl = length(grad);\n if (gl > 1e-5) {\n vec2 superNormal = (grad / gl) * s;\n vec2 mixedN = mix(baseNormal, superNormal, rimProximity);\n float mnl = length(mixedN);\n normal = (mnl > 1e-5) ? (mixedN / mnl) : baseNormal;\n }\n }\n\n return SdfSample(baseDist, normal);\n}\n\nfloat evalBezelHeight(float t, float code) {\n float u = clamp(1.0 - t, 0.0, 1.0);\n if (code > 3.5) {\n float u2 = u * u;\n return 1.0 - u2 * u2;\n }\n if (code > 2.5) {\n float sq = safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);\n float s = sin(3.14159265 * clamp(t, 0.0, 1.0));\n return sq + 0.14 * s * s;\n }\n if (code > 1.5) {\n return 1.0 - u * u;\n }\n if (code > 0.5) {\n return sqrt(max(0.0, 1.0 - u * u));\n }\n return safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);\n}\n\nSdfSample evaluateGlassSdf(vec2 p) {\n if (uPrimitiveCount <= 0) {\n return SdfSample(1e6, vec2(0.0, -1.0));\n }\n\n SdfSample acc = sdRoundedBoxRefined(p, uPrimitives[0].xy, uPrimitives[0].zw, uRadii[0]);\n float d = acc.dist;\n vec2 g = acc.normal;\n for (int i = 1; i < MAX_PRIMITIVES; i++) {\n if (i >= uPrimitiveCount) break;\n SdfSample b = sdRoundedBoxRefined(p, uPrimitives[i].xy, uPrimitives[i].zw, uRadii[i]);\n if (uBlendK <= 0.001) {\n if (b.dist < d) {\n d = b.dist;\n g = b.normal;\n }\n } else {\n float h = clamp(0.5 + 0.5 * (b.dist - d) / uBlendK, 0.0, 1.0);\n d = mix(b.dist, d, h) - uBlendK * h * (1.0 - h);\n g = mix(b.normal, g, h);\n }\n }\n\n // Soften cancelling normals in smin saddle necks (NORMAL_FLOOR = 0.25) so speculars never flip 180 degrees.\n float gl = max(length(g), 0.25);\n return SdfSample(d, g / gl);\n}\n\nvoid main() {\n if (uOpacity <= 0.001 || uPrimitiveCount <= 0) {\n fragColor = vec4(0.0);\n return;\n }\n\n // Convert GL UV (bottom-left origin) to local CSS px (top-left origin).\n vec2 pCss = vec2(vUv.x * uViewport.x, (1.0 - vUv.y) * uViewport.y);\n SdfSample sdf = evaluateGlassSdf(pCss);\n float d = sdf.dist;\n vec2 normal = sdf.normal;\n\n float aa = 1.0 / max(uDpr, 1.0);\n float glassMask = clamp(0.5 - d / aa, 0.0, 1.0);\n float outsideMask = 1.0 - glassMask;\n\n // 1. Outside-only drop shadow (d > 0)\n float shadowA = 0.0;\n if (outsideMask > 0.001 && uShadow.z > 0.001) {\n SdfSample shadowSdf = evaluateGlassSdf(pCss - vec2(0.0, uShadow.y));\n float sigma = max(uShadow.x * 0.5, 1.0);\n float shadowFalloff = 1.0 / (1.0 + exp(clamp(1.65 * shadowSdf.dist / sigma, -12.0, 12.0)));\n shadowA = outsideMask * shadowFalloff * uShadow.z * uOpacity;\n }\n\n // 2. Dark outer hairline: strictly outside when a uniform CSS inner border is present (d >= 0), otherwise straddling the outer edge (d >= -0.25)\n float hw = max(uHairline.x, 0.5 / max(uDpr, 1.0));\n float hasUniformBorder = step(0.001, uBorderParams.x);\n float hairlineLo = mix(-0.25, 0.0, hasUniformBorder);\n float hairlineBand = bandCoverage(d, hairlineLo, hairlineLo + hw, aa);\n float hairA = 0.0;\n if (hairlineBand > 0.001) {\n float ax = abs(normal.x);\n float ay = abs(normal.y);\n float cornerW = safePow(clamp(2.0 * ax * ay, 0.0, 1.0), 1.5);\n float sideVsFlat = mix(uHairline.y, uHairline.w, ay * ay);\n float hairlineProfileAlpha = mix(sideVsFlat, uHairline.z, cornerW);\n hairA = hairlineBand * hairlineProfileAlpha * uOpacity;\n }\n\n // 3. Optional 1px uniform inner border + directional/soft inner rim highlight (inset < 5.0 CSS px)\n float inset = max(-d, 0.0);\n float borderMask = 0.0;\n float borderA = 0.0;\n vec3 borderRgb = vec3(0.0);\n if (hasUniformBorder > 0.5 && d < 0.5 * aa && inset < uBorderParams.x + aa) {\n borderMask = bandCoverage(d, -uBorderParams.x, 0.0, aa);\n borderA = clamp(borderMask * uBorderColor.a * uOpacity, 0.0, 1.0);\n borderRgb = uBorderColor.rgb * borderA;\n }\n\n float rimA = 0.0;\n vec3 physLitRgb = vec3(0.0);\n float physLitA = 0.0;\n float physShadeA = 0.0;\n float isPhysicalRim = step(0.5, uPhysicalLight.w) * (1.0 - hasUniformBorder);\n\n if (glassMask > 0.001 && inset < 5.0 && uRimParams.x > 0.001 && isPhysicalRim < 0.5) {\n float ny = abs(normal.y);\n float wDirectional = 1.0 - clamp(uStyleMix, 0.0, 1.0);\n float wSoft = clamp(uStyleMix, 0.0, 1.0) * clamp(uRimOpacity, 0.0, 1.0);\n\n if (hasUniformBorder > 0.5) {\n // Match CSS inset box-shadow: keep inner rim inside the padding edge so the 1px border is not double-brightened\n float padInset = max(inset - uBorderParams.x, 0.0);\n float padMask = 1.0 - borderMask;\n float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);\n float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, padInset);\n float tail = exp(-padInset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);\n rimA = clamp((core + tail) * dirWeight * uRimParams.x * padMask * glassMask * uOpacity, 0.0, 1.0);\n } else {\n if (wDirectional > 0.001) {\n float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);\n float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, inset);\n float tail = exp(-inset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);\n rimA += (core + tail) * dirWeight * uRimParams.x * wDirectional;\n }\n\n if (wSoft > 0.001) {\n float bandT = clamp(inset / 5.0, 0.0, 1.0);\n float steppedT = min(floor(bandT * 9.0) / 9.0, 1.0);\n float bandCurve = mix(safePow(1.0 - bandT, 2.1), safePow(1.0 - steppedT, 2.1), 0.35);\n float rimCoreBoost = 0.65 + 0.35 * smoothstep(1.2, 0.0, inset);\n float angWeight = mix(0.015, 1.0, safePow(ny, 2.1)) * (normal.y < 0.0 ? 1.0 : 0.85);\n rimA += bandCurve * rimCoreBoost * angWeight * uRimParams.x * wSoft;\n }\n\n // Boost specular luminance to match source-over alpha blending expectations\n rimA = clamp(rimA * 1.35 * glassMask * uOpacity, 0.0, 1.0);\n }\n } else if (glassMask > 0.001 && isPhysicalRim > 0.5 && inset < uPhysicalParams.x) {\n float bezelPx = max(uPhysicalParams.x, 1.0);\n float t = clamp(inset / bezelPx, 0.0, 1.0);\n float eps = 0.015;\n float t0 = max(0.0, t - eps);\n float t1 = min(1.0, t + eps);\n float slope = (evalBezelHeight(t1, uPhysicalParams.w) - evalBezelHeight(t0, uPhysicalParams.w)) / max(t1 - t0, 1e-4);\n vec3 N = normalize(vec3(normal * slope, 1.0));\n vec3 L = uPhysicalLight.xyz;\n vec3 H = vec3(L.xy, L.z + 1.0);\n float hLen = max(length(H), 1e-4);\n const float shininess = 36.0;\n float flatBase = safePow(clamp((L.z + 1.0) / hLen, 0.0, 1.0), shininess);\n float denomBase = max(1.0 - flatBase, 1e-4);\n float hDot = clamp(dot(N, H) / hLen, 0.0, 1.0);\n float highlight = max(safePow(hDot, shininess) - flatBase, 0.0) / denomBase;\n vec3 H_bounce = vec3(-L.xy, L.z + 1.0);\n float bDot = clamp(dot(N, H_bounce) / hLen, 0.0, 1.0);\n float bounce = max(safePow(bDot, shininess) - flatBase, 0.0) / denomBase;\n vec2 planar = L.xy / max(length(L.xy), 1e-4);\n float facing = dot(normal, planar);\n float fresnel = safePow(clamp(1.0 - N.z, 0.0, 1.0), 4.0);\n float frontRim = fresnel * (0.25 + 0.75 * max(facing, 0.0));\n float backRim = fresnel * 0.45 * max(-facing, 0.0);\n float shineBand = bandCoverage(d, -1.2, 0.0, aa);\n float diagLobe = facing * facing;\n float bezelShine = shineBand * (0.18 + 0.82 * diagLobe * diagLobe) * uPhysicalOptics.z;\n float causticProfile = smoothstep(0.0, 0.25, t) * smoothstep(0.92, 0.18, t);\n float causticDir = max(max(-facing, 0.0), clamp(normal.y, 0.0, 1.0) * 0.65);\n float causticA = uPhysicalParams.y * causticProfile * causticDir * uPhysicalOptics.y * glassMask * uOpacity;\n float frontA = (uPhysicalParams.y * (highlight + bezelShine) + uRimParams.x * frontRim) * glassMask * uOpacity;\n float bounceA = (uPhysicalParams.y * uPhysicalOptics.x * bounce + uRimParams.x * backRim) * glassMask * uOpacity;\n vec3 bounceColor = mix(vec3(1.0), uRimBounceColor.rgb, clamp(uRimBounceColor.a, 0.0, 1.0));\n float litSum = frontA + bounceA + causticA;\n float totalLit = clamp(litSum, 0.0, 1.0);\n vec3 litRgb = vec3(frontA) + bounceColor * bounceA + vec3(1.0, 0.92, 0.86) * causticA;\n if (litSum > 1.0) {\n litRgb *= totalLit / litSum;\n }\n\n float shadeTerm = safePow(clamp(1.0 - N.z, 0.0, 1.0), 2.5) * (0.45 + 0.55 * max(-facing, 0.0));\n float dark = clamp(uPhysicalParams.z * shadeTerm * glassMask * uOpacity, 0.0, 1.0) * (1.0 - totalLit);\n physLitRgb = litRgb;\n physLitA = totalLit;\n physShadeA = dark;\n }\n\n // Keep interior strictly transparent when shadow, hairline, border, and rim are all zero\n if (shadowA <= 0.0001 && hairA <= 0.0001 && borderA <= 0.0001 && rimA <= 0.0001 && physLitA <= 0.0001 && physShadeA <= 0.0001) {\n fragColor = vec4(0.0);\n return;\n }\n\n // Premultiplied alpha composite: uniform border over inner/physical rim over hairline and shadow\n float darkA = clamp(shadowA + hairA * (1.0 - shadowA), 0.0, 1.0);\n vec3 shadeColor = vec3(0.03921569, 0.07058824, 0.10980392);\n vec3 afterRimRgb = vec3(rimA) + physLitRgb + shadeColor * physShadeA;\n float totalRimA = clamp(rimA + physLitA + physShadeA * (1.0 - rimA), 0.0, 1.0);\n float afterRimA = totalRimA + darkA * (1.0 - totalRimA);\n vec3 outRgb = borderRgb + afterRimRgb * (1.0 - borderA);\n float outA = clamp(borderA + afterRimA * (1.0 - borderA), 0.0, 1.0);\n fragColor = vec4(outRgb, outA);\n}\n";
|
|
19
19
|
/**
|
|
20
20
|
* Traces an SVG path string emitted by `extractFieldContourPath` (`M`, `L`, `A`, `Z`)
|
|
21
21
|
* directly onto a 2D Canvas context without relying on `Path2D`.
|
package/dist/overlayRenderer.js
CHANGED
|
@@ -15,6 +15,14 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { extractFieldContourPath } from "./contour.js";
|
|
17
17
|
import { MAX_GPU_PRIMITIVES } from "./contracts.js";
|
|
18
|
+
/** Exhaustive lookup matching `evalBezelHeight(t, code)` in `OVERLAY_FRAGMENT_SHADER`. */
|
|
19
|
+
const BEVEL_PROFILE_CODES = {
|
|
20
|
+
squircle: 0,
|
|
21
|
+
circle: 1,
|
|
22
|
+
parabolic: 2,
|
|
23
|
+
lip: 3,
|
|
24
|
+
'c2-squircle': 4,
|
|
25
|
+
};
|
|
18
26
|
export const OVERLAY_VERTEX_SHADER = /* glsl */ `#version 300 es
|
|
19
27
|
precision highp float;
|
|
20
28
|
|
|
@@ -54,6 +62,8 @@ uniform vec4 uBorderColor; // normalized RGBA of optional uniform inner bor
|
|
|
54
62
|
uniform vec2 uBorderParams; // (borderWidthPx, rimBottomRatio)
|
|
55
63
|
uniform vec4 uPhysicalLight; // (lx, ly, lz, enabled)
|
|
56
64
|
uniform vec4 uPhysicalParams; // (bevelPx, specularStrength, shadeStrength, profileCode)
|
|
65
|
+
uniform vec4 uPhysicalOptics; // (backBounceStrength, causticStrength, bezelShineStrength, reserved)
|
|
66
|
+
uniform vec4 uRimBounceColor; // normalized RGBA ambient bounce tint (a = tint mix in [0, 1])
|
|
57
67
|
|
|
58
68
|
struct SdfSample {
|
|
59
69
|
float dist;
|
|
@@ -127,6 +137,10 @@ SdfSample sdRoundedBoxRefined(vec2 p, vec2 center, vec2 halfSize, float r) {
|
|
|
127
137
|
|
|
128
138
|
float evalBezelHeight(float t, float code) {
|
|
129
139
|
float u = clamp(1.0 - t, 0.0, 1.0);
|
|
140
|
+
if (code > 3.5) {
|
|
141
|
+
float u2 = u * u;
|
|
142
|
+
return 1.0 - u2 * u2;
|
|
143
|
+
}
|
|
130
144
|
if (code > 2.5) {
|
|
131
145
|
float sq = safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);
|
|
132
146
|
float s = sin(3.14159265 * clamp(t, 0.0, 1.0));
|
|
@@ -221,6 +235,7 @@ void main() {
|
|
|
221
235
|
}
|
|
222
236
|
|
|
223
237
|
float rimA = 0.0;
|
|
238
|
+
vec3 physLitRgb = vec3(0.0);
|
|
224
239
|
float physLitA = 0.0;
|
|
225
240
|
float physShadeA = 0.0;
|
|
226
241
|
float isPhysicalRim = step(0.5, uPhysicalLight.w) * (1.0 - hasUniformBorder);
|
|
@@ -280,11 +295,28 @@ void main() {
|
|
|
280
295
|
vec2 planar = L.xy / max(length(L.xy), 1e-4);
|
|
281
296
|
float facing = dot(normal, planar);
|
|
282
297
|
float fresnel = safePow(clamp(1.0 - N.z, 0.0, 1.0), 4.0);
|
|
283
|
-
float
|
|
298
|
+
float frontRim = fresnel * (0.25 + 0.75 * max(facing, 0.0));
|
|
299
|
+
float backRim = fresnel * 0.45 * max(-facing, 0.0);
|
|
300
|
+
float shineBand = bandCoverage(d, -1.2, 0.0, aa);
|
|
301
|
+
float diagLobe = facing * facing;
|
|
302
|
+
float bezelShine = shineBand * (0.18 + 0.82 * diagLobe * diagLobe) * uPhysicalOptics.z;
|
|
303
|
+
float causticProfile = smoothstep(0.0, 0.25, t) * smoothstep(0.92, 0.18, t);
|
|
304
|
+
float causticDir = max(max(-facing, 0.0), clamp(normal.y, 0.0, 1.0) * 0.65);
|
|
305
|
+
float causticA = uPhysicalParams.y * causticProfile * causticDir * uPhysicalOptics.y * glassMask * uOpacity;
|
|
306
|
+
float frontA = (uPhysicalParams.y * (highlight + bezelShine) + uRimParams.x * frontRim) * glassMask * uOpacity;
|
|
307
|
+
float bounceA = (uPhysicalParams.y * uPhysicalOptics.x * bounce + uRimParams.x * backRim) * glassMask * uOpacity;
|
|
308
|
+
vec3 bounceColor = mix(vec3(1.0), uRimBounceColor.rgb, clamp(uRimBounceColor.a, 0.0, 1.0));
|
|
309
|
+
float litSum = frontA + bounceA + causticA;
|
|
310
|
+
float totalLit = clamp(litSum, 0.0, 1.0);
|
|
311
|
+
vec3 litRgb = vec3(frontA) + bounceColor * bounceA + vec3(1.0, 0.92, 0.86) * causticA;
|
|
312
|
+
if (litSum > 1.0) {
|
|
313
|
+
litRgb *= totalLit / litSum;
|
|
314
|
+
}
|
|
315
|
+
|
|
284
316
|
float shadeTerm = safePow(clamp(1.0 - N.z, 0.0, 1.0), 2.5) * (0.45 + 0.55 * max(-facing, 0.0));
|
|
285
|
-
float
|
|
286
|
-
|
|
287
|
-
physLitA =
|
|
317
|
+
float dark = clamp(uPhysicalParams.z * shadeTerm * glassMask * uOpacity, 0.0, 1.0) * (1.0 - totalLit);
|
|
318
|
+
physLitRgb = litRgb;
|
|
319
|
+
physLitA = totalLit;
|
|
288
320
|
physShadeA = dark;
|
|
289
321
|
}
|
|
290
322
|
|
|
@@ -297,7 +329,7 @@ void main() {
|
|
|
297
329
|
// Premultiplied alpha composite: uniform border over inner/physical rim over hairline and shadow
|
|
298
330
|
float darkA = clamp(shadowA + hairA * (1.0 - shadowA), 0.0, 1.0);
|
|
299
331
|
vec3 shadeColor = vec3(0.03921569, 0.07058824, 0.10980392);
|
|
300
|
-
vec3 afterRimRgb = vec3(rimA +
|
|
332
|
+
vec3 afterRimRgb = vec3(rimA) + physLitRgb + shadeColor * physShadeA;
|
|
301
333
|
float totalRimA = clamp(rimA + physLitA + physShadeA * (1.0 - rimA), 0.0, 1.0);
|
|
302
334
|
float afterRimA = totalRimA + darkA * (1.0 - totalRimA);
|
|
303
335
|
vec3 outRgb = borderRgb + afterRimRgb * (1.0 - borderA);
|
|
@@ -673,6 +705,8 @@ const UNIFORM_NAMES = [
|
|
|
673
705
|
'uBorderParams',
|
|
674
706
|
'uPhysicalLight',
|
|
675
707
|
'uPhysicalParams',
|
|
708
|
+
'uPhysicalOptics',
|
|
709
|
+
'uRimBounceColor',
|
|
676
710
|
];
|
|
677
711
|
/** Links the edge program on `gl`; throws when the driver rejects the shaders. */
|
|
678
712
|
function createOverlayProgram(gl) {
|
|
@@ -743,14 +777,11 @@ function createOverlayProgram(gl) {
|
|
|
743
777
|
const elevRad = (Math.max(1, Math.min(89, material.lightElevation ?? 22)) * Math.PI) / 180;
|
|
744
778
|
const cosElev = Math.cos(elevRad);
|
|
745
779
|
gl.uniform4f(uniforms.uPhysicalLight, Math.sin(angleRad) * cosElev, -Math.cos(angleRad) * cosElev, Math.sin(elevRad), isPhysicalRim ? 1 : 0);
|
|
746
|
-
const profileCode = material.bevelProfile ===
|
|
747
|
-
? 1
|
|
748
|
-
: material.bevelProfile === 'parabolic'
|
|
749
|
-
? 2
|
|
750
|
-
: material.bevelProfile === 'lip'
|
|
751
|
-
? 3
|
|
752
|
-
: 0;
|
|
780
|
+
const profileCode = material.bevelProfile === undefined ? 0 : (BEVEL_PROFILE_CODES[material.bevelProfile] ?? 0);
|
|
753
781
|
gl.uniform4f(uniforms.uPhysicalParams, Math.max(1, material.bevel), Math.min(1, Math.max(0, material.rimOpacity * 2)), Math.min(1, Math.max(0, material.rimShade ?? 0.32)), profileCode);
|
|
782
|
+
gl.uniform4f(uniforms.uPhysicalOptics, Math.min(1.5, Math.max(0, material.rimBackBounce ?? 0.45)), Math.min(1, Math.max(0, material.rimCaustic ?? 0.28)), Math.min(1, Math.max(0, material.bezelShine ?? 0.38)), 0);
|
|
783
|
+
const bounceRgba = material.rimBounceRgba ?? [1, 1, 1, 0];
|
|
784
|
+
gl.uniform4f(uniforms.uRimBounceColor, Math.min(1, Math.max(0, bounceRgba[0])), Math.min(1, Math.max(0, bounceRgba[1])), Math.min(1, Math.max(0, bounceRgba[2])), Math.min(1, Math.max(0, bounceRgba[3])));
|
|
754
785
|
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
755
786
|
gl.bindVertexArray(null);
|
|
756
787
|
return true;
|
package/dist/refractMap.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export interface RefractMapParams {
|
|
|
15
15
|
field: GlassFieldState;
|
|
16
16
|
bezel: number;
|
|
17
17
|
mapScale?: number;
|
|
18
|
-
/** Refraction cross-section profile (`'squircle'`, `'circle'`, `'parabolic'`, `'lip'`, or custom height fn). */
|
|
18
|
+
/** Refraction cross-section profile (`'squircle'`, `'c2-squircle'`, `'circle'`, `'parabolic'`, `'lip'`, or custom height fn). */
|
|
19
19
|
profile?: RefractionProfileSpec;
|
|
20
20
|
/** Index of refraction (`1` = air, `1.5` = glass; defaults to `1.5`). */
|
|
21
21
|
ior?: number;
|
|
@@ -40,6 +40,16 @@ export interface RefractMapBuilder {
|
|
|
40
40
|
export declare const tmpCornerGeom: Float64Array<ArrayBuffer>;
|
|
41
41
|
/** @internal */
|
|
42
42
|
export declare function ensureScanlineCapacity(needed: number): Uint8Array;
|
|
43
|
+
/**
|
|
44
|
+
* Encodes normalized bezel rim reflection weight into the displacement map's blue channel in
|
|
45
|
+
* `[128, 255]`: `255` at the outer rim (`t = 0`), smooth quadratic falloff to `128` at the
|
|
46
|
+
* plateau (`t >= 1`). Keeping outside/plateau at `128` preserves existing neutral RGBA contracts
|
|
47
|
+
* while letting SVG `feColorMatrix` extract an exact `[0, 1]` rim mask via
|
|
48
|
+
* `A = (255 / 127) * B - (128 / 127)`.
|
|
49
|
+
*
|
|
50
|
+
* @internal
|
|
51
|
+
*/
|
|
52
|
+
export declare function encodeRimMaskChannel(t: number): number;
|
|
43
53
|
/** @internal */
|
|
44
54
|
export declare function encodeStoredRgbaPngDataUrl(scanlines: Uint8Array, pngW: number, pngH: number): string;
|
|
45
55
|
/** @internal */
|
package/dist/refractMap.js
CHANGED
|
@@ -168,6 +168,21 @@ function ensureSoaCapacity(needed) {
|
|
|
168
168
|
rowQy = new Float64Array(nextCap);
|
|
169
169
|
rowSy = new Float64Array(nextCap);
|
|
170
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Encodes normalized bezel rim reflection weight into the displacement map's blue channel in
|
|
173
|
+
* `[128, 255]`: `255` at the outer rim (`t = 0`), smooth quadratic falloff to `128` at the
|
|
174
|
+
* plateau (`t >= 1`). Keeping outside/plateau at `128` preserves existing neutral RGBA contracts
|
|
175
|
+
* while letting SVG `feColorMatrix` extract an exact `[0, 1]` rim mask via
|
|
176
|
+
* `A = (255 / 127) * B - (128 / 127)`.
|
|
177
|
+
*
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
180
|
+
export function encodeRimMaskChannel(t) {
|
|
181
|
+
if (t >= 1)
|
|
182
|
+
return 128;
|
|
183
|
+
const u = t <= 0 ? 1 : 1 - t;
|
|
184
|
+
return 128 + Math.round(127 * u * u);
|
|
185
|
+
}
|
|
171
186
|
/** @internal */
|
|
172
187
|
export function encodeStoredRgbaPngDataUrl(scanlines, pngW, pngH) {
|
|
173
188
|
const rowBytes = 1 + pngW * 4;
|
|
@@ -419,11 +434,12 @@ function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
419
434
|
const edgeX = halfW - absDx;
|
|
420
435
|
if (bendCorners && edgeX < effBezel && edgeY < effBezel) {
|
|
421
436
|
computeCornerGeometry(edgeX > 0 ? edgeX : 0, edgeY > 0 ? edgeY : 0, rad, effBezel);
|
|
422
|
-
const
|
|
437
|
+
const tCorner = tmpCornerGeom[2] * invBezel;
|
|
438
|
+
const m = sampleRefractionLut(normLut, tCorner) * localScale;
|
|
423
439
|
const amp = clampAmp(m * 127);
|
|
424
440
|
scanlines[offset++] = clampChannel(Math.round(128 - sx * tmpCornerGeom[0] * amp));
|
|
425
441
|
scanlines[offset++] = clampChannel(Math.round(128 - sy * tmpCornerGeom[1] * amp));
|
|
426
|
-
scanlines[offset++] =
|
|
442
|
+
scanlines[offset++] = encodeRimMaskChannel(tCorner);
|
|
427
443
|
scanlines[offset++] = alpha;
|
|
428
444
|
}
|
|
429
445
|
else if (distIn >= effBezel) {
|
|
@@ -433,11 +449,12 @@ function rasterizeSingleRectScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
433
449
|
scanlines[offset++] = alpha;
|
|
434
450
|
}
|
|
435
451
|
else {
|
|
436
|
-
const
|
|
452
|
+
const tEdge = distIn * invBezel;
|
|
453
|
+
const m = sampleRefractionLut(normLut, tEdge) * localScale;
|
|
437
454
|
const amp = clampAmp(m * 127);
|
|
438
455
|
scanlines[offset++] = clampChannel(Math.round(128 - gx * amp));
|
|
439
456
|
scanlines[offset++] = clampChannel(Math.round(128 - gy * amp));
|
|
440
|
-
scanlines[offset++] =
|
|
457
|
+
scanlines[offset++] = encodeRimMaskChannel(tEdge);
|
|
441
458
|
scanlines[offset++] = alpha;
|
|
442
459
|
}
|
|
443
460
|
}
|
|
@@ -643,11 +660,12 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
643
660
|
const winSy = winIsA ? pSy : dSy;
|
|
644
661
|
if (winRad > 0 && winRad < winBezel && winEdgeX < winBezel && winEdgeY < winBezel) {
|
|
645
662
|
computeCornerGeometry(winEdgeX > 0 ? winEdgeX : 0, winEdgeY > 0 ? winEdgeY : 0, winRad, winBezel);
|
|
646
|
-
const
|
|
663
|
+
const tCorner = tmpCornerGeom[2] * winInvBezel;
|
|
664
|
+
const m = sampleRefractionLut(normLut, tCorner) * winScale;
|
|
647
665
|
const amp = clampAmp(m * 127);
|
|
648
666
|
scanlines[offset++] = clampChannel(Math.round(128 - winSx * tmpCornerGeom[0] * amp));
|
|
649
667
|
scanlines[offset++] = clampChannel(Math.round(128 - winSy * tmpCornerGeom[1] * amp));
|
|
650
|
-
scanlines[offset++] =
|
|
668
|
+
scanlines[offset++] = encodeRimMaskChannel(tCorner);
|
|
651
669
|
scanlines[offset++] = alpha;
|
|
652
670
|
}
|
|
653
671
|
else if (distIn >= winBezel) {
|
|
@@ -657,11 +675,12 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
657
675
|
scanlines[offset++] = alpha;
|
|
658
676
|
}
|
|
659
677
|
else {
|
|
660
|
-
const
|
|
678
|
+
const tEdge = distIn * winInvBezel;
|
|
679
|
+
const m = sampleRefractionLut(normLut, tEdge) * winScale;
|
|
661
680
|
const amp = clampAmp(m * 127);
|
|
662
681
|
scanlines[offset++] = clampChannel(Math.round(128 - gx * amp));
|
|
663
682
|
scanlines[offset++] = clampChannel(Math.round(128 - gy * amp));
|
|
664
|
-
scanlines[offset++] =
|
|
683
|
+
scanlines[offset++] = encodeRimMaskChannel(tEdge);
|
|
665
684
|
scanlines[offset++] = alpha;
|
|
666
685
|
}
|
|
667
686
|
}
|
|
@@ -680,11 +699,12 @@ function rasterizeDualShapeScanlines(scanlines, pngW, pngH, originX, originY, st
|
|
|
680
699
|
const nx = gx * invG;
|
|
681
700
|
const ny = gy * invG;
|
|
682
701
|
const blendScale = blendBezel * invSafeBezel;
|
|
683
|
-
const
|
|
702
|
+
const tBlend = distIn / blendBezel;
|
|
703
|
+
const m = sampleRefractionLut(normLut, tBlend) * blendScale;
|
|
684
704
|
const amp = clampAmp(m * 127);
|
|
685
705
|
scanlines[offset++] = clampChannel(Math.round(128 + nx * amp));
|
|
686
706
|
scanlines[offset++] = clampChannel(Math.round(128 + ny * amp));
|
|
687
|
-
scanlines[offset++] =
|
|
707
|
+
scanlines[offset++] = encodeRimMaskChannel(tBlend);
|
|
688
708
|
scanlines[offset++] = alpha;
|
|
689
709
|
}
|
|
690
710
|
}
|
|
@@ -950,11 +970,12 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
950
970
|
const winEdgeY = soaHalfH[winIdx] - rowAbsDy[winIdx];
|
|
951
971
|
if (winRad > 0 && winRad < winBezel && winEdgeX < winBezel && winEdgeY < winBezel) {
|
|
952
972
|
computeCornerGeometry(winEdgeX > 0 ? winEdgeX : 0, winEdgeY > 0 ? winEdgeY : 0, winRad, winBezel);
|
|
953
|
-
const
|
|
973
|
+
const tCorner = tmpCornerGeom[2] * soaInvBezel[winIdx];
|
|
974
|
+
const m = sampleRefractionLut(normLut, tCorner) * winScale;
|
|
954
975
|
const amp = clampAmp(m * 127);
|
|
955
976
|
scanlines[offset++] = clampChannel(Math.round(128 - winSx * tmpCornerGeom[0] * amp));
|
|
956
977
|
scanlines[offset++] = clampChannel(Math.round(128 - rowSy[winIdx] * tmpCornerGeom[1] * amp));
|
|
957
|
-
scanlines[offset++] =
|
|
978
|
+
scanlines[offset++] = encodeRimMaskChannel(tCorner);
|
|
958
979
|
scanlines[offset++] = alpha;
|
|
959
980
|
}
|
|
960
981
|
else if (distIn >= winBezel) {
|
|
@@ -964,11 +985,12 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
964
985
|
scanlines[offset++] = alpha;
|
|
965
986
|
}
|
|
966
987
|
else {
|
|
967
|
-
const
|
|
988
|
+
const tEdge = distIn * soaInvBezel[winIdx];
|
|
989
|
+
const m = sampleRefractionLut(normLut, tEdge) * winScale;
|
|
968
990
|
const amp = clampAmp(m * 127);
|
|
969
991
|
scanlines[offset++] = clampChannel(Math.round(128 - accGx * amp));
|
|
970
992
|
scanlines[offset++] = clampChannel(Math.round(128 - accGy * amp));
|
|
971
|
-
scanlines[offset++] =
|
|
993
|
+
scanlines[offset++] = encodeRimMaskChannel(tEdge);
|
|
972
994
|
scanlines[offset++] = alpha;
|
|
973
995
|
}
|
|
974
996
|
}
|
|
@@ -985,11 +1007,12 @@ function rasterizeMultiShapeScanlines(scanlines, pngW, pngH, originX, originY, s
|
|
|
985
1007
|
const nx = accGx * invG;
|
|
986
1008
|
const ny = accGy * invG;
|
|
987
1009
|
const blendScale = accBezel * invSafeBezel;
|
|
988
|
-
const
|
|
1010
|
+
const tBlend = distIn / accBezel;
|
|
1011
|
+
const m = sampleRefractionLut(normLut, tBlend) * blendScale;
|
|
989
1012
|
const amp = clampAmp(m * 127);
|
|
990
1013
|
scanlines[offset++] = clampChannel(Math.round(128 + nx * amp));
|
|
991
1014
|
scanlines[offset++] = clampChannel(Math.round(128 + ny * amp));
|
|
992
|
-
scanlines[offset++] =
|
|
1015
|
+
scanlines[offset++] = encodeRimMaskChannel(tBlend);
|
|
993
1016
|
scanlines[offset++] = alpha;
|
|
994
1017
|
}
|
|
995
1018
|
}
|
package/dist/refractProfile.d.ts
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
* Geometric-optics 1D bezel refraction profile LUT (Snell's law + anti-caustic fold clamping).
|
|
3
3
|
*
|
|
4
4
|
* Precomputes 256-sample `Float32Array` lookup tables for named bezel cross-sections
|
|
5
|
-
* (`squircle`, `circle`, `parabolic`, `lip`) or custom height functions, allowing per-pixel
|
|
5
|
+
* (`squircle`, `c2-squircle`, `circle`, `parabolic`, `lip`) or custom height functions, allowing per-pixel
|
|
6
6
|
* displacement map rasterizers to evaluate physical ray refraction via cheap linear
|
|
7
7
|
* interpolation with zero runtime dependencies or per-frame GC churn.
|
|
8
8
|
*
|
|
9
9
|
* SPDX-License-Identifier: MPL-2.0
|
|
10
10
|
*/
|
|
11
|
-
export type RefractionProfileName = 'squircle' | 'circle' | 'parabolic' | 'lip';
|
|
11
|
+
export type RefractionProfileName = 'squircle' | 'c2-squircle' | 'circle' | 'parabolic' | 'lip';
|
|
12
12
|
export type BezelHeightProfileName = RefractionProfileName;
|
|
13
13
|
/** Normalized bezel height function `f(t)` in `[0, 1]` from outer edge (`t=0`) to plateau (`t=1`). */
|
|
14
14
|
export type BezelHeightFn = (t: number) => number;
|
|
@@ -58,8 +58,34 @@ export interface BezelRayTrace {
|
|
|
58
58
|
/** Inward horizontal displacement where the refracted ray lands, in CSS px. */
|
|
59
59
|
shift: number;
|
|
60
60
|
}
|
|
61
|
+
/** Per-channel refractive indices `(nR, nG, nB)` derived from Cauchy's dispersion model. */
|
|
62
|
+
export interface CauchyChannelIor {
|
|
63
|
+
r: number;
|
|
64
|
+
g: number;
|
|
65
|
+
b: number;
|
|
66
|
+
}
|
|
67
|
+
/** Per-channel displacement scales `(scaleR, scaleG, scaleB)` in CSS px. */
|
|
68
|
+
export interface CauchyChannelScales {
|
|
69
|
+
r: number;
|
|
70
|
+
g: number;
|
|
71
|
+
b: number;
|
|
72
|
+
}
|
|
73
|
+
export interface CauchyDispersionScaleOptions {
|
|
74
|
+
/** Base green-channel (`d`-line) displacement scale in CSS px. */
|
|
75
|
+
baseScale: number;
|
|
76
|
+
/** Base refractive index (`n_d`, defaults to `1.5`). */
|
|
77
|
+
ior?: number;
|
|
78
|
+
/** Abbe number `V_d = (n_d - 1) / (n_F - n_C)`; lower values disperse more strongly (`<= 0` disables). */
|
|
79
|
+
abbeNumber: number;
|
|
80
|
+
}
|
|
61
81
|
export declare const DEFAULT_REFRACTION_SAMPLES = 256;
|
|
62
82
|
export declare const DEFAULT_REFRACTION_IOR = 1.5;
|
|
83
|
+
/** Fraunhofer C (656.3 nm), d (587.6 nm), and F (486.1 nm) wavelengths in micrometers. */
|
|
84
|
+
export declare const FRAUNHOFER_WAVELENGTHS_UM: {
|
|
85
|
+
readonly r: 0.6563;
|
|
86
|
+
readonly g: 0.5876;
|
|
87
|
+
readonly b: 0.4861;
|
|
88
|
+
};
|
|
63
89
|
/** Maximum inward displacement decrease per px (`-d(shift)/du < 1`) to keep mapping injective. */
|
|
64
90
|
export declare const CAUSTIC_FOLD_LIMIT = 0.85;
|
|
65
91
|
/** Lower bound on blended SDF gradient norm in `smin` necks to avoid 180-degree saddle flips. */
|
|
@@ -82,3 +108,16 @@ export declare function resolveRefractionProfileLut(options?: RefractionProfileO
|
|
|
82
108
|
export declare function sampleRefractionLut(table: Float32Array, t: number): number;
|
|
83
109
|
/** Traces a vertical view ray through the bezel at `d` px inward from the outline. */
|
|
84
110
|
export declare function traceBezelRay(input: BezelRayTraceInput, d: number): BezelRayTrace;
|
|
111
|
+
/**
|
|
112
|
+
* Computes per-channel RGB refractive indices `(n_C, n_d, n_F)` from a base index `ior` (`n_d`)
|
|
113
|
+
* and Abbe number `abbeNumber` (`V_d`) using the 2-term Cauchy dispersion relation
|
|
114
|
+
* `n(lambda) = A + B / lambda^2`.
|
|
115
|
+
*/
|
|
116
|
+
export declare function computeCauchyIor(ior: number, abbeNumber: number): CauchyChannelIor;
|
|
117
|
+
/**
|
|
118
|
+
* Derives physical per-channel RGB displacement scales from paraxial Snell's law
|
|
119
|
+
* (`deviation(n) / deviation(n_d) = (1 - 1 / n) / (1 - 1 / n_d)`) combined with the
|
|
120
|
+
* 2-term Cauchy dispersion model (`computeCauchyIor`). Runs in O(1) with zero allocations
|
|
121
|
+
* and remains well-behaved even when the bezel LUT is anti-caustic clamped.
|
|
122
|
+
*/
|
|
123
|
+
export declare function computeCauchyDispersionScales(options: CauchyDispersionScaleOptions): CauchyChannelScales;
|
package/dist/refractProfile.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Geometric-optics 1D bezel refraction profile LUT (Snell's law + anti-caustic fold clamping).
|
|
3
3
|
*
|
|
4
4
|
* Precomputes 256-sample `Float32Array` lookup tables for named bezel cross-sections
|
|
5
|
-
* (`squircle`, `circle`, `parabolic`, `lip`) or custom height functions, allowing per-pixel
|
|
5
|
+
* (`squircle`, `c2-squircle`, `circle`, `parabolic`, `lip`) or custom height functions, allowing per-pixel
|
|
6
6
|
* displacement map rasterizers to evaluate physical ray refraction via cheap linear
|
|
7
7
|
* interpolation with zero runtime dependencies or per-frame GC churn.
|
|
8
8
|
*
|
|
@@ -10,15 +10,40 @@
|
|
|
10
10
|
*/
|
|
11
11
|
export const DEFAULT_REFRACTION_SAMPLES = 256;
|
|
12
12
|
export const DEFAULT_REFRACTION_IOR = 1.5;
|
|
13
|
+
/** Fraunhofer C (656.3 nm), d (587.6 nm), and F (486.1 nm) wavelengths in micrometers. */
|
|
14
|
+
export const FRAUNHOFER_WAVELENGTHS_UM = {
|
|
15
|
+
r: 0.6563,
|
|
16
|
+
g: 0.5876,
|
|
17
|
+
b: 0.4861,
|
|
18
|
+
};
|
|
19
|
+
// Normalized 2-term Cauchy offsets `(lambda^-2 - lambda_d^-2) / (lambda_F^-2 - lambda_C^-2)`.
|
|
20
|
+
// By construction `CAUCHY_B_FRAC - CAUCHY_R_FRAC === 1`, preserving `n_F - n_C = (n_d - 1) / V_d`
|
|
21
|
+
// while capturing the physical ~2.3x stronger short-wavelength (blue) bending relative to green.
|
|
22
|
+
const INV_C2 = 1 / (FRAUNHOFER_WAVELENGTHS_UM.r * FRAUNHOFER_WAVELENGTHS_UM.r);
|
|
23
|
+
const INV_D2 = 1 / (FRAUNHOFER_WAVELENGTHS_UM.g * FRAUNHOFER_WAVELENGTHS_UM.g);
|
|
24
|
+
const INV_F2 = 1 / (FRAUNHOFER_WAVELENGTHS_UM.b * FRAUNHOFER_WAVELENGTHS_UM.b);
|
|
25
|
+
const CAUCHY_R_FRAC = (INV_C2 - INV_D2) / (INV_F2 - INV_C2);
|
|
26
|
+
const CAUCHY_B_FRAC = (INV_F2 - INV_D2) / (INV_F2 - INV_C2);
|
|
13
27
|
/** Maximum inward displacement decrease per px (`-d(shift)/du < 1`) to keep mapping injective. */
|
|
14
28
|
export const CAUSTIC_FOLD_LIMIT = 0.85;
|
|
15
29
|
/** Lower bound on blended SDF gradient norm in `smin` necks to avoid 180-degree saddle flips. */
|
|
16
30
|
export const NORMAL_FLOOR = 0.25;
|
|
17
31
|
const MAX_LUT_CACHE_ENTRIES = 64;
|
|
18
32
|
const squircleHeight = (t) => Math.pow(Math.max(0, 1 - Math.pow(Math.max(0, 1 - t), 4)), 0.25);
|
|
33
|
+
/**
|
|
34
|
+
* C2-continuous quartic bezel cross-section (`1 - (1 - t)^4`): finite slope at the outer rim
|
|
35
|
+
* (`f'(0) = 4`) and vanishing first and second derivatives at the plateau (`f'(1) = f''(1) = 0`),
|
|
36
|
+
* eliminating the curvature jump where circular/parabolic bezels meet the flat center.
|
|
37
|
+
*/
|
|
38
|
+
const c2SquircleHeight = (t) => {
|
|
39
|
+
const u = Math.min(1, Math.max(0, 1 - t));
|
|
40
|
+
const u2 = u * u;
|
|
41
|
+
return 1 - u2 * u2;
|
|
42
|
+
};
|
|
19
43
|
/** Built-in physical bezel cross-section height functions (`t = 0` outer rim, `t = 1` plateau). */
|
|
20
44
|
export const BEZEL_HEIGHT_PROFILES = {
|
|
21
45
|
squircle: squircleHeight,
|
|
46
|
+
'c2-squircle': c2SquircleHeight,
|
|
22
47
|
circle: (t) => Math.sqrt(Math.max(0, 1 - (1 - t) * (1 - t))),
|
|
23
48
|
parabolic: (t) => 1 - (1 - t) * (1 - t),
|
|
24
49
|
lip: (t) => squircleHeight(t) + 0.14 * Math.pow(Math.sin(Math.PI * t), 2),
|
|
@@ -187,3 +212,46 @@ export function traceBezelRay(input, d) {
|
|
|
187
212
|
shift: ior <= 1 + 1e-6 || thickness <= 0 ? 0 : height * Math.tan(deviation),
|
|
188
213
|
};
|
|
189
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Computes per-channel RGB refractive indices `(n_C, n_d, n_F)` from a base index `ior` (`n_d`)
|
|
217
|
+
* and Abbe number `abbeNumber` (`V_d`) using the 2-term Cauchy dispersion relation
|
|
218
|
+
* `n(lambda) = A + B / lambda^2`.
|
|
219
|
+
*/
|
|
220
|
+
export function computeCauchyIor(ior, abbeNumber) {
|
|
221
|
+
const safeIor = Math.max(1, Number.isFinite(ior) ? ior : DEFAULT_REFRACTION_IOR);
|
|
222
|
+
if (safeIor <= 1 + 1e-6 || !Number.isFinite(abbeNumber) || abbeNumber <= 0) {
|
|
223
|
+
return { r: safeIor, g: safeIor, b: safeIor };
|
|
224
|
+
}
|
|
225
|
+
const deltaN = (safeIor - 1) / abbeNumber;
|
|
226
|
+
return {
|
|
227
|
+
r: Math.max(1, safeIor + CAUCHY_R_FRAC * deltaN),
|
|
228
|
+
g: safeIor,
|
|
229
|
+
b: safeIor + CAUCHY_B_FRAC * deltaN,
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Derives physical per-channel RGB displacement scales from paraxial Snell's law
|
|
234
|
+
* (`deviation(n) / deviation(n_d) = (1 - 1 / n) / (1 - 1 / n_d)`) combined with the
|
|
235
|
+
* 2-term Cauchy dispersion model (`computeCauchyIor`). Runs in O(1) with zero allocations
|
|
236
|
+
* and remains well-behaved even when the bezel LUT is anti-caustic clamped.
|
|
237
|
+
*/
|
|
238
|
+
export function computeCauchyDispersionScales(options) {
|
|
239
|
+
const baseScale = Number.isFinite(options.baseScale) && options.baseScale > 0 ? options.baseScale : 0;
|
|
240
|
+
if (baseScale <= 0) {
|
|
241
|
+
return { r: 0, g: 0, b: 0 };
|
|
242
|
+
}
|
|
243
|
+
const rawIor = options.ior;
|
|
244
|
+
const safeIor = Math.max(1, rawIor !== undefined && Number.isFinite(rawIor) ? rawIor : DEFAULT_REFRACTION_IOR);
|
|
245
|
+
if (safeIor <= 1 + 1e-6 || !Number.isFinite(options.abbeNumber) || options.abbeNumber <= 0) {
|
|
246
|
+
return { r: baseScale, g: baseScale, b: baseScale };
|
|
247
|
+
}
|
|
248
|
+
const ch = computeCauchyIor(safeIor, options.abbeNumber);
|
|
249
|
+
const refFactor = 1 - 1 / ch.g;
|
|
250
|
+
const rRatio = (1 - 1 / ch.r) / refFactor;
|
|
251
|
+
const bRatio = (1 - 1 / ch.b) / refFactor;
|
|
252
|
+
return {
|
|
253
|
+
r: Math.max(0, baseScale * rRatio),
|
|
254
|
+
g: baseScale,
|
|
255
|
+
b: Math.max(0, baseScale * bRatio),
|
|
256
|
+
};
|
|
257
|
+
}
|
package/dist/refractTiles.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ export interface RefractTileParams {
|
|
|
19
19
|
bezel: number;
|
|
20
20
|
/** Optional logical CSS corner tile size; defaults to `ceil(max(radius, bezel))`. */
|
|
21
21
|
slice?: number;
|
|
22
|
-
/** Refraction cross-section profile (`'squircle'`, `'circle'`, `'parabolic'`, `'lip'`, or custom height fn). */
|
|
22
|
+
/** Refraction cross-section profile (`'squircle'`, `'c2-squircle'`, `'circle'`, `'parabolic'`, `'lip'`, or custom height fn). */
|
|
23
23
|
profile?: RefractionProfileSpec;
|
|
24
24
|
/** Index of refraction (`1` = air, `1.5` = glass; defaults to `1.5`). */
|
|
25
25
|
ior?: number;
|
package/dist/refractTiles.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*
|
|
11
11
|
* SPDX-License-Identifier: MPL-2.0
|
|
12
12
|
*/
|
|
13
|
-
import { clampAmp, clampChannel, computeCornerGeometry, encodeStoredRgbaPngDataUrl, ensureScanlineCapacity, tmpCornerGeom, } from "./refractMap.js";
|
|
13
|
+
import { clampAmp, clampChannel, computeCornerGeometry, encodeRimMaskChannel, encodeStoredRgbaPngDataUrl, ensureScanlineCapacity, tmpCornerGeom, } from "./refractMap.js";
|
|
14
14
|
import { DEFAULT_REFRACTION_IOR, getRefractionProfileKey, resolveRefractionProfileLut, sampleRefractionLut, } from "./refractProfile.js";
|
|
15
15
|
export const MAX_REFRACT_TILE_PX = 128;
|
|
16
16
|
const DEFAULT_THICKNESS_RATIO = 1;
|
|
@@ -88,10 +88,11 @@ function encodeCornerTile(slice, tilePx, rad, effBezel, invBezel, interiorGate,
|
|
|
88
88
|
}
|
|
89
89
|
if (bendCorners && edgeX < effBezel && edgeY < effBezel) {
|
|
90
90
|
computeCornerGeometry(edgeX > 0 ? edgeX : 0, edgeY > 0 ? edgeY : 0, rad, effBezel);
|
|
91
|
-
const
|
|
91
|
+
const tCorner = tmpCornerGeom[2] * invBezel;
|
|
92
|
+
const amp = clampAmp(sampleRefractionLut(normLut, tCorner) * 127);
|
|
92
93
|
scanlines[offset++] = clampChannel(Math.round(128 - sx * tmpCornerGeom[0] * amp));
|
|
93
94
|
scanlines[offset++] = clampChannel(Math.round(128 - sy * tmpCornerGeom[1] * amp));
|
|
94
|
-
scanlines[offset++] =
|
|
95
|
+
scanlines[offset++] = encodeRimMaskChannel(tCorner);
|
|
95
96
|
scanlines[offset++] = alpha;
|
|
96
97
|
}
|
|
97
98
|
else if (distIn >= effBezel) {
|
|
@@ -101,10 +102,11 @@ function encodeCornerTile(slice, tilePx, rad, effBezel, invBezel, interiorGate,
|
|
|
101
102
|
scanlines[offset++] = alpha;
|
|
102
103
|
}
|
|
103
104
|
else {
|
|
104
|
-
const
|
|
105
|
+
const tEdge = distIn * invBezel;
|
|
106
|
+
const amp = clampAmp(sampleRefractionLut(normLut, tEdge) * 127);
|
|
105
107
|
scanlines[offset++] = clampChannel(Math.round(128 - gx * amp));
|
|
106
108
|
scanlines[offset++] = clampChannel(Math.round(128 - gy * amp));
|
|
107
|
-
scanlines[offset++] =
|
|
109
|
+
scanlines[offset++] = encodeRimMaskChannel(tEdge);
|
|
108
110
|
scanlines[offset++] = alpha;
|
|
109
111
|
}
|
|
110
112
|
}
|
|
@@ -130,10 +132,11 @@ function encodeHorizontalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, h
|
|
|
130
132
|
scanlines[offset++] = alpha;
|
|
131
133
|
}
|
|
132
134
|
else {
|
|
133
|
-
const
|
|
135
|
+
const tEdge = edgeY * invBezel;
|
|
136
|
+
const amp = clampAmp(sampleRefractionLut(normLut, tEdge) * 127);
|
|
134
137
|
scanlines[offset++] = 128;
|
|
135
138
|
scanlines[offset++] = clampChannel(Math.round(128 - sy * amp));
|
|
136
|
-
scanlines[offset++] =
|
|
139
|
+
scanlines[offset++] = encodeRimMaskChannel(tEdge);
|
|
137
140
|
scanlines[offset++] = alpha;
|
|
138
141
|
}
|
|
139
142
|
}
|
|
@@ -157,10 +160,11 @@ function encodeVerticalEdgeStrip(slice, tilePx, effBezel, invBezel, normLut, has
|
|
|
157
160
|
scanlines[offset++] = alpha;
|
|
158
161
|
}
|
|
159
162
|
else {
|
|
160
|
-
const
|
|
163
|
+
const tEdge = edgeX * invBezel;
|
|
164
|
+
const amp = clampAmp(sampleRefractionLut(normLut, tEdge) * 127);
|
|
161
165
|
scanlines[offset++] = clampChannel(Math.round(128 - sx * amp));
|
|
162
166
|
scanlines[offset++] = 128;
|
|
163
|
-
scanlines[offset++] =
|
|
167
|
+
scanlines[offset++] = encodeRimMaskChannel(tEdge);
|
|
164
168
|
scanlines[offset++] = alpha;
|
|
165
169
|
}
|
|
166
170
|
}
|