Importar e animar vários objetos .usdz no RealityKit

Oct 06 2020

Eu tenho uma cena simples do RealityKit que exibe uma vaca .USDZ. Eu vi algumas respostas para essas perguntas, mas não fui capaz de implementá-las em meu código:

  1. Como posso reproduzir as animações que estão embutidas neste arquivo?
  2. Como posso trazer um segundo objeto (digamos um cavalo) para a cena?

Além disso, há algo que eu possa fazer para limpar meu código / torná-lo mais fácil de ler?

Agradeço qualquer ajuda que puder obter! Obrigado,

import UIKit
import RealityKit
import ARKit

 class WelcomeViewController: UIViewController {

//delay app launch to show splash screen
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Thread.sleep(forTimeInterval: 3.0)
        // Override point for customization after application launch.
        return true
    }
//end splash screen delay

   @IBAction func gotPressed(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController {
        self.present(viewController, animated: true, completion: nil) /// present the view controller (the one with the ARKit)!
    }    }

 }
 class ViewController: UIViewController {

@IBOutlet var arView: ARView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    overlayCoachingView()
    

}
//Overlay coaching view "adjust iphone scan"
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)
    
}//end overlay
{
let cow = try! ModelEntity.load(named: "COW_ANIMATIONS")
let horse = try! ModelEntity.load(named: "White_Horse")

cow.position.x = -1.0
horse.position.x = 1.0

let anchor = AnchorEntity()
cow.setParent(anchor)
horse.setParent(anchor)
arView.scene.anchors.append(anchor)

let cowAnimationResource = cow.availableAnimations[0]
let horseAnimationResource = horse.availableAnimations[0]

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

horse.playAnimation(horseAnimationResource.repeat(duration: .infinity),
                                        transitionDuration: 0.75,
                                              startsPaused: false)}


  }

Respostas

3 AndyFedoroff Oct 07 2020 at 00:49

Na API RealityKit, para reproduzir a animação contida no .usdzarquivo, você pode usar o seguinte código:

let cow = try ModelEntity.load(named: "cow")
let horse = try ModelEntity.load(named: "horse")

cow.position.x = -1.0
horse.position.x = 1.0

let anchor = AnchorEntity()
cow.setParent(anchor)
horse.setParent(anchor)
arView.scene.anchors.append(anchor)

let cowAnimationResource = cow.availableAnimations[0]
let horseAnimationResource = horse.availableAnimations[0]

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

horse.playAnimation(horseAnimationResource.repeat(duration: .infinity), 
                                        transitionDuration: 0.75, 
                                              startsPaused: false)

É uma pena, mas no RealityKit 2.0 você pode reproduzir apenas a primeira animação colocada dentro do seu arquivo - outras estão indisponíveis no momento.