@sessile/core 0.3.2 → 0.5.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.
@@ -15,295 +15,327 @@
15
15
  */
16
16
  import { extractFieldContourPath } from "./contour.js";
17
17
  import { MAX_GPU_PRIMITIVES } from "./contracts.js";
18
- export const OVERLAY_VERTEX_SHADER = /* glsl */ `#version 300 es
19
- precision highp float;
20
-
21
- out vec2 vUv;
22
-
23
- void main() {
24
- vec2 pos = vec2(
25
- (gl_VertexID == 1) ? 3.0 : -1.0,
26
- (gl_VertexID == 2) ? 3.0 : -1.0
27
- );
28
- vUv = pos * 0.5 + 0.5;
29
- gl_Position = vec4(pos, 0.0, 1.0);
30
- }
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
+ };
26
+ export const OVERLAY_VERTEX_SHADER = /* glsl */ `#version 300 es
27
+ precision highp float;
28
+
29
+ out vec2 vUv;
30
+
31
+ void main() {
32
+ vec2 pos = vec2(
33
+ (gl_VertexID == 1) ? 3.0 : -1.0,
34
+ (gl_VertexID == 2) ? 3.0 : -1.0
35
+ );
36
+ vUv = pos * 0.5 + 0.5;
37
+ gl_Position = vec4(pos, 0.0, 1.0);
38
+ }
31
39
  `;
32
- export const OVERLAY_FRAGMENT_SHADER = /* glsl */ `#version 300 es
33
- precision highp float;
34
-
35
- #define MAX_PRIMITIVES 16
36
-
37
- in vec2 vUv;
38
- out vec4 fragColor;
39
-
40
- uniform vec2 uViewport;
41
- uniform float uDpr;
42
- uniform int uPrimitiveCount;
43
- uniform vec4 uPrimitives[MAX_PRIMITIVES]; // (cx, cy, halfW, halfH) in local CSS px
44
- uniform float uRadii[MAX_PRIMITIVES];
45
- uniform float uBlendK;
46
- uniform float uRimOpacity;
47
- uniform float uStyleMix;
48
- uniform float uOpacity;
49
-
50
- uniform vec4 uRimParams; // (material.rimOpacity, material.rimWidth, material.rimTail, material.rimFocus)
51
- uniform vec4 uHairline; // (width, sideAlpha, cornerAlpha, flatAlpha)
52
- uniform vec3 uShadow; // (blur, offsetY, alpha)
53
- uniform vec4 uBorderColor; // normalized RGBA of optional uniform inner border
54
- uniform vec2 uBorderParams; // (borderWidthPx, rimBottomRatio)
55
- uniform vec4 uPhysicalLight; // (lx, ly, lz, enabled)
56
- uniform vec4 uPhysicalParams; // (bevelPx, specularStrength, shadeStrength, profileCode)
57
-
58
- struct SdfSample {
59
- float dist;
60
- vec2 normal;
61
- };
62
-
63
- float safePow(float x, float y) {
64
- return (x <= 1e-6) ? 0.0 : pow(x, y);
65
- }
66
-
67
- // Box-filtered coverage of the band lo <= d <= hi over a pixel footprint of width aa. Lines thinner
68
- // than a pixel keep the same total alpha at every subpixel offset; a smoothstep around the band
69
- // centre nearly vanishes or doubles as the edge moves, which reads as shimmer while morphing.
70
- float bandCoverage(float d, float lo, float hi, float aa) {
71
- return clamp(min(d + 0.5 * aa, hi) - max(d - 0.5 * aa, lo), 0.0, aa) / aa;
72
- }
73
-
74
- float computeDirectionalRimWeight(vec2 normal, float focus, float bottomRatio) {
75
- if (focus < 0.05) {
76
- return 1.0;
77
- }
78
- float topBias = clamp(-normal.y, 0.0, 1.0);
79
- float botBias = clamp(normal.y, 0.0, 1.0);
80
- return safePow(max(topBias, botBias * clamp(bottomRatio, 0.0, 1.0)), max(focus, 0.25));
81
- }
82
-
83
- SdfSample sdRoundedBoxRefined(vec2 p, vec2 center, vec2 halfSize, float r) {
84
- vec2 safeHalf = max(halfSize, vec2(0.001));
85
- float rad = clamp(r, 0.0, min(safeHalf.x, safeHalf.y));
86
- vec2 rel = p - center;
87
- vec2 s = vec2(rel.x >= 0.0 ? 1.0 : -1.0, rel.y >= 0.0 ? 1.0 : -1.0);
88
- vec2 q = abs(rel) - safeHalf + rad;
89
- vec2 qPos = max(q, 0.0);
90
-
91
- float outside = length(qPos);
92
- float inside = min(max(q.x, q.y), 0.0);
93
- float baseDist = outside + inside - rad;
94
-
95
- vec2 baseNormal;
96
- if (q.x > 0.0 && q.y > 0.0) {
97
- baseNormal = (outside > 1e-5) ? (qPos / outside) * s : normalize(s);
98
- } else if (q.x > q.y) {
99
- baseNormal = vec2(s.x, 0.0);
100
- } else {
101
- baseNormal = vec2(0.0, s.y);
102
- }
103
-
104
- // Superellipse octant normal refinement within 5 CSS px of the boundary for smooth corner speculars;
105
- // dist stays exact baseDist for 0px alignment with the Marching Squares contour.
106
- float rimProximity = smoothstep(5.0, 0.0, abs(baseDist));
107
- vec2 normal = baseNormal;
108
-
109
- if (rimProximity > 0.001 && rad > 1.0 && q.x > 0.0 && q.y > 0.0) {
110
- const float pExp = 2.55;
111
- vec2 qn = qPos / rad;
112
- vec2 grad = vec2(
113
- safePow(qn.x, pExp - 1.0),
114
- safePow(qn.y, pExp - 1.0)
115
- );
116
- float gl = length(grad);
117
- if (gl > 1e-5) {
118
- vec2 superNormal = (grad / gl) * s;
119
- vec2 mixedN = mix(baseNormal, superNormal, rimProximity);
120
- float mnl = length(mixedN);
121
- normal = (mnl > 1e-5) ? (mixedN / mnl) : baseNormal;
122
- }
123
- }
124
-
125
- return SdfSample(baseDist, normal);
126
- }
127
-
128
- float evalBezelHeight(float t, float code) {
129
- float u = clamp(1.0 - t, 0.0, 1.0);
130
- if (code > 2.5) {
131
- float sq = safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);
132
- float s = sin(3.14159265 * clamp(t, 0.0, 1.0));
133
- return sq + 0.14 * s * s;
134
- }
135
- if (code > 1.5) {
136
- return 1.0 - u * u;
137
- }
138
- if (code > 0.5) {
139
- return sqrt(max(0.0, 1.0 - u * u));
140
- }
141
- return safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);
142
- }
143
-
144
- SdfSample evaluateGlassSdf(vec2 p) {
145
- if (uPrimitiveCount <= 0) {
146
- return SdfSample(1e6, vec2(0.0, -1.0));
147
- }
148
-
149
- SdfSample acc = sdRoundedBoxRefined(p, uPrimitives[0].xy, uPrimitives[0].zw, uRadii[0]);
150
- float d = acc.dist;
151
- vec2 g = acc.normal;
152
- for (int i = 1; i < MAX_PRIMITIVES; i++) {
153
- if (i >= uPrimitiveCount) break;
154
- SdfSample b = sdRoundedBoxRefined(p, uPrimitives[i].xy, uPrimitives[i].zw, uRadii[i]);
155
- if (uBlendK <= 0.001) {
156
- if (b.dist < d) {
157
- d = b.dist;
158
- g = b.normal;
159
- }
160
- } else {
161
- float h = clamp(0.5 + 0.5 * (b.dist - d) / uBlendK, 0.0, 1.0);
162
- d = mix(b.dist, d, h) - uBlendK * h * (1.0 - h);
163
- g = mix(b.normal, g, h);
164
- }
165
- }
166
-
167
- // Soften cancelling normals in smin saddle necks (NORMAL_FLOOR = 0.25) so speculars never flip 180 degrees.
168
- float gl = max(length(g), 0.25);
169
- return SdfSample(d, g / gl);
170
- }
171
-
172
- void main() {
173
- if (uOpacity <= 0.001 || uPrimitiveCount <= 0) {
174
- fragColor = vec4(0.0);
175
- return;
176
- }
177
-
178
- // Convert GL UV (bottom-left origin) to local CSS px (top-left origin).
179
- vec2 pCss = vec2(vUv.x * uViewport.x, (1.0 - vUv.y) * uViewport.y);
180
- SdfSample sdf = evaluateGlassSdf(pCss);
181
- float d = sdf.dist;
182
- vec2 normal = sdf.normal;
183
-
184
- float aa = 1.0 / max(uDpr, 1.0);
185
- float glassMask = clamp(0.5 - d / aa, 0.0, 1.0);
186
- float outsideMask = 1.0 - glassMask;
187
-
188
- // 1. Outside-only drop shadow (d > 0)
189
- float shadowA = 0.0;
190
- if (outsideMask > 0.001 && uShadow.z > 0.001) {
191
- SdfSample shadowSdf = evaluateGlassSdf(pCss - vec2(0.0, uShadow.y));
192
- float sigma = max(uShadow.x * 0.5, 1.0);
193
- float shadowFalloff = 1.0 / (1.0 + exp(clamp(1.65 * shadowSdf.dist / sigma, -12.0, 12.0)));
194
- shadowA = outsideMask * shadowFalloff * uShadow.z * uOpacity;
195
- }
196
-
197
- // 2. Dark outer hairline: strictly outside when a uniform CSS inner border is present (d >= 0), otherwise straddling the outer edge (d >= -0.25)
198
- float hw = max(uHairline.x, 0.5 / max(uDpr, 1.0));
199
- float hasUniformBorder = step(0.001, uBorderParams.x);
200
- float hairlineLo = mix(-0.25, 0.0, hasUniformBorder);
201
- float hairlineBand = bandCoverage(d, hairlineLo, hairlineLo + hw, aa);
202
- float hairA = 0.0;
203
- if (hairlineBand > 0.001) {
204
- float ax = abs(normal.x);
205
- float ay = abs(normal.y);
206
- float cornerW = safePow(clamp(2.0 * ax * ay, 0.0, 1.0), 1.5);
207
- float sideVsFlat = mix(uHairline.y, uHairline.w, ay * ay);
208
- float hairlineProfileAlpha = mix(sideVsFlat, uHairline.z, cornerW);
209
- hairA = hairlineBand * hairlineProfileAlpha * uOpacity;
210
- }
211
-
212
- // 3. Optional 1px uniform inner border + directional/soft inner rim highlight (inset < 5.0 CSS px)
213
- float inset = max(-d, 0.0);
214
- float borderMask = 0.0;
215
- float borderA = 0.0;
216
- vec3 borderRgb = vec3(0.0);
217
- if (hasUniformBorder > 0.5 && d < 0.5 * aa && inset < uBorderParams.x + aa) {
218
- borderMask = bandCoverage(d, -uBorderParams.x, 0.0, aa);
219
- borderA = clamp(borderMask * uBorderColor.a * uOpacity, 0.0, 1.0);
220
- borderRgb = uBorderColor.rgb * borderA;
221
- }
222
-
223
- float rimA = 0.0;
224
- float physLitA = 0.0;
225
- float physShadeA = 0.0;
226
- float isPhysicalRim = step(0.5, uPhysicalLight.w) * (1.0 - hasUniformBorder);
227
-
228
- if (glassMask > 0.001 && inset < 5.0 && uRimParams.x > 0.001 && isPhysicalRim < 0.5) {
229
- float ny = abs(normal.y);
230
- float wDirectional = 1.0 - clamp(uStyleMix, 0.0, 1.0);
231
- float wSoft = clamp(uStyleMix, 0.0, 1.0) * clamp(uRimOpacity, 0.0, 1.0);
232
-
233
- if (hasUniformBorder > 0.5) {
234
- // Match CSS inset box-shadow: keep inner rim inside the padding edge so the 1px border is not double-brightened
235
- float padInset = max(inset - uBorderParams.x, 0.0);
236
- float padMask = 1.0 - borderMask;
237
- float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);
238
- float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, padInset);
239
- float tail = exp(-padInset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);
240
- rimA = clamp((core + tail) * dirWeight * uRimParams.x * padMask * glassMask * uOpacity, 0.0, 1.0);
241
- } else {
242
- if (wDirectional > 0.001) {
243
- float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);
244
- float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, inset);
245
- float tail = exp(-inset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);
246
- rimA += (core + tail) * dirWeight * uRimParams.x * wDirectional;
247
- }
248
-
249
- if (wSoft > 0.001) {
250
- float bandT = clamp(inset / 5.0, 0.0, 1.0);
251
- float steppedT = min(floor(bandT * 9.0) / 9.0, 1.0);
252
- float bandCurve = mix(safePow(1.0 - bandT, 2.1), safePow(1.0 - steppedT, 2.1), 0.35);
253
- float rimCoreBoost = 0.65 + 0.35 * smoothstep(1.2, 0.0, inset);
254
- float angWeight = mix(0.015, 1.0, safePow(ny, 2.1)) * (normal.y < 0.0 ? 1.0 : 0.85);
255
- rimA += bandCurve * rimCoreBoost * angWeight * uRimParams.x * wSoft;
256
- }
257
-
258
- // Boost specular luminance to match source-over alpha blending expectations
259
- rimA = clamp(rimA * 1.35 * glassMask * uOpacity, 0.0, 1.0);
260
- }
261
- } else if (glassMask > 0.001 && isPhysicalRim > 0.5 && inset < uPhysicalParams.x) {
262
- float bezelPx = max(uPhysicalParams.x, 1.0);
263
- float t = clamp(inset / bezelPx, 0.0, 1.0);
264
- float eps = 0.015;
265
- float t0 = max(0.0, t - eps);
266
- float t1 = min(1.0, t + eps);
267
- float slope = (evalBezelHeight(t1, uPhysicalParams.w) - evalBezelHeight(t0, uPhysicalParams.w)) / max(t1 - t0, 1e-4);
268
- vec3 N = normalize(vec3(normal * slope, 1.0));
269
- vec3 L = uPhysicalLight.xyz;
270
- vec3 H = vec3(L.xy, L.z + 1.0);
271
- float hLen = max(length(H), 1e-4);
272
- const float shininess = 36.0;
273
- float flatBase = safePow(clamp((L.z + 1.0) / hLen, 0.0, 1.0), shininess);
274
- float denomBase = max(1.0 - flatBase, 1e-4);
275
- float hDot = clamp(dot(N, H) / hLen, 0.0, 1.0);
276
- float highlight = max(safePow(hDot, shininess) - flatBase, 0.0) / denomBase;
277
- vec3 H_bounce = vec3(-L.xy, L.z + 1.0);
278
- float bDot = clamp(dot(N, H_bounce) / hLen, 0.0, 1.0);
279
- float bounce = max(safePow(bDot, shininess) - flatBase, 0.0) / denomBase;
280
- vec2 planar = L.xy / max(length(L.xy), 1e-4);
281
- float facing = dot(normal, planar);
282
- float fresnel = safePow(clamp(1.0 - N.z, 0.0, 1.0), 4.0);
283
- float rimTerm = fresnel * (0.25 + 0.75 * max(facing, 0.0) + 0.45 * max(-facing, 0.0));
284
- float shadeTerm = safePow(clamp(1.0 - N.z, 0.0, 1.0), 2.5) * (0.45 + 0.55 * max(-facing, 0.0));
285
- float lit = clamp((uPhysicalParams.y * (highlight + 0.45 * bounce) + uRimParams.x * rimTerm) * glassMask * uOpacity, 0.0, 1.0);
286
- float dark = clamp(uPhysicalParams.z * shadeTerm * glassMask * uOpacity, 0.0, 1.0) * (1.0 - lit);
287
- physLitA = lit;
288
- physShadeA = dark;
289
- }
290
-
291
- // Keep interior strictly transparent when shadow, hairline, border, and rim are all zero
292
- if (shadowA <= 0.0001 && hairA <= 0.0001 && borderA <= 0.0001 && rimA <= 0.0001 && physLitA <= 0.0001 && physShadeA <= 0.0001) {
293
- fragColor = vec4(0.0);
294
- return;
295
- }
296
-
297
- // Premultiplied alpha composite: uniform border over inner/physical rim over hairline and shadow
298
- float darkA = clamp(shadowA + hairA * (1.0 - shadowA), 0.0, 1.0);
299
- vec3 shadeColor = vec3(0.03921569, 0.07058824, 0.10980392);
300
- vec3 afterRimRgb = vec3(rimA + physLitA) + shadeColor * physShadeA;
301
- float totalRimA = clamp(rimA + physLitA + physShadeA * (1.0 - rimA), 0.0, 1.0);
302
- float afterRimA = totalRimA + darkA * (1.0 - totalRimA);
303
- vec3 outRgb = borderRgb + afterRimRgb * (1.0 - borderA);
304
- float outA = clamp(borderA + afterRimA * (1.0 - borderA), 0.0, 1.0);
305
- fragColor = vec4(outRgb, outA);
306
- }
40
+ export const OVERLAY_FRAGMENT_SHADER = /* glsl */ `#version 300 es
41
+ precision highp float;
42
+
43
+ #define MAX_PRIMITIVES 16
44
+
45
+ in vec2 vUv;
46
+ out vec4 fragColor;
47
+
48
+ uniform vec2 uViewport;
49
+ uniform float uDpr;
50
+ uniform int uPrimitiveCount;
51
+ uniform vec4 uPrimitives[MAX_PRIMITIVES]; // (cx, cy, halfW, halfH) in local CSS px
52
+ uniform float uRadii[MAX_PRIMITIVES];
53
+ uniform float uBlendK;
54
+ uniform float uRimOpacity;
55
+ uniform float uStyleMix;
56
+ uniform float uOpacity;
57
+
58
+ uniform vec4 uRimParams; // (material.rimOpacity, material.rimWidth, material.rimTail, material.rimFocus)
59
+ uniform vec4 uHairline; // (width, sideAlpha, cornerAlpha, flatAlpha)
60
+ uniform vec3 uShadow; // (blur, offsetY, alpha)
61
+ uniform vec4 uBorderColor; // normalized RGBA of optional uniform inner border
62
+ uniform vec2 uBorderParams; // (borderWidthPx, rimBottomRatio)
63
+ uniform vec4 uPhysicalLight; // (lx, ly, lz, enabled)
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])
67
+
68
+ struct SdfSample {
69
+ float dist;
70
+ vec2 normal;
71
+ };
72
+
73
+ float safePow(float x, float y) {
74
+ return (x <= 1e-6) ? 0.0 : pow(x, y);
75
+ }
76
+
77
+ // Box-filtered coverage of the band lo <= d <= hi over a pixel footprint of width aa. Lines thinner
78
+ // than a pixel keep the same total alpha at every subpixel offset; a smoothstep around the band
79
+ // centre nearly vanishes or doubles as the edge moves, which reads as shimmer while morphing.
80
+ float bandCoverage(float d, float lo, float hi, float aa) {
81
+ return clamp(min(d + 0.5 * aa, hi) - max(d - 0.5 * aa, lo), 0.0, aa) / aa;
82
+ }
83
+
84
+ float computeDirectionalRimWeight(vec2 normal, float focus, float bottomRatio) {
85
+ if (focus < 0.05) {
86
+ return 1.0;
87
+ }
88
+ float topBias = clamp(-normal.y, 0.0, 1.0);
89
+ float botBias = clamp(normal.y, 0.0, 1.0);
90
+ return safePow(max(topBias, botBias * clamp(bottomRatio, 0.0, 1.0)), max(focus, 0.25));
91
+ }
92
+
93
+ SdfSample sdRoundedBoxRefined(vec2 p, vec2 center, vec2 halfSize, float r) {
94
+ vec2 safeHalf = max(halfSize, vec2(0.001));
95
+ float rad = clamp(r, 0.0, min(safeHalf.x, safeHalf.y));
96
+ vec2 rel = p - center;
97
+ vec2 s = vec2(rel.x >= 0.0 ? 1.0 : -1.0, rel.y >= 0.0 ? 1.0 : -1.0);
98
+ vec2 q = abs(rel) - safeHalf + rad;
99
+ vec2 qPos = max(q, 0.0);
100
+
101
+ float outside = length(qPos);
102
+ float inside = min(max(q.x, q.y), 0.0);
103
+ float baseDist = outside + inside - rad;
104
+
105
+ vec2 baseNormal;
106
+ if (q.x > 0.0 && q.y > 0.0) {
107
+ baseNormal = (outside > 1e-5) ? (qPos / outside) * s : normalize(s);
108
+ } else if (q.x > q.y) {
109
+ baseNormal = vec2(s.x, 0.0);
110
+ } else {
111
+ baseNormal = vec2(0.0, s.y);
112
+ }
113
+
114
+ // Superellipse octant normal refinement within 5 CSS px of the boundary for smooth corner speculars;
115
+ // dist stays exact baseDist for 0px alignment with the Marching Squares contour.
116
+ float rimProximity = smoothstep(5.0, 0.0, abs(baseDist));
117
+ vec2 normal = baseNormal;
118
+
119
+ if (rimProximity > 0.001 && rad > 1.0 && q.x > 0.0 && q.y > 0.0) {
120
+ const float pExp = 2.55;
121
+ vec2 qn = qPos / rad;
122
+ vec2 grad = vec2(
123
+ safePow(qn.x, pExp - 1.0),
124
+ safePow(qn.y, pExp - 1.0)
125
+ );
126
+ float gl = length(grad);
127
+ if (gl > 1e-5) {
128
+ vec2 superNormal = (grad / gl) * s;
129
+ vec2 mixedN = mix(baseNormal, superNormal, rimProximity);
130
+ float mnl = length(mixedN);
131
+ normal = (mnl > 1e-5) ? (mixedN / mnl) : baseNormal;
132
+ }
133
+ }
134
+
135
+ return SdfSample(baseDist, normal);
136
+ }
137
+
138
+ float evalBezelHeight(float t, float code) {
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
+ }
144
+ if (code > 2.5) {
145
+ float sq = safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);
146
+ float s = sin(3.14159265 * clamp(t, 0.0, 1.0));
147
+ return sq + 0.14 * s * s;
148
+ }
149
+ if (code > 1.5) {
150
+ return 1.0 - u * u;
151
+ }
152
+ if (code > 0.5) {
153
+ return sqrt(max(0.0, 1.0 - u * u));
154
+ }
155
+ return safePow(max(0.0, 1.0 - safePow(u, 4.0)), 0.25);
156
+ }
157
+
158
+ SdfSample evaluateGlassSdf(vec2 p) {
159
+ if (uPrimitiveCount <= 0) {
160
+ return SdfSample(1e6, vec2(0.0, -1.0));
161
+ }
162
+
163
+ SdfSample acc = sdRoundedBoxRefined(p, uPrimitives[0].xy, uPrimitives[0].zw, uRadii[0]);
164
+ float d = acc.dist;
165
+ vec2 g = acc.normal;
166
+ for (int i = 1; i < MAX_PRIMITIVES; i++) {
167
+ if (i >= uPrimitiveCount) break;
168
+ SdfSample b = sdRoundedBoxRefined(p, uPrimitives[i].xy, uPrimitives[i].zw, uRadii[i]);
169
+ if (uBlendK <= 0.001) {
170
+ if (b.dist < d) {
171
+ d = b.dist;
172
+ g = b.normal;
173
+ }
174
+ } else {
175
+ float h = clamp(0.5 + 0.5 * (b.dist - d) / uBlendK, 0.0, 1.0);
176
+ d = mix(b.dist, d, h) - uBlendK * h * (1.0 - h);
177
+ g = mix(b.normal, g, h);
178
+ }
179
+ }
180
+
181
+ // Soften cancelling normals in smin saddle necks (NORMAL_FLOOR = 0.25) so speculars never flip 180 degrees.
182
+ float gl = max(length(g), 0.25);
183
+ return SdfSample(d, g / gl);
184
+ }
185
+
186
+ void main() {
187
+ if (uOpacity <= 0.001 || uPrimitiveCount <= 0) {
188
+ fragColor = vec4(0.0);
189
+ return;
190
+ }
191
+
192
+ // Convert GL UV (bottom-left origin) to local CSS px (top-left origin).
193
+ vec2 pCss = vec2(vUv.x * uViewport.x, (1.0 - vUv.y) * uViewport.y);
194
+ SdfSample sdf = evaluateGlassSdf(pCss);
195
+ float d = sdf.dist;
196
+ vec2 normal = sdf.normal;
197
+
198
+ float aa = 1.0 / max(uDpr, 1.0);
199
+ float glassMask = clamp(0.5 - d / aa, 0.0, 1.0);
200
+ float outsideMask = 1.0 - glassMask;
201
+
202
+ // 1. Outside-only drop shadow (d > 0)
203
+ float shadowA = 0.0;
204
+ if (outsideMask > 0.001 && uShadow.z > 0.001) {
205
+ SdfSample shadowSdf = evaluateGlassSdf(pCss - vec2(0.0, uShadow.y));
206
+ float sigma = max(uShadow.x * 0.5, 1.0);
207
+ float shadowFalloff = 1.0 / (1.0 + exp(clamp(1.65 * shadowSdf.dist / sigma, -12.0, 12.0)));
208
+ shadowA = outsideMask * shadowFalloff * uShadow.z * uOpacity;
209
+ }
210
+
211
+ // 2. Dark outer hairline: strictly outside when a uniform CSS inner border is present (d >= 0), otherwise straddling the outer edge (d >= -0.25)
212
+ float hw = max(uHairline.x, 0.5 / max(uDpr, 1.0));
213
+ float hasUniformBorder = step(0.001, uBorderParams.x);
214
+ float hairlineLo = mix(-0.25, 0.0, hasUniformBorder);
215
+ float hairlineBand = bandCoverage(d, hairlineLo, hairlineLo + hw, aa);
216
+ float hairA = 0.0;
217
+ if (hairlineBand > 0.001) {
218
+ float ax = abs(normal.x);
219
+ float ay = abs(normal.y);
220
+ float cornerW = safePow(clamp(2.0 * ax * ay, 0.0, 1.0), 1.5);
221
+ float sideVsFlat = mix(uHairline.y, uHairline.w, ay * ay);
222
+ float hairlineProfileAlpha = mix(sideVsFlat, uHairline.z, cornerW);
223
+ hairA = hairlineBand * hairlineProfileAlpha * uOpacity;
224
+ }
225
+
226
+ // 3. Optional 1px uniform inner border + directional/soft inner rim highlight (inset < 5.0 CSS px)
227
+ float inset = max(-d, 0.0);
228
+ float borderMask = 0.0;
229
+ float borderA = 0.0;
230
+ vec3 borderRgb = vec3(0.0);
231
+ if (hasUniformBorder > 0.5 && d < 0.5 * aa && inset < uBorderParams.x + aa) {
232
+ borderMask = bandCoverage(d, -uBorderParams.x, 0.0, aa);
233
+ borderA = clamp(borderMask * uBorderColor.a * uOpacity, 0.0, 1.0);
234
+ borderRgb = uBorderColor.rgb * borderA;
235
+ }
236
+
237
+ float rimA = 0.0;
238
+ vec3 physLitRgb = vec3(0.0);
239
+ float physLitA = 0.0;
240
+ float physShadeA = 0.0;
241
+ float isPhysicalRim = step(0.5, uPhysicalLight.w) * (1.0 - hasUniformBorder);
242
+
243
+ if (glassMask > 0.001 && inset < 5.0 && uRimParams.x > 0.001 && isPhysicalRim < 0.5) {
244
+ float ny = abs(normal.y);
245
+ float wDirectional = 1.0 - clamp(uStyleMix, 0.0, 1.0);
246
+ float wSoft = clamp(uStyleMix, 0.0, 1.0) * clamp(uRimOpacity, 0.0, 1.0);
247
+
248
+ if (hasUniformBorder > 0.5) {
249
+ // Match CSS inset box-shadow: keep inner rim inside the padding edge so the 1px border is not double-brightened
250
+ float padInset = max(inset - uBorderParams.x, 0.0);
251
+ float padMask = 1.0 - borderMask;
252
+ float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);
253
+ float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, padInset);
254
+ float tail = exp(-padInset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);
255
+ rimA = clamp((core + tail) * dirWeight * uRimParams.x * padMask * glassMask * uOpacity, 0.0, 1.0);
256
+ } else {
257
+ if (wDirectional > 0.001) {
258
+ float dirWeight = computeDirectionalRimWeight(normal, uRimParams.w, uBorderParams.y);
259
+ float core = smoothstep(max(uRimParams.y, 0.4) + 0.6, 0.0, inset);
260
+ float tail = exp(-inset / 1.8) * uRimParams.z * smoothstep(5.0, 3.2, inset);
261
+ rimA += (core + tail) * dirWeight * uRimParams.x * wDirectional;
262
+ }
263
+
264
+ if (wSoft > 0.001) {
265
+ float bandT = clamp(inset / 5.0, 0.0, 1.0);
266
+ float steppedT = min(floor(bandT * 9.0) / 9.0, 1.0);
267
+ float bandCurve = mix(safePow(1.0 - bandT, 2.1), safePow(1.0 - steppedT, 2.1), 0.35);
268
+ float rimCoreBoost = 0.65 + 0.35 * smoothstep(1.2, 0.0, inset);
269
+ float angWeight = mix(0.015, 1.0, safePow(ny, 2.1)) * (normal.y < 0.0 ? 1.0 : 0.85);
270
+ rimA += bandCurve * rimCoreBoost * angWeight * uRimParams.x * wSoft;
271
+ }
272
+
273
+ // Boost specular luminance to match source-over alpha blending expectations
274
+ rimA = clamp(rimA * 1.35 * glassMask * uOpacity, 0.0, 1.0);
275
+ }
276
+ } else if (glassMask > 0.001 && isPhysicalRim > 0.5 && inset < uPhysicalParams.x) {
277
+ float bezelPx = max(uPhysicalParams.x, 1.0);
278
+ float t = clamp(inset / bezelPx, 0.0, 1.0);
279
+ float eps = 0.015;
280
+ float t0 = max(0.0, t - eps);
281
+ float t1 = min(1.0, t + eps);
282
+ float slope = (evalBezelHeight(t1, uPhysicalParams.w) - evalBezelHeight(t0, uPhysicalParams.w)) / max(t1 - t0, 1e-4);
283
+ vec3 N = normalize(vec3(normal * slope, 1.0));
284
+ vec3 L = uPhysicalLight.xyz;
285
+ vec3 H = vec3(L.xy, L.z + 1.0);
286
+ float hLen = max(length(H), 1e-4);
287
+ const float shininess = 36.0;
288
+ float flatBase = safePow(clamp((L.z + 1.0) / hLen, 0.0, 1.0), shininess);
289
+ float denomBase = max(1.0 - flatBase, 1e-4);
290
+ float hDot = clamp(dot(N, H) / hLen, 0.0, 1.0);
291
+ float highlight = max(safePow(hDot, shininess) - flatBase, 0.0) / denomBase;
292
+ vec3 H_bounce = vec3(-L.xy, L.z + 1.0);
293
+ float bDot = clamp(dot(N, H_bounce) / hLen, 0.0, 1.0);
294
+ float bounce = max(safePow(bDot, shininess) - flatBase, 0.0) / denomBase;
295
+ vec2 planar = L.xy / max(length(L.xy), 1e-4);
296
+ float facing = dot(normal, planar);
297
+ float fresnel = safePow(clamp(1.0 - N.z, 0.0, 1.0), 4.0);
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
+
316
+ float shadeTerm = safePow(clamp(1.0 - N.z, 0.0, 1.0), 2.5) * (0.45 + 0.55 * max(-facing, 0.0));
317
+ float dark = clamp(uPhysicalParams.z * shadeTerm * glassMask * uOpacity, 0.0, 1.0) * (1.0 - totalLit);
318
+ physLitRgb = litRgb;
319
+ physLitA = totalLit;
320
+ physShadeA = dark;
321
+ }
322
+
323
+ // Keep interior strictly transparent when shadow, hairline, border, and rim are all zero
324
+ if (shadowA <= 0.0001 && hairA <= 0.0001 && borderA <= 0.0001 && rimA <= 0.0001 && physLitA <= 0.0001 && physShadeA <= 0.0001) {
325
+ fragColor = vec4(0.0);
326
+ return;
327
+ }
328
+
329
+ // Premultiplied alpha composite: uniform border over inner/physical rim over hairline and shadow
330
+ float darkA = clamp(shadowA + hairA * (1.0 - shadowA), 0.0, 1.0);
331
+ vec3 shadeColor = vec3(0.03921569, 0.07058824, 0.10980392);
332
+ vec3 afterRimRgb = vec3(rimA) + physLitRgb + shadeColor * physShadeA;
333
+ float totalRimA = clamp(rimA + physLitA + physShadeA * (1.0 - rimA), 0.0, 1.0);
334
+ float afterRimA = totalRimA + darkA * (1.0 - totalRimA);
335
+ vec3 outRgb = borderRgb + afterRimRgb * (1.0 - borderA);
336
+ float outA = clamp(borderA + afterRimA * (1.0 - borderA), 0.0, 1.0);
337
+ fragColor = vec4(outRgb, outA);
338
+ }
307
339
  `;
308
340
  const WEBGL2_CONTEXT_ATTRS = {
309
341
  alpha: true,
@@ -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 === 'circle'
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;