是否可以为 RealityKit 中的对象设置基于位置的锚点?

Is it possible to have a location based anchor for an object in RealityKit?

我创建了一个运行良好的 AR 应用程序,但我不是每次都在镜头前生成的对象的拥抱粉丝。我更愿意让他们在这个领域产卵,更远离相机,并面向预定的方向。例如,我想让一辆车每次都在同一个停车场 space 生成,所以当我走进停车场时,无论我从哪个方向进来,我都能看到车停在那里就像我离开时一样它来自.

如何根据位置生成对象?我认为这与用经纬度坐标替换平面检测有关,但我不知道该怎么做。 非常感谢任何帮助!

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 使用相应的 ARGeoAnchors ARGeoTrackingConfiguration

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