光线相交对象,用鼠标指针拾取对象

时间:2012-09-19 13:33:53

标签: javascript three.js webgl raytracing

感谢您的考虑,因为我的问题已在标题中,我将直接指出:

UPDATE 2: 

问题解决了。我没有将它们变成网格,或者看起来如此。由于从超新星得到的提示以及尝试启用shadowmap的失败,它变得如此清晰,尽管已经失去了半天......

UPDATE 1: 

示例如下:JSFiddle

Original problem:

使用鼠标坐标进行物体拾取的光线投射的标准示例,我的意思是:

var vector = new THREE.Vector3((event.clientX / window.innerWidth ) * 2 - 1, -(event.clientY / window.innerHeight ) * 2 + 1, 1);
projector.unprojectVector(vector, camera);
var ray = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize());
var intersects = ray.intersectObjects(cubes);

if (intersects.length > 0)
{
    console.log('hit');
}

,根本没用。

花了一些时间考虑一下后我尝试了这个:

// start a ray from your camera to the coordinates on 
// x,y plane you pointed with the mouse:
// *I also draw the actual ray on the screen, it looked perfect! :( but...

var cx = event.clientX;
var cy = event.clientY;

var rx = cx - (SCREEN_WIDTH / 2);
var ry = (SCREEN_HEIGHT / 2) - cy;

var origin = new THREE.Vector3(camera.position.x, camera.position.y, camera.position.z - 200);
var direction = new THREE.Vector3(rx, ry, -20);

var ray = new THREE.Ray(origin, direction);
var intersects = ray.intersectObjects(cubes);

但是“intersect”变量仍然是空的。 虽然看起来合乎逻辑,但因为我的元素如下:

for ( i = 0; i < cols; i++)
{
    for ( j = 0; j < rows; j++)
    {
        var cwidth = (SCREEN_WIDTH / cols) / scale;
        var cheight = (SCREEN_HEIGHT / rows) / scale;
        var cdepth = 10;

        var cube = THREE.SceneUtils.createMultiMaterialObject(new THREE.CubeGeometry(cwidth, cheight, cdepth, 1, 1, 1), getMaterials());
        // getMaterials() is a simple function returning an Array of materials
        cube.position.x = cwidth * i - (SCREEN_WIDTH / 2) + cwidth / 2;
        cube.position.x /= scale;
        cube.position.y = cheight * j - (SCREEN_HEIGHT / 2) + cheight / 2;
        cube.position.y /= scale;
        cube.position.z = 0;
        cubes.push(cube);
        scene.add(cube);
    }
}

我的相机是:

    camera.position.z = getDistance(window.innerHeight); 
    // or {x: 0, y:0, z: 403.00000000000006}

我现在试图解决这个问题超过两个小时,有人能看到我错过的东西吗?我在互联网上搜索无济于事。

P.S。我希望我没有用“ray”标记这个错误的问题。

0 个答案:

没有答案