gkjohnson / three-mesh-bvh

A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html
MIT License
2.39k stars 247 forks source link

Orthographic camera #354

Closed voiddat closed 2 years ago

voiddat commented 2 years ago

Hello, I have trouble running your raycasting with OrthographicCamera. Im using https://github.com/spite/THREE.MeshLine - basically wrapper for BufferGeometry with shader - up to 10-20k of them merged in several groups (let's say 5-10 merged geometries with 1-5k groups). It has it's own MeshLineRaycast. This implementation is pretty slow - 300ms per raycast on 15k geometries prevents me from achieving proper UX, therefore I'm thinking about BVH. It basically looks like this - lots of polygons on plane (2D). image My code:

Mesh.prototype.raycast = acceleratedRaycast;
BufferGeometry.prototype.computeBoundsTree = computeBoundsTree;
BufferGeometry.prototype.disposeBoundsTree = disposeBoundsTree;
this.raycaster.firstHitOnly = true;

const camera = this.cameraService.getViewportCamera(viewportId);
const normalizedPoint = this.normalizePoint(viewportId, point);
this.raycaster.setFromCamera(normalizedPoint, camera); // i also tried to set ray manually orthogonally through plane with no luck
const intersects = this.raycaster.intersectObjects(
  this.sceneService.getViewportScene(viewportId).children
);

All geometries have boundsTrees (computed after merge), but the program returns null at box intersection (intersects array is always empty regardless if it's cast on line or not). MeshLineRaycast implementation works with this setup, am i missing something? Or perhaps my tech stack isn't compatible with BVH? There are no examples with OrthographicCamera - does it work with your BVH implementation?

Thanks in advance, the code and examples are impressive :)

Regards

gkjohnson commented 2 years ago

Hello! The orthographic camera should have no effect. I'm not all that familiar with three mesh line but from what I can see it looks like a shader is used to distort the geometry which is not supported by the BVH library -- this is likely why a custom MeshLineRaycast function is provided by the original library.