简体   繁体   English

Three.js-无法将本地图像用于loadTexture

[英]Three.js - Cannot use local image for loadTexture

i am trying to use a local image for loadTexture in Three.js but i get the following error: Uncaught SecurityError: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at .... may not be loaded. 我正在尝试在Three.js中为loadTexture使用本地图像,但是出现以下错误: Uncaught SecurityError:无法在'WebGLRenderingContext'上执行'texImage2D':可能无法加载....上的跨源图像。

I have a java application running on apache tomcat (locally) which serves my image with the following url: http://localhost:8084/sve/img/wood-texture.jpg . 我有一个在Apache Tomcat(本地)上运行的Java应用程序,该应用程序通过以下URL为我的图像提供服务: http:// localhost:8084 / sve / img / wood-texture.jpg

Here is my javascript code where i try to load the image: 这是我尝试加载图像的JavaScript代码:

var woodTexture = new THREE.ImageUtils.loadTexture('http://localhost:8084/sve/img/wood-texture.jpg');

I am using Google Chrome (Version 40.0.2214.111 m) to run my Three.js program. 我正在使用Google Chrome浏览器(版本40.0.2214.111 m)运行Three.js程序。 I also try to start chrome like this: chrome.exe --allow-file-access-from-files. 我也尝试像这样启动chrome:chrome.exe --allow-file-access-from-files。 But i get the same error. 但是我得到同样的错误。

Does anyone have any solution for this? 有人对此有任何解决方案吗?

It's probably easiest to make sure the texture is served by the same server as your three.js app. 确保纹理与Three.js应用程序由同一服务器提供服务是最容易的。

If you still need it to come from another server (the Java app on the other port in your example) you need to make the Java app set some CORS headers. 如果仍然需要它来自其他服务器(示例中其他端口上的Java应用程序),则需要使Java应用程序设置一些CORS标头。

For example: "Access-Control-Allow-Origin: *" 例如: "Access-Control-Allow-Origin: *"

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS for more info. 有关更多信息,请参见https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM