import { useLoader } from 'react-three-fiber';
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader';
function Ocean() {
const objRef = useRef();
const { nodes } = useLoader(OBJLoader, '/path/to/file.obj');
useEffect(() => {
if (objRef.current) {
objRef.current.position.y = 0;
objRef.current.position.z = -10;
objRef.current.rotation.y = Math.PI;
objRef.current.scale.set(10, 10, 10);
const animation = nodes[0].children[0].animations[0];
const mixer = new THREE.AnimationMixer(objRef.current);
const action = mixer.clipAction(animation);
action.timeScale = 4;
action.setLoop(THREE.LoopRepeat);
action.play();
}
}, [nodes]);
return ;
}
注意,在这个代码示例中,动画通过THREE.AnimationMixer来控制。因此,在使用“useEffect”钩子初始化动画时,需要导入并使用THREE库。