Adding 3D object to ARGeoAnchor

拥有回忆 提交于 2020-12-13 05:40:08

问题


Please forgive me if this question is not that great. I've hit a bit of a road block on Apple's documentation of ARGeoAnchor.

Currently ARGeoAnchor just shows a blue dot in the AR Scene View. I'm trying to show any 3d rendereing or object instead.

My code:

let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lng)
let geoAnchor = ARGeoAnchor(name: "Point 1", coordinate: coordinate)
    
let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
let cube = SCNNode(geometry: boxGeometry)
geoAnchor.scene.rootNode.addChildNode(cube)
self.addGeoAnchor(geoAnchor)

The error i'm getting: Value of type 'ARGeoAnchor' has no member 'scene'

I have multiple ARGeoAnchors, they are all currently showing blue dots. How do I get them to show custom 3d objects instead?

Thanks for taking a look!


回答1:


At first you have to check ARGeoTrackingConfiguration:

import ARKit

@main class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, 
                       didFinishLaunchingWithOptions launchOptions: 
                                      [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        if !ARGeoTrackingConfiguration.isSupported {

            let sb = UIStoryboard(name: "Main", bundle: nil)

            window?.rootViewController = sb.instantiateViewController(withIdentifier:
                                                             "unsupportedConfiguration")
        }
        return true
    }
}

...then check whether Geo Tracking is available at your location:

ARGeoTrackingConfiguration.checkAvailability { (available, error) in

    if !available {
        let errorDescription = error?.localizedDescription ?? ""
        let recommendation = "You need a place where geo tracking is supported."
        let restart = UIAlertAction(title: "Restart", style: .default) { (_) in
            self.restartSession()
        }
        self.alertUser(withTitle: "Geo tracking unavailable",
                         message: "\(errorDescription)\n\(recommendation)",
                         actions: [restart])
    }
}

only several cities are supported at this moment...

Then you must run Geo Configuration:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    let config = ARGeoTrackingConfiguration()
    sceneView.session.run(config)
}

Then add parametrised anchor to the session:

@IBOutlet var sceneView: ARSCNView!

override func viewDidLoad() {
    super.viewDidLoad()
    sceneView.delegate = self
    sceneView.scene = SCNScene()
    
    let coordinate = CLLocationCoordinate2D(latitude: 40.730610, 
                                           longitude: -73.935242)

    let geoAnchor = ARGeoAnchor(name: "Geo Anchor",
                          coordinate: coordinate,
                            altitude: 33.0)
    
    sceneView.session.add(anchor: geoAnchor)
}

And after that you can add a model with the help of ARGeoAnchor:

extension ViewController: ARSCNViewDelegate {
    
    func renderer(_ renderer: SCNSceneRenderer,
                 didAdd node: SCNNode,
                  for anchor: ARAnchor) {
            
        guard let geoAnchor = anchor as? ARGeoAnchor,
                  geoAnchor.name == "Geo Anchor"
        else { return }
        
        print(geoAnchor.coordinate)
                
        let boxGeometry = SCNBox(width: 1.0,
                                height: 1.0,
                                length: 1.0,
                         chamferRadius: 0.1)

        boxGeometry.firstMaterial?.diffuse.contents = UIColor.red

        let cube = SCNNode(geometry: boxGeometry)
        
        node.addChildNode(cube)
    }
}


来源:https://stackoverflow.com/questions/64884844/adding-3d-object-to-argeoanchor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!