Skip to content

Commit

Permalink
Add blendShapes and humanoid bone support to VRM + bugfixes (#55)
Browse files Browse the repository at this point in the history
* Add blendShapes support

* Add humanoid bone support

* Fix GLTFSceneSource initializer error handling

* Remove the submodule

* SPM: add workaround for Foundation.Bundle to enable swift previews (#51)

* SPM: add workaround for Foundation.Bundle to enable swift previews

* SPM: move shaders into Resources/ directory to make Xcode Previews happy

* Add TEXCOORD_2 and TEXCOORD_3 to GLTFTypes

* Fix the shader file references

* Change VRM meta data to optional

* Remove print() for debug

Co-authored-by: Andrew Breckenridge <asbreckenridge@me.com>
  • Loading branch information
magicien and AndrewSB authored Dec 13, 2021
1 parent e624d00 commit d702c3f
Show file tree
Hide file tree
Showing 19 changed files with 328 additions and 170 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
[submodule "schema"]
path = schema
url = https://github.com/KhronosGroup/glTF.git
148 changes: 88 additions & 60 deletions GLTFSceneKit.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ let package = Package(
dependencies: [],
exclude: ["Info.plist"],
resources: [
.copy("GLTFShaderModifierFragment_alphaCutoff.shader"),
.copy("GLTFShaderModifierSurface.shader"),
.copy("GLTFShaderModifierSurface_alphaModeBlend.shader"),
.copy("GLTFShaderModifierSurface_doubleSidedWorkaround.shader"),
.copy("schema/extensions/KHR_materials_pbrSpecularGlossiness/GLTFShaderModifierSurface_pbrSpecularGlossiness.shader"),
.copy("schema/extensions/KHR_materials_pbrSpecularGlossiness/GLTFShaderModifierSurface_pbrSpecularGlossiness_doubleSidedWorkaround.shader"),
.copy("schema/extensions/KHR_materials_pbrSpecularGlossiness/GLTFShaderModifierSurface_pbrSpecularGlossiness_texture_doubleSidedWorkaround.shader"),
.copy("schema/extensions/VRM/GLTFShaderModifierFragment_VRMUnlitTexture.shader"),
.copy("schema/extensions/VRM/GLTFShaderModifierSurface_VRMMToon.shader"),
.copy("Resources/GLTFShaderModifierFragment_alphaCutoff.shader"),
.copy("Resources/GLTFShaderModifierSurface.shader"),
.copy("Resources/GLTFShaderModifierSurface_alphaModeBlend.shader"),
.copy("Resources/GLTFShaderModifierSurface_doubleSidedWorkaround.shader"),
.copy("Resources/KHR_materials_pbrSpecularGlossiness/GLTFShaderModifierSurface_pbrSpecularGlossiness.shader"),
.copy("Resources/KHR_materials_pbrSpecularGlossiness/GLTFShaderModifierSurface_pbrSpecularGlossiness_doubleSidedWorkaround.shader"),
.copy("Resources/KHR_materials_pbrSpecularGlossiness/GLTFShaderModifierSurface_pbrSpecularGlossiness_texture_doubleSidedWorkaround.shader"),
.copy("Resources/VRM/GLTFShaderModifierFragment_VRMUnlitTexture.shader"),
.copy("Resources/VRM/GLTFShaderModifierSurface_VRMMToon.shader"),
]
),
]
Expand Down
68 changes: 68 additions & 0 deletions Sources/GLTFSceneKit/GLTFSceneKit+BundleFinder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// GLTFSceneKit+BundleFinder.swift
//
//
// Created by Andrew Breckenridge on 9/6/21.
//

/// required to enable Swift previews in Xcode 12: https://github.com/pointfreeco/isowords/discussions/104

import Foundation

#if !DEBUG
extension Bundle {
static var module_workaround: Bundle = {
#if SWIFT_PACKAGE
return .module
#else
return Bundle(for: GLTFUnarchiver.self)
#endif
}()
}
#else
private let moduleName = "GLTFSceneKit_GLTFSceneKit"

extension Foundation.Bundle {
static var module_workaround: Bundle = {
#if !SWIFT_PACKAGE
return Bundle(for: GLTFUnarchiver.self)
#else
/* The name of your local package, prepended by "LocalPackages_" for iOS and "PackageName_" for macOS. You may have same PackageName and TargetName*/
let bundleNameIOS = "LocalPackages_\(moduleName)"
let bundleNameMacOs = "PackageName_\(moduleName)"
let andrewSBBundleName = moduleName

let candidates = [
/* Bundle should be present here when the package is linked into an App. */
Bundle.main.resourceURL,
/* Bundle should be present here when the package is linked into a framework. */
Bundle(for: GLTFUnarchiver.self).resourceURL,
/* For command-line tools. */
Bundle.main.bundleURL,

// AndrewSB reverse engineered this one
Bundle(for: GLTFUnarchiver.self).resourceURL?.appendingPathComponent("Bundle").appendingPathComponent("Application"),

/* Bundle should be present here when running previews from a different package (this is the path to "…/Debug-iphonesimulator/"). */
Bundle(for: GLTFUnarchiver.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent(),
Bundle(for: GLTFUnarchiver.self).resourceURL?.deletingLastPathComponent().deletingLastPathComponent(),
]

for candidate in candidates {
let bundlePathiOS = candidate?.appendingPathComponent(bundleNameIOS + ".bundle")
let bundlePathMacOS = candidate?.appendingPathComponent(bundleNameMacOs + ".bundle")
let andrewSBBundlePath = candidate?.appendingPathComponent(andrewSBBundleName + ".bundle")

if let bundle = bundlePathiOS.flatMap(Bundle.init(url:)) {
return bundle
} else if let bundle = bundlePathMacOS.flatMap(Bundle.init(url:)) {
return bundle
} else if let bundle = andrewSBBundlePath.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle")
#endif
}()
}
#endif
15 changes: 11 additions & 4 deletions Sources/GLTFSceneKit/GLTFSceneSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import SceneKit

@objcMembers
public class GLTFSceneSource : SCNSceneSource {
private var loader: GLTFUnarchiver! = nil
private var loader: GLTFUnarchiver?
private var error: Error?

public override init() {
super.init()
Expand All @@ -33,7 +34,7 @@ public class GLTFSceneSource : SCNSceneSource {
do {
self.loader = try GLTFUnarchiver(url: url, extensions: extensions)
} catch {
print("\(error.localizedDescription)")
self.error = error
}
}

Expand All @@ -42,7 +43,7 @@ public class GLTFSceneSource : SCNSceneSource {
do {
self.loader = try GLTFUnarchiver(data: data)
} catch {
print("\(error.localizedDescription)")
self.error = error
}
}

Expand All @@ -55,7 +56,13 @@ public class GLTFSceneSource : SCNSceneSource {
}

public override func scene(options: [SCNSceneSource.LoadingOption : Any]? = nil) throws -> SCNScene {
let scene = try self.loader.loadScene()
guard let loader = self.loader else {
if let error = self.error {
throw error
}
throw GLTFUnarchiveError.Unknown("loader is not initialized")
}
let scene = try loader.loadScene()
#if SEEMS_TO_HAVE_SKINNER_VECTOR_TYPE_BUG
let sceneData = NSKeyedArchiver.archivedData(withRootObject: scene)
let source = SCNSceneSource(data: sceneData, options: nil)!
Expand Down
2 changes: 2 additions & 0 deletions Sources/GLTFSceneKit/GLTFTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ let attributeMap: [String: SCNGeometrySource.Semantic] = [
"TANGENT": SCNGeometrySource.Semantic.tangent,
"TEXCOORD_0": SCNGeometrySource.Semantic.texcoord,
"TEXCOORD_1": SCNGeometrySource.Semantic.texcoord,
"TEXCOORD_2": SCNGeometrySource.Semantic.texcoord,
"TEXCOORD_3": SCNGeometrySource.Semantic.texcoord,
"COLOR_0": SCNGeometrySource.Semantic.color,
"JOINTS_0": SCNGeometrySource.Semantic.boneIndices,
"JOINTS_1": SCNGeometrySource.Semantic.boneIndices,
Expand Down
Loading

0 comments on commit d702c3f

Please sign in to comment.