将3D对象添加到ARGeoAnchor [英] Adding 3D object to ARGeoAnchor
问题描述
如果这个问题不是那么重要,请原谅我.我在Apple的ARGeoAnchor文档中遇到了一些障碍.
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.
当前,ARGEoAnchor在AR场景视图中仅显示一个蓝点.我正尝试显示任何3d rendereing或对象.
Currently ARGeoAnchor just shows a blue dot in the AR Scene View. I'm trying to show any 3d rendereing or object instead.
我的代码:
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)
我得到的错误:类型'ARGeoAnchor'的值没有成员'scene'
我有多个ARGeoAnchor,它们目前都显示蓝点.如何让它们显示自定义3d对象呢?
I have multiple ARGeoAnchors, they are all currently showing blue dots. How do I get them to show custom 3d objects instead?
感谢您的光临!
推荐答案
首先,您必须检查 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])
}
}
...目前仅支持美国城市...
支持ARGeoTrackingConfiguration的城市列表.
然后,您必须运行地理配置:
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)
}
然后,您可以借助ARGeoAnchor添加模型:
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)
}
}
这篇关于将3D对象添加到ARGeoAnchor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!