Starfield

Multi-layer twinkling starfield rendered via a SwiftUI Metal stitchable shader, with parallax-scrolling layers, hashed star placement, and a per-star sin-driven twinkle

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

Full-screen twinkling starfield rendered through SwiftUI's colorEffect Metal pipeline. Each parallax layer is a hashed grid: a per-cell scalar hash decides which cells light up (h > 1 - density), a per-cell 2D hash places the star inside the cell, and a sin-driven term twinkles its brightness. Back layers are finer-grained and dimmer, sliding downward faster for a parallax effect.

Requires iOS 18+ / macOS 15+ (uses SwiftUI ShaderLibrary, Shader / ShaderFunction, and Metal stitchable color effects).

What you get

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

Usage

// Default — white stars on black, full-screen
ZStack {
    SWStarfield()
        .ignoresSafeArea()
    // Your content here
}

// Custom color and denser field
SWStarfield(starColor: .yellow, density: 0.5, layers: 6)

// As a section background
myContent
    .background { SWStarfield() }

// Slower, calmer field for a sleep / focus screen
SWStarfield(speed: 0.4, twinkleAmount: 0.15)

Parameters

ParameterTypeDefaultRangeNotes
starColorColor.white—Color of stars
backgroundColor.black—Color rendered behind the stars
speedFloat1.00…3Multiplier on per-layer parallax scroll
layersInt41…8Number of parallax layers (clamped by shader)
baseScaleFloat605…200Cell grid resolution of the front layer (higher = smaller, more numerous stars)
scaleStepFloat300…100Cell grid increment per layer behind the front
densityFloat0.30…1Fraction of cells containing a star
starSizeFloat0.40.05…2Star radius in cell-space
twinkleSpeedFloat3.00…10Angular frequency of the per-star twinkle
twinkleAmountFloat0.30…1Twinkle amplitude — 0 = steady, 1 = full blink
showsControlsBoolfalse—Demo-only gear ToolbarItem; requires an enclosing NavigationStack

Integration Checklist

  1. Copy SWStarfield.swift and SWStarfield.metal into your Xcode target (e.g. under SWAnimation/SWMetal/).
  2. Confirm the deployment target is iOS 18+ / macOS 15+.
  3. Use it as a ZStack background and apply .ignoresSafeArea() if you want full-bleed.
  4. Use the parameter defaults first — they are tuned for full-screen on iPhone-class GPUs. Only raise baseScale × layers after measuring.
  5. If you adopt the optional showsControls demo flag, the gear button is registered as a native ToolbarItem and requires the call site to be inside a NavigationStack.

Notes / Gotchas

  • The shader caps layers at 8 so the per-pixel loop bound stays static; values above 8 are silently truncated.
  • Cost is per-pixel × layers. Defaults are tuned for full-screen on iPhone-class GPUs; raise baseScale and layers cautiously on lower-end devices.
  • The twinkleAmount parameter is re-expressed as mean = 1 - amt, amplitude = amt, so amt = 0 gives perfectly steady stars and amt = 0.3 reproduces the default look.
  • The cell-fall-off uses 1 - smoothstep(0, ssz, d) rather than an edge-flipped smoothstep, so behavior matches the WGSL preview where edge0 > edge1 is undefined.

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.