Search code examples
swiftxcodeaugmented-realityarkitrealitykit

AR objects not anchoring or sizing correctly in RealityKit


I have an AR scene with two objects, one brown cow and one black one. They're both supposed to be displayed in the scene, distanced a little apart. I originally only had the brown cow, which was just a little bit too big. I changed something, which I can't remember, and now my scene is from the inside of the cow, and I can't exit the cow's corpse. It seems like it moves around when I do. I think the issue is because of a positive number for the [minimum bounds]but I'm not entirely sure. I've set the z axis for the cow as well.How can I make the cow a little bit smaller and about 5-7 yards away from me at spawn?

enter image description here

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

Solution

  • First step

    In RealityKit, if a model was tethered with its personal anchor (the case when one anchor holds just one model), you have two ways to scale it:

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

    and you have minimum two ways to position cow model along any axis (for instance along Z axis):

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

    So, as you see, when you transform cowAnchor, all its children get this transformation as well.

    Second step

    You need to appropriately place a model's pivot point 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 instance property.