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

การกําหนดค่ากล้องจะอธิบายคุณสมบัติของเซ็นเซอร์กล้องที่อยู่เบื้องหลังของแอป ใน 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 ใช้เซ็นเซอร์ตรวจจับความลึก

หากแอปไม่จำเป็นต้องใช้ข้อมูลเชิงลึก คุณสามารถป้องกันไม่ให้ 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;
}