Skip to content
pruffitPublic

About

A liquid glass material for interfaces, derived from optics rather than assembled from effects. One material model, three render targets: WebGL2, live DOM, and Android AGSL.

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

VireGlass

A liquid glass material for interfaces, derived from optics rather than assembled from effects.

One material model, three render targets: WebGL2 and live DOM in the browser, AGSL on Android. The DOM target refracts the page itself — real elements, real text, no canvas — so glass can go over content a renderer never drew. Apache-2.0.

npm install vireglass

The idea

Most glass effects are a stack of knobs: blur radius, rim brightness, rainbow strength, a highlight slider. Every knob is independent, so most of the combinations you can dial in describe glass that does not exist — a razor-thin bevel with a dense white rim, a rim whose optics scale with the element's size.

VireGlass exposes causes and derives the rest:

import { resolveOptics } from 'vireglass';

const optics = resolveOptics({
  ior: 1.5,        // index of refraction — water 1.33, glass 1.5, sapphire 1.77
  thickness: 20,   // dp
  bevel: 10,       // dp, absolute: a real rim doesn't scale with the piece
  roughness: 0.12,
  film: 0,         // thin-film thickness, nm — iridescence
});

Those turn into the values the shaders need — refraction strength, magnification, Fresnel, specular, dispersion, diffraction, interference, medium tint, body density, ambient colour pickup. There is no "rainbow strength" knob because dispersion, diffraction and interference are one cause (wavelength dependence) and one hue multiplier, normalised to its own mean: they tint the reflection without brightening it.

Dispersion follows Cauchy, absorption follows Beer–Lambert, reflectance follows Schlick. Where the model departs from physics, the comment says so and says why.

Presets

import { MATERIAL_PRESETS, resolveOptics } from 'vireglass';

resolveOptics(MATERIAL_PRESETS.crystal);

water · glass · crystal · frosted · thick · thin · iridescent — points in the space of causes, not bundles of finished effects.

Web

import { createVireGlassRenderer } from 'vireglass/web';
import { resolveOptics, roundedRectGeometry, MATERIAL_PRESETS } from 'vireglass';

const renderer = createVireGlassRenderer(canvas);
renderer.resize(canvas.width, canvas.height);

renderer.render({
  density: devicePixelRatio,
  debug: 'normal',
  scene: (ctx, width, height) => {
    ctx.fillStyle = '#12141a';
    ctx.fillRect(0, 0, width, height);
    // ...draw whatever the glass should refract
  },
  pieces: [
    {
      optics: resolveOptics(MATERIAL_PRESETS.glass),
      geometry: roundedRectGeometry(220, 120, 32),
      centerX: canvas.width / 2,
      centerY: canvas.height / 2,
    },
  ],
});

render also returns one backdrop probe per element — mean lightness, 10th/90th percentiles and variegation of what lies under it. That is what the adaptation layer reads.

WebGL does not see the DOM, so this renderer refracts only what it drew itself — hence the scene callback. For glass over your actual page, use vireglass/dom below.

GPU backdrop

scene costs a CPU rasterize and a texSubImage2D upload every frame. If your backdrop is already a GPU simulation — a fluid, a shader animation, a video frame — draw it straight into the texture the lens samples with backdrop instead, in the same WebGL2 context, no canvas and no readback:

import { createProgram, drawFullscreenTriangle, FULLSCREEN_TRIANGLE_VERTEX_SOURCE } from 'vireglass/web';

let program: WebGLProgram | undefined;

renderer.render({
  density: devicePixelRatio,
  debug: 'normal',
  pieces: [/* ... */],
  backdrop: (gl, target) => {
    program ??= createProgram(gl, FULLSCREEN_TRIANGLE_VERTEX_SOURCE, MY_FRAGMENT_SOURCE);
    gl.bindFramebuffer(gl.FRAMEBUFFER, target.framebuffer);
    gl.viewport(0, 0, target.width, target.height);
    gl.useProgram(program);
    drawFullscreenTriangle(gl);
  },
});

backdrop: (gl: WebGL2RenderingContext, target: { framebuffer, width, height }) => void takes over from scene entirely — exactly one of the two is required. The pass may render through as many of its own FBOs first as it likes; it must finish by drawing into target.framebuffer.

Orientation. The texture this FBO wraps is top-row-first (row 0 is the scene's top row), and that row sits at gl_FragCoord.y = 0 — the window-coordinate bottom, not the top. A pass that blits an ordinary image (loaded the normal way, via texImage2D) needs no flip at all: texture(src, gl_FragCoord.xy / size) lands it correctly. A pass that treats increasing y as its own "up", the natural convention for a physics simulation, has to flip before this final write, or its top ends up stored as the scene's bottom.

Live DOM

import { attachGlass } from 'vireglass/dom';

const glass = attachGlass(document.querySelector('.player'), {
  material: MATERIAL_PRESETS.glass,
});
// glass.update() after a scroll or a theme change, glass.destroy() on unmount

The element now refracts the real page behind it. No canvas, no duplicate render of your UI.

It works by handing backdrop-filter an SVG filter whose feDisplacementMap bends the backdrop along a map derived from the same material model the other renderers use. The browser never gives the page's pixels to script — that boundary is what stops a page reading your cross-origin frames and visited links — so the displacement happens inside the compositor, where the pixels already are.

It carries the material, not a corner of it: refraction with per-channel dispersion, the rim with its two opposing arcs and dark edge, the adaptive shadow, the body under legibility and presence, and the finger response — press, drag, the release wave and the rise into glass.

glass.setMorph({ smoothing: 34, shape: { offsetX: 150, offsetY: -78, width: 170, height: 92, cornerRadius: 28 } });

Two shapes joined through the smooth union — §5's merging and splitting. What the shapes mean is your choreography; the material only knows how two silhouettes join.

glass.setAppear(t) brings the element in and out by how much light it bends, not by opacity. The maps are baked once at full presence and appear is written in place on the filter, so driving it every frame costs no bake and no new filter, at rest or interim.

Morphing elements

import { attachGlass, renderStats, resetRenderStats } from 'vireglass/dom';

glass.setInterim(true);   // before a morph, a drag or a growth
glass.prepare({ geometry: end, morph: endMorph, sample: endSample }); // where it ends, announced at the start
// ...animate the element's box, and `setMorph` shapes if it flows into others...
glass.land();             // in the last frame: 'exact', or 'blend' if the destination was not ready

A glass element that changes shape every frame used to re-run the whole pipeline every frame: a backdrop probe, a full-resolution displacement map (and spectral map) and a new filter element. That is the difference between 60 fps and 10 to 20. While interim, the element does none of it: it reuses its last sample and keeps its filter. The body, rim, shadow and custom properties still update every frame. A change of the box, of the morph shapes passed to setMorph or of the material asks a worker for a pair, the displacement map and the spectral hue map for that box, one build at a time and the newest wins. The pair is shown once both maps have decoded; until then the browser stretches the last one, and the refraction strength already follows the live box. Interim and settled maps share one key, so the pair built for the final box is the pair the lens rests on. prepare(target) builds and decodes the destination's pair ahead (priority over interim maps, the newest call wins), and land() writes it in the task of the call: no timer, probe or bake, and the frame after is the frame before. If the destination is not ready, land() blends from the shown pair into it over about 120 ms and returns 'blend'. setInterim(false) is land(); it takes the held sample and does not probe, so call update() when the backdrop really changed (the colours ease over 200 ms). Without a worker the maps are built on the main thread in slices of 4 ms after a frame. See docs/dom.md.

Always pair setInterim(true) with setInterim(false). As a safety net an element that gets no call at all for 2 seconds (no resize, morph, appear or update) leaves interim by itself, exactly as setInterim(false) would, so a morph that never reported landing cannot leave the glass on a stretched map. A pause longer than that in the middle of a morph ends interim early. A gesture held still sends no geometry, so a host that keeps a drag or a press open longer than that calls setInterim(true) again while it lasts: each call restarts the 2 seconds.

renderStats() returns { probes, fullBakes, reducedBakes, mainThreadBakes, workerBakes, filterSwaps, landedExact, landedBlend, probesAtLand, syncSwaps }, counted since the last resetRenderStats(). Every map counts: a rebuild with a spectral edge is two full bakes, one for each map; a cache hit is none. It is the hook for a performance gate that counts work instead of timing it: drive a morph, then assert the counters stayed at zero.

attachGlass writes CSS custom properties you can use directly:

--vireglass-ink-color ink colour for this backdrop, already resolved
--vireglass-body-color the glass body: density from the model, hue from the surroundings
--vireglass-tint-color ambient colour alone
--vireglass-rim, --vireglass-rim-width the hairline's gradient, applied for you
--vireglass-shadow the adaptive shadow, also written to box-shadow unless you opt out; its colour is --vireglass-shadow-color
--vireglass-radius, --vireglass-radius-min for concentric children (§11)
--vireglass-ink, --vireglass-tint, --vireglass-body-density the model's raw numbers

Telling the glass what is behind it

A host that knows its backdrop — an image, a gradient, a video's average colour — supplies it as a BackdropSample and changes it whenever the backdrop changes, without re-attaching:

import { attachGlass, statsFromSamples } from 'vireglass/dom';

const cover = statsFromSamples([{ r: 198, g: 92, b: 40 }, { r: 210, g: 120, b: 60 }]); // 0..255
const glass = attachGlass(el, { material, sample: cover });
glass.setSample(statsFromSamples([{ r: 20, g: 30, b: 60 }])); // next track
glass.setSample(null); // back to probing declared backgrounds

The sample drives the body, the rim's colour and strength, the ink polarity and the shadow. statsFromSamples turns a handful of colours into one; a hand-written object with luma, busy, lo, hi, r, g, b (0..1) works as well.

Body and rim

The body is drawn as the element's background-color on the refracting path too, composed from the same law the shaders apply (tint, matte lift, the medium's pull, concentration of light), so over a dark surround it reads lighter than the surround. --vireglass-body-color carries the same colour. Pass body: false if the host paints the background itself.

The rim is a thin light line round the whole contour, brighter toward the key light, over a dark edge. Its strength follows the lightness around the element (faint over dark, plain over bright) and the medium's reflectance; it is no longer white at full strength everywhere.

The three system accessibility settings are read from the browser's own media queries and followed while the page is open — reduced transparency frosts the glass, increased contrast takes the element by its silhouette, reduced motion holds it still. A system setting outranks the material preset and the user's own clarity slider (scale), because the user needs contrast more than they need the look.

Three things to know before you reach for it:

  • It renders the whole material. Refraction with per-channel dispersion, the rim with its two opposing arcs and dark edge, the adaptive shadow, the body under legibility and presence, the finger response, and the spectral edge — diffraction and interference baked into a hue map and multiplied in, since a filter graph cannot evaluate a function per pixel but can multiply by an image.

  • The refraction is Chromium-only today. Firefox does not support a filter reference in backdrop-filter and has closed the request as not planned; Safari does not yet, though WebKit has patches in flight. Elsewhere attachGlass falls back to blur, saturation and tint from the same optics — detected by measurement, never by user-agent string.

  • There is no pixel probe, so adaptation reads declared styles. The grid under the element is sampled through elementsFromPoint and the background stack is composited, and a point on a line of text counts the text as an estimated share of the point's spread (busy), not of its lightness. That is exact for colour-defined surfaces, an estimate for type, and blind to images, video and canvas — tell it what is there instead, since your app already knows its cover-art accent (below).

Android / React Native

import { VireGlassProvider, VireGlassSurface } from 'vireglass/native';

The Android target is an Expo module: expo-module.config.json ships in the package, so autolinking picks up the Kotlin in an Expo app and stays silent everywhere else. Run expo prebuild after installing. vireglass/native ships as source on purpose — the surface uses a Reanimated worklet, and worklets are compiled by the consumer's Babel plugin.

VireGlassProvider is optional. Mount it to give the host app an escape hatch:

<VireGlassProvider glassEnabled={settings.glass} reduceMotion={settings.reduceMotion}>
  <App />
</VireGlassProvider>

Without it, everything works on sensible defaults.

How the glass decides what to be

Three loops, all automatic, all in the core and shared by both platforms.

The body adapts to the backdrop by a gradient, not a number. The probe returns a plane of lightness — a slope along each axis — so the glass separates itself from the ink at every point of the element: it darkens harder over the bright half and barely touches the dark one. A plane is the crudest model that can express "one half is brighter than the other", and the only one that is smooth by construction. A point estimate at a density break produced ghost copies of the text underneath.

Density adapts to variegation. Fine texture — a checkerboard, a line of type — cannot be described by a plane, so density rises as a whole, driven by the lightness range. This is what real glass does with roughness: it stops being a window.

Presence. Over a uniform backdrop there is nothing to refract, and the glass honestly disappears. Correct for a piece of background, wrong for a control. presence is a floor on the body's density, tinted toward light and fading as the backdrop nears the light pole, so there is no veil or seam near white: the reference body is lighter than its surroundings from dark video to sky, whatever the glyph on it. What sends it to the ink's own pole is the ink's legibility over texture, a system setting (reduced transparency, increased contrast), or a density past BODY.inkPoleTo.

Ink polarity is decided by the cost of holding it, not by contrast: how much body density it would take to keep light ink legible. Above the threshold the element would stop being glass and become a painted plate, and the decision passes to the app. Contrast is the wrong measure — it also collapses on saturated yellow, and light icons kept flipping to dark over coloured blocks.

React

The core is dependency-free on purpose, so the one piece that needs hooks lives behind its own entry point:

import { useGlassAdaptation } from 'vireglass/react';

It carries the confirmation policy and the recolor fade. The decision itself — shouldInkBeLight — is a pure function in the core, shared by every platform.

Accessibility

applyAccessibility maps the three system settings — reduced transparency, increased contrast, reduced motion — onto the material. They set floors and they win over the user's clarity preference; applyGlassScale gives that preference its own axis, ultra clear to fully tinted.

Quality gates

npm run check:english # every tracked file is English
npm run typecheck
npm test
npm run check:glsl   # both shaders compile and link as GLSL ES 3.0
npm run check:agsl   # ...and are valid SkSL, which is what Android runs
npm run check:optics
npm run check:backdrop # the GPU backdrop path reproduces the scene path, pixel for pixel
npm run check:law    # every calibrated number is in src/law.ts, cited or named unmeasured
npm run check:dom    # a real browser: the glass bends live DOM, and only where it should
npm run check:package # packs, installs into an empty project, loads every entry point
npm run check:readme  # every name the documentation tells you to import exists

check:optics is the interesting one. It renders the material over a sweep of backdrops and asserts that the glass stays both a window and an object: enough of the backdrop's lightness range survives inside the element, and the element stays visible against it — across the whole range, not at a convenient value. It also checks that ink under a finger defocuses, that a raised element separates from its backdrop, that ink and content both survive over a busy canvas, and that the rim gathers what is behind it.

Limits — read these before adopting

  • Glass over live DOM refracts in Chromium only, and falls back to blur elsewhere. Its adaptation reads declared styles rather than pixels, so images and video need a supplied sample. See Live DOM above.
  • Refraction needs Android 13+ (RenderEffect). Below that it degrades to an affine magnifier: a loupe, not a lens.
  • No iOS. There is no public API for reading what is behind a view.
  • Real glass only works outside a capture target. You cannot put glass inside the node it is capturing — the RenderNode tree closes on itself and the runtime crashes. In practice that means chrome (tab bars, mini players), not content. See docs/adr-001-rendering.md §2.
  • Budget: about 0.8 ms of GPU per surface, growing linearly — 4 ms at one surface, 7 at three, 8 at six, against an 8.33 ms frame budget at 120 Hz on the reference device. Three is the confirmed product maximum. Six is where the budget runs out, not a measured cliff: the saturation point was never found. Cost also depends on the scene, not just the count. Do not judge frame cost on an emulator — it inflates GPU cost roughly 20× (it does reproduce the phone closely for correctness, within 2.4 units across the reference canvases).
  • The probe cannot see texture finer than a 22 px cell. Below that it averages, and the glass treats the backdrop as uniform.
  • Interference (film) derives a value but produces no visible iridescence yet. Known, unexplained.
  • Editing a shader needs an app restart on Android, not just Fast Refresh: the source travels as a prop out of a memoised adapter.

Documentation

docs/reference.md what the material was calibrated against, and what each number must satisfy
docs/architecture.md layers, parameters, limits
docs/adr-001-rendering.md the rendering architecture decision and its evidence
docs/platform-parity.md how web and Android are kept numerically in agreement
docs/benchmarks.md measurement protocol, device, numbers
docs/dom.md the DOM renderer: the technique, what it renders, what it costs
docs/law.md where every calibrated number lives, and which ones nobody has measured
docs/material-lab.md selected entries from the experiment journal

Calibration

The material was measured against publicly available reference material — Apple's WWDC 2025 sessions 219 and 356 and related design talks — and the documentation cites them by session and timestamp so the claims can be checked. No Apple code, assets or footage is included in or distributed with this package.

Every calibrated number lives in src/law.ts with its provenance, and check:law names the ones that have none: 52 of 131 today. See docs/law.md.

License

Apache-2.0. See LICENSE and NOTICE.

The patent grant is deliberate: this is an optical algorithm, not glue code.

About

A liquid glass material for interfaces, derived from optics rather than assembled from effects. One material model, three render targets: WebGL2, live DOM, and Android AGSL.

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages