กำลังกำหนดค่ากล้อง

การกำหนดค่ากล้องจะอธิบายคุณสมบัติของเซ็นเซอร์ตรวจจับผ่านกล้องที่สำคัญของแอป ใน Unity การกำหนดค่าเหล่านี้จะเข้าถึงได้ผ่าน XRCameraConfiguration

ในแพลตฟอร์ม Android นั้น ARCore ให้บริการ XRCameraConfigurationExtensions เพื่อแสดงพร็อพเพอร์ตี้เพิ่มเติมสำหรับ ARCore ภายใน XRCameraConfiguration คุณใช้คุณสมบัติเหล่านี้เพื่อตั้งการกำหนดค่ากล้องที่เหมาะสมสำหรับแอปของคุณได้

คุณสมบัติการกำหนดค่ากล้องเพิ่มเติม (Android)

ARCore บนแพลตฟอร์ม Android รองรับพร็อพเพอร์ตี้เพิ่มเติมต่อไปนี้

เข้าถึงการกำหนดค่ากล้องที่รองรับ

ใช้ ARCameraManager.GetConfigurations() เพื่อเข้าถึงการกำหนดค่ากล้องที่รองรับสำหรับอุปกรณ์ที่ต้องการ ซึ่งแสดงผล NativeArray ที่มี XRCameraConfiguration หลายอินสแตนซ์ แต่ละอินสแตนซ์คือการกำหนดค่ากล้องแต่ละตัวที่ระบุคุณสมบัติ เช่น การใช้งานความลึก อัตราเฟรมการจับภาพเป้าหมาย ความละเอียด และขนาดพื้นผิว

กำหนดค่ากล้องในฉากของแอป

ทำตามขั้นตอนต่อไปนี้เพื่อกำหนดค่ากล้องในโหมดของแอป

  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 ใช้เซ็นเซอร์ความลึก

หากแอปของคุณไม่ต้องใช้ Depth คุณสามารถป้องกันไม่ให้ 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;
}