three.js gltfloader不用react渲染
对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>
);
}
如果有人可以提供帮助,那将是很多重新计算的。 谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您的模型路径是错误的?
我认为您应该使用相对路径或正确的绝对路径修复它。
当我一开始使用三j时,这是我身上发生的,并且在单页应用程序中为我的
.gltf
编写了错误的路径复制错误和原因
我做了“ trix.js import gltf” sandbox的叉子在codesandbox上 sandbox,在那里我用随机的,错误的一条路径替换了路径。
如果打开控制台(在沙盒中 fork ),您会看到相同的错误。
这是因为获取仍然具有状态
200
,但带有html
而不是模型的JSON,来自沙盒
由于路径是错误的,因此服务器返回您的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-appReplication 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 withHTML
instead of your model's JSON,From the sandbox

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
Conclusion
Try to check out the network devtools while running your website to check if your
.gltf
fetch response isHTML
(intead of your model JSON).If so, your model path is wrong.
谢谢,我似乎发现了错误,这是相机太大的组合,模型是错误的路径,而得克萨斯人没有加载,您非常感谢您帮助我解决这个问题!
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!