Three.js射线拣选平面/网格v69的面部分

时间:2015-03-26 09:15:08

标签: javascript three.js

因为在three.js中不再支持四边形面。我遇到麻烦Raycast在4个顶点之间挑选分段作为正方形,因为它们现在由三角形面组成。

所以我的选择会返回三角形而不是正方形:

                var faces = 10;
                var myplane = new THREE.Mesh(
                new THREE.PlaneGeometry(1000, 1000, faces, faces),
                new THREE.MeshNormalMaterial({
                color: 0x993333,
                wireframe: false
                }));

                objects.push(myplane);
                scene.add(myplane); 

                //raycast code


                if ( intersects.length > 0 ) {

                    _controls.noRotate = true;
                    var face = intersects[0].face;
                    var temp = intersects[0].object;
                    var geo = temp.geometry;

                    geo.vertices[face.a].z -= 20;
                    geo.vertices[face.b].z -= 20;
                    geo.vertices[face.c].z -= 20;
                    //want other face verts below basically
                    geo.vertices[face.d].z -= 20;
                    geo.vertices[face.e].z -= 20;
                    geo.vertices[face.f].z -= 20;   
                    geo.verticesNeedUpdate = true;

                    if(intersects[ 0 ].faceIndex % 2 == 0){
                        // geo.vertices[intersects[0].faceIndex + 1].z = 40;     
                       // console.log("Even face Index: " + intersects[ 0 ].faceIndex);

                     }else{
                        // console.log("Odd face Index: " + intersects[ 0 ].faceIndex -1);
                       //geo.vertices[intersects[0].faceIndex - 1].z = 40, of course doest work
                     }

                    }   

下面是一个更好的vsual的第二个例子... 目前我得到了红色的结果,但我想选择绿色的例子:

example of plane segments

2 个答案:

答案 0 :(得分:7)

尝试使用此代码获取交叉面

if ( intersects.length > 0 )
{
    var faceIndex = intersects[0].faceIndex;
    var obj = intersects[0].object;
    var geom = obj.geometry;
    var faces = obj.geometry.faces;
    var facesIndices = ["a","b","c"];
    facesIndices.forEach(function(indices){
        geom.vertices[faces[faceIndex][indices]].setZ(-10);
    });
    if(faceIndex%2 == 0){
        faceIndex = faceIndex+1;
    }else{
        faceIndex = faceIndex-1;
    }
    facesIndices.forEach(function(indices){
        geom.vertices[faces[faceIndex][indices]].setZ(-10);
    }); 
    geom.verticesNeedUpdate = true;                               
}

答案 1 :(得分:0)

在一般情况下,获取并将光线投影的几何图形更改为网格三角形交叉点只是:

var face = intersects[0].face;
var obj = intersects[0].object;
var geom = obj.geometry; 

var vertA = geom.vertices[face.a];
var vertB = geom.vertices[face.b];
var vertC = geom.vertices[face.c];

vertA.setY(100);
vertB.setY(110);
vertC.setY(105);

geom.verticesNeedUpdate = true;

无需迭代faces数组,或构建一个人工设计的面顶点数组,只是为了在下一步中循环它。