我从这里下载了一个DAE
Collada 文件,并在最新版本中打开并将其转换为文件。Xcode beta
SCN
我使用此方法加载场景对象(其中modelName
= model
,fileExtension
= scn
):
private func loadModel() {
guard let virtualObjectScene = SCNScene(named: "\(modelName).\(fileExtension)", inDirectory: "art.scnassets/\(modelName)") else {
return
}
let wrapperNode = SCNNode()
for child in virtualObjectScene.rootNode.childNodes {
wrapperNode.addChildNode(child)
}
self.addChildNode(wrapperNode)
}
然而,什么都没有出现!只有当我加载dae
文件(通过更改fileExtension
= dae
)时,恐龙才会出现,但恐龙的闪烁真的很奇怪。
而且我不明白为什么这不起作用,因为如果Apple提供了从dae
到scn
的转换,我觉得使用结果文件Xcode
应该很容易。scn
任何帮助,将不胜感激!
代码
恐龙.swift
class Dinosaur: VirtualObject {
override init() {
super.init(modelName: "model", fileExtension: "scn", title: "Dinosaur")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
VirtualObject.swift
class VirtualObject: SCNNode {
var modelName: String = ""
var fileExtension: String = ""
var title: String = ""
override init() {
super.init()
self.name = "Virtual object root node"
}
init(modelName: String, fileExtension: String, title: String) {
super.init()
self.name = "Virtual object root node"
self.modelName = modelName
self.fileExtension = fileExtension
self.title = title
loadModel()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func loadModel() {
guard let virtualObjectScene = SCNScene(named: "\(modelName).\(fileExtension)", inDirectory: "art.scnassets/\(modelName)") else {
return
}
let wrapperNode = SCNNode()
for child in virtualObjectScene.rootNode.childNodes {
wrapperNode.addChildNode(child)
}
self.addChildNode(wrapperNode)
}
private func unloadModel() {
for child in self.childNodes {
child.removeFromParentNode()
}
}
}
ViewController.swift
var newObject: VirtualObject = Dinosaur()
@IBAction func loadAd(_ sender: UIButton) {
newObject.removeFromParentNode()
newObject.position = SCNVector3(0, 0, -1)
sceneView.scene.rootNode.addChildNode(newObject)
}