Skip to content

Can't play .glb animation with RealityKit #78

@HalTobin

Description

@HalTobin

Hello,

I'm working on an app using RealityKit and .glb files. The models are loaded and displayed correctly, but their animations simply don't play.

I've tried the following:

  • RealityKit + GLTFKit2 + .glb file (Not working)
  • Filament + .glb file (Working)

Here's my current code:

public struct RealityKitRendererSimpleView: View {    
    let element: ArElement
    
    public init(element: ArElement) {
        self.element = element
    }
    
    public var body: some View {
        ZStack {
            RealityView { content in
                content.camera = .virtual
                
                let light = DirectionalLight()
                light.light.intensity = 2000
                light.position = [0, 3, 3]
                content.add(light)
                
                if let entity = await loadModel(element) {
                    for animation in entity.availableAnimations {
                        entity.playAnimation(
                            animation.repeat(duration: .infinity),
                            transitionDuration: 1.25,
                            startsPaused: false
                        )
                    }
                    
                    content.add(entity)
                }
            }
            .realityViewCameraControls(.orbit)
            .gesture(DragGesture(minimumDistance: 0).simultaneously(with: MagnificationGesture()))
            .background(Color.black.opacity(0.02))
        }
        .ignoresSafeArea(edges: .bottom)
    }
    
    private func loadModel(_ arElement: ArElement) async -> Entity? {
        let arModel = arElement.model
        let url = URL(fileURLWithPath: arElement.model.path)
        
        print("Loading: \(arElement.id)")
            
        switch url.pathExtension {
        case "usdz": do {
            print("USDZ Loader")
            do {
                let entity = try Entity.load(contentsOf: url)
                entity.name = String(arElement.id)
                
                let scaleFactor = arModel.scaleFactor?.floatValue ?? 0.5
                let yOffset = arModel.zOffset?.floatValue ?? 0.0
                entity.scale = .init(repeating: scaleFactor)
                entity.position = [0, yOffset, -1]
                
                return entity
            } catch {
                print("Couldn't load: \(arElement.id), error: \(error)")
            }
        }
        case "glb", "gltf": do {
            print("GLTF Loader")
            do {
                let entity = try await GLTFRealityKitLoader.load(from: url)
                entity.name = String(arElement.id)
                
                let scaleFactor = arModel.scaleFactor?.floatValue ?? 0.5
                let yOffset = arModel.zOffset?.floatValue ?? 0.0
                entity.scale = .init(repeating: scaleFactor)
                entity.position = [0, yOffset, -1]
                                
                return entity
            } catch {
                print("Couldn't load: \(arElement.id), error: \(error)")
            }
        }
        default:
            print("Unknown file format: \(url.pathExtension)")
        }
        
        return nil
    }
        
}

I've also attached a screenshot with the .glb loaded and a .zip containing the .glb file that I'm using.

tyrannosaurus.glb.zip

Image

Thank you for reading this and have a great day!
Alexis

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions