Reputation: 91
Well I have .gltf animated model. I was successful in loading the model but wasn't able to play the embedded animation. I would like to know if in any way one can resolve it. Btw I am working this in react. Thank you in advance.
Here you can find the model https://drive.google.com/file/d/1ZVyklaQuqKbSliu33hFxdyNpg4EQtcBH/view?usp=sharing
Here is the code that I tried.
import * as THREE from 'three'
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
import React, { Suspense,useRef, useEffect, useState } from 'react'
import { Canvas ,extend, useThree, useFrame} from 'react-three-fiber'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
import './style.module.css'
extend({ OrbitControls })
function Hardware() {
const [scene, set] = useState()
useEffect(() => {
new GLTFLoader().load("tern_construct_animation.gltf", gltf => {
set(gltf.scene)
const mixer = new THREE.AnimationMixer(gltf.scene)
gltf.animations.forEach(clip => mixer.clipAction(clip).play())
})
}, [])
return scene ? <primitive object={scene} /> : null
}
const Controls = () => {
const orbitRef = useRef()
const { camera, gl } = useThree()
useFrame(() => {
orbitRef.current.update()
})
return (
<orbitControls
autoRotate
maxPolarAngle={Math.PI / 3}
minPolarAngle={Math.PI / 3}
args={[camera, gl.domElement]}
ref={orbitRef}
/>
)
}
const animated_element = () => {
return (
<Canvas camera={{ position: [0, 0, 5] }}>
<ambientLight intensity={2} />
<pointLight position={[40, 40, 40]} />
<Controls/>
<Suspense fallback={null}>
<Hardware/>
</Suspense>
</Canvas>
)
}
export default animated_element;
Upvotes: 8
Views: 12698
Reputation: 434
If you're using @react-three/drei
you can also do the following:
import {useAnimations} from '@react-three/drei'
const model = useLoader(
GLTFLoader,
props.path
)
const modelAnimations = useAnimations(model.animations)
useEffect(() => {
modelAnimations.actions[modelAnimations.names].play()
}, [])
Upvotes: 2
Reputation: 890
I struggled with the same.
Here's the code that worked for me.
Obviously this component is wrapped with Suspense
higher in the tree.
import { useLoader, useFrame } from 'react-three-fiber';
import {
GLTFLoader
} from 'three/examples/jsm/loaders/GLTFLoader';
import * as THREE from 'three'
const Model = props => {
const model = useLoader(
GLTFLoader,
props.path
)
// Here's the animation part
// *************************
let mixer
if (model.animations.length) {
mixer = new THREE.AnimationMixer(model.scene);
model.animations.forEach(clip => {
const action = mixer.clipAction(clip)
action.play();
});
}
useFrame((state, delta) => {
mixer?.update(delta)
})
// *************************
model.scene.traverse(child => {
if (child.isMesh) {
child.castShadow = true
child.receiveShadow = true
child.material.side = THREE.FrontSide
}
})
return (
<primitive
object={model.scene}
scale={props.scale}
/>
)
}
export default Model;
Upvotes: 15