Swift初心者です.
ARkitを用いて二つののオブジェクトを表示させたいです.
一つは,Boxのオブジェクト(node)で,こちらは表示できています.
もう一つはARkitに標準に入っているship.scnオブジェクト(myNode)です.
エラーはないのですが,実行するとship.scnオブジェクトのみ表示されません.
将来的に複数のscnファイルのオブジェクトを平面上に表示させたく,試しに色々とやっています.
やってみたこと
・myNodeの座標を変えてみる
至らぬ点が多いですが,よろしくおねがいいたします.
ソースコードは以下のようになります.
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 sceneView.delegate = self 13 sceneView.showsStatistics = true 14 let scene = SCNScene() 15 16 //Color Setting , random 17 let random: CGFloat = CGFloat(arc4random() % 10) //乱数値設定(使用してない,0~9の範囲を指定している) 18 let r: CGFloat = CGFloat(arc4random_uniform(255)+1) / 255.0 19 let g: CGFloat = CGFloat(arc4random_uniform(255)+1) / 255.0 20 let b: CGFloat = CGFloat(arc4random_uniform(255)+1) / 255.0 21 //let color: UIColor = UIColor(red: r, green: g, blue: b, alpha: 1.0) 22 23 // node (Box) 24 let box = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0) //形,大きさ 25 let material = SCNMaterial() 26 material.diffuse.contents = UIColor(red:r, green:g, blue:b, alpha: 1) 27 let node = SCNNode() 28 node.geometry = box 29 material.name = "Color" 30 node.geometry?.materials = [material] 31 node.position = SCNVector3(0, 0.2, -1) 32 scene.rootNode.addChildNode(node) 33 34 // myNode 35 let myScene = SCNScene(named: "art.scnassets/ship.scn")! 36 let myNode = myScene.rootNode.childNode(withName: "ship",recursively: true) 37 myNode?.position = SCNVector3(0,-1,0) 38 sceneView.scene.rootNode.addChildNode(myNode!) 39 40 41 //gesture 検知 42 let tapRecognizer = UITapGestureRecognizer(target: self, action:#selector(tapped)) //タップされたら呼ばれる関数tapped 43 self.sceneView.addGestureRecognizer(tapRecognizer) 44 45 sceneView.scene = scene 46 sceneView.debugOptions = ARSCNDebugOptions.showFeaturePoints //デバッグ用オプション 47 48 } 49 50 //objective-C func tapped 51 @objc func tapped(recognizer: UIGestureRecognizer) { 52 let sceneView = recognizer.view as! SCNView //as!型変換,タップされたオブジェクトのシーンを取得 53 let touchLocation = recognizer.location(in: sceneView) //タッチされた座標を返す 54 let hitResults = sceneView.hitTest(touchLocation, options: [:]) //hitTestメソッドを利用, 55 56 if !hitResults.isEmpty { //もしタッチしたデータがあれば(先頭の!はnotの意味) 57 let node = hitResults[0].node //ノードに,先頭のノードの値を取り出す 58 let material = node.geometry?.material(named: "Color") //Colorは対応する上で設定済み 59 //node.removeFromParentNode() 60 let sphere = SCNSphere(radius: 0.2) 61 node.geometry = sphere 62 //material?.diffuse.contents = UIImage(named: "earthmap.jpeg") //material?でアンラップ,color chnage, change graphic 63 } 64 } 65 66 override func viewWillAppear(_ animated: Bool) { 67 super.viewWillAppear(animated) 68 69 let configuration = ARWorldTrackingConfiguration() 70 configuration.planeDetection = .horizontal //平面検知 71 sceneView.session.run(configuration) 72 } 73 74 override func viewWillDisappear(_ animated: Bool) { 75 super.viewWillDisappear(animated) 76 77 sceneView.session.pause() 78 } 79 80 81} 82
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。