Cast ray with cloned camera

Hi!

I’m trying to cast a ray from a camera different than the active one. The reason for this is that I’m doing some calculation in the background, by the time that’s done the camera position won’t be relevant anymore.
I’m trying to do this by cloning the main camera (this is in WebXR mode, in case that matters) and using that to do:

const newRay = sceneRef.current!.createPickingRay(
   50,
   50,
   Matrix.Identity(),
   clonedCamera, // this is a clone of the WebXR camera
   false
);

I then visualize them:
RayHelper.CreateAndShow(newRay, sceneRef.current!, new Color3(1, 1, 1));
However, I keep having odd issues where the rays seem to be starting in the right place, but going in the wrong direction. I can’t seem to pin down what the direction is related to.

I’ve also tried:

            const newRay = Ray.CreateNew(
                50,
                50,
                clonedCamera.viewport.width,
                clonedCamera.viewport.height,
                clonedCamera.getWorldMatrix(),
                clonedCamera.getViewMatrix(),
                clonedCamera.getProjectionMatrix()
            );

But that’s given me similar issues.

I’ve checked that the position and rotation of the cloned camera is correct by spawning a cube at that location, with a PositionGizmo and that all seems to be correct.

I can raycast with the current camera and get those rays pointing in the right direction, but I can’t with the cloned camera. Any idea if there’s something I’m missing? Or another way to accomplish this?

Hmmm are you doing all of this fully inside XR mode? Or do you leave XR at some moment? And when is the camera cloned? Are you able to repro on a PG?

1 Like

Hi! Thanks for the questions, I guess I haven’t tried to repro in a PG, I’ll do that next :+1:

1 Like