Skip to content

Commit

Permalink
fix PMMReader accessory data length
Browse files Browse the repository at this point in the history
  • Loading branch information
magicien committed May 30, 2017
1 parent 5243d12 commit 5105edb
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 92 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ Carthage/Build
# etc
Framework/
*~
*.swp

14 changes: 13 additions & 1 deletion MMDSceneKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
DD6296F01C2FD49400707364 /* MMDShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = DD6296EF1C2FD49400707364 /* MMDShader.metal */; };
DD6296F11C2FD49400707364 /* MMDShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = DD6296EF1C2FD49400707364 /* MMDShader.metal */; };
DD6296F41C2FF21E00707364 /* EmptyShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = DD6296F31C2FF21E00707364 /* EmptyShader.metal */; };
DD8A4A111E26CD380047995C /* CommonTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A4A101E26CD380047995C /* CommonTypes.swift */; };
DD8A4A121E26CD380047995C /* CommonTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A4A101E26CD380047995C /* CommonTypes.swift */; };
DD8A4A131E26CD380047995C /* CommonTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A4A101E26CD380047995C /* CommonTypes.swift */; };
DD8A4A141E26CD380047995C /* CommonTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD8A4A101E26CD380047995C /* CommonTypes.swift */; };
DD97A4921C1E29C500127FC5 /* MMDSceneSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD12BF151C18361400B20F37 /* MMDSceneSource.swift */; };
DD9ECF7A1DE96F86007D3989 /* MMDVACReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9ECF791DE96F85007D3989 /* MMDVACReader.swift */; };
DD9ECF7B1DE96F86007D3989 /* MMDVACReader.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9ECF791DE96F85007D3989 /* MMDVACReader.swift */; };
Expand Down Expand Up @@ -160,6 +164,7 @@
DDCF5D2D1DD596B80037BD66 /* MMDIKController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDCF5D2A1DD596B80037BD66 /* MMDIKController.swift */; };
DDCF5D2E1DD596B80037BD66 /* MMDIKController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDCF5D2A1DD596B80037BD66 /* MMDIKController.swift */; };
DDCF5D2F1DD596B80037BD66 /* MMDIKController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDCF5D2A1DD596B80037BD66 /* MMDIKController.swift */; };
DDD2AE791E2EA1C500B988EA /* MMDShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = DD6296EF1C2FD49400707364 /* MMDShader.metal */; };
DDDE8A3E1C27CE76008DF1A6 /* MMDIKConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDE8A3D1C27CE76008DF1A6 /* MMDIKConstraint.swift */; };
DDDE8A3F1C27CE76008DF1A6 /* MMDIKConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDDE8A3D1C27CE76008DF1A6 /* MMDIKConstraint.swift */; };
DDE586CE1C7123D900CF3B48 /* MMDSceneViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDE586CB1C7123CE00CF3B48 /* MMDSceneViewController.swift */; };
Expand Down Expand Up @@ -388,6 +393,7 @@
DD6296D81C2D120800707364 /* MMDProgram.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MMDProgram.swift; sourceTree = "<group>"; };
DD6296EF1C2FD49400707364 /* MMDShader.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = MMDShader.metal; sourceTree = "<group>"; };
DD6296F31C2FF21E00707364 /* EmptyShader.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = EmptyShader.metal; sourceTree = "<group>"; };
DD8A4A101E26CD380047995C /* CommonTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommonTypes.swift; sourceTree = "<group>"; };
DD9ECF791DE96F85007D3989 /* MMDVACReader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MMDVACReader.swift; sourceTree = "<group>"; };
DDBFF9711DD3A81400E4CE78 /* MMDSceneKitSample_watchOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MMDSceneKitSample_watchOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
DDBFF9741DD3A81400E4CE78 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Interface.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -584,6 +590,7 @@
DD370BC11DDA589700F55DF0 /* MMDCameraNode.swift */,
DD12BF151C18361400B20F37 /* MMDSceneSource.swift */,
DD41D8961C23F42500A7F834 /* MMDReader.swift */,
DD8A4A101E26CD380047995C /* CommonTypes.swift */,
DD41D8991C23F93100A7F834 /* MMDPMDReader.swift */,
DDEC59891C38B4570043F939 /* MMDPMXReader.swift */,
DD41D89C1C23FAE700A7F834 /* MMDVMDReader.swift */,
Expand Down Expand Up @@ -1552,6 +1559,7 @@
DD12C00D1C1ABF5600B20F37 /* MMDNode.swift in Sources */,
DD41D89A1C23F93100A7F834 /* MMDPMDReader.swift in Sources */,
DD6296F01C2FD49400707364 /* MMDShader.metal in Sources */,
DD8A4A111E26CD380047995C /* CommonTypes.swift in Sources */,
DDCF5D2B1DD596B80037BD66 /* MMDIKController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1586,6 +1594,7 @@
DDAE3ACA1C1D41520014F375 /* MMDSceneSource.swift in Sources */,
DD41D89B1C23F93100A7F834 /* MMDPMDReader.swift in Sources */,
DD6296F11C2FD49400707364 /* MMDShader.metal in Sources */,
DD8A4A121E26CD380047995C /* CommonTypes.swift in Sources */,
DDCF5D2C1DD596B80037BD66 /* MMDIKController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1623,6 +1632,7 @@
DDE586CF1C7123DB00CF3B48 /* MMDSceneViewController.swift in Sources */,
DD6296F41C2FF21E00707364 /* EmptyShader.metal in Sources */,
DD12BFE71C1AB85800B20F37 /* GameView.swift in Sources */,
DDD2AE791E2EA1C500B988EA /* MMDShader.metal in Sources */,
DD12BFE91C1AB85800B20F37 /* GameViewController.swift in Sources */,
DD12BFE51C1AB85800B20F37 /* AppDelegate.swift in Sources */,
);
Expand Down Expand Up @@ -1657,6 +1667,7 @@
DD3313C51D39C32A0027FCA9 /* MMDShader.metal in Sources */,
DD3313BF1D39C3160027FCA9 /* MMDVMDReader.swift in Sources */,
DD3313C31D39C3160027FCA9 /* MMDProgram.swift in Sources */,
DD8A4A131E26CD380047995C /* CommonTypes.swift in Sources */,
DDCF5D2D1DD596B80037BD66 /* MMDIKController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -1692,6 +1703,7 @@
DDCF5C631DD4F3830037BD66 /* MMDShader.metal in Sources */,
DDCF5C571DD4F3720037BD66 /* MMDVMDReader.swift in Sources */,
DDCF5C5B1DD4F3720037BD66 /* MMDProgram.swift in Sources */,
DD8A4A141E26CD380047995C /* CommonTypes.swift in Sources */,
DDCF5D2E1DD596B80037BD66 /* MMDIKController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -2060,7 +2072,7 @@
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
ENABLE_BITCODE = NO;
ENABLE_BITCODE = YES;
INFOPLIST_FILE = "$(SRCROOT)/Source/iOS/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
Expand Down
36 changes: 36 additions & 0 deletions Source/Common/CommonTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// CommonTypes.swift
// MMDSceneKit
//
// Created by magicien on 1/11/17.
// Copyright © 2017 DarkHorse. All rights reserved.
//

import Foundation

#if os(iOS) || os(tvOS) || os(watchOS)
import UIKit
#elseif os(OSX)
import AppKit
#endif

#if os(iOS) || os(tvOS) || os(watchOS)
public typealias OSFloat = Float
public typealias OSColor = UIColor
public typealias OSImage = UIImage

extension CGColor {
static public let black = UIColor.black.cgColor
static public let white = UIColor.white.cgColor
static public let clear = UIColor.clear.cgColor
}
#elseif os(macOS)
public typealias OSFloat = CGFloat
public typealias OSColor = NSColor
public typealias OSImage = NSImage
#endif

#if os(watchOS)
public typealias CAAnimation = Any
public typealias CAAnimationGroup = Any
#endif
44 changes: 33 additions & 11 deletions Source/Common/MMDNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private let MMDAnimationCompletionBlockKey = "MMDAnimationCompletionBlockKey"
}
*/

open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
open class MMDNode: SCNNode, MMDNodeProgramDelegate {
open internal(set) var physicsBehaviors: [SCNPhysicsBehavior]! = []
open internal(set) var type: MMDNodeType = .unknown
open internal(set) var isKnee: Bool = false
Expand Down Expand Up @@ -219,7 +219,9 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
self.renderingOrder = node.renderingOrder
self.castsShadow = node.castsShadow
self.movabilityHint = node.movabilityHint
#if os(macOS) || os(iOS) || os(tvOS)
self.filters = node.filters
#endif
self.rendererDelegate = node.rendererDelegate

if let body = node.physicsBody {
Expand Down Expand Up @@ -778,7 +780,7 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
}
}

func getRootNode() -> SCNNode {
public func getRootNode() -> SCNNode {
var node: SCNNode = self
while let parentNode = node.parent {
node = parentNode
Expand Down Expand Up @@ -999,6 +1001,7 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
return ans
}

#if os(macOS) || os(iOS) || os(tvOS)
// TODO: implement getPose(forKey key: String, atTime: Float, boneName: String) -> (SCNVector3, SCNVector4)
public func getFirstFramePose(forKey key: String, boneName: String) -> (SCNVector3, SCNVector4) {
let transXKey = "/\(boneName).transform.translation.x"
Expand All @@ -1017,17 +1020,17 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
switch keyAnim.keyPath! {
case transXKey:
print("transXKey found")
translate.x = CGFloat(keyAnim.values!.first! as! NSNumber)
translate.x = OSFloat(keyAnim.values!.first! as! NSNumber)
print("value: \(translate.x)")
break
case transYKey:
print("transYKey found")
translate.y = CGFloat(keyAnim.values!.first! as! NSNumber)
translate.y = OSFloat(keyAnim.values!.first! as! NSNumber)
print("value: \(translate.y)")
break
case transZKey:
print("transZKey found")
translate.z = CGFloat(keyAnim.values!.first! as! NSNumber)
translate.z = OSFloat(keyAnim.values!.first! as! NSNumber)
print("value: \(translate.z)")
break
case quatKey:
Expand Down Expand Up @@ -1062,17 +1065,17 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
switch keyAnim.keyPath! {
case transXKey:
print("transXKey found")
translate.x = CGFloat(keyAnim.values!.last! as! NSNumber)
translate.x = OSFloat(keyAnim.values!.last! as! NSNumber)
print("value: \(translate.x)")
break
case transYKey:
print("transYKey found")
translate.y = CGFloat(keyAnim.values!.last! as! NSNumber)
translate.y = OSFloat(keyAnim.values!.last! as! NSNumber)
print("value: \(translate.y)")
break
case transZKey:
print("transZKey found")
translate.z = CGFloat(keyAnim.values!.last! as! NSNumber)
translate.z = OSFloat(keyAnim.values!.last! as! NSNumber)
print("value: \(translate.z)")
break
case quatKey:
Expand All @@ -1089,7 +1092,24 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {

return (translate, quaternion)
}

#else
// TODO: implement
public func getFirstFramePose(forKey key: String, boneName: String) -> (SCNVector3, SCNVector4) {
let vec3 = SCNVector3(x: 0, y: 0, z: 0)
let vec4 = SCNVector4(x: 0, y: 0, z: 0, w: 0)

return (vec3, vec4)
}

public func getLastFramePose(forKey key: String, boneName: String) -> (SCNVector3, SCNVector4) {
let vec3 = SCNVector3(x: 0, y: 0, z: 0)
let vec4 = SCNVector4(x: 0, y: 0, z: 0, w: 0)

return (vec3, vec4)
}
#endif

var checkParentPosition: SCNVector3 = SCNVector3()
var checkParentRotation: SCNVector4 = SCNVector4()
var checkParentPresentPosition: SCNVector3 = SCNVector3()
Expand Down Expand Up @@ -1255,9 +1275,10 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
print("\(index): \(boneName)")
}
}

// MARK: CAAnimationDelegate
//public func animationDidStart(_ anim: CAAnimation) {}
}

#if os(macOS) || os(iOS) || os(tvOS)
extension MMDNode: CAAnimationDelegate {
public func animationDidStop(_ anim: CAAnimation, finished: Bool) {
if finished {
if let block = anim.value(forKey: MMDAnimationCompletionBlockKey) as? () -> Void {
Expand All @@ -1272,3 +1293,4 @@ open class MMDNode: SCNNode, MMDNodeProgramDelegate, CAAnimationDelegate {
}
}
}
#endif
48 changes: 43 additions & 5 deletions Source/Common/MMDPMMReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SceneKit
#if os(watchOS)

class MMDPMMReader: MMDReader {
func getScene(_ data: Data, directoryPath: String! = "", models: [MMDNode?]? = nil, motions: [CAAnimation?]? = nil) -> SCNScene? {
static func getScene(_ data: Data, directoryPath: String! = "", models: [MMDNode?]? = nil, motions: [CAAnimation?]? = nil) -> SCNScene? {
return SCNScene()
}
}
Expand Down Expand Up @@ -269,9 +269,25 @@ fileprivate class MMDVMDAccessoryInfo {
var parentNode: MMDNode? = nil
if modelIndex >= 0 && modelIndex < reader.models.count {
let parentModel = reader.models[modelIndex]

//print(String(format: "<Pointer> MMDVMDAccessoryInfo parentModel: %p", parentModel))
if boneIndex < parentModel.boneArray.count {
parentNode = parentModel.boneArray[boneIndex]
//print("accessory parentNode: \(parentNode!.name)")
guard let boneName = parentModel.boneArray[boneIndex].name else {
fatalError("bone.name == nil")
}
parentNode = parentModel.childNode(withName: boneName, recursively: true) as? MMDNode
guard parentNode != nil else {
fatalError("parentNode == nil")
}

print("accessory parentNode: \(parentNode!.name)")
//print(String(format: "<Pointer><Center> MMDVMDAccessoryInfo parentNode: %p", parentNode!))

//var node:SCNNode? = parentNode
//while node != nil {
// print(String(format: "<Pointer><Recursive>%@: %p", node!.name ?? "", node!))
// node = node!.parent
//}
}
}
self.parent = parentNode
Expand Down Expand Up @@ -388,6 +404,8 @@ class MMDPMMReader: MMDReader {

*/
static func getScene(_ data: Data, directoryPath: String! = "", models: [MMDNode?]? = nil, motions: [CAAnimation?]? = nil) -> SCNScene? {

//print(String(format: "<Pointer> MMDPMMReader.getScene models[0]: %p", models![0]!))
let reader = MMDPMMReader(data: data, directoryPath: directoryPath)
let scene = reader.loadPMMFile(models: models, motions: motions)

Expand Down Expand Up @@ -629,6 +647,9 @@ class MMDPMMReader: MMDReader {

self.models.append(model)
}

//print(String(format: "<Pointer> MMDPMMReader.readModels self.models[0]: %p", self.models[0]))

}

private func readBone() {
Expand Down Expand Up @@ -1472,6 +1493,7 @@ class MMDPMMReader: MMDReader {

// MARK: - Accessory Frame
private func readAccessories() {
print("before accessoryCount pos: \(self._pos)")
self.accessoryCount = Int(getUnsignedByte())
self.accessoryNameArray = [String]()

Expand All @@ -1494,6 +1516,7 @@ class MMDPMMReader: MMDReader {

for index in 0..<self.accessoryCount {
var accessory = MMDNode()
print("reader pos: \(self._pos)")
if let m = self.substituteModels[self.modelCount + index] {
accessory = m

Expand Down Expand Up @@ -1671,10 +1694,14 @@ class MMDPMMReader: MMDReader {
mmdParentNode.addChildNode(node)
}

//print(String(format: "<Pointer><Center><callback> readOneAccessoryFrame callback mmdParentNode: %p", mmdParentNode))

//print("accessory.transform: \(node.transform)")
//print("accessory.presentation.transform: \(node.presentation.transform)")
}
})
//print(String(format: "<Pointer><Center> readOneAccessoryFrame mmdParentNode: %p", mmdParentNode))

parentEvents.append(parentEvent)
prevParent = mmdParentNode
}
Expand Down Expand Up @@ -1782,9 +1809,20 @@ class MMDPMMReader: MMDReader {
// float3 pos
// float scale
// float3 rot
// byte flag?
// byte flag1?
// byte flag2?

print("readAccessoryStatus")
for i in 0..<38 {
let data = getUnsignedByte()
print("\(i): \(data)")
}
if(self.version >= 2){
let data = getUnsignedByte()
print("38: \(data)")
}

skip(38)
//skip(39)
}

// MARK: - other settings
Expand Down
Loading

0 comments on commit 5105edb

Please sign in to comment.