【问题标题】:Adding 3D object to ARGeoAnchor将 3D 对象添加到 ARGeoAnchor
【发布时间】:2021-03-01 05:38:32
【问题描述】:

如果这个问题不是很好,请原谅我。我在 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 对象?

感谢观看!

【问题讨论】:

    标签: swift augmented-reality scenekit arkit aranchor


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多