RealityKit 中的对象是否可以有一个基于位置的锚点?

问题描述 投票:0回答:2

我创建了一个效果很好的 AR 应用程序,但我不太喜欢每次在镜头前生成的物体。我更愿意让它们在这个区域中生成,距离相机更远,并面向预定的方向。例如,我希望每次都在同一个停车场空间中生成一辆车,因此当我走进停车场时,无论我从哪个方向过来,我都可以看到停在那里的汽车,就像我离开它一样。

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

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)
    }  }
}
}
swift xcode augmented-reality arkit realitykit
2个回答
3
投票

对于地理位置,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)

目前正在当前城市和地区工作。

您还可以使用 getGeoLocation(forPoint:completionHandler:) 实例方法将框架本地坐标系中的位置转换为 GPS 纬度、经度和海拔。

arView.session.getGeoLocation(forPoint: xyzWorld) { (coord, alt, error) in
    let anchor = ARGeoAnchor(coordinate: coord, altitude: alt)
}

0
投票

您好,您的问题解决了吗?

© www.soinside.com 2019 - 2024. All rights reserved.