将 3D 对象添加到 ARGeoAnchor

Adding 3D object to ARGeoAnchor

如果这个问题不是很好,请原谅我。我在 Apple 的 ARGeoAnchor 文档中遇到了一些障碍。

目前 ARGeoAnchor 仅在 AR 场景视图中显示一个蓝点。我正在尝试显示任何 3d 渲染或对象。

我的代码:

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)

我得到的错误:Value of type 'ARGeoAnchor' has no member 'scene'

我有多个 ARGeoAnchors,它们目前都显示蓝点。我如何让它们显示自定义 3d 对象?

感谢观看!

首先你必须检查你的设备是否支持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
    }
}

...然后检查您所在位置是否可以使用地理跟踪:

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])
    }
}

之后向 Info.plist 提供 相机 位置权限 。这是有关 Authorization Request for Location Services 的综合信息,包括:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationUsageDescription
  • NSLocationAlwaysUsageDescription


现在只支持美国城市和英国首都...

A list of cities that support ARGeoTrackingConfiguration.


那么你必须运行地理配置:

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

然后将参数化锚点添加到会话中:

@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)
}

然后您可以在 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)
    }
}


P.S.

如果您对类似功能在 ARCore 中的工作原理感兴趣,请阅读我关于 Geospatial API 的 post。