Skip to content

Commit 207d36f

Browse files
authored
Introduce VRMKitRuntime (#43)
* feat: Introduce VRMKitRuntime for common implementation * Clean code
1 parent 4e4f1c1 commit 207d36f

21 files changed

+114
-207
lines changed

Example/Example/RealityKitViewController.swift

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class RealityKitViewController: UIViewController, UIGestureRecognizerDeleg
1515
private var orbitPitch: Float = -0.1
1616
private var orbitDistance: Float = 2
1717
private var orbitTarget = SIMD3<Float>(0, 0.8, 0)
18-
private var currentExpression: RKExpression = .neutral
18+
private var currentExpression: Expression = .neutral
1919

2020
override func viewDidLoad() {
2121
super.viewDidLoad()
@@ -52,7 +52,7 @@ final class RealityKitViewController: UIViewController, UIGestureRecognizerDeleg
5252
segmentedControl.translatesAutoresizingMaskIntoConstraints = false
5353
view.addSubview(segmentedControl)
5454

55-
let expressionItems = RKExpression.allCases.map { $0.displayName }
55+
let expressionItems = Expression.allCases.map { $0.displayName }
5656
let expressionSegmentedControl = UISegmentedControl(items: expressionItems)
5757
expressionSegmentedControl.selectedSegmentIndex = 0
5858
expressionSegmentedControl.addTarget(self, action: #selector(expressionSegmentChanged(_:)), for: .valueChanged)
@@ -73,7 +73,7 @@ final class RealityKitViewController: UIViewController, UIGestureRecognizerDeleg
7373
}
7474

7575
@objc private func expressionSegmentChanged(_ sender: UISegmentedControl) {
76-
let expression = RKExpression.allCases[sender.selectedSegmentIndex]
76+
let expression = Expression.allCases[sender.selectedSegmentIndex]
7777
loadedEntity?.setBlendShape(value: 0.0, for: .preset(currentExpression.preset))
7878
currentExpression = expression
7979
loadedEntity?.setBlendShape(value: 1.0, for: .preset(currentExpression.preset))
@@ -249,22 +249,3 @@ final class RealityKitViewController: UIViewController, UIGestureRecognizerDeleg
249249
return true
250250
}
251251
}
252-
253-
@available(iOS 18.0, *)
254-
private enum RKExpression: String, CaseIterable {
255-
case neutral, joy, angry, sorrow, fun
256-
257-
var preset: BlendShapePreset {
258-
switch self {
259-
case .neutral: return .neutral
260-
case .joy: return .joy
261-
case .angry: return .angry
262-
case .sorrow: return .sorrow
263-
case .fun: return .fun
264-
}
265-
}
266-
267-
var displayName: String {
268-
return rawValue.capitalized
269-
}
270-
}

Package.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ let package = Package(
1212
targets: [
1313
.target(name: "VRMKit"),
1414
.target(
15-
name: "VRMSceneKit",
15+
name: "VRMKitRuntime",
1616
dependencies: ["VRMKit"]
1717
),
18+
.target(
19+
name: "VRMSceneKit",
20+
dependencies: ["VRMKit", "VRMKitRuntime"]
21+
),
1822
.target(
1923
name: "VRMRealityKit",
20-
dependencies: ["VRMKit"]
24+
dependencies: ["VRMKit", "VRMKitRuntime"]
2125
),
2226

2327
.testTarget(
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public enum BlendShapeKey: Hashable {
2+
case preset(BlendShapePreset)
3+
case custom(String)
4+
5+
public var isPreset: Bool {
6+
switch self {
7+
case .preset: return true
8+
case .custom: return false
9+
}
10+
}
11+
}
12+
13+
/// VRM 0.x Blend Shape Preset
14+
public enum BlendShapePreset: String {
15+
case unknown
16+
case neutral
17+
case a
18+
case i
19+
case u
20+
case e
21+
case o
22+
case blink
23+
case joy
24+
case angry
25+
case sorrow
26+
case fun
27+
case lookUp = "lookup"
28+
case lookDown = "lookdown"
29+
case lookLeft = "lookleft"
30+
case lookRight = "lookright"
31+
case blinkL = "blink_l"
32+
case blinkR = "blink_r"
33+
34+
package init(name: String) {
35+
self = BlendShapePreset(rawValue: name.lowercased()) ?? .unknown
36+
}
37+
}

Sources/VRMRealityKit/Extensions/GLTF+.swift renamed to Sources/VRMKitRuntime/GLTF+Runtime.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import VRMKit
22

3-
protocol GLTFTextureInfoProtocol {
3+
package protocol GLTFTextureInfoProtocol {
44
var index: Int { get }
55
var texCoord: Int { get }
66
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import simd
2+
3+
public extension SIMD3 where Scalar == Float {
4+
var normalized: SIMD3 {
5+
simd_normalize(self)
6+
}
7+
8+
var length: Scalar {
9+
simd_length(self)
10+
}
11+
12+
var length_squared: Scalar {
13+
simd_length_squared(self)
14+
}
15+
16+
mutating func normalize() {
17+
self = normalized
18+
}
19+
}
20+
21+
public extension simd_quatf {
22+
static func * (_ left: simd_quatf, _ right: SIMD3<Float>) -> SIMD3<Float> {
23+
simd_act(left, right)
24+
}
25+
}
26+
27+
public let quat_identity_float = simd_quatf(matrix_identity_float4x4)
28+
29+
public func cross(_ left: SIMD3<Float>, _ right: SIMD3<Float>) -> SIMD3<Float> {
30+
simd_cross(left, right)
31+
}
32+
33+
public func normal(_ v0: SIMD3<Float>, _ v1: SIMD3<Float>, _ v2: SIMD3<Float>) -> SIMD3<Float> {
34+
let e1 = v1 - v0
35+
let e2 = v2 - v0
36+
return simd_normalize(simd_cross(e1, e2))
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public protocol UnityTransformCompatible {
2+
associatedtype CompatibleType
3+
var utx: CompatibleType { get }
4+
}
5+
6+
public final class UnityTransform<Base> {
7+
package let base: Base
8+
9+
public init(_ base: Base) {
10+
self.base = base
11+
}
12+
}
13+
14+
public extension UnityTransformCompatible {
15+
var utx: UnityTransform<Self> {
16+
UnityTransform(self)
17+
}
18+
}
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if canImport(RealityKit)
22
import RealityKit
3+
import VRMKitRuntime
34

45
struct BlendShapeClip {
56
let name: String
@@ -23,40 +24,4 @@ struct MaterialValueBinding {
2324
let targetValue: SIMD4<Float>
2425
let baseValue: SIMD4<Float>
2526
}
26-
27-
public enum BlendShapeKey: Hashable {
28-
case preset(BlendShapePreset)
29-
case custom(String)
30-
var isPreset: Bool {
31-
switch self {
32-
case .preset: return true
33-
case .custom: return false
34-
}
35-
}
36-
}
37-
38-
public enum BlendShapePreset: String {
39-
case unknown
40-
case neutral
41-
case a
42-
case i
43-
case u
44-
case e
45-
case o
46-
case blink
47-
case joy
48-
case angry
49-
case sorrow
50-
case fun
51-
case lookUp = "lookup"
52-
case lookDown = "lookdown"
53-
case lookLeft = "lookleft"
54-
case lookRight = "lookright"
55-
case blinkL = "blink_l"
56-
case blinkR = "blink_r"
57-
58-
init(name: String) {
59-
self = BlendShapePreset(rawValue: name.lowercased()) ?? .unknown
60-
}
61-
}
6227
#endif

Sources/VRMRealityKit/CustomType/VRMEntity.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import CoreGraphics
33
import Foundation
44
import RealityKit
55
import VRMKit
6+
import VRMKitRuntime
67

78
@available(iOS 18.0, macOS 15.0, visionOS 2.0, *)
89
struct BlendShapeNormalTangentComponent: Component {
@@ -360,4 +361,3 @@ public final class VRMEntity {
360361
}
361362
}
362363
#endif
363-

Sources/VRMRealityKit/CustomType/VRMEntitySpringBone.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#if canImport(RealityKit)
22
import RealityKit
33
import VRMKit
4+
import VRMKitRuntime
45
import Foundation
56

67
@available(iOS 18.0, macOS 15.0, visionOS 2.0, *)

Sources/VRMRealityKit/Extensions/Entity+UnityTransform.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
#if canImport(RealityKit)
22
import RealityKit
33
import simd
4-
5-
public protocol UnityTransformCompatible {
6-
associatedtype CompatibleType
7-
var utx: CompatibleType { get }
8-
}
9-
10-
public final class UnityTransform<Base> {
11-
private let base: Base
12-
public init(_ base: Base) {
13-
self.base = base
14-
}
15-
}
16-
17-
public extension UnityTransformCompatible {
18-
var utx: UnityTransform<Self> {
19-
UnityTransform(self)
20-
}
21-
}
4+
import VRMKitRuntime
225

236
extension Entity: UnityTransformCompatible {}
247

0 commit comments

Comments
 (0)