I've got a three.js project going where I need to be able to pick individual faces upon mouse movement on meshes of around 200,000 - 300,000 triangles.
I'm using a function like this:
function raycast (x, y) {
//transform mouse coords relative to main window and normalize
var normalized = normalizeCoordinates(x, y);
// create a Ray with origin at the mouse position and direction into the scene (camera direction)
var vector = new THREE.Vector3( normalized.x, normalized.y, 1 );
projector.unprojectVector( vector, camera );
raycaster.set( camera.position, vector.sub( camera.position ).normalize() );
// create an array containing all objects in the list with which the ray intersects
return raycaster.intersectObject(mesh);
}
Where normalizeCoordinates is just converting mouse page coordinates to normalized device coordinates.
When mesh uses a THREE.Geometry it's workable, albeit a little slow. For a variety of reasons I decided to try out BufferGeometry instead. This sees great results throughout some other aspects of the project but raycasting becomes too slow. When mesh uses a THREE.BufferGeometry (non-indexed) instead the intersectObject method takes around 4.5 times as long to complete.
Firstly, is this expected behaviour with BufferGeometry, and secondly is there any thing I could look at doing to improve raycasting performance in general (whether Geometry or BufferGeometry)? I'm relatively new to shaders but in particular I wondered whether using a shader to perform accelerated raycasting could be doable.