Next.js和@react-three/drei – 类型错误:无法解析URL

huangapple go评论74阅读模式
英文:

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):

  1. Dynamic import - https://github.com/ItayTur/Portfolio/tree/invalid-url-error-dynamic-solution
  2. Lazy import - https://github.com/ItayTur/Portfolio/tree/invalid-url-error-lazy-solution
  3. Installing the Three & three-stdlib npm packages.
  4. Specify the exact path from the component to the file location.
  5. Adding a dot at the start of the path string.

答案1

得分: 1

Solution:

  1. 下载 threereact-three-fiber 包。
  2. 使用 @react-three/fiber 钩子: useLoader
  • 而不是来自 @react-three/dreiuseGLTF
  1. 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;
  1. 用作其他组件一样
const Hero = () => {
  return (
    <section className={`relative w-full h-screen mx-auto`}>
      <ComputersCanvas />
    </section>
  );
};

export default Hero;
}

GitHub 链接:

  1. 首次工作提交
  2. 完整 PR
英文:

Solution:

  1. Download three and react-three-fiber packages.
  2. Use the @react-three/fiber hook: useLoader.
  • NOT useGLTF from @react-three/drei
  1. Wrap the GLTF model component with a Canvas component.
import { Canvas } from &quot;@react-three/fiber&quot;;
import { GLTFLoader } from &quot;three/examples/jsm/loaders/GLTFLoader&quot;;
import { useLoader } from &quot;@react-three/fiber&quot;;

const Computers = ({ isMobile = false }) =&gt; {
  const gltf = useLoader(GLTFLoader, &quot;/desktop_pc/scene.gltf&quot;);

  return (
    &lt;group&gt;
      &lt;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}
      /&gt;
    &lt;/group&gt;
  );
};

const ComputersCanvas = () =&gt; {
  return (
    &lt;Canvas&gt;
      &lt;Computers /&gt;
    &lt;/Canvas&gt;
  );
};

export default ComputersCanvas;
  1. Use as any other component
const Hero = () =&gt; {
  return (
    &lt;section className={`relative w-full h-screen mx-auto`}
      &lt;ComputersCanvas /&gt;
    &lt;/section&gt;
  );
};

export default Hero;
}

GitHub Links:

  1. First working commit
  2. Full PR

huangapple
  • 本文由 发表于 2023年4月6日 22:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定