##困っていること
ARKitを用いてオブジェクトを表示させたいが、画像のように黒いオブジェクトが表示されてしまう。
##ソースコード
Swift
1import UIKit 2import ARKit 3import SceneKit 4class BasicsViewController: UIViewController{ 5 6 7 @IBOutlet weak var sceneView: ARSCNView! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 sceneView.delegate = self 12 sceneView.showsStatistics = true 13 } 14 15 override func viewWillAppear(_ animated: Bool) { 16 super.viewWillAppear(animated) 17 let configuration = ARWorldTrackingConfiguration()//設定をする 18 configuration.planeDetection = .horizontal//水平面を認識するように設定[.horizontal, .vertical]で両方 19 sceneView.session.run(configuration, options: []) 20 } 21 22 override func viewWillDisappear(_ animated: Bool) { 23 super.viewWillDisappear(animated) 24 sceneView.session.pause() 25 } 26 27 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 28 let ship = SCNScene(named: "art.scnassets/ship.scn")! 29 let shipNode = ship.rootNode.childNodes.first! 30 shipNode.scale = SCNVector3(0.2,0.2,0.2) 31 32 guard let location = touches.first?.location(in: sceneView) else { return } 33 let pos: SCNVector3 = SCNVector3(location.x, location.y, 0.996) 34 let finalPosition = sceneView.unprojectPoint(pos) 35 shipNode.position = finalPosition 36 sceneView.scene.rootNode.addChildNode(shipNode) 37 } 38 39} 40 41extension BasicsViewController: ARSCNViewDelegate { 42 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) { 43 print("anchor added") 44 if anchor is ARPlaneAnchor { 45 print("this is ARPlaneAnchor") 46 } 47 } 48}
あなたの回答
tips
プレビュー