为什么我的3D模型不能使用glTF加载器加载到three.js中?

问题描述 投票:-1回答:1

我是第一次将对象加载到three.js中。我在我的电脑上下载了该对象。 three.js库连接到我的html文档以及glTF加载器。但是当我去运行代码时,我一直收到这个错误:

three_js_library.js:713来自'null'的'file:/// C:/Users/Firstname%20Lastname/Desktop/neuron.glb'访问XMLHttpRequest已被CORS策略阻止:仅支持协议的交叉源请求方案:http,数据,chrome,chrome-extension,https。

所以我认为由于CORS策略而加载我的3d模型时遇到了一些麻烦。我尝试了reading the CORS policy,那是什么,但它没有帮助我解决我在屏幕上显示3D模型而没有错误的问题。

然后我尝试下载CORS everywhere extension for firefox浏览器,我从forum了解到类似的问题,但这并没有产生预期的结果。它在Firefox上返回了这个错误,我认为这是我从谷歌获得的错误:

跨源请求已阻止:同源策略禁止在file:/// C:/Users/Firstname%20Lastname/Desktop/neuron.glb读取远程资源。 >>(原因:CORS请求不是http)。

我只是想在屏幕上成功加载3D模型。

如果有人能解决我做错了什么以及如何让我的模型加载到屏幕上,那将非常感激。

这是代码:

HTML

 <!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8>
    <title>My first three.js app</title>
    <style>
        body { margin: 0; }
        canvas { width: 100%; height: 100% }
    </style>
</head>
<body>
    <script src="three_js_library.js"></script>
    <script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/loaders/GLTFLoader.js"></script>
    <script src='MS_Interactive_Sim.js'>    </script>
</body>

使用Javascript

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const loader = new THREE.GLTFLoader();
loader.load( 'neuron.glb', function ( gltf ) {
scene.add( gltf.scene );
}, undefined, function ( error ) {
console.error( error );
} );
const animate =()=>{
    requestAnimationFrame( animate);
    renderer.render(scene, camera);
}
animate();
javascript three.js 3d cors xmlhttprequest
1个回答
1
投票

您收到此错误是因为浏览器具有相同来源的策略安全限制。 here描述了一些解决方案。我建议您从本地服务器运行文件,例如MAMP

© www.soinside.com 2019 - 2024. All rights reserved.