gpt4 book ai didi

swift - 是否可以为 RealityKit 中的对象设置基于位置的 anchor ?

转载 作者:行者123 更新时间:2023-12-04 15:16:37 28 4
gpt4 key购买 nike

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

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

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

最佳答案

苹果制造的地理位置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)
}

关于swift - 是否可以为 RealityKit 中的对象设置基于位置的 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64214957/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com