three.js gltfloader不用react渲染

发布于 2025-02-12 03:12:26 字数 1520 浏览 0 评论 0 原文

对react和three.js都是很新的,我试图使用方法 const loader = new Gltfloader()呈现.gltf文件,但是当呈现网站时,我会在浏览器中查看它错误消息,发生了一个错误SyntaxError:意外的令牌<在位置0 的JSON中,如果有人知道将我指向什么方向,那就太好了。

这是代码

rende.js:

import * as THREE from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
export default class Rende extends Component {
  componentDidMount() {
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      0.1,
      1000
    );
    var renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(0x424242, 1);
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.getElementById("scene").appendChild(renderer.domElement);

    const loader = new GLTFLoader();
    loader.load(
      "untitled.gltf",
      (gltf) => {
        scene.add(gltf.scene);
      },
      (xhr) => {
        console.log(`${(xhr.loaded / xhr.total) * 100}% loaded`);
      },
      (error) => {
        console.error("An error happened", error);
      }
    );
 
  }
  render() {
    return <div id="scene" className="potato"></div>;
  }
}

和corexpents组件页面

home.jsx:

export default function Home() {
  return (
    <Fragment>
      {" "}
      <div key={1} className="class">
        tagline
      </div>
      <Rende />
    </Fragment>
  );
}

如果有人可以提供帮助,那将是很多重新计算的。 谢谢

am quite new to both react and three.js, im trying to render a .gltf file using the method const Loader = new GLTFLoader() however when the website is rendered and i view it in browser i see the error message, An error happened SyntaxError: Unexpected token < in JSON at position 0, if anyone would know what direction to point me in that would be great.

here's the code

rende.js:

import * as THREE from "three";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
export default class Rende extends Component {
  componentDidMount() {
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      0.1,
      1000
    );
    var renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(0x424242, 1);
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.getElementById("scene").appendChild(renderer.domElement);

    const loader = new GLTFLoader();
    loader.load(
      "untitled.gltf",
      (gltf) => {
        scene.add(gltf.scene);
      },
      (xhr) => {
        console.log(`${(xhr.loaded / xhr.total) * 100}% loaded`);
      },
      (error) => {
        console.error("An error happened", error);
      }
    );
 
  }
  render() {
    return <div id="scene" className="potato"></div>;
  }
}

and the coresponding component page

home.jsx:

export default function Home() {
  return (
    <Fragment>
      {" "}
      <div key={1} className="class">
        tagline
      </div>
      <Rende />
    </Fragment>
  );
}

if anyone can help that would be much apreciated.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江挽川 2025-02-19 03:12:26

也许您的模型路径是错误的?

我认为您应该使用相对路径或正确的绝对路径修复它。

当我一开始使用三j时,这是我身上发生的,并且在单页应用程序中为我的 .gltf 编写了错误的路径


复制错误和原因

我做了“ trix.js import gltf” sandbox的叉子在codesandbox上 sandbox,在那里我用随机的,错误的一条路径替换了路径。

如果打开控制台(在沙盒中 fork ),您会看到相同的错误。

这是因为获取仍然具有状态 200 ,但带有 html 而不是模型的JSON,

由于错误的路径和单个页面应用程序的服务器的行为,通常它返回index.html,除非它是属于其中一个资产的路径,默认情况下使用ReactJS和ReactJ和其他库/框架。

来自沙盒

由于路径是错误的,因此服务器返回您的HTML,而不是模型,因此JSON解析器会丢弃您的同一错误,因为实际上,第一个char resed是&lt;

...正如您在此屏幕上从浏览器开发器&GT中看到的那样。网络选项卡,运行SandBox

”在此处输入图像说明”


结论

在运行网站时尝试查看网络DevTools,以检查您的 .gltf Fetch响应是 html (模型JSON的INTEAD)。
如果是这样,您的模型路径是错误的。

Maybe the path of your model is wrong?

I think you should fix it with a relative path, or the right absolute one.

It happened to me firsthand when I worked with threejs at the beginning and was writing the wrong path for my .gltf in a single-page-app


Replication of the error and reasons

I made a fork of a "Three.js import GLTF" sandbox on codesandbox, where I replaced the path with a random, wrong one.

If you open the console (in the sandbox fork), you will see your same error.

This is because the fetch still has the status 200 but with HTML instead of your model's JSON,

due to the wrong path and the behavior of the server of a single page application, which, generally, returns the index.html, unless it is a path belonging to one of the assets, by default with reactjs and other libraries/frameworks.

From the sandbox
enter image description here

Since the path is wrong, the server returns your html instead of your model, so the JSON parser throws your same error, because actually, the first char recived is <.

...as you can see in this screen from the browser dev-tools > Network tab, while running the sandbox

enter image description here


Conclusion

Try to check out the network devtools while running your website to check if your .gltf fetch response is HTML (intead of your model JSON).
If so, your model path is wrong.

一曲爱恨情仇 2025-02-19 03:12:26

谢谢,我似乎发现了错误,这是相机太大的组合,模型是错误的路径,而得克萨斯人没有加载,您非常感谢您帮助我解决这个问题!

Thank you i seem to have found the error, it was a combination of the camera being too zoomed in, the model being the wrong path and the textrue not loading in, that you very much for helping me solve this!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文