Why is checking raycast intersections so slow in Three.js?

144 views Asked by At

I'm making an FPS game using Three.js.

Take a look at this code:

let raycaster = new THREE.Raycaster();
raycastTargets = [otherPlayer];
center = new THREE.Vector2(0,0);
function animate() {
  raycastTargets[0] = otherPlayer;
  camera.updateMatrixWorld(), raycaster.setFromCamera(center, camera);
  let intersections = raycaster.intersectObjects(raycastTargets);
  intersection = intersections[0] || null;
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

When this code runs, if I look at the otherPlayer (even when the camera is near) the page starts getting choppy/laggy.

Note that I'm using THREE.PointerLockControls to look around.

I don't understand why it's slowing down.

All objects are pre-defined, I'm using default JavaScript variables, everything is normal.

Why is it slowing down, and how can I stop it?

If I ran all these lines of code individually, I doubt it would lag.

0

There are 0 answers