5

我从这里下载了一个DAECollada 文件,并在最新版本中打开并将其转换为文件。Xcode betaSCN

我使用此方法加载场景对象(其中modelName= modelfileExtension= 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提供了从daescn的转换,我觉得使用结果文件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)
}
4

1 回答 1

0

我有同样的问题。我的解决方案:将 .dae 文件拖到项目中并通过“复制”按钮进行转换。在我这样做之后,.scn 文件出现并且可见。

于 2019-03-05T20:13:45.030 回答