I have been struggling immensely to find solutions on how to play the animation that accompanies the .gltf model I downloaded from Sketchfab. Despite my efforts, I have been unable to resolve this issue. I am particularly seeking assistance on how to make the embedded animation work. It would be greatly appreciated if anyone could provide guidance or support. I am developing this project using React. Thank you in advance for your help.
import React, { Suspense, useEffect, useState } from "react";
import { Canvas } from '@react-three/fiber';
import { OrbitControls, Preload, useGLTF } from '@react-three/drei';
import CanvasLoader from '../Loader';
const Computers = () => {
const computer = useGLTF('./robot_playground/scene.gltf')
return (
<mesh>
<hemisphereLight intensity={0.15} groundColor="black" />
<pointLight intensity={1} />
<primitive
object={computer.scene}
scale={1.13}
position={[1.5,-1.5,0.08]}
rotation={[-0.3,-0.5,0]}
/>
</mesh>
);
};
const ComputersCanvas = () => {
return (
<Canvas
frameloop='demand'
shadows
camera={{ position: [0,1.5,3], fov: 65 }}
gl={{ preserveDrawingBuffer: true }}
>
<Suspense>
<Computers />
</Suspense>
<Preload all />
</Canvas>
);
};
export default ComputersCanvas
I tried various methods that I can find in this platform, and I also tried ChatGPT but still no success.
edit: I have checked that the GLTF file path is correct and confirmed that the file contains animations. Despite these efforts, the 3D model remains stationary. I would greatly appreciate any insights or suggestions on how to troubleshoot and resolve this issue. Thank you for your assistance!
import React, { Suspense, useEffect, useState } from "react";
import { Canvas, useFrame } from '@react-three/fiber';
import { OrbitControls, Preload, useGLTF } from '@react-three/drei';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';
import { AnimationMixer } from 'three';
import CanvasLoader from '../Loader';
let mixer = null; // Declare mixer variable outside the component's scope
const Computers = () => {
const gltf = useGLTF('./robot_playground/scene.gltf');
useEffect(() => {
const model = gltf.scene;
const animations = gltf.animations;
if (model && animations && mixer === null) {
mixer = new AnimationMixer(model);
const action = mixer.clipAction(animations[0]);
action.play();
}
}, [gltf]);
useFrame((_, delta) => {
if (mixer) {
mixer.update(delta);
}
});
return (
<mesh>
<hemisphereLight intensity={0.15} groundColor="black" />
<pointLight intensity={1} />
<primitive
object={gltf.scene}
scale={1.13}
position={[1.5,-1.5,0.08]}
rotation={[-0.3,-0.5,0]}
/>
</mesh>
);
};
const ComputersCanvas = () => {
useEffect(() => {
const loader = new GLTFLoader();
loader.load('./robot_playground/scene.gltf', (gltf) => {
const model = gltf.scene;
const animations = gltf.animations;
if (model && animations && mixer === null) {
mixer = new AnimationMixer(model);
const action = mixer.clipAction(animations[0]);
action.play();
}
});
}, []);
return (
<Canvas
frameloop='demand'
shadows
camera={{ position: [0,1.5,3], fov: 65 }}
gl={{ preserveDrawingBuffer: true }}
>
<Suspense>
<Computers />
</Suspense>
<Preload all />
</Canvas>
);
};
export default ComputersCanvas;