EDIT: oops, just spotted you are using unproject not project - but in any case the two methods are pretty similar so this might still be the same issue
I encountered this problem in a project recently. I haven’t been able to work out what causes it yet so here’s my workaround:
const v1 = new Vector3();
const objectPos = v1.setFromMatrixPosition(el.matrixWorld);
objectPos.project(camera);
// replace any NaN with zeros
objectPos.set(
Number.isNaN(objectPos.x) ? 0 : objectPos.x,
Number.isNaN(objectPos.y) ? 0 : objectPos.y,
Number.isNaN(objectPos.z) ? 0 : objectPos.z
);
It happens intermittently when v1 contains normal values (i.e. small numbers, not Infinity or NaN) so I guess it must be something to do with my camera settings.
However all the camera matrices seem to contain normal values too and in the meantime, setting the NaN values to zero gives the correct results for me so I haven’t investigated further yet.
You can see it depends on the camera.projectionMatrixInverse, camera.matrixWorld, and your vector vv. So you can also check all of these to see if they contain invalid data (NaN, Infinity, or worse).
why dont we get the elephant out of the room 1st by logging currentCameraTHREE.projectionMatrixInverse.elements and currentCameraTHREE.matrixWorld.elements
This suggests that invalid data is being passed to the camera somewhere. What does camera.projectionMatrix look like?
If you check the source code for camera.updateProjectionMatrix you’ll see all the inputs used in this function (near, far, aspect, and so on). Check all these too to try and find where the invalid data is coming from.