@sessile/core 0.1.0 → 0.3.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.
@@ -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 < acc.dist) {
130
- acc = b;
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 - acc.dist) / uBlendK, 0.0, 1.0);
134
- float d = mix(b.dist, acc.dist, h) - uBlendK * h * (1.0 - h);
135
- vec2 n = mix(b.normal, acc.normal, h);
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
- return acc;
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
- if (glassMask > 0.001 && inset < 5.0 && uRimParams.x > 0.001) {
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 afterRimRgb = vec3(rimA);
245
- float afterRimA = rimA + darkA * (1.0 - rimA);
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: scale,
544
- scaleY: scale,
591
+ scaleX,
592
+ scaleY,
545
593
  offsetX: ox.value,
546
594
  offsetY: oy.value,
547
595
  lift: lift.value,
@@ -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. */