AR Foundation with second camera

Hey,
I’ve run into an issue with having a second camera not matching the AR Camera having the AR Camera Background on it.
i am using that second camera in my scene - set to clear depth. I am copying the Camera Matrix by script from the AR Camera which is having the AR Camera Background Script on it. I think the problem is that besides the Projection Matrix additional (un)distortion is done through a Shader so it matches the Device Camera closer (ipad pro in my case). I am using that second Camera to make stuff appear around a target using Depth Masking etc. Problem is that the mismatch (due to that distortion) is really visible.
Is there a provided way within ARFoundation of setting up that second camera with the distortion? Or do i need to write a shader that applies that matrix during Post on the second camera?

The relevant code in ARCameraBackground Script looks like this:

void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
        {
            UpdateMaterial();

            var mat = material;
            var count = eventArgs.textures.Count;
            for (int i = 0; i < count; ++i)
            {
                mat.SetTexture(
                    eventArgs.propertyNameIds[i],
                    eventArgs.textures[i]);
            }

            mode = ARRenderMode.MaterialAsBackground;

            if (eventArgs.displayMatrix.HasValue)
                mat.SetMatrix(k_DisplayTransformId, eventArgs.displayMatrix.Value);

            if (eventArgs.projectionMatrix.HasValue)
                m_Camera.projectionMatrix = eventArgs.projectionMatrix.Value;
        }

Where the part i think causing my mismatch is that:
mat.SetMatrix(k_DisplayTransformId, eventArgs.displayMatrix.Value);

Camera Setup

Any help is appreciated!

1 Like

hey, stumbled upon a close issue… did you solve the problem?

What I’ve done is use the OnPreRender method, where I copy the projectionMatrix from the ARCamera into my own camera, this seems to work. I think the OnCameraFrameReceived is called after all camera’s render.

public void OnPreRender() {
        myCam.projectionMatrix = copyCam.projectionMatrix;
        myCam.fieldOfView = copyCam.fieldOfView;
        myCam.transform.localPosition = Vector3.zero;
        myCam.transform.localRotation = Quaternion.Euler(Vector3.zero);
    }

this gives me two exactly the same view frustums :slight_smile:

1 Like