我想将对象从dae文件加载到作为SCNNode子类的变量。文件包含模型和动画。我有一个函数加载模型作为SCNNode,但我不知道如何更改它以使用SCNNode子类。
extension SCNNode {
class func loadModel(named fileName: String) -> SCNNode? {
let modelFileURL = Bundle.main.url(forResource: fileName, withExtension: "dae")!
let sceneSource = SCNSceneSource(url: modelFileURL, options: [SCNSceneSource.LoadingOption.animationImportPolicy : SCNSceneSource.AnimationImportPolicy.playRepeatedly])
return sceneSource?.entryWithIdentifier("MDL_Obj", withClass: SCNNode.self)
}
}
答案 0 :(得分:0)
我认为这有效,但仍需要通过更多示例进行验证:
class Model3D: SCNNode {
init?(modelName: String) {
guard let scnNode = SCNNode.loadModel(named: modelName) else { return nil }
super.init()
geometry = scnNode.geometry
position = scnNode.position
scnNode.childNodes.forEach {
addChildNode($0)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}