2015-07-20 57 views
3

我想對BufferGeometry創建的行進行光線投射。但它似乎不支持光線投射?對BufferGeometry Raycasting行threejs

當啓動BufferGeometry如圖所示here光線投射不適用於此對象。

但是,當我用Geometry代替BufferGeometry時,光線投射工作正常。

var geometry = new THREE.Geometry(); 
var lines = new THREE.Object3D(); 

for (var i = 0; i <array.length; i++) { 
    x = (Math.random() - 0.5) * 30; 
    y = (Math.random() - 0.5) * 30; 
    z = (Math.random() - 0.5) * 30; 
    geometry.vertices.push(new THREE.Vector3(x,y,z)); 
} 

var line = new THREE.Line(geometry, new THREE.LineBasicMaterial({ color: 0x9999FF, opacity: 0.5 })); 
lines.add(line); 
scene.add(lines); 

我試着換BufferGeometryObject3D也不會影響結果。如何對BufferGeometry行進行光線投射?

編輯

Fiddle with BufferGeometry

Fiddle with Geometry

+0

你能提供一個小提琴來演示問題嗎? – WestLangley

+0

@WestLangley工作在小提琴上時,我發現它不是由'BufferGeometry'造成的。我的線太小,我無法用射線擊中它。所以問題解決了,感謝評論! – user3960875

+0

我錯了,它不是太小。它總是相交,可能是某處的錯誤。 – user3960875

回答

0

我的工作一點上您BufferGeometry例如,現在,它的工作原理

http://jsfiddle.net/eviltoad/s9fsds19/11/

material.originalColor = material.color; 
// line 
lines = new THREE.Object3D(); 
line = new THREE.Line(geometry, material); 
lines.add(line); 
scene.add(lines); 

document.onmousemove = function(event){ 
    mousePosition.x = 2 * (event.clientX/window.innerWidth) - 1; 
    mousePosition.y = 1 - 2 * (event.clientY/window.innerHeight); 
    mousePosition.unproject(camera); 
    var raycaster = new THREE.Raycaster(camera.position,mousePosition.sub(camera.position).normalize()); 
    console.log(lines.children); 
    var intersections = raycaster.intersectObjects(lines.children); 
    console.log(intersections); 
    lines.children.forEach(function(child) { 
     child.material.color = child.material.originalColor; 
    }); 
    for(var j = 0; j < intersections.length;j++) { 
     var intersection = intersections[j]; 
     var object = intersection.object; 
     object.material.color = new THREE.Color("#FFFFFF"); 
    } 
}; 
+0

謝謝,但正如我在評論中所說的,礦山也有效(見提供的小提琴)。但是同樣的事情(在小提琴中工作)在我的繪圖應用程序中不起作用,因爲在某處存在錯誤... – user3960875