VisionOS Continuously Rotate a 3D Object

I want to open a view in my App that contains a Model3D view and I want that object to rotate continuously around the Y axis while it is visible. Is it possible to animate the rotation of a Model3D view?

I've tried this code, but the object just sits there and doesn't rotate.

import RealityKit
import RealityKitContent
import SwiftUI

struct QuantumComputerArea: View {
    @State var degreesRotating = 0.0
    
    var body: some View {
        VStack {
            Model3D(named: "quantumComputer") { phase in
                switch phase {
                case .empty:
                    ProgressView()
                case let .failure(error):
                    Text(error.localizedDescription)
                case let .success(model):
                    model
                        .resizable()
                        .scaledToFit()
                        .offset(x: -75, y: 0)
                        .rotation3DEffect(.degrees(degreesRotating), axis: (x: 0, y: 1, z: 0))
                        
                @unknown default:
                    fatalError()
                } //phase
            } //Model3D
            .onAppear {
                withAnimation(Animation.linear(duration: 10).repeatForever(autoreverses: false)) {
                    degreesRotating = 360
                }
            }
        } //VStack
    } //View
} //View

I'm probably missing something simple but if anyone has any suggestions (including use a RealityView) I'd be grateful for the advice.

Answered by Aussiefishman in 779917022

Here is the solution:

case let .success(model):
                        model
                            .resizable()
                            .scaledToFit()
                            .offset(x: -100, y: 0)
                            .rotation3DEffect(
                                Rotation3D(angle: Angle2D(degrees: 1 * context.date.timeIntervalSinceReferenceDate),
                                           axis: .y
                                          )
                            )
Accepted Answer

Here is the solution:

case let .success(model):
                        model
                            .resizable()
                            .scaledToFit()
                            .offset(x: -100, y: 0)
                            .rotation3DEffect(
                                Rotation3D(angle: Angle2D(degrees: 1 * context.date.timeIntervalSinceReferenceDate),
                                           axis: .y
                                          )
                            )

I think the full solution would require this to be wrapped in a TimelineView to work? Something such as:

TimelineView(.periodic(from: .now, by: 1)) { context in
   VStack {
            Model3D(named: "quantumComputer") { phase in
                switch phase {
                case .empty:
                    ProgressView()
                case let .failure(error):
                    Text(error.localizedDescription)
                case let .success(model):
                    model
                         .resizable()
                         .scaledToFit()
                          .offset(x: -100, y: 0)
                          .rotation3DEffect(
                                Rotation3D(angle: Angle2D(degrees: 1 * context.date.timeIntervalSinceReferenceDate),
                                           axis: .y
                                          )
                            )
/// Other code in here

    }
}

VisionOS Continuously Rotate a 3D Object
 
 
Q