using System;
using Unity.Collections;
namespace UnityEngine.XR.ARSubsystems
{
///
/// A default implementation of a .
///
public class DefaultConfigurationChooser : ConfigurationChooser
{
///
/// Selects a configuration from the given and .
///
///
/// Selection works as follows:
/// For each of the configuration , compute the number of supported
/// s that are present in and choose the
/// configuration descriptor with the highest count. is
/// used to break ties.
///
/// A set of s supported by the .
/// A set of requested s.
/// The configuration that best matches the .
/// Thrown if does not contain any descriptors.
/// Thrown if contains more than one tracking mode.
/// Thrown if contains more than one camera mode.
public override Configuration ChooseConfiguration(NativeSlice descriptors, Feature requestedFeatures)
{
if (descriptors.Length == 0)
throw new ArgumentException("No configuration descriptors to choose from.", nameof(descriptors));
if (requestedFeatures.Intersection(Feature.AnyTrackingMode).Count() > 1)
throw new ArgumentException($"Zero or one tracking mode must be requested. Requested tracking modes => {requestedFeatures.Intersection(Feature.AnyTrackingMode).ToStringList()}", nameof(requestedFeatures));
if (requestedFeatures.Intersection(Feature.AnyCamera).Count() > 1)
throw new ArgumentException($"Zero or one camera mode must be requested. Requested camera modes => {requestedFeatures.Intersection(Feature.AnyCamera).ToStringList()}", nameof(requestedFeatures));
int highestFeatureCount = -1;
int highestRank = int.MinValue;
ConfigurationDescriptor bestDescriptor = default;
foreach (var descriptor in descriptors)
{
int featureCount = requestedFeatures.Intersection(descriptor.capabilities).Count();
if ((featureCount > highestFeatureCount) ||
(featureCount == highestFeatureCount && descriptor.rank > highestRank))
{
highestFeatureCount = featureCount;
highestRank = descriptor.rank;
bestDescriptor = descriptor;
}
}
return new Configuration(bestDescriptor, requestedFeatures.Intersection(bestDescriptor.capabilities));
}
}
}