@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.
- package/LICENSE +373 -373
- package/dist/contracts.d.ts +19 -0
- package/dist/entries/internal.d.ts +14 -0
- package/dist/entries/internal.js +14 -0
- package/dist/entries/legibility.d.ts +8 -0
- package/dist/entries/legibility.js +8 -0
- package/dist/entries/overlay.d.ts +9 -0
- package/dist/entries/overlay.js +8 -0
- package/dist/index.d.ts +8 -6
- package/dist/index.js +7 -6
- package/dist/legibility.js +6 -0
- package/dist/optics.js +30 -0
- package/dist/overlayRenderer.d.ts +1 -1
- package/dist/overlayRenderer.js +326 -295
- 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 +14 -1
package/dist/overlayRenderer.js
CHANGED
|
@@ -15,295 +15,327 @@
|
|
|
15
15
|
*/
|
|
16
16
|
import { extractFieldContourPath } from "./contour.js";
|
|
17
17
|
import { MAX_GPU_PRIMITIVES } from "./contracts.js";
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
//
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
vec2
|
|
87
|
-
|
|
88
|
-
vec2
|
|
89
|
-
vec2
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
float
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
baseNormal =
|
|
100
|
-
} else {
|
|
101
|
-
baseNormal = vec2(
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (
|
|
250
|
-
float
|
|
251
|
-
float
|
|
252
|
-
float
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
float
|
|
272
|
-
|
|
273
|
-
float
|
|
274
|
-
float
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
vec3
|
|
278
|
-
float
|
|
279
|
-
float
|
|
280
|
-
|
|
281
|
-
float
|
|
282
|
-
float
|
|
283
|
-
float
|
|
284
|
-
|
|
285
|
-
float
|
|
286
|
-
float
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
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 ===
|
|
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;
|