配置相机

相机配置描述应用的底层相机传感器的属性。在 Unity 中,这些配置可通过 XRCameraConfiguration 访问。

在 Android 平台上,ARCore 提供了 XRCameraConfigurationExtensions 以在 XRCameraConfiguration 内公开其他特定于 ARCore 的属性。您可以使用这些属性为您的应用设置适当的摄像头配置。

扩展的相机配置属性 (Android)

Android 平台上的 ARCore 支持以下扩展属性。

访问支持的相机配置

您可以使用 ARCameraManager.GetConfigurations() 获取指定设备支持的摄像头配置。这将返回一个包含多个 XRCameraConfiguration 实例的 NativeArray。每个实例都是一个单独的相机配置,用于指定深度使用、目标拍摄帧速率、分辨率和纹理尺寸等属性。

在应用场景中配置相机

请按照以下步骤在应用场景中配置相机。

  1. ARCameraManagerARCameraManager.GetConfigurations() 搭配使用,可查询受支持的 XRCameraConfiguration 列表。

  2. 如果您要针对 Android 进行构建,请使用 XRCameraConfigurationExtensions 中函数的任意组合来获取 ARCore 专用属性。

  3. 使用 cameraManager.currentConfiguration 设置当前配置。

using UnityEngine.XR.ARFoundation;


// Adds XRCameraConfigurationExtensions extension methods to XRCameraConfiguration.
// This is for the Android platform only.
using Google.XR.ARCoreExtensions;

// Must be set in the editor.
public ARCameraManager cameraManager;

// Use ARCameraManager to obtain the camera configurations.
using (NativeArray<XRCameraConfiguration> configurations = cameraManager.GetConfigurations(Allocator.Temp))
{
    if (!configurations.IsCreated || (configurations.Length <= 0))
    {
        return;
    }

    // Iterate through the list of returned configs to locate the config you want.
    var desiredConfig = configurations[0];
    for (int i = 1; i < configurations.Length; ++i)
    {
        // Choose a config for a given camera that uses the maximum
        // target FPS and texture dimension. If supported, this config also enables
        // the depth sensor.
        if (configurations[i].GetFPSRange().y > desiredConfig.GetFPSRange().y &&
            configurations[i].GetTextureDimensions().x > desiredConfig.GetTextureDimensions().x &&
            configurations[i].GetTextureDimensions().y > desiredConfig.GetTextureDimensions().y &&
            configurations[i].CameraConfigDepthSensorUsage() == CameraConfigDepthSensorUsage.RequireAndUse)
        {
            desiredConfig = configurations[i];
        }
    }

    // Set the configuration you want. If it succeeds, the session
    // automatically pauses and resumes to apply the new configuration.
    // If it fails, cameraManager.currentConfiguration throws an exception.
    if (desiredConfig != cameraManager.currentConfiguration)
    {
        cameraManager.currentConfiguration = desiredConfig;
    }
}

相机配置过滤条件

您可以使用 ARCoreExtensionsCameraConfigFilter 根据应用的需求进行过滤,从而在运行时缩减给定设备的可用摄像头配置范围。

将相机拍摄帧速率限制为 30 FPS

如果您的应用不需要更高的相机帧速率,您可以将其限制为 30 FPS。在支持 60 FPS 相机帧速率的设备上,ARCore 会默认优先支持支持该帧速率的相机配置。如需过滤掉所有支持 60 FPS 的相机配置,请确保将 Target Camera Framerate 设置为 Target 30FPS

阻止 ARCore 使用深度传感器

如果您的应用不需要深度传感器,您可以阻止 ARCore 使用深度传感器。在具有受支持深度传感器的设备上,ARCore ARCore 会优先考虑使用深度传感器的摄像头配置。如需过滤掉所有使用深度传感器的相机配置,请确保将 Depth Sensor Usage 设置为 Do Not Use

使用摄像头配置过滤器

请按照以下步骤操作,让您的应用能够过滤相机配置。

转到 Assets > Create > XR > Camera Config Filter 以创建新的摄像头配置过滤器。

选择您希望过滤器使用的配置。

创建过滤器后,在 ARCoreExtensions 组件中使用它。

在运行时配置相机

您可以根据设备类型等因素,使用回调事件 ARCoreExtensions.OnChooseXRCameraConfiguration 在运行时配置相机。

// Unity's Awake() method
public void Awake()
{
    …
    // If the return value is not a valid index (ex. the value if -1),
    // then no camera configuration will be set. If no previous selection exists, 
    // the ARCore session will use the previously selected camera configuration 
    // or a default configuration.
    arcoreExtensions.OnChooseXRCameraConfiguration = SelectCameraConfiguration;
    …
}

// A custom camera configuration selection function
int SelectCameraConfiguration(List<XRCameraConfiguration> supportedConfigurations)
{
    int index = 0;

    // Use custom logic here to choose the desired configuration from supportedConfigurations.

    return index;
}