我正在使用
nextjs
构建一个带有 3D 动画的 @react-three/drei
应用程序。
我在
gltf
文件夹中有一个public
文件,在desktop_pc
文件夹下,名为scene. gltf
.
从项目根目录开始的路径是:
/public/desktop_pc/scene.gltf
.
将
gltf
文件导入useGLTF
钩子时,出现错误:
TypeError: Failed to parse URL from /desktop_pc/scene.gltf
Github 存储库 - nextjs 和 @react-three/drei - 类型错误:无法解析 URL
这是组件代码:
import { useGLTF } from "@react-three/drei";
import React from "react";
const Computers = () => {
const computer = useGLTF("/desktop_pc/scene.gltf");
return <div>Computers</div>;
};
export default Computers;
这些是我到目前为止的尝试(链接指向它们的 GitHub 解决方案):
Three
和 three-stdlib
npm 包。解决方案:
three
和react-three-fiber
包。@react-three/fiber
挂钩:useLoader
.useGLTF
来自@react-three/drei
GLTF
组件包裹Canvas
模型组件。import { Canvas } from "@react-three/fiber";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
import { useLoader } from "@react-three/fiber";
const Computers = ({ isMobile = false }) => {
const gltf = useLoader(GLTFLoader, "/desktop_pc/scene.gltf");
return (
<group>
<primitive
scale={isMobile ? 0.7 : 0.75}
position={isMobile ? [0, -3, -2.2] : [0, -3.25, -1.5]}
rotation={[-0.01, -0.2, -0.1]}
object={gltf.scene}
/>
</group>
);
};
const ComputersCanvas = () => {
return (
<Canvas>
<Computers />
</Canvas>
);
};
export default ComputersCanvas;
const Hero = () => {
return (
<section className={`relative w-full h-screen mx-auto`}
<ComputersCanvas />
</section>
);
};
export default Hero;
}
GitHub 链接: