Glass

SDF-shaped glass lens layerEffect — magnifies and refracts a source view inside a circle or rounded-rect with frosted blur, a Fresnel rim, chromatic aberration, a directional highlight and tint, all driven by an analytic signed-distance field

Free
Free with a ShipSwift account.

Copies a short instruction with your private download link. Paste it into your agent: it downloads the whole skill (the source code, the setup steps and the mistakes we already made for you) and builds the screen.

Works with

+70

What it does

A refractive glass sheet laid over any content. SWGlass wraps a piece of background content and applies a Metal layerEffect that bends, frosts, tints and lights that background inside an analytic SDF region — a circle or a rounded rectangle. Outside the shape the background passes through, or is cut away when cutout is on.

The look is driven entirely by the shape's signed-distance field and its gradient: the rim refracts hardest while the centre stays calm, a single in-shader golden-angle disk frosts the content, the same taps split chromatically for dispersion, and tint, a directional edge light, a 3D specular glint and a Fresnel rim are layered on top before being cross-faded into the background by an edge mask. The light angle is converted to a (cos, sin) direction on the CPU so the shader avoids per-pixel trig, and Colors are resolved to RGB triples on the Swift side.

This is a sibling of glass-orb (which renders a sphere with its own gradient fill); SWGlass instead glasses over arbitrary content with a flat sheet and a far larger control surface.

Requires iOS 18+ / macOS 15+ (SwiftUI ShaderLibrary, Metal stitchable).

What you get

  • ✓783 lines of source code, ready to paste
  • ✓File Layout
  • ✓Source Code
  • ✓Usage
  • ✓Parameters
  • ✓Integration Checklist
  • ✓Notes / Gotchas
animationmetalshaderglassrefractionlensSDFlayerEffectSwiftUI

Usage

// Glass a circle over your own content.
SWGlass { MyHeroImage() }

// Rounded-rectangle glass card, isolated on transparency (only the glass remains).
SWGlass(shape: .roundedRect, cutout: true) { MyHeroImage() }

// Frosted, magnifying, slightly tinted lens.
SWGlass(blur: 6, innerZoom: 1.3, tintColor: .cyan, tintIntensity: 0.25) {
    MyHeroImage()
}

// Demo — built-in sample background + gear toolbar item + live-tuning sheet.
// Requires an enclosing NavigationStack.
NavigationStack {
    SWGlass(showsControls: true)
}

Parameters

ParameterTypeDefaultRangeNotes
shapeSWGlassShape.circlecircle / roundedRectThe analytic SDF region the glass covers
centerCGPoint(0.5, 0.5)0…1Shape centre in normalised view space
scaleCGFloat10.5…2Shape scale — >1 shrinks the glass, <1 grows it
cornerRadiusCGFloat0.120…0.34Corner radius for the rounded-rectangle shape
cutoutBoolfalse—When true, the exterior is transparent and only the glass remains
refractionCGFloat10…3Master strength of the refractive bend
edgeSoftnessCGFloat0.10.01…0.5Width of the soft edge fill band
blurCGFloat00…20Frosted-glass disk radius (0 = sharp)
thicknessCGFloat0.20.02…1Apparent glass thickness; widens the refractive band
aberrationCGFloat0.50…2Chromatic split strength along the refraction vector
innerZoomCGFloat10.5…2Magnifies the refracted content (>1 zooms in)
lightAngleCGFloat3000…360Light direction in degrees (0 = +x, counter-clockwise)
highlightCGFloat0.050…1Master strength of edge light + specular glint
highlightColorColor.white—Highlight / specular color
highlightSoftnessCGFloat0.50…1Specular tightness (higher = broader glint)
fresnelCGFloat0.10…1Strength of the Fresnel rim
fresnelSoftnessCGFloat0.10.01…1Width of the Fresnel rim band
fresnelColorColor.white—Fresnel rim color
tintColorColor.white—The color the glass tints the refracted content toward
tintIntensityCGFloat00…1How strongly the tint is mixed in (0 = none)
tintPreserveLuminosityBooltrue—When true, the tint keeps the original luminance (hue/chroma only)
showsControlsBoolfalse—Demo-only gear ToolbarItem; requires an enclosing NavigationStack

Integration Checklist

  1. Copy SWGlass.swift and SWGlass.metal into your Xcode target; the .metal file is compiled into the default ShaderLibrary automatically.
  2. Confirm the deployment target is iOS 18+ / macOS 15+.
  3. Pass the content you want glassed in the trailing closure, or use the convenience initialiser (no closure) for the built-in sample background.
  4. The maxSampleOffset budget is computed for you from blur + refraction + the aberration split — heavier frost / refraction automatically reserves a larger tile budget.
  5. Use cutout: true when you want the glass isolated on transparency (e.g. a glass badge over your own layout) rather than composited over the supplied background.

Notes / Gotchas

  • Everything is driven by the SDF and its gradient. The surface normal is a finite-difference gradient of the signed-distance field; thickness near the edge drives a squared refraction falloff so the rim bends hardest and the centre stays calm.
  • The shape enum travels as a float. SwiftUI's Shader.Argument has no integer case, so SWGlassShape.rawValue is sent as a .float and compared with > 0.5 in the shader. Keep the enum raw values 0 / 1.
  • One golden-angle disk does both frost and dispersion. The same tap set is reused at three chromatically-shifted centres, so the heavy taps only run when blur or aberration is non-zero (the sharp single-tap path is taken otherwise).
  • Light angle is precomputed on the CPU. The Swift side converts lightAngle to a (cos, sin) direction so the shader avoids per-pixel trig; Colors are likewise resolved to RGB triples on the Swift side.
  • The argument order in the .layerEffect call must match the Metal swGlass signature exactly: boundingRect, shape, center, scale, cornerRadius, cutout, refraction, edgeSoftness, blur, thickness, aberration, innerZoom, lightDir, highlight, highlightColor, highlightSoftness, fresnel, fresnelSoftness, fresnelColor, tintColor, tintIntensity, tintPreserveLuminosity.

More in Effects and animation

Metal shaders: fields of dots that roll like waves and flow into living patterns, all drawn on the GPU.
Metal dot fields
Free
Metal shaders: fields of dots that roll like waves and flow into living patterns, all drawn on the GPU.
A deep-space starfield in Metal: glowing nebulae drift past twinkling stars.
Deep-space starfield
Free
A deep-space starfield in Metal: glowing nebulae drift past twinkling stars.
Neon outlines that bloom from the center with prism-colored edges, rendered live in Metal.
Neon outlines
Free
Neon outlines that bloom from the center with prism-colored edges, rendered live in Metal.
Colored blobs that drift, melt into one another like liquid, and blend their colors as they merge.
Liquid metaballs
Free
Colored blobs that drift, melt into one another like liquid, and blend their colors as they merge.
A 3D network graph you can spin. Tap any node and its links light up, with the details below.
3D network graph
Free
A 3D network graph you can spin. Tap any node and its links light up, with the details below.
Items orbit into a dotted ring, and payment cards fan out of a wallet as the balance rolls in.
Orbit ring and wallet
Free
Items orbit into a dotted ring, and payment cards fan out of a wallet as the balance rolls in.
A field of bright colored dots that float and shimmer across the whole screen.
Floating color dots
Free
A field of bright colored dots that float and shimmer across the whole screen.
Stacked glass panels in mirrored layers, with neon gradients flowing through them.
Glass color panels
Free
Stacked glass panels in mirrored layers, with neon gradients flowing through them.
Drag the slider and scattered points of light pull together into a perfect 3D sphere.
Dot sphere
Free
Drag the slider and scattered points of light pull together into a perfect 3D sphere.
Living patterns: a mosaic of cells that keeps reshaping, then a hypnotic color swirl.
Cells and swirl
Free
Living patterns: a mosaic of cells that keeps reshaping, then a hypnotic color swirl.
Organic noise shaders: glowing veins that pulse through the dark, then bold color blobs in constant flow.
Noise veins and blobs
Free
Organic noise shaders: glowing veins that pulse through the dark, then bold color blobs in constant flow.