0

I have a problem with picking implementation. I found a number of different examples doing what I want, but I really cannot make it work as it should. I mainly followed this example

Basically, I have some meshes in my scene and, double clicking any of them, I'd like to change the color of the chosen one. In the scene there are 3 small cubes that are always selected and some more complex meshes that often aren't. I'd like to know if anybody can help me figuring why, if the cubes can be selected, the others often can't.

The code I used to detect the clicked mesh is:

var projector = new THREE.Projector();

var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );

projector.unprojectVector( vector, camera );

var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );

var intersects = ray.intersectObjects( scene.children, true );

if ( intersects.length > 0 ) 
{
    intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );
}
Emonale
  • 513
  • 2
  • 7
  • 22

1 Answers1

1

ray.intersectObject(), which is called by ray.intersectObjects(), requires face centroids.

mesh.geometry.computeCentroids();

This is important if you are creating your own custom geometries.

three.js r.51

As of r54 this is no longer necessary. See comment from WestLangley

morpheus05
  • 4,772
  • 2
  • 32
  • 47
WestLangley
  • 102,557
  • 10
  • 276
  • 276
  • mesh.geometry.computeCentroids() , where should I write this piece of code? while creating the geometry or while projecting the ray? my cone custom geometry : http://jsfiddle.net/suvKg/12/ Please help me – three.jsaddict Dec 30 '12 at 18:55
  • 1
    @webGLnoobie ...when creating the geometry... however, three.js r.54 uses `Raycaster` now, and it does not require face centroids anymore. – WestLangley Dec 30 '12 at 20:48