rust-glass 0.4.0

GPU rendered Liquid Glass style UI framework with an immediate mode API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// The glass material. One draw covers shadow, lens, face, edge and glow.
//
// Optics: the surface is flat in the middle and curves down near its outline.
// A ray through the curved part is bent towards the middle, so pixels near the
// edge show the backdrop from further inside (compressed, and mirrored right at
// the rim). `inner_*` controls that bend. `outer_*` adds a thin opposite ring
// that pulls in light from outside the shape, `pinch` a wide opposite zone
// further in, as in a drop with a raised shoulder. Red bends more than blue.

struct Glass {
    bounds: vec4f,
    clip: vec4f,
    // x: 0 bends equally in all directions, 1 only sideways.
    // y: 0 disperses evenly, 1 only in the corners. z: darkness. w: tint follow.
    tint: vec4f,
    shape_start: u32,
    shape_count: u32,
    mask: u32,
    fuse: f32,

    inner_amount: f32,
    inner_height: f32,
    outer_amount: f32,
    outer_height: f32,

    dispersion: f32,
    blur: f32,          // radius in pixels
    magnify: f32,
    opacity: f32,

    face_lo: f32,       // the backdrop range is remapped to lo..hi
    face_hi: f32,
    saturation: f32,
    light_bend: f32,    // extra rotation of the light, radians

    rim: f32,
    rim_width: f32,
    dark_edge: f32,
    light_spill: f32,

    inner_glow: f32,
    inner_shadow: f32,
    brightness: f32,           // even brightening, press feedback
    rim_base: f32,      // part of the rim that does not depend on the light

    pinch: f32,         // outward bend in the middle of the curved part
    pinch_width: f32,
    origin: vec2f,      // center of magnification

    shadow: f32,
    shadow_radius: f32,
    shadow_y: f32,
    shadow_tint: f32,

    glow_pos: vec2f,    // where a press lights the glass
    glow: f32,
    glow_radius: f32,

    // The inside of the body that is drawn at half resolution, in pixels.
    // Empty (zero size) when the whole element is drawn here.
    interior: vec4f,
};

struct GShape {
    center: vec2f,
    half: vec2f,
    radius: vec2f,
    power: f32,
    tint_a: f32,
    tint: vec4f,
};

@group(0) @binding(3) var<storage, read> glasses: array<Glass>;
@group(0) @binding(4) var<storage, read> gshapes: array<GShape>;
@group(1) @binding(0) var backdrop: texture_2d<f32>;

struct VOut {
    @builtin(position) pos: vec4f,
    @location(0) p: vec2f,
    @location(1) @interpolate(flat) i: u32,
};

// Ten vertices: a quad over the element, or, when its inside is drawn at
// half resolution, a strip around that inside so it is not drawn twice.
@vertex
fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VOut {
    let g = glasses[ii];
    let lo = max(g.bounds.xy, g.clip.xy);
    let hi = min(g.bounds.zw, g.clip.zw);
    var o: VOut;
    if (g.interior.z > g.interior.x) {
        // Corners in turn, top left, top right, bottom right, bottom left,
        // each outside then inside, back to the first.
        let k = (vi / 2u) % 4u;
        let c = vec2f(f32(k == 1u || k == 2u), f32(k >= 2u));
        if (vi % 2u == 0u) {
            o.p = mix(lo, hi, c);
        } else {
            o.p = mix(g.interior.xy, g.interior.zw, c);
        }
    } else {
        o.p = mix(lo, hi, quad_corner(min(vi, 3u)));
    }
    o.pos = to_ndc(o.p);
    o.i = ii;
    return o;
}

struct Field {
    d: f32,
    n: vec2f,
    tint: vec4f,
    half: f32,
};

// Union of the group's shapes with a smooth minimum, so nearby shapes merge
// like drops of liquid.
fn field(start: u32, count: u32, k: f32, p: vec2f) -> Field {
    let s0 = gshapes[start];
    let f0 = sd_squircle(p - s0.center, s0.half, s0.radius, s0.power);
    var f: Field;
    f.d = f0.x;
    f.n = f0.yz;
    f.tint = s0.tint;
    f.half = min(s0.half.x, s0.half.y);
    for (var i = 1u; i < count; i++) {
        let s = gshapes[start + i];
        let b = sd_squircle(p - s.center, s.half, s.radius, s.power);
        let kk = max(k, 0.001);
        let t = clamp(0.5 + 0.5 * (b.x - f.d) / kk, 0.0, 1.0);
        f.n = mix(b.yz, f.n, t);
        f.tint = mix(s.tint, f.tint, t);
        f.half = mix(min(s.half.x, s.half.y), f.half, t);
        f.d = mix(b.x, f.d, t) - kk * t * (1.0 - t);
    }
    f.n = f.n / max(length(f.n), 1e-4);
    return f;
}

fn field_d(start: u32, count: u32, k: f32, p: vec2f) -> f32 {
    let s0 = gshapes[start];
    var d = sd_squircle(p - s0.center, s0.half, s0.radius, s0.power).x;
    for (var i = 1u; i < count; i++) {
        let s = gshapes[start + i];
        let b = sd_squircle(p - s.center, s.half, s.radius, s.power).x;
        let kk = max(k, 0.001);
        let t = clamp(0.5 + 0.5 * (b - d) / kk, 0.0, 1.0);
        d = mix(b, d, t) - kk * t * (1.0 - t);
    }
    return d;
}

// Profile of a quarter circle bevel: 1 at the rim, falling steeply to 0 at the
// end of the curved part.
fn bevel(t: f32) -> f32 {
    let u = 1.0 - clamp(t, 0.0, 1.0);
    return 1.0 - sqrt(max(1.0 - u * u, 0.0));
}

// Displacement along the outward normal at `depth` pixels below the outline.
// `w` widens the curved part for one color.
fn lens(g: Glass, depth: f32, side: f32, w: f32) -> f32 {
    let wide = clamp(1.0 + 1.5 * w, 0.4, 3.0);
    var d = g.outer_amount * bevel(depth / max(g.outer_height, 0.5))
        - g.inner_amount * mix(1.0, side, g.tint.x) * bevel(depth / max(g.inner_height * wide, 0.5));
    if (g.pinch > 0.0) {
        let h = sin(3.14159265 * clamp(depth / max(g.pinch_width, 1.0), 0.0, 1.0));
        d += g.pinch * (1.0 + 0.3 * w) * mix(1.0, 1.0 - side, g.tint.x) * h * h;
    }
    return d;
}

fn gauss(x: f32) -> f32 {
    return exp(-x * x);
}

fn bicubic(uv: vec2f, lod: f32) -> vec3f {
    let dim = vec2f(textureDimensions(backdrop, i32(lod)));
    let x = uv * dim - 0.5;
    let f = fract(x);
    let i = floor(x);
    let w0 = (1.0 - f) * (1.0 - f) * (1.0 - f) / 6.0;
    let w1 = (4.0 - 6.0 * f * f + 3.0 * f * f * f) / 6.0;
    let w3 = f * f * f / 6.0;
    let w2 = 1.0 - w0 - w1 - w3;
    let g0 = w0 + w1;
    let g1 = w2 + w3;
    let h0 = (i - 0.5 + w1 / g0) / dim;
    let h1 = (i + 1.5 + w3 / g1) / dim;
    let a = textureSampleLevel(backdrop, samp, vec2f(h0.x, h0.y), lod).rgb;
    let b = textureSampleLevel(backdrop, samp, vec2f(h1.x, h0.y), lod).rgb;
    let c = textureSampleLevel(backdrop, samp, vec2f(h0.x, h1.y), lod).rgb;
    let d = textureSampleLevel(backdrop, samp, vec2f(h1.x, h1.y), lod).rgb;
    return mix(mix(d, c, g0.x), mix(b, a, g0.x), g0.y);
}

// Backdrop at pixel `p`, blurred with a radius of about `r` pixels.
fn sample_bg(p: vec2f, r: f32) -> vec3f {
    let q = clamp(p, vec2f(0.5), G.size - 0.5);
    let uv = q / G.tex_size;
    if (r < 0.35) {
        return textureSampleLevel(backdrop, samp, uv, 0.0).rgb;
    }
    let lod = clamp(log2(1.0 + r * 0.75), 0.0, 5.0);
    if (G.quality > 1.5) {
        return textureSampleLevel(backdrop, samp, uv, lod).rgb;
    }
    let l0 = floor(lod);
    let a = bicubic(uv, l0);
    if (lod - l0 < 0.02) {
        return a;
    }
    var first = a;
    if (l0 < 0.5) {
        first = textureSampleLevel(backdrop, samp, uv, 0.0).rgb;
    }
    return mix(first, bicubic(uv, min(l0 + 1.0, 5.0)), lod - l0);
}

// Cheaper version for very soft lookups: one level only.
fn sample_wide(p: vec2f, r: f32) -> vec3f {
    let q = clamp(p, vec2f(0.5), G.size - 0.5);
    let lod = clamp(round(log2(1.0 + r * 0.75)), 0.0, 5.0);
    if (G.quality > 1.5) {
        return textureSampleLevel(backdrop, samp, q / G.tex_size, lod).rgb;
    }
    return bicubic(q / G.tex_size, lod);
}

fn vibrance(c: vec3f, s: f32) -> vec3f {
    return max(mix(vec3f(luma(c)), c, s), vec3f(0.0));
}

// The inside of large blurred glass is smooth, so it is shaded at half
// resolution into its own texture and scaled up. The full size draw then only
// covers the edge around it.
@vertex
fn vs_half(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VOut {
    let g = glasses[ii];
    // Two pixels more than the inside, for the filter that scales it up.
    let r = g.interior + vec4f(-2.0, -2.0, 2.0, 2.0);
    var o: VOut;
    o.p = mix(r.xy, r.zw, quad_corner(vi));
    o.pos = to_ndc(o.p);
    o.i = ii;
    return o;
}

@fragment
fn fs_half(in: VOut) -> @location(0) vec4f {
    return shade(glasses[in.i], in.p);
}

@fragment
fn fs(in: VOut) -> @location(0) vec4f {
    let g = glasses[in.i];
    let p = in.p;
    return shade(g, p) * mask_coverage(g.mask, p) * clip_coverage(g.clip, p);
}

// The glass at pixel `p`, premultiplied, before masks and clip.
fn shade(g: Glass, p: vec2f) -> vec4f {
    let f = field(g.shape_start, g.shape_count, g.fuse, p);
    let d = f.d;
    let n = f.n;
    let cov = edge_aa(d);

    // Shadow, under the glass and tinted by the light that passed through it.
    var under = vec4f(0.0);
    if (cov < 1.0 && g.shadow > 0.001) {
        let ds = field_d(g.shape_start, g.shape_count, g.fuse, p - vec2f(0.0, g.shadow_y));
        let soft = 1.0 / (1.0 + exp(ds / (g.shadow_radius * 0.38)));
        let contact = exp(-max(d, 0.0) / (1.2 * G.ppp)) * min(g.dark_edge, 0.3) * 0.30;
        let a = clamp(g.shadow * soft + contact * g.opacity, 0.0, 1.0);
        var rgb = vec3f(0.0);
        if (g.shadow_tint > 0.0 && a > 0.004 && G.quality < 1.5) {
            // Light that crossed the body: from the middle, widely spread.
            let q = p - n * (max(d, 0.0) + f.half * 0.95);
            let spread_px = max(f.half * 1.6, 10.0 * G.ppp);
            let bg = vibrance(sample_wide(mix(q, g.origin, 0.55), spread_px), 2.4);
            rgb = min(bg * bg * 1.05, vec3f(1.0)) * g.shadow_tint;
        }
        under = vec4f(rgb * a, a);
    }

    var out = under;
    if (cov > 0.0) {
        let depth = max(-d, 0.0);
        let op = g.opacity;

        // Virtual light in element space.
        let cb = cos(g.light_bend);
        let sb = sin(g.light_bend);
        let light = vec2f(G.light.x * cb - G.light.y * sb, G.light.x * sb + G.light.y * cb);
        let facing = dot(n, light);
        let near = max(facing, 0.0);
        let far = max(-facing, 0.0);

        // Lens. Dispersion flips on the far side, where light leaves the body.
        let even = mix(1.0, -0.55, smoothstep(0.0, 0.5, far));
        let dispersion = g.dispersion * even * (1.0 - g.tint.y);
        let corner = g.dispersion * 2.0 * n.x * n.y * g.tint.y;
        let base = g.origin + (p - g.origin) / max(g.magnify, 0.05);
        let side = n.x * n.x * n.x * n.x;
        let shift = lens(g, depth, side, 0.0);
        // How many backdrop pixels one screen pixel covers along the normal.
        // Near the rim this grows quickly, so those pixels are supersampled.
        let foot = abs(1.0 - lens(g, depth + 0.5, side, 0.0) + lens(g, max(depth - 0.5, 0.0), side, 0.0));
        // The rim of real glass stays clearer than its body.
        var blur = g.blur * mix(0.45, 1.0, smoothstep(0.0, max(g.inner_height, 1.0), depth));

        var c: vec3f;
        let spread = abs(shift * dispersion) + abs(corner) * 4.0 * step(depth, max(g.pinch_width, g.inner_height));
        if (blur < 3.0 && (spread > 0.35 || foot > 1.3) && G.quality < 1.5) {
            var sum = vec3f(0.0);
            var wsum = vec3f(0.0);
            let rr = max(select(blur, 0.0, blur < 1.2), min(foot * 0.09, 2.0));
            // Wide rainbows need more steps to stay smooth.
            let steps = select(8, 16, abs(corner) > 0.1);
            for (var i = 0; i < steps; i++) {
                let t = (f32(i) + 0.5) / f32(steps);
                // Wavelength and sub pixel depth run through different orders.
                let sub = (f32((i * 5 + 2) % steps) + 0.5) / f32(steps) - 0.5;
                let w = max(vec3f(1.0) - abs(vec3f(t) - vec3f(0.0, 0.5, 1.0)) * 1.9, vec3f(0.0));
                let hue = 1.0 - 2.0 * t;
                let k = lens(g, max(depth + sub, 0.0), side, corner * hue) * (1.0 + dispersion * hue) - sub;
                sum += sample_bg(base + n * k, rr) * w;
                wsum += w;
            }
            c = sum / wsum;
        } else {
            blur = max(blur, min((foot - 1.0) * 0.4, 6.0));
            if (spread > 0.35 && G.quality < 1.5) {
                c.r = sample_bg(base + n * shift * (1.0 + dispersion), blur).r;
                c.g = sample_bg(base + n * shift, blur).g;
                c.b = sample_bg(base + n * shift * (1.0 - dispersion), blur).b;
            } else {
                c = sample_bg(base + n * shift, blur);
            }
        }

        // Light from the wider surroundings spills onto the edge.
        if (g.light_spill > 0.0 && G.quality < 1.5) {
            let e = g.light_spill * (1.0 - 0.5 * g.tint.z) * exp(-depth / max(g.inner_height * 0.8, 1.0));
            if (e > 0.008) {
                let q = p + n * (g.inner_height * 0.8 + 6.0 * G.ppp);
                c = mix(c, vibrance(sample_wide(q, 14.0 * G.ppp), 1.5), e);
            }
        }

        // Face: vibrancy, range compression, tint.
        c = vibrance(c, mix(1.0, g.saturation, op));
        let lum = luma(c);
        let lo = mix(0.0, g.face_lo, op);
        let hi = mix(1.0, g.face_hi, op);
        c = vec3f(lo) + c * (hi - lo);

        let ta = f.tint.a * op;
        if (ta > 0.0) {
            // Colored glass keeps its hue but follows the brightness behind it.
            let toned = f.tint.rgb * (0.86 + 0.26 * lum) + vec3f(0.05 * lum);
            c = mix(c, mix(f.tint.rgb, toned, g.tint.a), ta);
        }

        // Directional light. The near side gets a soft shade, the far side the
        // light that was focused through the body.
        let body = max(min(f.half * 0.95, 34.0 * G.ppp), 1.0);
        let e = 1.0 - smoothstep(0.0, body, depth);
        c = c * (1.0 - g.inner_shadow * e * e * near * op);
        c = mix(c, vec3f(1.0), g.inner_glow * e * e * far * op);

        // Dark line just inside the outline, then the specular rim on top. Both
        // are soft profiles of the signed distance, so they stay even around
        // corners. The rim is where rays turn the most, so the dark line takes
        // the color of the whole body behind it.
        let sd = -d;
        let edge = gauss(max(sd - 0.18 * G.ppp, 0.0) / max(0.40 * G.ppp, 0.7));
        if (edge > 0.01 && g.dark_edge > 0.0) {
            let q = mix(p - n * f.half, g.origin, 0.8);
            let body_c = vibrance(sample_wide(q, max(f.half * 0.85, 6.0 * G.ppp)), 10.0);
            let k = g.dark_edge * edge * op * (1.0 - 0.92 * smoothstep(0.1, 0.8, far));
            c = mix(c, body_c * 0.5, k);
        }
        // The end of the glass: a soft band where the surface curves away,
        // a shade darker on light glass and lighter on dark glass, so the
        // edge reads as the body turning rather than a drawn outline.
        let band_w = max(min(g.inner_height * 0.55, f.half * 0.5), 2.0 * G.ppp);
        let band = 1.0 - smoothstep(0.0, band_w, depth);
        let toward = mix(vec3f(0.0), vec3f(1.0), g.tint.z);
        c = mix(c, toward, band * band * mix(0.05, 0.0, g.tint.z) * op);

        // The specular rim: a fine line that fades softly into the body.
        let rw = max(g.rim_width, 0.9);
        let line = gauss((sd - rw * 0.55) / (rw * 0.68));
        let soft = exp(-max(sd, 0.0) / (rw * 3.2));
        let profile = 0.55 * line + 0.45 * soft * (1.0 - 0.5 * g.tint.z);
        let halo = exp(-depth / max(2.0 * G.ppp, 0.8)) * min(g.inner_glow * 2.4, 0.6) * smoothstep(0.0, 0.5, far);
        let dir = (1.0 - 0.9 * g.dark_edge) * pow(near, 1.6) + pow(far, 1.2);
        let key = g.rim_base + (1.0 - g.rim_base) * min(dir, 1.0);
        let rim_rgb = mix(vec3f(1.0), min(vibrance(c, 1.4) * 1.5 + 0.15, vec3f(1.0)), 0.25);
        let rim_a = clamp(g.rim * (key * profile + dir * halo), 0.0, 1.0) * op;
        c = mix(c, rim_rgb, rim_a);

        c = mix(c, vec3f(1.0), g.brightness * op);

        // Press light: spreads from the touch through the body and catches
        // the rim nearby.
        if (g.glow > 0.001) {
            let q = (p - g.glow_pos) / max(g.glow_radius, 1.0);
            let w = g.glow * exp(-dot(q, q) * 1.6);
            c = mix(c, vec3f(1.0), clamp(w * mix(0.42, 0.30, g.tint.z) + w * profile * 0.6, 0.0, 1.0) * op);
        }

        let fade = smoothstep(0.0, 0.25, op);
        let a = cov * fade;
        out = vec4f(clamp(c, vec3f(0.0), vec3f(1.0)) * a, a) + under * (1.0 - a);
    }

    return out;
}