AR 对象在 RealityKit 中没有正确锚定或调整大小

AR objects not anchoring or sizing correctly in RealityKit

我有一个包含两个对象的 AR 场景,一头棕色牛和一头黑色牛。它们都应该显示在场景中,稍微分开一点。我本来只有棕色的牛,有点太大了。我改了个东西,记不清了,现在我的场景是从牛里面出来的,我出不了牛的尸体。当我这样做时,它似乎在四处移动。我认为问题是因为 [最小界限] 为正数,但我不完全确定。我已经将奶牛的 z 轴设置为 well.How 我可以让奶牛小一点,让它在产卵时离我大约 5-7 码吗?

  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.7, 0.7])
    
    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.")
        
        //cow animations
        let robot = try! ModelEntity.load(named: "COW_ANIMATIONS")
        let anchor = AnchorEntity()
        anchor.children.append(robot)
        arView.scene.anchors.append(anchor)

        robot.playAnimation(robot.availableAnimations[0].repeat(duration: .infinity),
                                                      transitionDuration: 0.5,
                                                            startsPaused: false)
        
        //start cow animation
        let brownCow = try! ModelEntity.load(named: "COW_ANIMATIONS")
        let blackCow = try! ModelEntity.load(named: "Cow")

        brownCow.position.x = -1.0
        blackCow.position.x = 1.0

        
        brownCow.setParent(anchor)
        blackCow.setParent(anchor)
        arView.scene.anchors.append(anchor)

        let cowAnimationResource = brownCow.availableAnimations[0]
        let horseAnimationResource = blackCow.availableAnimations[0]

        brownCow.playAnimation(cowAnimationResource.repeat(duration: .infinity),
                                            transitionDuration: 1.25,
                                                  startsPaused: false)

        blackCow.playAnimation(horseAnimationResource.repeat(duration: .infinity),
                                                transitionDuration: 0.75,
                                                      startsPaused: false)
        
        //end cow animations

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

第一步

在 RealityKit 中,如果一个模型与其个人锚点相连(即一个锚点仅持有一个模型的情况),您有两种缩放它的方法:

cowEntity.scale = [0.7, 0.7, 0.7]
// or
cowAnchor.scale = SIMD3<Float>([1, 1, 1] * 0.7)

并且您至少有两种方法可以沿任何轴(例如沿 Z 轴)定位奶牛模型:

cowEntity.position = SIMD3<Float>(0, 0,-2)
// or
cowAnchor.position.z = -2.0

因此,如您所见,当您转换 cowAnchor 时,它的所有子级也会得到这种转换。

第二步

您需要适当放置模型的 in 3D authoring app. At the moment RealityKit doesn't have a tool to fix pivot's position as you can do in SceneKit using simdPivot 实例 属性。