Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t加载带有3.js和ObjLoader2的obj文件_Javascript_Three.js_3d_Objloader - Fatal编程技术网

Javascript Can';t加载带有3.js和ObjLoader2的obj文件

Javascript Can';t加载带有3.js和ObjLoader2的obj文件,javascript,three.js,3d,objloader,Javascript,Three.js,3d,Objloader,我从cdn ObjLoader2()导入。我试图加载obj文件,但在控制台上出现两个错误: Access to XMLHttpRequest at 'myfilepath.obj' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome

我从cdn ObjLoader2()导入。我试图加载obj文件,但在控制台上出现两个错误:

Access to XMLHttpRequest at 'myfilepath.obj' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
而且

three.module.js:35911 GET myfilepath.obj net::ERR_FAILED
load    @   three.module.js:35911
load    @   OBJLoader2.js:301
(anonymous) @   index.html:31
这是我的密码:

  <script src="js/three.js"></script>
  <script src="js/three.min.js"></script>
  <script type="module">
      import { OBJLoader2 } from "https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/loaders/OBJLoader2.js";
      import { OrbitControls } from "https://threejsfundamentals.org/threejs/resources/threejs/r122/examples/jsm/controls/OrbitControls.js";
      const canvas = document.querySelector("#canvasObj");
      const renderer = new THREE.WebGLRenderer({ canvas });
      const fov = 45;
      const aspect = 2;
      const near = 0.1;
      const far = 5;
      const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
      camera.position.set(0, 5, 2);
      const scena = new THREE.Scene();
      const objLoader = new OBJLoader2();
      objLoader.load("Iphone8.obj", (root) => {
        scena.add(root);
        render();
      });
        renderer.render(scena, camera);
    </script>

从导入{OBJLoader2}”https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/loaders/OBJLoader2.js";
从导入{OrbitControls}”https://threejsfundamentals.org/threejs/resources/threejs/r122/examples/jsm/controls/OrbitControls.js";
const canvas=document.querySelector(#canvasObj”);
const renderer=new THREE.WebGLRenderer({canvas});
常数fov=45;
常量方面=2;
常数近=0.1;
常数far=5;
常量摄影机=新的三个透视摄影机(视野、方位、近距离、远距离);
摄像机位置设置(0,5,2);
const scena=new THREE.Scene();
const objLoader=new OBJLoader2();
objLoader.load(“Iphone8.obj”,(根)=>{
添加(根);
render();
});
渲染器。渲染(场景、摄影机);

我认为库导入有问题,但我不理解错误。如何解决此问题?

看起来您正在本地运行,并且由于浏览器的同源策略安全限制,从文件系统加载将失败并出现安全异常

您需要在本地web服务器上运行代码才能加载三维模型

您可以在three.js的官方网站和文档上了解如何使用


希望这有帮助!祝你好运

是的,在30分钟的火灾之后:,)我启动了一个VisualStudioLive服务器,它成功了,无论如何,谢谢!:)