forked from tattn/VRMKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
150 lines (127 loc) · 4.83 KB
/
ContentView.swift
File metadata and controls
150 lines (127 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// ContentView.swift
// MacExample
//
// Created by tattn on 2026/01/26.
// Copyright © 2026 tattn. All rights reserved.
//
import SwiftUI
import RealityKit
internal import VRMRealityKit
internal import Combine
struct ContentView: View {
@State private var viewModel = ContentViewModel()
@State private var selectedModel: MacExampleModel = .alicia
var body: some View {
VStack {
Picker("Model", selection: $selectedModel) {
ForEach(MacExampleModel.allCases) { model in
Text(model.displayName).tag(model)
}
}
.pickerStyle(.segmented)
.padding([.top, .horizontal])
RealityView { content in
content.add(viewModel.rootEntity)
}
.task(id: selectedModel) {
await viewModel.loadEntity(model: selectedModel)
}
.onReceive(viewModel.updateTimer) { _ in
viewModel.update()
}
if let errorMessage = viewModel.errorMessage {
Text("Error: \(errorMessage)")
.foregroundColor(.red)
.padding()
}
}
.frame(minWidth: 800, minHeight: 600)
}
}
@MainActor
@Observable
final class ContentViewModel {
let rootEntity = Entity()
private(set) var errorMessage: String?
private var vrmEntity: VRMEntity?
private var time: TimeInterval = 0
private var lastUpdateTime: Date?
private var currentModel: MacExampleModel = .alicia
let updateTimer = Timer.publish(every: 1.0 / 60.0, on: .main, in: .common).autoconnect()
func loadEntity(model: MacExampleModel) async {
do {
if let vrmEntity {
vrmEntity.entity.removeFromParent()
self.vrmEntity = nil
}
let loader = try VRMEntityLoader(named: model.rawValue)
let vrmEntity = try loader.loadEntity()
vrmEntity.entity.transform.translation = SIMD3<Float>(0, -1, 0)
vrmEntity.entity.transform.rotation = simd_quatf(angle: model.initialRotation, axis: SIMD3<Float>(0, 1, 0))
rootEntity.addChild(vrmEntity.entity)
// Adjust pose
let neck = vrmEntity.humanoid.node(for: .neck)
let leftShoulder = vrmEntity.humanoid.node(for: .leftShoulder) ?? vrmEntity.humanoid.node(for: .leftUpperArm)
let rightShoulder = vrmEntity.humanoid.node(for: .rightShoulder) ?? vrmEntity.humanoid.node(for: .rightUpperArm)
let neckRotation = simd_quatf(angle: 20 * .pi / 180, axis: SIMD3<Float>(0, 0, 1))
let shoulderRotation = simd_quatf(angle: 40 * .pi / 180, axis: SIMD3<Float>(0, 0, 1))
if let neck {
neck.transform.rotation = neck.transform.rotation * neckRotation
}
if let leftShoulder {
leftShoulder.transform.rotation = leftShoulder.transform.rotation * shoulderRotation
}
if let rightShoulder {
rightShoulder.transform.rotation = rightShoulder.transform.rotation * shoulderRotation
}
vrmEntity.setBlendShape(value: 1.0, for: .custom("><"))
self.vrmEntity = vrmEntity
self.currentModel = model
self.lastUpdateTime = Date()
} catch {
errorMessage = error.localizedDescription
print("VRM Load Error: \(error)")
}
}
func update() {
guard let vrmEntity else { return }
let now = Date()
let deltaTime = lastUpdateTime.map { now.timeIntervalSince($0) } ?? (1.0 / 60.0)
lastUpdateTime = now
time += deltaTime
// An animation that sways left and right
let cycle = time.truncatingRemainder(dividingBy: 1.0)
let angle: Float
if cycle < 0.5 {
let progress = Float(cycle) / 0.5
angle = -0.5 * progress
} else {
let progress = Float(cycle - 0.5) / 0.5
angle = -0.5 + 0.5 * progress
}
vrmEntity.entity.transform.rotation = simd_quatf(angle: currentModel.initialRotation + angle,
axis: SIMD3<Float>(0, 1, 0))
vrmEntity.update(at: deltaTime)
}
}
enum MacExampleModel: String, CaseIterable, Identifiable {
case alicia = "AliciaSolid.vrm"
case vrm1 = "VRM1_Constraint_Twist_Sample.vrm"
var id: String { rawValue }
var displayName: String {
switch self {
case .alicia: return "Alicia"
case .vrm1: return "VRM 1.0"
}
}
var initialRotation: Float {
switch self {
case .alicia: return .pi
case .vrm1: return 0
}
}
}
#Preview {
ContentView()
}