JoshG
JoshG

Reputation: 6745

Raycasting involving individual points in a Three.js PointCloud

I have a PointCloud object that displays several points. I'd like to be able to draw a bounding box around the clicked point. Unfortunately, I can't seem to figure out how to access the individual point within the PointCloud, if this is even possible.

This is the raycasting code I'm using...

window.addEventListener('dblclick', function (ev) {
    var mouse = { x: 1, y: 1 };
    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = -( event.clientY / window.innerHeight ) * 2 + 1;

    var raycaster = new THREE.Raycaster();

    raycaster.params.PointCloud.threshold = 15;
    var vector = new THREE.Vector3(mouse.x, mouse.y, 0.5).unproject(camera);
    raycaster.ray.set(camera.position, vector.sub(camera.position).normalize());
    scene.updateMatrixWorld();
    var intersects = raycaster.intersectObject(particles);

    if (intersects.length > 0) {

        console.log(intersects[0]);
        var hex  = 0x000000;

        var bbox = new THREE.BoundingBoxHelper(intersects[0], hex);
        bbox.update();
        scene.add( bbox );

    } else {
        // do nothing
    }
}
);

I can access the coordinates of the clicked point using intersects[0].point.x. But passing intersects[0] to BoxHelper obviously doesn't work because intersects[0] is not an Object3D. Likewise, if I use intersects[0].object, this draws a box around the entire PointCloud, whereas I'd like it just around the clicked point.

Is this possible, and if so, how might I go about doing it?

Thanks very much for any assistance!

Upvotes: 4

Views: 3916

Answers (1)

Wilt
Wilt

Reputation: 44413

Check these examples, they will help you working with particle/point clouds:

webgl_interactive_particles and webgl_interactive_raycasting_pointcloud.

With intersects[0].index you can find the index of the point you intersected. So you can use this index to find your point:

var index = intersects[0].index;
var point = particles.geometry.vertices[ index ];

Like you already said to be able to draw a BoundingBox you need an Object3D. A single particle is only a point in space so drawing a BoundingBox around that point would actually be a box with size 0. So with that part I cannot help you.

Upvotes: 5

Related Questions