Skip to content

Commit

Permalink
Merge pull request #5 from tattn/swift5
Browse files Browse the repository at this point in the history
Swift5
  • Loading branch information
tattn committed Apr 22, 2019
2 parents e497a9f + 40df3ec commit 3b76840
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2
5.0
12 changes: 6 additions & 6 deletions Sources/VRMKit/BinaryGLTF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public struct BinaryGLTF {
extension BinaryGLTF {
public init(data: Data) throws {
var offset = MemoryLayout<UInt32>.size // skip `magic`
let rawVersion: UInt32 = read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let rawVersion: UInt32 = try read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
guard let version = GLTF.Version(rawValue: rawVersion), version == .two else {
throw VRMError.notSupportedVersion(rawVersion)
}
self.version = version

let length: UInt32 = read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let chunk0Length: UInt32 = read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let chunk0Type: UInt32 = read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let length: UInt32 = try read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let chunk0Length: UInt32 = try read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let chunk0Type: UInt32 = try read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
guard ChunkType(rawValue: chunk0Type) == .json else {
throw VRMError.notSupportedChunkType(chunk0Type)
}
Expand All @@ -45,8 +45,8 @@ extension BinaryGLTF {
self.jsonData = try decoder.decode(GLTF.self, from: jsonData)

if length > offset {
let chunk1Length: UInt32 = read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let chunk1Type: UInt32 = read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let chunk1Length: UInt32 = try read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
let chunk1Type: UInt32 = try read(data, offset: &offset, size: MemoryLayout<UInt32>.size)
guard ChunkType(rawValue: chunk1Type) == .bin else {
throw VRMError.notSupportedChunkType(chunk1Type)
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/VRMKit/Extensions/GlobalFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

import Foundation

func read<T>(_ data: Data, offset: inout Int, size: Int) -> T {
func read<T>(_ data: Data, offset: inout Int, size: Int) throws -> T {
defer { offset += size }
return data.subdata(in: offset..<(offset+size)).withUnsafeBytes { $0.pointee }
return try data.subdata(in: offset..<(offset+size)).withUnsafeBytes {
try $0.bindMemory(to: T.self).baseAddress?.pointee ??? VRMError.dataInconsistent("failed to read data")
}
}

func read(_ data: Data, offset: inout Int, size: Int) -> Data {
Expand Down
14 changes: 7 additions & 7 deletions Sources/VRMKit/VRM/VRM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct VRM {
}

public extension VRM {
public struct Meta: Codable {
struct Meta: Codable {
public let title: String?
public let author: String?
public let contactInformation: String?
Expand All @@ -59,7 +59,7 @@ public extension VRM {
public let otherLicenseUrl: String?
}

public struct MaterialProperty: Codable {
struct MaterialProperty: Codable {
public let name: String
public let shader: String
public let renderQueue: Int
Expand All @@ -70,7 +70,7 @@ public extension VRM {
public let vectorProperties: CodableAny
}

public struct Humanoid: Codable {
struct Humanoid: Codable {
public let armStretch: Double
public let feetSpacing: Double
public let hasTranslationDoF: Bool
Expand Down Expand Up @@ -101,7 +101,7 @@ public extension VRM {
}
}

public struct BlendShapeMaster: Codable {
struct BlendShapeMaster: Codable {
public let blendShapeGroups: [BlendShapeGroup]
public struct BlendShapeGroup: Codable {
public let binds: [Bind]?
Expand Down Expand Up @@ -137,7 +137,7 @@ public extension VRM {
}
}

public struct FirstPerson: Codable {
struct FirstPerson: Codable {
public let firstPersonBone: Int
public let firstPersonBoneOffset: Vector3
public let meshAnnotations: [MeshAnnotation]
Expand All @@ -147,7 +147,7 @@ public extension VRM {
}
}

public struct SecondaryAnimation: Codable {
struct SecondaryAnimation: Codable {
public let boneGroups: [BoneGroup]
public struct BoneGroup: Codable {
public let bones: [Int]
Expand Down Expand Up @@ -175,7 +175,7 @@ public extension VRM {
}
}

public struct Vector3: Codable {
struct Vector3: Codable {
public let x, y, z: Double
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand Down
4 changes: 2 additions & 2 deletions Sources/VRMKit/VRM/VRMExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import Foundation

public extension VRM.MaterialProperty {
public var vrmShader: Shader? {
var vrmShader: Shader? {
return Shader(rawValue: shader)
}

public enum Shader: String {
enum Shader: String {
case mToon = "VRM/MToon"
case unlitTransparent = "VRM/UnlitTransparent"
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/VRMSceneKit/GLTF2SCN/Data+GLTF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ extension Data {

var indexData = Data(capacity: dataSize)

indexData.withUnsafeMutableBytes { (dst: UnsafeMutablePointer<UInt8>) in
withUnsafeBytes { (src: UnsafePointer<UInt8>) in
indexData.withUnsafeMutableBytes { rawDst in
guard let dst = rawDst.bindMemory(to: UInt8.self).baseAddress else { return }
withUnsafeBytes { rawSrc in
guard let src = rawSrc.bindMemory(to: UInt8.self).baseAddress else { return }
for pos in 0..<count {
let srcPos = stride * pos + offset
let dstPos = size * pos
Expand Down
1 change: 1 addition & 0 deletions Sources/VRMSceneKit/GLTF2SCN/GLTF2SCN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extension SCNGeometryPrimitiveType {
case .polygon: return count - 2
case .triangles: return count / 3
case .triangleStrip: return count - 2
@unknown default: fatalError()
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/VRMSceneKit/GLTF2SCN/InverseBindMatrix+GLTF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ extension Array where Element == InverseBindMatrix {
var matrices: [InverseBindMatrix] = []
matrices.reserveCapacity(accessor.count)

try bufferView.withUnsafeBytes { (pointer: UnsafePointer<UInt8>) in
try bufferView.withUnsafeBytes { rawPointer in
guard let pointer = rawPointer.bindMemory(to: UInt8.self).baseAddress else { return }
var ptr = pointer.advanced(by: accessor.byteOffset)
for _ in 0..<accessor.count {
let rawPtr = UnsafeRawPointer(ptr)
Expand Down
12 changes: 7 additions & 5 deletions Sources/VRMSceneKit/GLTF2SCN/SCNNode+GLTF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ private extension SCNGeometrySource {

var vertices: [SCNVector3] = []
vertices.reserveCapacity(vectorCount)
data.withUnsafeBytes { (ptr: UnsafePointer<Float32>) in
data.withUnsafeBytes { rawPtr in
guard let ptr = rawPtr.bindMemory(to: Float32.self).baseAddress else { return }
var index = dataOffset / bytesPerComponent
let step = dataStride / bytesPerComponent
for _ in 0..<vectorCount {
Expand All @@ -169,7 +170,8 @@ private extension SCNGeometryElement {
var indices: [Int] = []
indices.reserveCapacity(indexCount)

func createIndices<T: UnsignedInteger>(ptr: UnsafePointer<T>) {
func createIndices<T: UnsignedInteger>(_ type: T.Type = T.self, rawPtr: UnsafeRawBufferPointer) {
guard let ptr = rawPtr.bindMemory(to: T.self).baseAddress else { return }
for i in 0..<indexCount {
indices.append(Int(ptr[i]))
}
Expand All @@ -179,11 +181,11 @@ private extension SCNGeometryElement {

switch bytesPerIndex {
case MemoryLayout<UInt16>.size:
data.withUnsafeBytes(createIndices as Func<UInt16>)
data.withUnsafeBytes { createIndices(UInt16.self, rawPtr: $0) }
case MemoryLayout<UInt32>.size:
data.withUnsafeBytes(createIndices as Func<UInt32>)
data.withUnsafeBytes { createIndices(UInt32.self, rawPtr: $0) }
case MemoryLayout<UInt64>.size:
data.withUnsafeBytes(createIndices as Func<UInt64>)
data.withUnsafeBytes { createIndices(UInt64.self, rawPtr: $0) }
default: ()
}

Expand Down
2 changes: 1 addition & 1 deletion VRMKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'VRMKit'
s.version = '0.2.5'
s.version = '0.3.0'
s.summary = 'VRM loader and VRM renderer'

s.description = <<-DESC
Expand Down
29 changes: 15 additions & 14 deletions VRMKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -603,22 +603,23 @@
TargetAttributes = {
245725922146EF99003AA5D7 = {
CreatedOnToolsVersion = 9.4.1;
LastSwiftMigration = 1010;
LastSwiftMigration = 1020;
};
2457259A2146EF99003AA5D7 = {
CreatedOnToolsVersion = 9.4.1;
LastSwiftMigration = 1010;
LastSwiftMigration = 1020;
};
245725D62146F47A003AA5D7 = {
CreatedOnToolsVersion = 9.4.1;
LastSwiftMigration = 1010;
LastSwiftMigration = 1020;
};
24CF7F7A2143EEDB007A5C6C = {
CreatedOnToolsVersion = 9.4.1;
LastSwiftMigration = 1010;
LastSwiftMigration = 1020;
};
24CF7F832143EEDB007A5C6C = {
CreatedOnToolsVersion = 9.4.1;
LastSwiftMigration = 1020;
};
};
};
Expand Down Expand Up @@ -855,7 +856,7 @@
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -881,7 +882,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMSceneKit;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -899,7 +900,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMSceneKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -917,7 +918,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMSceneKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -936,7 +937,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -955,7 +956,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMExample;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down Expand Up @@ -1102,7 +1103,7 @@
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -1128,7 +1129,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMKit;
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -1146,7 +1147,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -1164,7 +1165,7 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.github.tattn.VRMKitTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand Down
2 changes: 1 addition & 1 deletion VRMSceneKit.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'VRMSceneKit'
s.version = '0.2.5'
s.version = '0.3.0'
s.summary = 'VRM loader and VRM renderer'

s.description = <<-DESC
Expand Down

0 comments on commit 3b76840

Please sign in to comment.