在纹理中加载跨域图像
问题描述:
如何在textureloader 中加载跨域图像并且我收到无法在'WebGLRenderingContext' 上执行'texImage2D':跨域..."错误
How to load cross domain image in textureloader and im getting "Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin..." error
var loader = new THREE.TextureLoader();
loader.load('http://myurl.com/img/box.png', onTextureLoaded);
答
首选
为资产添加 CORS 标头,允许跨域请求:
Add a CORS header to the asset, permitting cross-domain requests:
Access-Control-Allow-Origin: *
第二种选择
您可以创建一个服务器端代理,然后像这样加载资产
You can create a server side proxy, and then load the asset like this
loader.load('myproxy?url=http://myurl.com/img/box.png', onTextureLoaded);
但是您需要小心,因为代理可能会占用大量带宽,并且您在设置时需要非常彻底,以免意外打开服务器受到某些代码注入攻击.
However you need to be careful, as proxies can use up a lot of bandwidth and you need to be very thorough when setting it up so you don't accidentally open your server to some code injection attacks.