如何在RealityKit中为转换设置动画

时间:2019-12-14 12:25:38

标签: swift xcode animation transform realitykit

在我的previous question中,我已经找到了如何仅在对象上的一个轴上放置旋转变换,现在我希望将其动画化。

在RealityKit中有办法做到这一点吗?

2 个答案:

答案 0 :(得分:1)

  

以下是在RealityKit 中制作对象动画的方式(为简单起见,我使用macOS应用程序制作了该对象):

import RealityKit

class ViewController: NSViewController {

    @IBOutlet var arView: ARView!
    let boxAnchor = try! Experience.loadBox()

    override func awakeFromNib() {

        boxAnchor.steelBox?.scale = [10, 10, 10]

        let rotation = Transform(pitch: 0, yaw: 0, roll: .pi)

        boxAnchor.steelBox?.orientation = rotation.rotation

        arView.scene.anchors.append(boxAnchor)

        boxAnchor.steelBox?.move(to: rotation,
                         relativeTo: boxAnchor.steelBox,
                           duration: 5.0,
                     timingFunction: .linear)
    }
}

enter image description here

  

或者您可以使用animationPlaybackController来播放3D应用中制作的动画:

import RealityKit

class ViewController: NSViewController {

    @IBOutlet var arView: ARView!

    override func awakeFromNib() {

        do {
            let robot = try ModelEntity.load(named: "drummer")

            let anchor = AnchorEntity(world: [0, -0.7, 0])

            anchor.transform.rotation = simd_quatf(angle: .pi/4, 
                                                    axis: [0, 1, 0])
            arView.scene.anchors.append(anchor)

            robot.scale = [1, 1, 1] * 0.1

            anchor.children.append(robot)

            robot.playAnimation(robot.availableAnimations[0].repeat(), 
                                transitionDuration: 0.5, 
                                      startsPaused: false)
        } catch {
            fatalError("Cannot load USDZ asset.")
        }
    }
}

enter image description here

答案 1 :(得分:0)

动画旋转:

  

复制框的当前变换

var rotationTransform = boxAnchor.steelBox?.transform
  

将框设置为在z轴上旋转90度

rotationTransform?.rotation = simd_quatf(angle: .pi/2, axis: [0,0,1])
  

在10秒钟内将框移动到新的转换

boxAnchor.steelBox?.move(to: rotationTransform!, relativeTo: boxAnchor.steelBox?.parent, duration: 10, timingFunction: .easeInOut)

动画翻译:

var translationTransform = boxAnchor.steelBox?.transform

translationTransform?.translation = SIMD3<Float>(x: 5, y: 0, z: 0)

boxAnchor.steelBox?.move(to: translationTransform!, relativeTo: boxAnchor.steelBox?.parent, duration: 10, timingFunction: .easeInOut)

随着动画缩放:

var scaleTransform = boxAnchor.steelBox?.transform

scaleTransform?.scale = SIMD3<Float>(x: 1, y: 1, z: 1)

boxAnchor.steelBox?.move(to: scaleTransform!, relativeTo: boxAnchor.steelBox?.parent, duration: 10, timingFunction: .easeInOut)