英文:
nextjs & @react-three/drei - TypeError: Failed to parse URL
问题
正在使用 @react-three/drei 构建 nextjs 应用程序,实现 3D 动画。gltf 文件位于 public 文件夹下的 desktop_pc 文件夹中,文件名为 scene.gltf。项目根目录路径为:/public/desktop_pc/scene.gltf。在将 gltf 文件导入 useGLTF 钩子时,出现错误:TypeError: Failed to parse URL from /desktop_pc/scene.gltf。
Github Repository - nextjs & @react-three/drei - TypeError: Failed to parse URL
英文:
I'm building a nextjs app with 3D animations using @react-three/drei.
I have a gltf file in the public folder, under the desktop_pc folder called scene. gltf.
The path from the project root is: /public/desktop_pc/scene.gltf.
When importing the gltf file into the useGLTF hook, I'm getting the error:
TypeError: Failed to parse URL from /desktop_pc/scene.gltf
Github Repository - nextjs & @react-three/drei - TypeError: Failed to parse URL
This is the component code:
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;
These are my attempts so far (The links leads to the GitHub solution of them):
- Dynamic import - https://github.com/ItayTur/Portfolio/tree/invalid-url-error-dynamic-solution
- Lazy import - https://github.com/ItayTur/Portfolio/tree/invalid-url-error-lazy-solution
- Installing the
Three&three-stdlibnpm packages. - Specify the exact path from the component to the file location.
- Adding a dot at the start of the path string.
答案1
得分: 1
Solution:
- 下载
three和react-three-fiber包。 - 使用
@react-three/fiber钩子:useLoader。
- 而不是来自
@react-three/drei的useGLTF
- 用
Canvas组件包装GLTF模型组件。
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 链接:
英文:
Solution:
- Download
threeandreact-three-fiberpackages. - Use the
@react-three/fiberhook:useLoader.
- NOT
useGLTFfrom@react-three/drei
- Wrap the
GLTFmodel component with aCanvascomponent.
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;
- Use as any other component
const Hero = () => {
return (
<section className={`relative w-full h-screen mx-auto`}
<ComputersCanvas />
</section>
);
};
export default Hero;
}
GitHub Links:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论