簡體   English   中英

將 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

要求:您需要具有 A12+ 芯片和蜂窩 (GPS) 支持的設備。

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提供相機位置權限 以下是有關位置服務授權請求的綜合信息,其中包括:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysAndWhenInUseUsageDescription
  • NSLocationUsageDescription
  • NSLocationAlwaysUsageDescription


目前僅支持美國、英國、澳大利亞、加拿大、日本和新加坡的幾個城市......

支持 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)
    }
}


附言

如果您對 ARCore 中的類似功能如何工作感興趣,請閱讀我關於Geospatial API的文章。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM