設定相機

相機設定會說明應用程式底層相機感應器的屬性。在 Unity 中,您可以透過 XRCameraConfiguration 存取這些設定。

在 Android 平台上,ARCore 提供 XRCameraConfigurationExtensions,可顯示 XRCameraConfiguration 內的其他 ARCore 專屬屬性。您可以使用這些屬性為應用程式設定適當的相機設定。

擴充相機設定屬性 (Android)

Android 平台上的 ARCore 支援下列擴充屬性。

存取支援的相機設定

使用 ARCameraManager.GetConfigurations() 即可存取指定裝置支援的相機設定。系統會傳回含有多個 XRCameraConfiguration 執行個體的 NativeArray。每個執行個體都是個別的相機設定,用於指定深度使用、目標擷取影格速率、解析度和紋理尺寸等屬性。

在應用程式場景中設定相機

請按照下列步驟,在應用程式場景中設定相機。

  1. 使用 ARCameraManager 搭配 ARCameraManager.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;
}