@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/contracts.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*
|
|
6
6
|
* SPDX-License-Identifier: MPL-2.0
|
|
7
7
|
*/
|
|
8
|
+
import type { BezelHeightProfileName } from './refractProfile.ts';
|
|
8
9
|
export interface Point {
|
|
9
10
|
x: number;
|
|
10
11
|
y: number;
|
|
@@ -45,8 +46,8 @@ export interface GlassFieldState {
|
|
|
45
46
|
/** Material transition weight from directional to soft rim highlights in [0, 1] (defaults to 0). */
|
|
46
47
|
styleMix?: number;
|
|
47
48
|
}
|
|
48
|
-
/** Rim highlight mode: 'directional' favors directional speculars; 'soft' favors wide diffuse glow. */
|
|
49
|
-
export type RimMode = 'directional' | 'soft';
|
|
49
|
+
/** Rim highlight mode: 'directional' favors directional speculars; 'soft' favors wide diffuse glow; 'physical' uses 3D bevel-slope optics. */
|
|
50
|
+
export type RimMode = 'directional' | 'soft' | 'physical';
|
|
50
51
|
export type ThemeMode = 'light' | 'dark';
|
|
51
52
|
export type OpticalPresetVariant = 'crisp' | 'frosted';
|
|
52
53
|
export interface OpticalMaterial {
|
|
@@ -78,6 +79,14 @@ export interface OpticalMaterial {
|
|
|
78
79
|
borderRgba?: readonly [number, number, number, number];
|
|
79
80
|
/** Optional bottom directional highlight ratio relative to the top edge (defaults to 0.72). */
|
|
80
81
|
rimBottomRatio?: number;
|
|
82
|
+
/** Optional bezel cross-section height curve used when `rimMode === 'physical'` (defaults to `'squircle'`). */
|
|
83
|
+
bevelProfile?: BezelHeightProfileName;
|
|
84
|
+
/** Optional directional light azimuth in degrees clockwise from top for `rimMode === 'physical'` (defaults to `-45`). */
|
|
85
|
+
lightAngle?: number;
|
|
86
|
+
/** Optional directional light elevation above the surface in degrees for `rimMode === 'physical'` (defaults to `22`). */
|
|
87
|
+
lightElevation?: number;
|
|
88
|
+
/** Optional grazing blue-black rim shade strength in `[0, 1]` for `rimMode === 'physical'` (defaults to `0.32`). */
|
|
89
|
+
rimShade?: number;
|
|
81
90
|
}
|
|
82
91
|
export declare const DEFAULT_OPTICAL_MATERIALS: {
|
|
83
92
|
readonly light: {
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ export { DEFAULT_OPTICAL_MATERIALS, DEFAULT_SPRINGS, MAX_GPU_PRIMITIVES, type Ed
|
|
|
8
8
|
export { computeFieldBounds, computePairMergeStatus, computeRoundedBoxDistance, computeRoundedBoxSdf, computeSmin, computeSminDistance, createPreparedField, evaluateFieldDistance, evaluateFieldSdf, prepareField, type PairMergeStatus, type PreparedField, } from './sdf.ts';
|
|
9
9
|
export { extractFieldContourPath, formatRoundedRectPath, } from './contour.ts';
|
|
10
10
|
export { buildRefractMapDataUrl, createRefractMapBuilder, type RefractMapBuilder, type RefractMapParams, type RefractMapResult, } from './refractMap.ts';
|
|
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 BezelHeightProfileName, type BezelRayTrace, type BezelRayTraceInput, type RefractionProfileLut, type RefractionProfileName, type RefractionProfileOptions, type RefractionProfileSpec, } from './refractProfile.ts';
|
|
11
13
|
export { OVERLAY_FRAGMENT_SHADER, OVERLAY_VERTEX_SHADER, createGlassOverlayRenderer, } from './overlayRenderer.ts';
|
|
12
|
-
export { clamp, createElasticPressController, createMorphTimeline, createSpringChannel, cubicBezier, easeInOutCubic, easeInQuad, easeOutCubic, evaluateSpring, evaluateSpringState, lerp, riseAndFall, smoothstep, type ElasticPressController, type ElasticPressOptions, type ElasticPressState, type MorphTimeline, type MorphTimelineOptions, type MorphTimelinePhase, type MorphTimelineState, type SpringChannel, type SpringChannelOptions, type SpringChannelState, type SpringSample, } from './physics.ts';
|
|
14
|
+
export { clamp, computeAreaPreservingScale, createElasticPressController, createMorphTimeline, createSpringChannel, cubicBezier, deformSizePreservingArea, easeInOutCubic, easeInQuad, easeOutCubic, evaluateSpring, evaluateSpringState, lerp, riseAndFall, smoothstep, type AreaPreservingScale, type ElasticPressController, type ElasticPressOptions, type ElasticPressState, type MorphTimeline, type MorphTimelineOptions, type MorphTimelinePhase, type MorphTimelineState, type SpringChannel, type SpringChannelOptions, type SpringChannelState, type SpringSample, } from './physics.ts';
|
|
13
15
|
export { buildCircularBevelTable, buildColorOpticsMatrix, buildContourAlphaDataUrl, interpolateCssGlassSurfaceToMaterial, interpolateOpticalMaterial, interpolateVeilOptics, parseCssBoxShadow, parseCssFilterOptics, parseCssGlassSurface, parseCssRgba, resolveOpticalMaterial, type BackdropVeilOptics, type CircularBevelTable, type ExtractedCssGlassSurface, type ParseCssGlassSurfaceInput, type ParsedBoxShadowLayer, } from './optics.ts';
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,8 @@ export { DEFAULT_OPTICAL_MATERIALS, DEFAULT_SPRINGS, MAX_GPU_PRIMITIVES, } from
|
|
|
8
8
|
export { computeFieldBounds, computePairMergeStatus, computeRoundedBoxDistance, computeRoundedBoxSdf, computeSmin, computeSminDistance, createPreparedField, evaluateFieldDistance, evaluateFieldSdf, prepareField, } from "./sdf.js";
|
|
9
9
|
export { extractFieldContourPath, formatRoundedRectPath, } from "./contour.js";
|
|
10
10
|
export { buildRefractMapDataUrl, createRefractMapBuilder, } from "./refractMap.js";
|
|
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";
|
|
11
13
|
export { OVERLAY_FRAGMENT_SHADER, OVERLAY_VERTEX_SHADER, createGlassOverlayRenderer, } from "./overlayRenderer.js";
|
|
12
|
-
export { clamp, createElasticPressController, createMorphTimeline, createSpringChannel, cubicBezier, easeInOutCubic, easeInQuad, easeOutCubic, evaluateSpring, evaluateSpringState, lerp, riseAndFall, smoothstep, } from "./physics.js";
|
|
14
|
+
export { clamp, computeAreaPreservingScale, createElasticPressController, createMorphTimeline, createSpringChannel, cubicBezier, deformSizePreservingArea, easeInOutCubic, easeInQuad, easeOutCubic, evaluateSpring, evaluateSpringState, lerp, riseAndFall, smoothstep, } from "./physics.js";
|
|
13
15
|
export { buildCircularBevelTable, buildColorOpticsMatrix, buildContourAlphaDataUrl, interpolateCssGlassSurfaceToMaterial, interpolateOpticalMaterial, interpolateVeilOptics, parseCssBoxShadow, parseCssFilterOptics, parseCssGlassSurface, parseCssRgba, resolveOpticalMaterial, } from "./optics.js";
|
package/dist/optics.js
CHANGED
|
@@ -478,6 +478,19 @@ export function interpolateOpticalMaterial(from, to, t) {
|
|
|
478
478
|
if (from.rimBottomRatio !== undefined || to.rimBottomRatio !== undefined) {
|
|
479
479
|
result.rimBottomRatio = lerp(from.rimBottomRatio ?? 0.72, to.rimBottomRatio ?? 0.72, u);
|
|
480
480
|
}
|
|
481
|
+
if (from.bevelProfile !== undefined || to.bevelProfile !== undefined) {
|
|
482
|
+
result.bevelProfile =
|
|
483
|
+
u < 0.5 ? (from.bevelProfile ?? to.bevelProfile) : (to.bevelProfile ?? from.bevelProfile);
|
|
484
|
+
}
|
|
485
|
+
if (from.lightAngle !== undefined || to.lightAngle !== undefined) {
|
|
486
|
+
result.lightAngle = lerp(from.lightAngle ?? -45, to.lightAngle ?? -45, u);
|
|
487
|
+
}
|
|
488
|
+
if (from.lightElevation !== undefined || to.lightElevation !== undefined) {
|
|
489
|
+
result.lightElevation = lerp(from.lightElevation ?? 22, to.lightElevation ?? 22, u);
|
|
490
|
+
}
|
|
491
|
+
if (from.rimShade !== undefined || to.rimShade !== undefined) {
|
|
492
|
+
result.rimShade = lerp(from.rimShade ?? 0.32, to.rimShade ?? 0.32, u);
|
|
493
|
+
}
|
|
481
494
|
return result;
|
|
482
495
|
}
|
|
483
496
|
/** 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)\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\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\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 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 < acc.dist) {\n acc = b;\n }\n } else {\n float h = clamp(0.5 + 0.5 * (b.dist - acc.dist) / uBlendK, 0.0, 1.0);\n float d = mix(b.dist, acc.dist, h) - uBlendK * h * (1.0 - h);\n vec2 n = mix(b.normal, acc.normal, h);\n float nl = length(n);\n n = (nl > 1e-5) ? (n / nl) : acc.normal;\n acc = SdfSample(d, n);\n }\n }\n\n return acc;\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 = smoothstep(0.5 * aa, -0.5 * aa, d);\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 hairlineHalfW = hw * 0.5;\n float hasUniformBorder = step(0.001, uBorderParams.x);\n float hairlineCenter = mix(-0.25 + hairlineHalfW, hairlineHalfW, hasUniformBorder);\n float hairlineMinD = mix(-0.25 - 0.5 * aa, -0.15 * aa, hasUniformBorder);\n float hairlineBand =\n smoothstep(hairlineHalfW + 0.5 * aa, hairlineHalfW * 0.25, abs(d - hairlineCenter)) *\n smoothstep(hairlineMinD, hairlineMinD + 0.75 * aa, d);\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 (glassMask > 0.001 && hasUniformBorder > 0.5 && inset < uBorderParams.x + aa) {\n borderMask = smoothstep(uBorderParams.x + 0.5 * aa, max(0.0, uBorderParams.x - 0.5 * aa), inset) * glassMask;\n borderA = clamp(borderMask * uBorderColor.a * uOpacity, 0.0, 1.0);\n borderRgb = uBorderColor.rgb * borderA;\n }\n\n float rimA = 0.0;\n if (glassMask > 0.001 && inset < 5.0 && uRimParams.x > 0.001) {\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 }\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) {\n fragColor = vec4(0.0);\n return;\n }\n\n // Premultiplied alpha composite: uniform border over inner rim over hairline and shadow\n float darkA = clamp(shadowA + hairA * (1.0 - shadowA), 0.0, 1.0);\n vec3 afterRimRgb = vec3(rimA);\n float afterRimA = rimA + darkA * (1.0 - rimA);\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)\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\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 = smoothstep(0.5 * aa, -0.5 * aa, d);\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 hairlineHalfW = hw * 0.5;\n float hasUniformBorder = step(0.001, uBorderParams.x);\n float hairlineCenter = mix(-0.25 + hairlineHalfW, hairlineHalfW, hasUniformBorder);\n float hairlineMinD = mix(-0.25 - 0.5 * aa, -0.15 * aa, hasUniformBorder);\n float hairlineBand =\n smoothstep(hairlineHalfW + 0.5 * aa, hairlineHalfW * 0.25, abs(d - hairlineCenter)) *\n smoothstep(hairlineMinD, hairlineMinD + 0.75 * aa, d);\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 (glassMask > 0.001 && hasUniformBorder > 0.5 && inset < uBorderParams.x + aa) {\n borderMask = smoothstep(uBorderParams.x + 0.5 * aa, max(0.0, uBorderParams.x - 0.5 * aa), inset) * glassMask;\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";
|
|
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
|
@@ -52,6 +52,8 @@ uniform vec4 uHairline; // (width, sideAlpha, cornerAlpha, flatAlpha)
|
|
|
52
52
|
uniform vec3 uShadow; // (blur, offsetY, alpha)
|
|
53
53
|
uniform vec4 uBorderColor; // normalized RGBA of optional uniform inner border
|
|
54
54
|
uniform vec2 uBorderParams; // (borderWidthPx, rimBottomRatio)
|
|
55
|
+
uniform vec4 uPhysicalLight; // (lx, ly, lz, enabled)
|
|
56
|
+
uniform vec4 uPhysicalParams; // (bevelPx, specularStrength, shadeStrength, profileCode)
|
|
55
57
|
|
|
56
58
|
struct SdfSample {
|
|
57
59
|
float dist;
|
|
@@ -116,30 +118,48 @@ SdfSample sdRoundedBoxRefined(vec2 p, vec2 center, vec2 halfSize, float r) {
|
|
|
116
118
|
return SdfSample(baseDist, normal);
|
|
117
119
|
}
|
|
118
120
|
|
|
121
|
+
float evalBezelHeight(float t, float code) {
|
|
122
|
+
float u = clamp(1.0 - t, 0.0, 1.0);
|
|
123
|
+
if (code > 2.5) {
|
|
124
|
+
float sq = safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);
|
|
125
|
+
float s = sin(3.14159265 * clamp(t, 0.0, 1.0));
|
|
126
|
+
return sq + 0.14 * s * s;
|
|
127
|
+
}
|
|
128
|
+
if (code > 1.5) {
|
|
129
|
+
return 1.0 - u * u;
|
|
130
|
+
}
|
|
131
|
+
if (code > 0.5) {
|
|
132
|
+
return sqrt(max(0.0, 1.0 - u * u));
|
|
133
|
+
}
|
|
134
|
+
return safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);
|
|
135
|
+
}
|
|
136
|
+
|
|
119
137
|
SdfSample evaluateGlassSdf(vec2 p) {
|
|
120
138
|
if (uPrimitiveCount <= 0) {
|
|
121
139
|
return SdfSample(1e6, vec2(0.0, -1.0));
|
|
122
140
|
}
|
|
123
141
|
|
|
124
142
|
SdfSample acc = sdRoundedBoxRefined(p, uPrimitives[0].xy, uPrimitives[0].zw, uRadii[0]);
|
|
143
|
+
float d = acc.dist;
|
|
144
|
+
vec2 g = acc.normal;
|
|
125
145
|
for (int i = 1; i < MAX_PRIMITIVES; i++) {
|
|
126
146
|
if (i >= uPrimitiveCount) break;
|
|
127
147
|
SdfSample b = sdRoundedBoxRefined(p, uPrimitives[i].xy, uPrimitives[i].zw, uRadii[i]);
|
|
128
148
|
if (uBlendK <= 0.001) {
|
|
129
|
-
if (b.dist <
|
|
130
|
-
|
|
149
|
+
if (b.dist < d) {
|
|
150
|
+
d = b.dist;
|
|
151
|
+
g = b.normal;
|
|
131
152
|
}
|
|
132
153
|
} else {
|
|
133
|
-
float h = clamp(0.5 + 0.5 * (b.dist -
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
float nl = length(n);
|
|
137
|
-
n = (nl > 1e-5) ? (n / nl) : acc.normal;
|
|
138
|
-
acc = SdfSample(d, n);
|
|
154
|
+
float h = clamp(0.5 + 0.5 * (b.dist - d) / uBlendK, 0.0, 1.0);
|
|
155
|
+
d = mix(b.dist, d, h) - uBlendK * h * (1.0 - h);
|
|
156
|
+
g = mix(b.normal, g, h);
|
|
139
157
|
}
|
|
140
158
|
}
|
|
141
159
|
|
|
142
|
-
|
|
160
|
+
// Soften cancelling normals in smin saddle necks (NORMAL_FLOOR = 0.25) so speculars never flip 180 degrees.
|
|
161
|
+
float gl = max(length(g), 0.25);
|
|
162
|
+
return SdfSample(d, g / gl);
|
|
143
163
|
}
|
|
144
164
|
|
|
145
165
|
void main() {
|
|
@@ -198,7 +218,11 @@ void main() {
|
|
|
198
218
|
}
|
|
199
219
|
|
|
200
220
|
float rimA = 0.0;
|
|
201
|
-
|
|
221
|
+
float physLitA = 0.0;
|
|
222
|
+
float physShadeA = 0.0;
|
|
223
|
+
float isPhysicalRim = step(0.5, uPhysicalLight.w) * (1.0 - hasUniformBorder);
|
|
224
|
+
|
|
225
|
+
if (glassMask > 0.001 && inset < 5.0 && uRimParams.x > 0.001 && isPhysicalRim < 0.5) {
|
|
202
226
|
float ny = abs(normal.y);
|
|
203
227
|
float wDirectional = 1.0 - clamp(uStyleMix, 0.0, 1.0);
|
|
204
228
|
float wSoft = clamp(uStyleMix, 0.0, 1.0) * clamp(uRimOpacity, 0.0, 1.0);
|
|
@@ -231,18 +255,48 @@ void main() {
|
|
|
231
255
|
// Boost specular luminance to match source-over alpha blending expectations
|
|
232
256
|
rimA = clamp(rimA * 1.35 * glassMask * uOpacity, 0.0, 1.0);
|
|
233
257
|
}
|
|
258
|
+
} else if (glassMask > 0.001 && isPhysicalRim > 0.5 && inset < uPhysicalParams.x) {
|
|
259
|
+
float bezelPx = max(uPhysicalParams.x, 1.0);
|
|
260
|
+
float t = clamp(inset / bezelPx, 0.0, 1.0);
|
|
261
|
+
float eps = 0.015;
|
|
262
|
+
float t0 = max(0.0, t - eps);
|
|
263
|
+
float t1 = min(1.0, t + eps);
|
|
264
|
+
float slope = (evalBezelHeight(t1, uPhysicalParams.w) - evalBezelHeight(t0, uPhysicalParams.w)) / max(t1 - t0, 1e-4);
|
|
265
|
+
vec3 N = normalize(vec3(normal * slope, 1.0));
|
|
266
|
+
vec3 L = uPhysicalLight.xyz;
|
|
267
|
+
vec3 H = vec3(L.xy, L.z + 1.0);
|
|
268
|
+
float hLen = max(length(H), 1e-4);
|
|
269
|
+
const float shininess = 36.0;
|
|
270
|
+
float flatBase = safePow(clamp((L.z + 1.0) / hLen, 0.0, 1.0), shininess);
|
|
271
|
+
float denomBase = max(1.0 - flatBase, 1e-4);
|
|
272
|
+
float hDot = clamp(dot(N, H) / hLen, 0.0, 1.0);
|
|
273
|
+
float highlight = max(safePow(hDot, shininess) - flatBase, 0.0) / denomBase;
|
|
274
|
+
vec3 H_bounce = vec3(-L.xy, L.z + 1.0);
|
|
275
|
+
float bDot = clamp(dot(N, H_bounce) / hLen, 0.0, 1.0);
|
|
276
|
+
float bounce = max(safePow(bDot, shininess) - flatBase, 0.0) / denomBase;
|
|
277
|
+
vec2 planar = L.xy / max(length(L.xy), 1e-4);
|
|
278
|
+
float facing = dot(normal, planar);
|
|
279
|
+
float fresnel = safePow(clamp(1.0 - N.z, 0.0, 1.0), 4.0);
|
|
280
|
+
float rimTerm = fresnel * (0.25 + 0.75 * max(facing, 0.0) + 0.45 * max(-facing, 0.0));
|
|
281
|
+
float shadeTerm = safePow(clamp(1.0 - N.z, 0.0, 1.0), 2.5) * (0.45 + 0.55 * max(-facing, 0.0));
|
|
282
|
+
float lit = clamp((uPhysicalParams.y * (highlight + 0.45 * bounce) + uRimParams.x * rimTerm) * glassMask * uOpacity, 0.0, 1.0);
|
|
283
|
+
float dark = clamp(uPhysicalParams.z * shadeTerm * glassMask * uOpacity, 0.0, 1.0) * (1.0 - lit);
|
|
284
|
+
physLitA = lit;
|
|
285
|
+
physShadeA = dark;
|
|
234
286
|
}
|
|
235
287
|
|
|
236
288
|
// Keep interior strictly transparent when shadow, hairline, border, and rim are all zero
|
|
237
|
-
if (shadowA <= 0.0001 && hairA <= 0.0001 && borderA <= 0.0001 && rimA <= 0.0001) {
|
|
289
|
+
if (shadowA <= 0.0001 && hairA <= 0.0001 && borderA <= 0.0001 && rimA <= 0.0001 && physLitA <= 0.0001 && physShadeA <= 0.0001) {
|
|
238
290
|
fragColor = vec4(0.0);
|
|
239
291
|
return;
|
|
240
292
|
}
|
|
241
293
|
|
|
242
|
-
// Premultiplied alpha composite: uniform border over inner rim over hairline and shadow
|
|
294
|
+
// Premultiplied alpha composite: uniform border over inner/physical rim over hairline and shadow
|
|
243
295
|
float darkA = clamp(shadowA + hairA * (1.0 - shadowA), 0.0, 1.0);
|
|
244
|
-
vec3
|
|
245
|
-
|
|
296
|
+
vec3 shadeColor = vec3(0.03921569, 0.07058824, 0.10980392);
|
|
297
|
+
vec3 afterRimRgb = vec3(rimA + physLitA) + shadeColor * physShadeA;
|
|
298
|
+
float totalRimA = clamp(rimA + physLitA + physShadeA * (1.0 - rimA), 0.0, 1.0);
|
|
299
|
+
float afterRimA = totalRimA + darkA * (1.0 - totalRimA);
|
|
246
300
|
vec3 outRgb = borderRgb + afterRimRgb * (1.0 - borderA);
|
|
247
301
|
float outA = clamp(borderA + afterRimA * (1.0 - borderA), 0.0, 1.0);
|
|
248
302
|
fragColor = vec4(outRgb, outA);
|
|
@@ -580,7 +634,7 @@ function renderFallback2d(ctx, frame) {
|
|
|
580
634
|
// 4. Top gradient rim catchlight
|
|
581
635
|
const styleMix = field.styleMix ?? 0;
|
|
582
636
|
const fieldRimOpacity = field.rimOpacity ?? 1;
|
|
583
|
-
const effectiveRim = borderWidth > 0.001
|
|
637
|
+
const effectiveRim = borderWidth > 0.001 || material.rimMode === 'physical'
|
|
584
638
|
? material.rimOpacity * opacity
|
|
585
639
|
: material.rimOpacity * (1 - styleMix + styleMix * fieldRimOpacity) * opacity;
|
|
586
640
|
if (effectiveRim > 0.001) {
|
|
@@ -652,6 +706,8 @@ export function createGlassOverlayRenderer() {
|
|
|
652
706
|
uShadow: gl.getUniformLocation(program, 'uShadow'),
|
|
653
707
|
uBorderColor: gl.getUniformLocation(program, 'uBorderColor'),
|
|
654
708
|
uBorderParams: gl.getUniformLocation(program, 'uBorderParams'),
|
|
709
|
+
uPhysicalLight: gl.getUniformLocation(program, 'uPhysicalLight'),
|
|
710
|
+
uPhysicalParams: gl.getUniformLocation(program, 'uPhysicalParams'),
|
|
655
711
|
};
|
|
656
712
|
return true;
|
|
657
713
|
}
|
|
@@ -797,6 +853,19 @@ export function createGlassOverlayRenderer() {
|
|
|
797
853
|
const borderRgba = material.borderRgba ?? [1, 1, 1, 0];
|
|
798
854
|
gl.uniform4f(uniforms.uBorderColor, borderRgba[0], borderRgba[1], borderRgba[2], borderRgba[3]);
|
|
799
855
|
gl.uniform2f(uniforms.uBorderParams, material.borderWidth ?? 0, material.rimBottomRatio ?? 0.72);
|
|
856
|
+
const isPhysicalRim = material.rimMode === 'physical';
|
|
857
|
+
const angleRad = ((material.lightAngle ?? -45) * Math.PI) / 180;
|
|
858
|
+
const elevRad = (Math.max(1, Math.min(89, material.lightElevation ?? 22)) * Math.PI) / 180;
|
|
859
|
+
const cosElev = Math.cos(elevRad);
|
|
860
|
+
gl.uniform4f(uniforms.uPhysicalLight, Math.sin(angleRad) * cosElev, -Math.cos(angleRad) * cosElev, Math.sin(elevRad), isPhysicalRim ? 1 : 0);
|
|
861
|
+
const profileCode = material.bevelProfile === 'circle'
|
|
862
|
+
? 1
|
|
863
|
+
: material.bevelProfile === 'parabolic'
|
|
864
|
+
? 2
|
|
865
|
+
: material.bevelProfile === 'lip'
|
|
866
|
+
? 3
|
|
867
|
+
: 0;
|
|
868
|
+
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);
|
|
800
869
|
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
|
801
870
|
gl.bindVertexArray(null);
|
|
802
871
|
return;
|
package/dist/physics.d.ts
CHANGED
|
@@ -76,6 +76,11 @@ export interface MorphTimeline {
|
|
|
76
76
|
subscribe(listener: (state: MorphTimelineState) => void): () => void;
|
|
77
77
|
dispose(): void;
|
|
78
78
|
}
|
|
79
|
+
/** Axis-aligned 2D squash-and-stretch scale factors preserving area (`scaleX * scaleY === 1`). */
|
|
80
|
+
export interface AreaPreservingScale {
|
|
81
|
+
scaleX: number;
|
|
82
|
+
scaleY: number;
|
|
83
|
+
}
|
|
79
84
|
/** Configuration options for the elastic press and rubber-band drag controller. */
|
|
80
85
|
export interface ElasticPressOptions {
|
|
81
86
|
spring?: SpringParams;
|
|
@@ -83,6 +88,8 @@ export interface ElasticPressOptions {
|
|
|
83
88
|
maxGrowthRatio?: number;
|
|
84
89
|
maxGrowthPx?: number;
|
|
85
90
|
dragMaxRatio?: number;
|
|
91
|
+
/** Directional area-preserving squash-and-stretch intensity during rubber-band drag (`0` disables; defaults to `0.12`). */
|
|
92
|
+
dragStretchRatio?: number;
|
|
86
93
|
reducedMotion?: boolean;
|
|
87
94
|
}
|
|
88
95
|
/** Current state of the elastic press and drag controller. */
|
|
@@ -150,6 +157,17 @@ export declare function createSpringChannel(options?: SpringChannelOptions): Spr
|
|
|
150
157
|
* mid-flight reversals so both the primary morph and delayed corner curves remain C1-continuous.
|
|
151
158
|
*/
|
|
152
159
|
export declare function createMorphTimeline(options?: MorphTimelineOptions): MorphTimeline;
|
|
160
|
+
/**
|
|
161
|
+
* Computes axis-aligned 2D squash-and-stretch scale factors preserving area (`scaleX * scaleY === 1`)
|
|
162
|
+
* along direction `(dirX, dirY)`. Using `alpha = (dirX^2 - dirY^2) / (dirX^2 + dirY^2) = cos(2*theta)`
|
|
163
|
+
* gives `(s, 1/s)` horizontally, `(1/s, s)` vertically, and `(1, 1)` on 45-degree diagonals with smooth
|
|
164
|
+
* rotational continuity.
|
|
165
|
+
*/
|
|
166
|
+
export declare function computeAreaPreservingScale(stretch: number, dirX: number, dirY: number, maxStretch?: number): AreaPreservingScale;
|
|
167
|
+
/**
|
|
168
|
+
* Applies `computeAreaPreservingScale` to a 2D `Size` so `result.width * result.height === size.width * size.height`.
|
|
169
|
+
*/
|
|
170
|
+
export declare function deformSizePreservingArea(size: Size, stretch: number, dirX: number, dirY: number, maxStretch?: number): Size;
|
|
153
171
|
/**
|
|
154
172
|
* Creates a generic elastic press and rubber-band drag controller.
|
|
155
173
|
* Uses a critically damped tracking spring while dragging for tight pointer follow,
|
package/dist/physics.js
CHANGED
|
@@ -20,6 +20,8 @@ const DEFAULT_ELASTIC_DRAG_TRACK_SEC = 0.12;
|
|
|
20
20
|
const DEFAULT_ELASTIC_MAX_GROWTH_RATIO = 0.25;
|
|
21
21
|
const DEFAULT_ELASTIC_MAX_GROWTH_PX = 16;
|
|
22
22
|
const DEFAULT_ELASTIC_DRAG_MAX_RATIO = 7 / 32;
|
|
23
|
+
const DEFAULT_ELASTIC_DRAG_STRETCH_RATIO = 0.12;
|
|
24
|
+
const DEFAULT_MAX_AREA_STRETCH = 1.6;
|
|
23
25
|
/**
|
|
24
26
|
* Clamps `value` to the inclusive range `[min, max]`. An inverted range
|
|
25
27
|
* (`max < min`) resolves to `min`, matching CSS `clamp()`, so content larger
|
|
@@ -497,6 +499,39 @@ export function createMorphTimeline(options = {}) {
|
|
|
497
499
|
},
|
|
498
500
|
};
|
|
499
501
|
}
|
|
502
|
+
/**
|
|
503
|
+
* Computes axis-aligned 2D squash-and-stretch scale factors preserving area (`scaleX * scaleY === 1`)
|
|
504
|
+
* along direction `(dirX, dirY)`. Using `alpha = (dirX^2 - dirY^2) / (dirX^2 + dirY^2) = cos(2*theta)`
|
|
505
|
+
* gives `(s, 1/s)` horizontally, `(1/s, s)` vertically, and `(1, 1)` on 45-degree diagonals with smooth
|
|
506
|
+
* rotational continuity.
|
|
507
|
+
*/
|
|
508
|
+
export function computeAreaPreservingScale(stretch, dirX, dirY, maxStretch = DEFAULT_MAX_AREA_STRETCH) {
|
|
509
|
+
if (!Number.isFinite(stretch) || stretch <= 0) {
|
|
510
|
+
return { scaleX: 1, scaleY: 1 };
|
|
511
|
+
}
|
|
512
|
+
const safeMax = Number.isFinite(maxStretch) && maxStretch >= 1 ? maxStretch : DEFAULT_MAX_AREA_STRETCH;
|
|
513
|
+
const s = clamp(stretch, 1 / safeMax, safeMax);
|
|
514
|
+
if (Math.abs(s - 1) <= 1e-9) {
|
|
515
|
+
return { scaleX: 1, scaleY: 1 };
|
|
516
|
+
}
|
|
517
|
+
const lenSq = dirX * dirX + dirY * dirY;
|
|
518
|
+
if (!Number.isFinite(lenSq) || lenSq <= 1e-12) {
|
|
519
|
+
return { scaleX: 1, scaleY: 1 };
|
|
520
|
+
}
|
|
521
|
+
const alpha = (dirX * dirX - dirY * dirY) / lenSq;
|
|
522
|
+
const scaleX = Math.pow(s, alpha);
|
|
523
|
+
return { scaleX, scaleY: 1 / scaleX };
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Applies `computeAreaPreservingScale` to a 2D `Size` so `result.width * result.height === size.width * size.height`.
|
|
527
|
+
*/
|
|
528
|
+
export function deformSizePreservingArea(size, stretch, dirX, dirY, maxStretch = DEFAULT_MAX_AREA_STRETCH) {
|
|
529
|
+
const { scaleX, scaleY } = computeAreaPreservingScale(stretch, dirX, dirY, maxStretch);
|
|
530
|
+
return {
|
|
531
|
+
width: Math.max(0, size.width * scaleX),
|
|
532
|
+
height: Math.max(0, size.height * scaleY),
|
|
533
|
+
};
|
|
534
|
+
}
|
|
500
535
|
/**
|
|
501
536
|
* Creates a generic elastic press and rubber-band drag controller.
|
|
502
537
|
* Uses a critically damped tracking spring while dragging for tight pointer follow,
|
|
@@ -508,6 +543,7 @@ export function createElasticPressController(options = {}) {
|
|
|
508
543
|
const maxGrowthRatio = options.maxGrowthRatio ?? DEFAULT_ELASTIC_MAX_GROWTH_RATIO;
|
|
509
544
|
const maxGrowthPx = options.maxGrowthPx ?? DEFAULT_ELASTIC_MAX_GROWTH_PX;
|
|
510
545
|
const dragMaxRatio = options.dragMaxRatio ?? DEFAULT_ELASTIC_DRAG_MAX_RATIO;
|
|
546
|
+
const dragStretchRatio = clamp(options.dragStretchRatio ?? DEFAULT_ELASTIC_DRAG_STRETCH_RATIO, 0, 0.45);
|
|
511
547
|
// Critically damped tracking spring (~95% settled in dragTrackSec):
|
|
512
548
|
// derived from 1 - exp(-w0 * t) * (1 + w0 * t) = 0.95 at w0 * t ≈ 4.744.
|
|
513
549
|
const dragTrackSpring = {
|
|
@@ -539,9 +575,21 @@ export function createElasticPressController(options = {}) {
|
|
|
539
575
|
const longestSide = Math.max(1, targetSize.width, targetSize.height);
|
|
540
576
|
const maxGrowth = Math.min(maxGrowthRatio, maxGrowthPx / longestSide);
|
|
541
577
|
const scale = 1 + maxGrowth * lift.value;
|
|
578
|
+
let scaleX = scale;
|
|
579
|
+
let scaleY = scale;
|
|
580
|
+
if (dragStretchRatio > 0) {
|
|
581
|
+
const shortestSide = Math.max(1, Math.min(targetSize.width, targetSize.height));
|
|
582
|
+
const dragLimit = Math.max(1, dragMaxRatio * shortestSide);
|
|
583
|
+
const dragNorm = clamp(Math.hypot(ox.value, oy.value) / dragLimit, 0, 1);
|
|
584
|
+
if (dragNorm > 0) {
|
|
585
|
+
const directional = computeAreaPreservingScale(1 + dragStretchRatio * dragNorm, ox.value, oy.value);
|
|
586
|
+
scaleX = scale * directional.scaleX;
|
|
587
|
+
scaleY = scale * directional.scaleY;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
542
590
|
return {
|
|
543
|
-
scaleX
|
|
544
|
-
scaleY
|
|
591
|
+
scaleX,
|
|
592
|
+
scaleY,
|
|
545
593
|
offsetX: ox.value,
|
|
546
594
|
offsetY: oy.value,
|
|
547
595
|
lift: lift.value,
|
package/dist/refractMap.d.ts
CHANGED
|
@@ -9,23 +9,49 @@
|
|
|
9
9
|
* SPDX-License-Identifier: MPL-2.0
|
|
10
10
|
*/
|
|
11
11
|
import type { GlassFieldState, Rect } from './contracts.ts';
|
|
12
|
+
import { type RefractionProfileSpec } from './refractProfile.ts';
|
|
12
13
|
export interface RefractMapParams {
|
|
13
14
|
bounds: Rect;
|
|
14
15
|
field: GlassFieldState;
|
|
15
16
|
bezel: number;
|
|
16
|
-
falloff?: number;
|
|
17
17
|
mapScale?: number;
|
|
18
|
+
/** Refraction cross-section profile (`'squircle'`, `'circle'`, `'parabolic'`, `'lip'`, or custom height fn). */
|
|
19
|
+
profile?: RefractionProfileSpec;
|
|
20
|
+
/** Index of refraction (`1` = air, `1.5` = glass; defaults to `1.5`). */
|
|
21
|
+
ior?: number;
|
|
22
|
+
/** Glass plateau thickness divided by bezel width (defaults to `1`). */
|
|
23
|
+
thicknessRatio?: number;
|
|
24
|
+
/** Allow steep-edge caustic folds (defaults to `false`). */
|
|
25
|
+
caustics?: boolean;
|
|
18
26
|
}
|
|
19
27
|
export interface RefractMapResult {
|
|
20
28
|
dataUrl: string;
|
|
21
29
|
width: number;
|
|
22
30
|
height: number;
|
|
23
31
|
changed: boolean;
|
|
32
|
+
/** Peak absolute horizontal displacement in CSS px for `feDisplacementMap` scale calibration. */
|
|
33
|
+
maxDisplacementPx: number;
|
|
24
34
|
}
|
|
25
35
|
export interface RefractMapBuilder {
|
|
26
36
|
build(params: RefractMapParams): RefractMapResult;
|
|
27
37
|
reset(): void;
|
|
28
38
|
}
|
|
39
|
+
/** @internal Reusable 3-element scratch vector [ux, uy, distIn] for corner-bend geometry without heap allocation. */
|
|
40
|
+
export declare const tmpCornerGeom: Float64Array<ArrayBuffer>;
|
|
41
|
+
/** @internal */
|
|
42
|
+
export declare function ensureScanlineCapacity(needed: number): Uint8Array;
|
|
43
|
+
/** @internal */
|
|
44
|
+
export declare function encodeStoredRgbaPngDataUrl(scanlines: Uint8Array, pngW: number, pngH: number): string;
|
|
45
|
+
/** @internal */
|
|
46
|
+
export declare function clampAmp(v: number): number;
|
|
47
|
+
/** @internal */
|
|
48
|
+
export declare function clampChannel(v: number): number;
|
|
49
|
+
/**
|
|
50
|
+
* @internal
|
|
51
|
+
* Solves corner-bend inward distance and first-quadrant outward unit direction
|
|
52
|
+
* into `tmpCornerGeom` (`[ux, uy, distIn]`) without heap allocations.
|
|
53
|
+
*/
|
|
54
|
+
export declare function computeCornerGeometry(x: number, y: number, radius: number, bezel: number): void;
|
|
29
55
|
/** Creates a Stored-PNG displacement map builder with quantized caching; reuse the instance across frames. */
|
|
30
56
|
export declare function createRefractMapBuilder(): RefractMapBuilder;
|
|
31
57
|
/** Stateless one-shot build; prefer `createRefractMapBuilder()` for multi-frame reuse. */
|