Chrome 中的 Three.js 拒绝跨源图像加载
尝试像这样在 THREE.js
中添加材料
var materialWall = new materialClass( { color: 0xffffff, map: THREE.ImageUtils.loadTexture( 'images/a.png' ) } );
它在 Chrome、IE、FF 中运行良好,直到 3 天前,Chrome 将自身更新到最新的开发版本 17 后。Chrome
17 只是无法加载图像并抱怨以下内容
Cross-origin image load denied by Cross-Origin Resource Sharing policy.
这太疯狂了,因为图像显然位于同一个域中,所以这是 chrome 或 THREE.js 或其他问题的问题?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您从本地主机运行 Chrome 并使用 Three.js,您可能需要使用以下命令行标志运行 Chrome:
If you are running Chrome from localhost and using Three.js, you probably need to run Chrome with this command line flag:
https://github.com/mrdoob/ Three.js/issues/687 指的是 Three.js 的 GitHub 上的一个问题,其中有很好的解决方法列表,包括 描述如何在本地运行的 wiki 页面的链接。该线程中还有一些其他解决方法,包括将以下内容添加到脚本中:
或者添加 CORS 标头,以便明确允许它们。
请注意,大部分信息是从现有的问题链接中添加的,而该答案的原始作者并未包含该链接。
https://github.com/mrdoob/three.js/issues/687 refers to an issue on three.js' GitHub, which has good list of workarounds, including a link to a wiki page describing how to run locally. There are also some other workarounds in the thread, including adding the following to your scripts:
Or, adding CORS headers so that they are specifically allowed.
Note that most of this information was added from the already existing link to the issue, which the original author of this answer did not include.
如果您:
,那么我想出了一种解决方法,只需一点点努力:
完整的详细信息可以在 http://tp69.wordpress.com/2013/06/17/cors-bypass/ 对于那些感兴趣的人。
If you:
then I worked out a way around this which involves only a little bit of effort:
Full details can be found at http://tp69.wordpress.com/2013/06/17/cors-bypass/ for those that are interested.
您还可以通过从根文件夹运行以下命令来使用 python 运行简单的 HTTP 服务器。
You can also run a simple HTTP server using python by running the following command from your root folder.
1)Chrome快捷方式->属性->快捷方式选项卡 ->目标和
最后在目标中添加--allow-file-access-from-files。 (在执行此操作之前终止所有 chrome 任务。)
或
2) 下载 Mongoose Web 服务器 软件。将其放入您的工作目录中,然后
运行它。它将在浏览器 http://localhost:PORT 中打开,它将为您的所有文件提供服务。
或
3) 您还可以在应用程序中使用NodeJS 服务器。
1) Chrome shortcut -> Properties -> Shortcut tab -> target and
add --allow-file-access-from-files in target at last. (kill all the chrome tasks before doing this.)
OR
2) Download Mongoose web server software. Put it in your working directory, and
run it. It will open in the browser http://localhost:PORT where it will serve all your files.
OR
3) You can also use NodeJS server in your application.
这在命令行\终端对我有用:
*请注意,在执行命令之前,您必须关闭 chrome 的所有实例才能使其工作。
this worked for me at the command line\terminal:
*note that you must close all instances of chrome before executing the command for it to work.
完美的解决方案:
只需将时间戳添加到图像网址即可。我不知道其背后的逻辑,但它确实有效。
例子:
Perfect solution for:
Just add timestamp to the image url. I don't know the logic behind it, but it works.
Example: