我从DAE
下载了一个here 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
文件应该很容易。
任何帮助,将不胜感激!
Dinosaur.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)
}
我有同样的问题。我的解决方案:将.dae文件拖到项目中并通过“复制”按钮进行转换。在我这样做之后,.scn文件出现并且可见。