针对BufferGeometry Line threejs的光线投射

时间:2015-07-20 13:21:05

标签: javascript three.js buffer-geometry

我尝试对使用BufferGeometry创建的行进行光线投射。但它似乎不支持光线投射?

如图所示启动BufferGeometry here光线投射对此对象不起作用。

但是当我用BufferGeometry替换Geometry时,光线投射工作正常。

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);

我也尝试将BufferGeometry包裹到Object3D,但不影响结果。如何对BufferGeometry行进行光线投射?

修改

Fiddle with BufferGeometry

Fiddle with Geometry

1 个答案:

答案 0 :(得分: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");
    }
};