2013-07-17 62 views
1

我意識到這可能是不可能的...我一直在四處尋找並嘗試不同的東西無濟於事,但我認爲這可能值得放棄之前的職位...three.js(canvas/webGL)的跨域圖像,代理?

我在放在一起一個使用three.js(webGL)的應用程序,我想讓用戶可以選擇在Web上輸入任何圖像的URL,並使用它在Web應用程序中構建3D對象。如果不是整個跨域安全問題,這將是沒有問題的。

我知道應該爲CORS批准的圖像做一些變通,儘管我並不完全明白這一點,但我的印象是必須設置在主機端(我的用戶需要能夠拉動一個圖像從網上的任何地方,並作爲紋理使用它)>>我試過這個:https://hacks.mozilla.org/2011/11/using-cors-to-load-webgl-textures-from-cross-domain-images/ ...但它沒有工作(可能是由於我誤解什麼構成「CORS批准」)

我認爲也許做一些類似的PHP代理可能工作?我試過這個:http://benalman.com/code/projects/php-simple-proxy/docs/files/ba-simple-proxy-php.html ...但似乎沒有任何運氣。 (它可能沒有被寫入與圖片...我得到MIME類型錯誤的工作......當我砍死了一下週圍設法擺脫錯誤的...但仍沒有運氣)

。 ..有人在那裏可以幫助!

+0

你總是可以從其他服務器下載圖像,然後使用它。 –

回答

0

EUREKA! LOOX像代理的路要走, 這並獲得成功:)

<?php 
// PHP Proxy 
// Responds to both HTTP GET and POST requests 
// 
// Author: Abdul Qabiz 
// March 31st, 2006 
// 

// Get the url of to be proxied 
// Is it a POST or a GET? 
$url = ($_POST['url']) ? $_POST['url'] : $_GET['url']; 
$headers = ($_POST['headers']) ? $_POST['headers'] : $_GET['headers']; 
$mimeType =($_POST['mimeType']) ? $_POST['mimeType'] : $_GET['mimeType']; 


//Start the Curl session 
$session = curl_init($url); 

// If it's a POST, put the POST data in the body 
if ($_POST['url']) { 
    $postvars = ''; 
    while ($element = current($_POST)) { 
     $postvars .= key($_POST).'='.$element.'&'; 
     next($_POST); 
    } 
    curl_setopt ($session, CURLOPT_POST, true); 
    curl_setopt ($session, CURLOPT_POSTFIELDS, $postvars); 
} 

// Don't return HTTP headers. Do return the contents of the call 
curl_setopt($session, CURLOPT_HEADER, ($headers == "true") ? true : false); 

curl_setopt($session, CURLOPT_FOLLOWLOCATION, true); 
//curl_setopt($ch, CURLOPT_TIMEOUT, 4); 
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

// Make the call 
$response = curl_exec($session); 

if ($mimeType != "") 
{ 
    // The web service returns XML. Set the Content-Type appropriately 
    header("Content-Type: ".$mimeType); 
} 

echo $response; 

curl_close($session); 

?> 
2

我發現,three.js所WebGL的+ CORS沒有爲我使用THREE.ImageUtils.loadTexture功能時使用。

此代碼爲我做的工作,雖然(注:corsproxy.com不一樣尼克的答案PHP)

var url = 'http://www.corsproxy.com/yourdomain/yourfolder/yourimage.png';  
var image = document.createElement('img'); 
image.crossOrigin = ''; 
image.src = url; 
var texture = new THREE.Texture(image); 
texture.needsUpdate = true; 
material.map = texture;