Loading OBJ model from local machine with Three.js?

北城以北 提交于 2019-12-24 05:13:13

问题


I'm trying to load an OBJ model with Three.js.

At first I tried this:

var loader = new THREE.OBJLoader( );
loader.load( 'chair.obj', function ( object )
{
   scene.add( object );
});

But I got an error saying that cross origin request are only supported in HTTP.

I looked on the internet for help, found this page: https://github.com/mrdoob/three.js/wiki/How-to-run-things-locally

and tried to run Chrome from cmd line: chrome --allow-file-access-from-files

Still nothing.

Finally I tried using Python's built in Http server. and tried loading like this:

var loader = new THREE.OBJLoader( );
loader.load( 'http://localhost:8000/chair.obj', function ( object )            
{
   scene.add( object );

});

Now the scene loads but the model that I tried to load does not show up. In Chrome Dev tools, I see the following error:

XMLHTTPRequest Could not load.Origin null is not allowed by Access-Control-Allow-Origin.

I have expertise with 3D graphics on the desktop, but I am completely new to JavaScript and internet technology. I'm really excited by Three.js and am really motivated to explore it but I have these kinds of hiccups getting set.

I've tried to look this up on the internet but have not succeeded. I really need help. I'd appreciate any pointers anyone might have on how to get past this.

Thanks


回答1:


This sounds like a Chrome security issue of running three.js locally. Firefox would probably show the model without any additional steps.

To get this working, you need to add this line to the Chrome shortcut target

--allow-file-access-from-files

So it would look something like this on Windows

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-file-access-from-files

There is more information on this topic here.



来源:https://stackoverflow.com/questions/19651797/loading-obj-model-from-local-machine-with-three-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!