Skip to content

Commit

Permalink
Merge pull request #29 from is0263rf/support-vrm1
Browse files Browse the repository at this point in the history
VRM 1.0 parsing support
  • Loading branch information
tattn authored Apr 18, 2024
2 parents 053a612 + cd2213d commit 9f5c247
Show file tree
Hide file tree
Showing 13 changed files with 1,716 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ let package = Package(
.testTarget(
name: "VRMKitTests",
dependencies: ["VRMKit"],
resources: [.copy("Assets/AliciaSolid.vrm")]
resources: [.copy("Assets/AliciaSolid.vrm"), .copy("Assets/Seed-san.vrm")]
),
.testTarget(
name: "VRMSceneKitTests",
dependencies: ["VRMSceneKit"],
resources: [.copy("Assets/AliciaSolid.vrm")]
resources: [.copy("Assets/AliciaSolid.vrm"), .copy("Assets/Seed-san.vrm")]
),
]
)
1 change: 1 addition & 0 deletions Sources/VRMKit/Extensions/GlobalFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ func ???<T>(lhs: T?,
guard let value = lhs else { throw error() }
return value
}

61 changes: 60 additions & 1 deletion Sources/VRMKit/VRM/Material.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Foundation
extension GLTF {
public struct Material: Codable {
public let name: String?
public let extensions: CodableAny?
public let extensions: MaterialExtensions?
public let extras: CodableAny?
public let pbrMetallicRoughness: PbrMetallicRoughness?
public let normalTexture: NormalTextureInfo?
Expand Down Expand Up @@ -102,5 +102,64 @@ extension GLTF {
case MASK
case BLEND
}

public struct MaterialExtensions: Codable {
public let materialsMToon: MaterialsMToon?

private enum CodingKeys: String, CodingKey {
case materialsMToon = "VRMC_materials_mtoon"
}

public struct MaterialsMToon: Codable {
public let specVersion: String
public let transparentWithZWrite: Bool?
public let renderQueueOffsetNumber: Int?
public let shadeColorFactor: [Double]?
public let shadeMultiplyTexture: MaterialsMToonTextureInfo?
public let shadingShiftFactor: Double?
public let shadingShiftTexture: MaterialsMToonShadingShiftTexture?
public let shadingToonyFactor: Double?
public let giEqualizationFactor: Double?
public let matcapFactor: [Double]?
public let matcapTexture: MaterialsMToonTextureInfo?
public let parametricRimColorFactor: [Double]?
public let rimMultiplyTexture: MaterialsMToonTextureInfo?
public let rimLightingMixFactor: Double?
public let parametricRimFresnelPowerFactor: Double?
public let parametricRimLiftFactor: Double?
public let outlineWidthMode: MaterialsMToonOutlineWidthMode?
public let outlineWidthFactor: Double?
public let outlineWidthMultiplyTexture: MaterialsMToonTextureInfo?
public let outlineColorFactor: [Double]?
public let outlineLightingMixFactor: Double?
public let uvAnimationMaskTexture: MaterialsMToonTextureInfo?
public let uvAnimationScrollXSpeedFactor: Double?
public let uvAnimationScrollYSpeedFactor: Double?
public let uvAnimationRotationSpeedFactor: Double?
public let extensions: CodableAny?
public let extras: CodableAny?

public struct MaterialsMToonTextureInfo: Codable {
public let index: Int
public let texCoord: Int?
public let extensions: CodableAny?
public let extras: CodableAny?
}

public struct MaterialsMToonShadingShiftTexture: Codable {
public let index: Int
public let texCoord: Int?
public let scale: Double?
public let extensions: CodableAny?
public let extras: CodableAny?
}

public enum MaterialsMToonOutlineWidthMode: String, Codable {
case none
case worldCoordinates
case screenCoordinates
}
}
}
}
}
78 changes: 77 additions & 1 deletion Sources/VRMKit/VRM/Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension GLTF {
}
public let weights: [Float]?
public let name: String?
public let extensions: CodableAny?
public let extensions: NodeExtensions?
public let extras: CodableAny?

private enum CodingKeys: String, CodingKey {
Expand All @@ -51,5 +51,81 @@ extension GLTF {
case extensions
case extras
}

public struct NodeExtensions: Codable {
public let nodeConstraint: NodeConstraint?

private enum CodingKeys: String, CodingKey {
case nodeConstraint = "VRMC_node_constraint"
}

public struct NodeConstraint: Codable {
public let specVersion: String
public let constraint: Constraint
public let extensions: CodableAny?
public let extras: CodableAny?

public struct Constraint: Codable {
public let roll: RollConstraint?
public let aim: AimConstraint?
public let rotation: RotationConstraint?
public let extensions: CodableAny?
public let extras: CodableAny?

public struct RollConstraint: Codable {
public let source: Int
public let rollAxis: RollAxis
public let weight: Double?
public let extensions: CodableAny?
public let extras: CodableAny?

public enum RollAxis: String, Codable {
case x
case y
case z

private enum CodingKeys: String, CodingKey {
case x = "X"
case y = "Y"
case z = "Z"
}
}
}

public struct AimConstraint: Codable {
public let source: Int
public let aimAxis: AimAxis
public let weight: Double?
public let extensions: CodableAny?
public let extras: CodableAny?

public enum AimAxis: String, Codable {
case positiveX
case negativeX
case positiveY
case negativeY
case positiveZ
case negativeZ

private enum CodingKeys: String, CodingKey {
case positiveX = "PositiveX"
case negativeX = "NegativeX"
case positiveY = "PositiveY"
case negativeY = "NegativeY"
case positiveZ = "PositiveZ"
case negativeZ = "NegativeZ"
}
}
}

public struct RotationConstraint: Codable {
public let source: Int
public let weight: Double?
public let extensions: CodableAny?
public let extras: CodableAny?
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion Sources/VRMKit/VRM/VRM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

public struct VRM {
public struct VRM: VRMFile {
public let gltf: BinaryGLTF
public let meta: Meta
public let version: String?
Expand Down
Loading

0 comments on commit 9f5c247

Please sign in to comment.