DefaultConfigurationChooser.cs
3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using Unity.Collections;
namespace UnityEngine.XR.ARSubsystems
{
/// <summary>
/// A default implementation of a <see cref="ConfigurationChooser"/>.
/// </summary>
public class DefaultConfigurationChooser : ConfigurationChooser
{
/// <summary>
/// Selects a configuration from the given <paramref name="descriptors"/> and <paramref name="requestedFeatures"/>.
/// </summary>
/// <remarks>
/// Selection works as follows:
/// For each of the configuration <paramref name="descriptors"/>, compute the number of supported
/// <see cref="Feature"/>s that are present in <paramref name="requestedFeatures"/> and choose the
/// configuration descriptor with the highest count. <see cref="ConfigurationDescriptor.rank"/> is
/// used to break ties.
/// </remarks>
/// <param name="descriptors">A set of <see cref="ConfigurationDescriptor"/>s supported by the <see cref="XRSessionSubsystem"/>.</param>
/// <param name="requestedFeatures">A set of requested <see cref="Feature"/>s.</param>
/// <returns>The configuration that best matches the <paramref name="requestedFeatures"/>.</returns>
/// <exception cref="System.ArgumentException">Thrown if <paramref name="descriptors"/> does not contain any descriptors.</exception>
/// <exception cref="System.ArgumentException">Thrown if <paramref name="requestedFeatures"/> contains more than one tracking mode.</exception>
/// <exception cref="System.ArgumentException">Thrown if <paramref name="requestedFeatures"/> contains more than one camera mode.</exception>
public override Configuration ChooseConfiguration(NativeSlice<ConfigurationDescriptor> 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));
}
}
}