【发布时间】:2014-01-08 17:06:13
【问题描述】:
Here你会找到一个jsFiddle改编的问题。
我想创建一个 3d Web 应用程序,用户可以在其中选择本地计算机上的图像文件:
<input id="userImage" type="file"/>
选择文件时,图像会作为参数加载到 THREE.ShaderMaterial 对象中。将 glsl 着色器应用于图像,并将结果渲染到浏览器中的容器:
$("#userImage").change(function(){
var texture = THREE.ImageUtils.loadTexture( $("#userImage").val() );
texture.image.crossOrigin = "anonymous";
shader.uniforms.input.value = texture;
shader.uniforms.input.needsUpdate = true;
});
很遗憾,这会导致以下错误:
Cross-origin image load denied by Cross-Origin Resource Sharing policy.
我知道尝试在 WebGL 中访问跨域资源存在问题,但同时我看到 official specification for WebGL 中提供了以下解决方法:
以下 ECMAScript 示例演示了如何为来自另一个域的图像发出 CORS 请求。图片是在没有任何凭据(即 cookie)的情况下从服务器获取的。
var gl = ...;
var image = new Image();
// The onload handler should be set to a function which uploads the HTMLImageElement
// using texImage2D or texSubImage2D.
image.onload = ...;
image.crossOrigin = "anonymous";
image.src = "http://other-domain.com/image.jpg";
在我的代码中,您可以看到我为图像对象指定了 crossOrigin 参数,但我仍然收到错误消息。我的示例与规范有何不同?甚至可以像使用托管在另一台服务器上的资源一样使用 WebGL 中的本地资源吗?除此之外,我还应该考虑其他解决方法来完成这项任务吗?
【问题讨论】:
标签: javascript opengl-es three.js webgl cors