【问题标题】:Is it possible to have a location based anchor for an object in RealityKit?是否可以在 RealityKit 中为对象提供基于位置的锚点?
【发布时间】:2020-10-05 19:26:24
【问题描述】:

我创建了一个运行良好的 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


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,对于延迟回复我深表歉意。我已经更新了我的帖子以包含我当前的代码。那么,由于我不在这些城市,我无法使用 ARGeotrackingConfiguration?
    • 唉,如果您不在这些城市,您将无法运行ARGeoTrackingConfig。让我们等一下,Apple 会添加一些新区域。
    • 非常感谢您提供的信息!另外,我在 LinkedIn 上添加了你!
    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多