Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.03k stars 1.13k forks source link

cameraManager.TryGetIntrinsics returning false [Bug] #515

Closed andnovar closed 4 years ago

andnovar commented 4 years ago

Unity bug report case number Unity bug case number 1257094.

Describe the bug TryGetIntrinsics(out XRCameraIntrinsics) is always returning false. I am using ARFoundation 4.0

To Reproduce Steps to reproduce the behavior:

  1. Invoke the function and try to get the intrinsics.focalLength.x and intrinsics.focalLength.y
  2. It gave me false in a Pixel phone and a Moto G6

Expected behavior I expect to get the focal length for further CV processing.

Actual behavior GetIntrinsics is not returning any values.

Smartphone (please complete the following information):

tdmowrer commented 4 years ago

I added this script to the AR Camera GameObject in the SimpleAR sample and ran it on a Pixel 3a XL:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;

[RequireComponent(typeof(ARCameraManager))]
public class PrintCameraIntrinsics : MonoBehaviour
{
    XRCameraIntrinsics? m_CameraIntrinsics;

    void Start()
    {
        GetComponent<ARCameraManager>().frameReceived += OnFrameReceived;
    }

    void OnFrameReceived(ARCameraFrameEventArgs eventArgs)
    {
        if (GetComponent<ARCameraManager>().TryGetIntrinsics(out var intrinsics))
        {
            m_CameraIntrinsics = intrinsics;
        }
        else
        {
            m_CameraIntrinsics = null;
        }
    }

    // Update is called once per frame
    void OnGUI()
    {
        if (m_CameraIntrinsics.HasValue)
        {
            GUI.skin.label.fontSize = 100;
            GUILayout.Label($"Camera intrinsics: {m_CameraIntrinsics.Value.ToString()}");
        }
    }
}

Camera intrinsics were displayed as expected.

  1. What AR features did you have enabled (e.g., camera facing direction, plane detection, etc)
  2. Which alpha of 2020.2 specifically did you use?
andnovar commented 4 years ago

The problem was on my end. Thanks for sharing code about it. I was not doing TryGetIntrinsics inside OnFrameReceived. I am just using XRCpuImage and reading the raw pixels.

tdmowrer commented 4 years ago

You can call TryGetIntrinsics from anywhere (doesn't have to be in the frameReceived callback), but you need to have gotten a new ARFrame that frame. It takes a few frames to establish the session, which is why calling it once from Start would quite likely return false.