PixeluxGlass

A SwiftUI implementation of Apple’s liquid glass visual effect for ARKit. This library brings real-time refractive glass rendering to AR experiences using Metal shaders.

See It In Action
Features
- Real-time refractive glass effect over ARKit camera feed
- SDF-based shape rendering with rounded corners
- Interactive mode with liquid merging between glass elements
- Customizable blur intensity and corner radius
- Pure SwiftUI API backed by Metal shaders
- 60fps rendering via direct frame relay (bypasses SwiftUI state)
Requirements
- iOS 15.0+
- Swift 5.5+
- Xcode 15+
- ARKit-compatible device (iPhone/iPad with A12 chip or later)
- Camera permission
Project Structure
Pixelux-Glass/
├── src/
│ ├── LiquidGlass/
│ │ └── LiquidGlass.swift # Core SwiftUI component + Metal renderer
│ └── Shader/
│ └── Shaders.metal # Vertex & fragment shaders (enhanced, interactive, shared)
├── Example/
│ └── Example.xcodeproj # Full demo app with AR + interactive glass UI
├── res/
│ └── images/
│ └── banner.png
├── LICENSE
└── TRADEMARKS.md
The entire library is two files: LiquidGlass.swift and Shaders.metal.
Installation
This is not a Swift package. Copy the source files directly into your project:
- Copy
src/LiquidGlass/LiquidGlass.swift into your Xcode project
- Copy
src/Shader/Shaders.metal into your Xcode project
- Ensure both files are added to your app target
Running the Example App
- Clone this repository
- Open
Example/Example.xcodeproj in Xcode
- Select your development team under Signing & Capabilities
- Build and run on a physical device (ARKit requires a real camera — the Simulator will not work)
- Grant camera permission when prompted
The example app demonstrates a full interactive glass UI with an AR camera background, an expandable card, and interactive circular buttons that merge when dragged close together.
Quick Start
1. Set Up ARKit
Create an ARViewContainer that publishes camera frames to the glass renderer:
import SwiftUI
import ARKit
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARSCNView {
let arView = ARSCNView()
let config = ARWorldTrackingConfiguration()
arView.session.delegate = context.coordinator
arView.session.run(config)
return arView
}
func updateUIView(_ uiView: ARSCNView, context: Context) {}
func makeCoordinator() -> Coordinator { Coordinator() }
class Coordinator: NSObject, ARSessionDelegate {
func session(_ session: ARSession, didUpdate frame: ARFrame) {
LiquidGlassMetalView.publishARFrame(frame)
}
}
}
2. Add a Glass Element
struct ContentView: View {
var body: some View {
ZStack {
ARViewContainer()
.ignoresSafeArea()
LiquidGlass()
.cornerRadius(40)
.blurIntensity(0.3)
.frame(width: 200, height: 200)
}
}
}
View Modifiers
| Modifier |
Description |
Default |
.cornerRadius(_ radius: CGFloat) |
Corner radius of the glass shape |
30 |
.blurIntensity(_ intensity: CGFloat) |
Blur strength, 0.0 (clear) to 1.0 (max) |
0.0 |
.interactive(_ enabled: Bool) |
Enable liquid merging with nearby glasses |
false |
.animatable(_ enabled: Bool) |
Sync frame position on every draw (for animated elements) |
false |
LiquidGlass()
.cornerRadius(25)
.blurIntensity(0.5)
.interactive(true)
.animatable(true)
.frame(width: 100, height: 100)
Interactive Mode
When two or more LiquidGlass views have .interactive(true) and are positioned close together, they merge with a liquid water-like effect using SDF smooth minimum blending.
HStack(spacing: 20) {
LiquidGlass()
.cornerRadius(25)
.interactive(true)
.frame(width: 100, height: 100)
LiquidGlass()
.cornerRadius(25)
.interactive(true)
.frame(width: 100, height: 100)
}
Use .animatable(true) on glasses that move (e.g. with drag gestures or animations) so the renderer tracks their screen position every frame.
How It Works
- ARKit captures camera frames and publishes them via
LiquidGlassMetalView.publishARFrame()
- Metal textures are created from the camera’s
CVPixelBuffer (YCbCr or BGRA)
- SDF rendering clips the glass shape with rounded corners
- Refraction distorts the camera feed through a lens-like UV warp
- 9x9 blur kernel applies configurable gaussian blur
- Interactive blending uses
smin() to merge nearby glass SDFs for the liquid effect
- Edge lighting adds gradient and diagonal highlights for depth
Troubleshooting
Glass appears black or empty
- Must run on a physical device (not Simulator)
- Verify ARSession is running and camera permission is granted
No interaction between glasses
- Both elements need
.interactive(true)
- Elements must be close enough to interact
- Use
.animatable(true) on moving elements
Performance issues
- Reduce the number of glass instances
- Lower
.blurIntensity() to reduce shader workload
- Disable
.interactive() when merging isn’t needed
License
The source code is licensed under the MIT License. See LICENSE.
Logos, icons, and visual brand assets are not covered by the MIT License. The names “Pixelux” and “CruzCortes” are trademarks. See TRADEMARKS.md.
Created by Gonzalo Cruz Cortes
评论