A powerful Metal-based implementation for creating beautiful mesh gradients in Swift. This library provides an easy-to-use interface for generating stunning, animated mesh gradients for your iOS, macOS, tvOS, and Mac Catalyst applications.
- 🎨 Beautiful mesh gradient generation
- ⚡️ Metal-powered performance
- 🔄 Support for both static and animated gradients
- 📱 Cross-platform support (iOS 13+, macOS 10.15+, tvOS 13+, Mac Catalyst 13+)
- 🎮 SwiftUI and UIKit support
- 🎲 Built-in randomization utilities
Add the following to your Package.swift file:
dependencies: [
.package(url: "https://github.com/rogerioth/MeshGradient", from: "1.1.2")
]struct MyStaticGrid: View {
var body: some View {
MeshGradient(grid: generatePlainGrid())
}
}struct MyAnimatedGrid: View {
@State var meshRandomizer = MeshRandomizer(
colorRandomizer: MeshRandomizer.arrayBasedColorRandomizer(
availableColors: meshColors
)
)
var body: some View {
MeshGradient(
initialGrid: generatePlainGrid(),
animatorConfiguration: .init(
animationSpeedRange: 2...4,
meshRandomizer: meshRandomizer
)
)
}
}The mesh is generated using a grid of ControlPoints with the following parameters:
- Colors: Define the gradient colors using
SIMD3<Float> - Locations: Control points in metal coordinate space (-1...1)
- Turbulence: Control mesh distortion using
uTangentandvTangent
Example implementation:
typealias MeshColor = SIMD3<Float>
var meshRandomizer = MeshRandomizer(
colorRandomizer: MeshRandomizer.arrayBasedColorRandomizer(
availableColors: meshColors
)
)
func generatePlainGrid(size: Int = 6) -> Grid<ControlPoint> {
let preparationGrid = Grid<MeshColor>(repeating: .zero, width: size, height: size)
var result = MeshGenerator.generate(colorDistribution: preparationGrid)
for y in stride(from: 0, to: result.width, by: 1) {
for x in stride(from: 0, to: result.height, by: 1) {
meshRandomizer.locationRandomizer(&result[x, y].location, x, y, result.width, result.height)
meshRandomizer.turbulencyRandomizer(&result[x, y].uTangent, x, y, result.width, result.height)
meshRandomizer.turbulencyRandomizer(&result[x, y].vTangent, x, y, result.width, result.height)
meshRandomizer.colorRandomizer(&result[x, y].color, result[x, y].color, x, y, result.width, result.height)
}
}
return result
}For UIKit applications, use MTKView and MetalMeshRenderer. Check the SwiftUI MeshGradient implementation source code for reference on how to wrap MTKView.
The repository includes a publishing script to help manage releases:
./scripts/publish.sh <version-tag>Example:
./scripts/publish.sh 1.0.0The script will:
- Verify that a version tag is provided
- Check if the working directory is clean
- Create and push the new version tag
- iOS 13.0+
- macOS 10.15+
- tvOS 13.0+
- Mac Catalyst 13.0+
- Swift 5.6+
This project is licensed under the terms found in the LICENSE file.
See CHANGELOG.md for a list of changes and version history.
