Xcode3Dモデル読み込みと表示このサイトを参考にARkitで3Dモデルを表示させたかったのですがうまく表示されません。
実行させようとすると、下記のようにエラーが出てしまいます。
sceneにしているディレクトリの設定も間違っていないと思います、記事にあるようにモデルの名前もGARLICに変換しました。
どこが悪いのか自分では判断できないのでソースコードを全て載せておきます
Swift
1import UIKit 2import SceneKit 3import ARKit 4 5class ViewController: UIViewController, ARSCNViewDelegate { 6 7 @IBOutlet var sceneView: ARSCNView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 let scene = SCNScene() 13 sceneView.delegate = self 14 sceneView.showsStatistics = true 15 //特徴点をデバッグ用に表示 16 sceneView.debugOptions = ARSCNDebugOptions.showFeaturePoints 17 } 18 19 override func viewWillAppear(_ animated: Bool) { 20 super.viewWillAppear(animated) 21 22 let configuration = ARWorldTrackingConfiguration() 23 24 configuration.planeDetection = .horizontal 25 sceneView.session.run(configuration) 26 } 27 28 override func viewWillDisappear(_ animated: Bool) { 29 super.viewWillDisappear(animated) 30 31 // Pause the view's session 32 sceneView.session.pause() 33 } 34 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { 35 guard let planeAnchor = anchor as? ARPlaneAnchor else {fatalError()} 36 //sceneの読み込み 37 guard let scene = SCNScene(named: "Garlic.scn", inDirectory: "art.scnassets/Garlic", options: nil) else {fatalError()} 38 //個々のwithNameはscnファイルのshows the Node inspectorの中で設定されている名前を参照 39 guard let garlicNode = scene.rootNode.childNode(withName: "GARLIC", recursively: true) else {fatalError()} 40 41 //nodeのスケール調整 42 let (min, max) = garlicNode.boundingBox 43 let w = CGFloat(max.x - min.x) 44 45 //1mを基準にした縮尺を計算 46 let magnification = 0.1 / w 47 garlicNode.scale = SCNVector3(magnification, magnification, magnification) 48 //nodeのポジションを設定 49 garlicNode.position = SCNVector3(planeAnchor.center.x, 0, planeAnchor.center.z) 50 //作成したノードを追加 51 DispatchQueue.main.async { 52 node.addChildNode(garlicNode) 53 } 54 } 55} 56
エラーログ
Add3DObject[8912:2460433] [SceneKit] Error: Scene <SCNScene: 0x2869297c0> is modified within a rendering callback of another scene (<SCNScene: 0x282e103c0>). This is not allowed and may lead to crash
回答1件
あなたの回答
tips
プレビュー