2

我有一个带有两个对象的 AR 场景,一个是棕色的牛,一个是黑色的。它们都应该显示在场景中,距离有点远。我原本只有棕牛,只是有点太大了。我改变了一些我不记得的东西,现在我的场景是从牛的内部,我无法退出牛的尸体。当我这样做时,它似乎会四处移动。我认为这个问题是因为[最小界限]的正数,但我不完全确定。我也为奶牛设置了 z 轴。我怎样才能让奶牛更小一点,在产卵时离我大约 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)
    }  }
}
}
4

1 回答 1

2

第一步

在 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 时,它的所有子节点也都会进行此变换。

第二步

您需要在 3D 创作应用程序中适当地放置模型的轴心点。目前,RealityKit 没有像在 SceneKit 中使用simdPivot实例属性那样修复枢轴位置的工具。

于 2020-10-07T04:45:47.750 回答