A powerful and easy to use live mesh gradient renderer for iOS, macOS, and Mac Catalyst, living branch of archived EthanLipnik's MeshKit
This project wouldn't be possible without the awesome work from Moving Parts and their Swift Playground
MeshKit is an easy to use live mesh gradient renderer for Apple platforms. In just a few lines of code, you can create a mesh gradient.
Add the following line to your package dependencies in Package.swift:
.package(url: "https://github.com/rogerioth/meshkit.git", from: "0.4.5")Or add it directly through Xcode:
- File > Swift Packages > Add Package Dependency
- Enter the repository URL:
https://github.com/rogerioth/meshkit.git - Select the version you want to use
private let randomizer = MeshRandomizer(colorRandomizer: { _, _, _, _, _, _ in
return
})
private let colors: MeshColorGrid = MeshKit.generate(palette: Hue.purple, size: MeshSize(width: 4, height: 4))
public var grainAlpha: Float = MeshDefaults.grainAlpha
public var subdivisions: Float = Float(MeshDefaults.subdivisions)
public var colorSpace: CGColorSpace?
public var body: some View {
ZStack {
Mesh(colors: colors,
animatorConfiguration: .init(meshRandomizer: randomizer),
grainAlpha: grainAlpha,
subdivisions: Int(subdivisions),
colorSpace: colorSpace)
.edgesIgnoringSafeArea(.all)
VStack {
}
}
}The snippet above will generate the effect seen in the video demo.
A cross-platform SwiftUI sample app is included in Examples/MeshKitSample.
To regenerate the sample Xcode project:
ruby scripts/generate-sample-project.rbTo open the sample in Xcode:
open Examples/MeshKitSample/MeshKitSample.xcodeprojMeshView is at the core of MeshKit, encapsulating an SCNView with enhanced capabilities to render mesh gradients efficiently. It's straightforward to use:
To create a mesh gradient, first initialize a MeshView and add it to your view hierarchy. Then, call the create method with an array of MeshNode.Color:
let meshView = MeshView()
view.addSubview(meshView)
meshView.create([
// Define your mesh points and colors here
])You can invoke this method multiple times to update the gradient dynamically.
Each mesh point is defined by the MeshNode.Color structure, comprising three components:
Specifies the grid position for the color, such as 0, 0 for a corner. This parameter is critical for the gradient's structure and no two colors should have the same point.
Represents the color's x and y coordinates for interpolation. Adjusting this will shift the color within the gradient. Avoid altering the location of edge points to maintain the gradient's shape.
The UIColor for the point. Note that alpha values are not utilized and will be disregarded. Choose colors that blend well for a smoother gradient.
MeshView supports customization of mesh width and height, though it's recommended to keep them equal for simplicity. The default dimensions are 3x3. If you choose to alter this, remember to provide a corresponding number of colors. For instance, a 2x2 mesh requires 4 colors, while a 4x4 mesh needs 16.
You can modify the mesh's subdivisions to change its "resolution." The default setting is 18. Increasing this value can enhance detail but may affect performance.
The project includes several utility scripts in the scripts directory to help with development:
build.sh: Builds the Swift package and sample app for macOS, iOS Simulator, and Mac Catalystbuild-and-test.sh: Runs the multi-platform builds and then runs all testssetup-environment.sh: Sets up the development environmentpublish.sh: Helps publish new versions of the package
To publish a new version of MeshKit:
- Ensure all your changes are committed
- Make sure you're on the main branch
- Run the publish script with the new version number:
Replace X.Y.Z with your version number (e.g., 1.0.0)
./scripts/publish.sh X.Y.Z
The publish script will:
- Validate the version format
- Ensure you're on the main branch
- Check for uncommitted changes
- Run tests
- Create and push a git tag for the version
- iOS 14.0+
- macOS 11.0+
- Mac Catalyst 14.0+
- Xcode 13.0+
- Swift 5.7+
Special thanks to Moving Parts for their foundational work in gradient meshes.
And to EthanLipnik for creating the initial version.
This project is licensed under the terms found in LICENSE.txt.