I'm trying to make a model rotate a full rotation repeatedly forever using the following code:
func loadModel(named modelName: String) {
if let modelEntity = try? Entity.loadModel(named: modelName) {
modelEntity.generateCollisionShapes(recursive: true)
modelEntity.transform.rotation = simd_quatf(angle: .pi/2, axis: [0, 1, 0])
let anchorEntity = AnchorEntity(world: position)
anchorEntity.addChild(modelEntity)
installGestures(for: modelEntity)
self.scene.addAnchor(anchorEntity)
//***Animation***
var transform = modelEntity.transform
modelEntity.transform.rotation = simd_quatf(angle: -2 * .pi, axis: [0, 1, 0])
let animationDefinition = FromToByAnimation(to: transform, duration: 3, bindTarget: .transform, repeatMode: .repeat)
let animationViewDefinition = AnimationView(source: animationDefinition)
let animationResource = try! AnimationResource.generate(with: animationViewDefinition)
modelEntity.playAnimation(animationResource)
}
}
However, the model rotates just around 90 degrees and restart repeatedly.
Question 1: How can I make the model make a full rotation (360 degrees) repeatedly instead?
Also, I found that after adding the animation to the model, I can't control (translate, rotate, scale) the model by gesture anymore.
Question 2: How can I keep controlling by gesture after adding the animation?
If you need the equivalent of SceneKit's .repeatForever action in RealityKit framework, use just a positive or negative .pi
value rather than double PI
. To control the speed of rotation, use duration
parameter of FromToByAnimation struct.
model.transform.rotation = .init(angle: .pi, axis: [1, 1, 0])