【问题标题】:Importing and animating multiple .usdz objects in RealityKit在 RealityKit 中导入多个 .usdz 对象并为其设置动画
【发布时间】:2021-01-21 08:44:05
【问题描述】:

我有一个显示 .USDZ 奶牛的简单 RealityKit 场景。我看到了这些问题的几个答案,但我无法将它们实现到我的代码中:

  1. 如何播放此文件中内置的动画?
  2. 如何才能将第二个对象(比如一匹马)带入场景?

此外,我可以做些什么来清理我的代码/使其更易于阅读?

感谢我能得到的任何帮助! 谢谢,

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


  }

【问题讨论】:

    标签: swift xcode augmented-reality arkit realitykit


    【解决方案1】:

    在 RealityKit API 中播放 .usdz 文件中包含的动画,您可以使用以下代码:

    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)
    

    很遗憾,但在 RealityKit 2.0 中,您只能播放放置在文件中的第一个动画 - 其他动画目前不可用。

    【讨论】:

    • 非常感谢!当我弄清楚我遇到的这个锚问题时,我会让你知道它的外观。感谢您的帮助!
    • 嘿,安迪,我希望你能在这段代码上帮助我。我已将它添加到我的项目中(代码在帖子中更新),然后我在您提供的代码的开头和结尾放置了大括号。现在我在“let cow =”语句之前收到“预期声明”错误。单击它会将我带到 ViewController 类并显示“1. 在声明 'ViewController' 中”我做错了吗?非常感谢任何帮助!
    • 嘿泰勒。请告诉我你的代码中产生错误的行。
    • 我在帖子中附上了截图!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-04-11
    • 2011-12-15
    • 2015-04-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多