我从 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>
我认为这是库导入的问题,但我不明白错误。我该如何解决这个问题?