当前位置:   article > 正文

three.js加载模型失败(跨域问题)_vue打包threejs后所有模型报跨域

vue打包threejs后所有模型报跨域
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>test Load OBJ model </title>
  6. <script type="text/javascript" src="three.js-dev/build/three.js"></script>
  7. <script type="text/javascript" src="three.js-dev/examples/js/loaders/OBJLoader.js"></script>
  8. <style>
  9. body {
  10. /* set margin to 0 and overflow to hidden, to go fullscreen */
  11. margin: 0;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <!-- Javascript code that runs our Three.js examples -->
  18. <script type="text/javascript">
  19. function init() {
  20. var scene = new THREE.Scene();
  21. var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
  22. var webGLRenderer = new THREE.WebGLRenderer();
  23. webGLRenderer.setClearColor(new THREE.Color(0xaaaaff, 1.0));
  24. webGLRenderer.setSize(window.innerWidth, window.innerHeight);
  25. webGLRenderer.shadowMapEnabled = true;
  26. camera.position.x = 130;
  27. camera.position.y = 140;
  28. camera.position.z = 150;
  29. camera.lookAt(scene.position);
  30. scene.add(camera);
  31. var spotLight = new THREE.DirectionalLight(0xffffff);
  32. spotLight.position.set(30, 40, 50);
  33. spotLight.intensity = 1;
  34. scene.add(spotLight);
  35. var step = 0;
  36. var controls = new function () {
  37. // we need the first child, since it's a multimaterial
  38. };
  39. /* 加载OBJLoader*/
  40. var mesh;
  41. var loader = new THREE.OBJLoader();
  42. loader.load('venusl.obj', function (loadedMesh) {
  43. var material = new THREE.MeshLambertMaterial({color: 0x5C3A21});
  44. loadedMesh.children.forEach(function (child) {
  45. child.material = material;
  46. child.geometry.computeFaceNormals();
  47. child.geometry.computeVertexNormals();
  48. });
  49. mesh = loadedMesh;
  50. loadedMesh.scale.set(1, 1, 1);
  51. loadedMesh.rotation.x = -0.3;
  52. scene.add(loadedMesh);
  53. });
  54. render();
  55. function render() {
  56. if (mesh) {
  57. mesh.rotation.y += 0.006;
  58. mesh.rotation.x += 0.006;
  59. }
  60. requestAnimationFrame(render);
  61. webGLRenderer.render(scene, camera);
  62. }
  63. }
  64. window.onload = init;
  65. </script>
  66. </body>
  67. </html>

使用three.js加载一个obj模型;

在360急速浏览器出错如下;

Indicate whether to send a cookie in a cross-site request by specifying its SameSite attribute............

这是跨域问题
Google 在2020年2月4号发布的 Chrome 80 版本(schedule:https://www.chromestatus.com/features/schedule)中默认屏蔽所有第三方 Cookie,即默认为所有 Cookie 加上 SameSite=Lax 属性(https://www.chromestatus.com/feature/5088147346030592),并且拒绝非Secure的Cookie设为 SameSite=None(https://www.chromestatus.com/feature/5633521622188032)
SameSite的作用就是:防止跨域传送cookie,从而防止 CSRF 攻击和用户追踪,此举是为了从源头屏蔽 CSRF 漏洞。
参考:https://blog.csdn.net/yhyc812/article/details/108623844

在搜狗浏览器出错如下;

has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes:

已经被CORS策略阻止.....;


CORS,全称Cross-Origin Resource Sharing ,是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domain)的脚本请求访问的机制,通常由于同域安全策略(the same-origin security policy)浏览器会禁止这种跨域请求。

也就是浏览器禁止加载一个本地的obj文件,任何的外部文件;为安全起见;

如果做成asp.net项目;

也是报一样的错;

 

从前端调试看一下Network标签;浏览器可以获取到各个资源,自己的代码文件,three.js、OBJLoader.js;只有模型文件不能获取到;

 

 

下回继续; 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/687629
推荐阅读
相关标签
  

闽ICP备14008679号