swift - 在 RealityKit 中导入多个 .usdz 对象并为其设置动画

标签 swift xcode augmented-reality arkit realitykit

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

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


  }

enter image description here

enter image description here

最佳答案

在 RealityKit SDK 中,要播放单个 .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 中,默认情况下,您只能播放单个 usdz 文件中包含的第一个动画。然而,有workaround 1workaround 2 .

关于swift - 在 RealityKit 中导入多个 .usdz 对象并为其设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64226321/

相关文章:

ios - 如何减少 Collection View 中的内存使用量?

ios - 如何捆绑名称、捆绑显示名称和 AppStoreConnect 应用程序名称

ios - Github 操作 : No signing certificate "iOS Development" found

cocoa - 在 Cocoa 中查找插入的 CD 的名称

ios - 从 Xcode 备份应用程序

ios - ARKit 平面检测 - 类型 'ARView' 的值没有成员 'session'

python - OpenCV:增强视频流源 .png 未按预期呈现透明度

swift - 索引标签 UITapGestureRecognizer 在应用于 socket 集合时不响应

swift - 修改枚举关联值

ubuntu - ArtoolKit 修改代码