如何解决在RealityKit中是否可以为对象提供基于位置的锚点?
我创建了一个运行良好的AR应用程序,但我并不是每次都在镜头前产生的物体的拥护者。我宁愿让它们在此区域中生成,并朝相机更远的方向并面向预定方向。例如,我希望每次都有一辆汽车在相同的停车场内产生,所以当我走进停车场时,无论我从哪个方向走,都可以看到那辆车停在那儿就像我离开了车。 / p>
如何根据对象的位置生成对象?我认为这与用纬度和经度坐标替换平面检测有关,但是我不知道该怎么做。 任何帮助将不胜感激!
import UIKit
import RealityKit
import ARKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
showModel()
overlayCoachingView()
setupARView()
arView.addGestureRecognizer(UITapGestureRecognizer(target: self,action: #selector(handleTap(recognizer:))))
}
func showModel(){
let anchorEntity = AnchorEntity(plane: .horizontal,minimumBounds:[0.2,0.2])
let entity = try! Entity.loadModel(named: "COW_ANIMATIONS")
entity.setParent(anchorEntity)
arView.scene.addAnchor(anchorEntity)
}
func overlayCoachingView () {
let coachingView = ARCoachingOverlayView(frame: CGRect(x: 0,y: 0,width: arView.frame.width,height: arView.frame.height))
coachingView.session = arView.session
coachingView.activatesAutomatically = true
coachingView.goal = .horizontalPlane
view.addSubview(coachingView)
}
// Load the "Box" scene from the "Experience" Reality File
// let boxAnchor = try! Experience.loadBox()
// Add the box anchor to the scene
//arView.scene.anchors.append(boxAnchor)
func setupARView(){
arView.automaticallyConfigureSession = false
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal,.vertical]
configuration.environmentTexturing = .automatic
arView.session.run(configuration)
}
//object placement
@objc
func handleTap(recognizer: UITapGestureRecognizer){
let location = recognizer.location(in:arView)
let results = arView.raycast(from: location,allowing: .estimatedPlane,alignment: .horizontal)
if let firstResult = results.first {
let anchor = ARAnchor(name: "COW_ANIMATIONS",transform: firstResult.worldTransform)
arView.session.add(anchor: anchor)
} else {
print("Object placement failed - couldn't find surface.")
}
}
func placeObject(named entityName: String,for anchor: ARAnchor) {
let entity = try! ModelEntity.loadModel(named: entityName)
entity.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation,.translation],for: entity)
let anchorEntity = AnchorEntity(anchor: anchor)
anchorEntity.addChild(entity)
arView.scene.addAnchor(anchorEntity)
}
}
extension ViewController: ARSessionDelegate {
func session( session: ARSession,didAdd anchors: [ARAnchor]) {
for anchor in anchors {
if let anchorName = anchor.name,anchorName == "COW_ANIMATIONS" {
placeObject(named: anchorName,for: anchor)
} }
}
}
解决方法
对于地理位置,Apple制作了ARGeoTrackingConfiguration和相应的ARGeoAnchors。
let location = CLLocationCoordinate2D(latitude: -18.9137,longitude: 47.5361)
let geoAnchor = ARGeoAnchor(name: "Tana",coordinate: location,altitude: 1250)
arView.session.add(anchor: geoAnchor)
let realityKitAnchor = AnchorEntity(anchor: geoAnchor)
arView.scene.anchors.append(realityKitAnchor)
目前它正在current cities and areas中工作。
您还可以使用getGeoLocation(forPoint:completionHandler:)实例方法,将框架的本地坐标系中的位置转换为GPS纬度,经度和海拔高度。
arView.session.getGeoLocation(forPoint: xyzWorld) { (coord,alt,error) in
let anchor = ARGeoAnchor(coordinate: coord,altitude: alt)
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。