SCNScene
。但是这个文件无法加载。
这个文件在“ARDicee/art.assets”文件夹中。不仅是"diceCollada.scn",连默认的"ship.scn"也无法加载。我不知道为什么无法加载文件。
以下是我的代码。
import UIKit
import SceneKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
// Create a new scene. ---------- The error is here ---------------
guard let diceScene = SCNScene(named: "art.scnassets/diceCollada.scn") else {
fatalError()
}
// Setting node
if let diceNode = diceScene.rootNode.childNode(withName: "Dice", recursively: true) {
diceNode.position = SCNVector3(x: 0, y: 0, z: -0.1)
sceneView.scene.rootNode.addChildNode(diceNode)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if ARWorldTrackingConfiguration.isSupported {
// Create a session configuration
let configuration = ARWorldTrackingConfiguration()
// Run the view's session
sceneView.session.run(configuration)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Pause the view's session
sceneView.session.pause()
}
}
Xcode - 版本14.1
macOS Ventura - 版本13.0.1
GitHub - 这个项目
我还尝试了另一种创建SCNScene
的方法。
override func viewDidLoad() {
super.viewDidLoad()
// Set the view's delegate
sceneView.delegate = self
// Show statistics such as fps and timing information
sceneView.showsStatistics = true
// --- Another way to create SCNScene ---
let filePath = URL(fileURLWithPath: "/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn")
do {
let diceScene = try SCNScene(url: filePath)
if let diceNode = diceScene.rootNode.childNode(withName: "Dice", recursively: true) {
diceNode.position = SCNVector3(x: 0, y: 0, z: -0.1)
sceneView.scene.rootNode.addChildNode(diceNode)
}
} catch {
print(error)
}
}
但是它出现了这个错误。
错误域=NSCocoaErrorDomain Code=260 "无法打开文件“diceCollada.scn”,因为没有这样的文件。" UserInfo={NSFilePath=/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn, NSUnderlyingError=0x282924570 {Error Domain=NSPOSIXErrorDomain Code=2 "没有这样的文件或目录"}}
我正在尝试从'diceCollada.scn'文件创建一个新的SCNScene
。