In my previous question I already found how to put a rotation transform over only one axis on an object, now I want this to be animated.
Is there a way to do this in RealityKit?
You can move, rotate and scale a model in RealityKit using .move(...)
instance method. For a faster compiling I used SwiftUI macOS app – although, you can use this code in iOS app as well.
import SwiftUI
import RealityKit
struct ContentView: View {
var body: some View {
ARInterface().ignoresSafeArea()
}
}
struct ARInterface: NSViewRepresentable {
let arView = ARView(frame: .zero)
func makeNSView(context: Context) -> ARView {
let scene = try! Experience.loadBox()
scene.steelBox?.scale = [10, 10, 10]
let transform = Transform(pitch: 0, yaw: 0, roll: .pi)
scene.steelBox?.orientation = transform.rotation
arView.scene.anchors.append(scene)
scene.steelBox?.move(to: transform,
relativeTo: scene.steelBox,
duration: 5.0,
timingFunction: .linear)
return arView
}
func updateNSView(_ uiView: ARView, context: Context) { }
}
For those who prefer to use a matrix math, I recommend reading this post:
Change a rotation of AnchorEntity in RealityKit
For those who like to use dynamics, I give a link to this post:
How to move a model and generate its collision shape at the same time?
To play an asset animation (whether it's a skeletal character animation or a set of transform animations, including a rotation about mesh's pivot point) made in 3D apps, like Maya or Houdini, use an animationPlaybackController:
import Cocoa
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.")
}
}
}
For those who prefer UI, there's a "perpetual" spin behavior in Reality Composer:
Reality Composer - How to rotate an object forever?
USDZ schemas become more and more popular in everyday Python scripting for Pixar's format.
Augmented Reality 911 — USDZ Schemas
float xformOp:rotateY:spin.timeSamples = { 1: 0, 300: 1800 }
uniform token[] xformOpOrder = ["xformOp:rotateY:spin"]
Trigonometric functions sin()
and cos()
, Timer and counter objects will allow you to orbit a model around any axis.
Hovering an entity in front of ARCamera
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With