Three js Raycasting intercepting particles with attenuated sizes

1.2k views Asked by At

I'm trying to use a Raycaster to intercept particles in a PointCloud object.

The point cloud is being created as such:

... other initializations, like the scene object...

var geometry = new THREE.Geometry();

var sprite = THREE.ImageUtils.loadTexture("myAwesomeIcon");

for (i = 0; i < 100000; i++) {

    var vertex = new THREE.Vector3();
    vertex.x = 15000 * Math.random() - 1000;
    vertex.y = 15000 * Math.random() - 1000;
    vertex.z = 15000 * Math.random() - 1000;

    geometry.vertices.push(vertex);

}

var material = new THREE.PointCloudMaterial({
    size : 30,
    sizeAttenuation : true,
    map : sprite,
    transparent : true
});

var particles = new THREE.PointCloud(geometry, material);
particles.sortParticles = true;
scene.add(particles);

The raycaster, in its turn, is created as such:

var raycaster = new THREE.Raycaster();
raycaster.params.PointCloud.threshold = 15;

However, given that I want the sizes of the particles to change based on the camera's proximity (sizeAttenuation : true), I need the threshold param to "adapt" to each particle.

Right now, it may detect the particle too soon (when it's closer to the camera), or too late (when it's further from the camera. By too soon/late I mean in respect to the mouse position over the particle.

I found this solution online, but it assumes that I know the sizes of each particle, which I don't. I only know the initial (non-attenuated) size.

Thank you very much.

0

There are 0 answers