Add a 3D avatar to SwiftUI
A profile view needs one persistent appearance and a fresh event whenever someone taps a reaction. Clayfaces uses an AvatarRecipe for the look and a ClayfacesPerformance for each gesture.
Read the guideAdd the local Swift package
The 0.1.2 kit targets iOS 26 or later and uses Swift 6.2 or later. To see the native renderer first, open the example Xcode project inside the download’s iOS/Example directory.
- Extract the complete iOS directory into a stable location in your project. Keep Package.swift and Sources together.
- In Xcode, choose File → Add Package Dependencies → Add Local and select that directory.
- Link Clayfaces to your app target. Build once so Swift Package Manager processes the resources and Metal shaders.
Give the profile one editable recipe
Add this view to your app and present AvatarProfile() from your profile screen. The editor receives a binding to the same recipe as the player. Tapping Done applies the edited appearance. Each tap on Wave constructs another performance, so a second tap can replay the same gesture.
import SwiftUI
import Clayfaces
struct AvatarProfile: View {
@State private var look = AvatarRecipe()
@State private var performance: ClayfacesPerformance?
@State private var editing = false
var body: some View {
VStack {
ClayfacesView(appearance: look, performance: performance)
.aspectRatio(1, contentMode: .fit)
.frame(maxWidth: 320)
HStack {
Button("Wave") {
performance = ClayfacesPerformance("person_waving")
}
Button("Edit avatar") { editing = true }
}
}
.sheet(isPresented: $editing) {
ClayfacesEditor(appearance: $look)
}
.onChange(of: look) { performance = nil }
}
}Prepare the first frame
Once you restore a saved recipe, an earlier screen can call try await ClayfacesView.preload(appearance: look) in a task. Clayfaces prepares geometry and a matching neutral portrait. The view can show that saved portrait while the first live frame arrives. A new custom look may need its first render before a matching portrait exists.
To check the integration, change the glasses, tap Done and replay Wave. A square stage should show the entire gesture. The sample keeps the recipe in view state. To bring back the edited look after relaunch, persist the data returned by try look.encoded() and use AvatarRecipe.decode(_:) on your next launch.