2017-02-09 95 views
0

在我的ThreeJS應用程序中,如果該對象靠近視圖的中心(並且對象比預定量更近),則視圖擺動以居中對象。我知道所有物體和觀察者(相機)的緯度/經度。但是,我不知道是否有另一個像牆一樣的物體。ThreeJS對象在視圖中?

有沒有辦法讓ThreeJS告訴我該物體是否可以從相機看到?

這個答案似乎是除了它的一個很好的方式爲ThreeJS的早期版本,我不能找到webglObjects陣列以及更高版本的等價物:https://github.com/mrdoob/three.js/issues/3627#issuecomment-20763458

ThreeJS域剔除似乎並沒有得到答案因爲它只告訴我相機是否指向正確的方向,而不是物體的視圖被阻擋。

Raycaster解決方案不好,因爲目標對象不是一個點,它的寬度和高度。

實現我自己的遮擋剔除似乎超出了我的能力,除非某個地方有樣本(我無法找到)。

有什麼建議嗎?

+0

您鏈接到的代碼將無濟於事。 Three.js對它的對象執行簡單的平截頭體剔除,它不會進行手動遮擋剔除。這就是z緩衝區的用途。 –

+0

解決這個問題的實用方法(取決於您的確切要求)是繼續研究基於Raycaster的解決方案:投射多條射線(不僅僅是中心)以合理估計目標對象是否被遮擋。 –

+0

謝謝,這就是我要做的。我會嘗試將物體的光線投射回相機(哪個*是一個點)。 – Carbonsink

回答

0

好的,我已經編寫了一個解決方案,告誡如果相機無法看到其原點,則認爲物體的視圖被阻擋。批評歡迎!我希望這可以幫助未來的人。

// Returns true if a named object blocks the view of the origin point of the target object 
// threeScene and threeCamera are defined elsewhere as: 
// threeScene = new THREE.Scene(); 
// threeScene.add(mesh); 
// ... 
// threeCamera = new THREE.PerspectiveCamera(... 
// ... 
var objectViewBlocked = function(objectName) { 
    if (threeScene) { 
    var object = threeScene.getObjectByName(objectName); 
    if (object) { 
     var direction = new THREE.Vector3(); 
     direction.subVectors(object.position, threeCamera.position); // Subtracting two vectors gives a direction vector to one from another 
     direction.normalize();          // THREE.Raycaster() requires a normalized direction vector 
     var raycaster = new THREE.Raycaster(threeCamera.position, direction); 
     var intersects = raycaster.intersectObjects(threeScene.children, false); // Get list of objects that intersect the ray 
     if (intersects.length > 0) { 
     for (j=0; j<intersects.length; j++) { 
      if ((intersects[j].object !== undefined) && (intersects[j].object['name'] !== objectName) && (intersects[j].object['name'] !== '')) { 
      // Ray intersects an object other than the target object and nameless objects 
      return true; 
      } 
     } 
     } 

     // The ray intersects only the target object and nameless objects (i.e. the grid on the floor, etc.) but nothing that occludes it 
     return false; 
    } 
    } 

    // Scene is null or can't get object 
    return true; 
};