using System; using Unity.Collections; using UnityEngine.SubsystemsImplementation; namespace UnityEngine.XR.ARSubsystems { /// /// Defines an interface for interacting with environment probe functionality for creating realistic lighting and /// environment texturing in AR scenes. /// public class XREnvironmentProbeSubsystem : TrackingSubsystem { #if DEVELOPMENT_BUILD || UNITY_EDITOR ValidationUtility m_ValidationUtility = new(); #endif /// /// Constructs an . /// Do not create this directly. /// Call Create on an obtained from the SubsystemManager. /// public XREnvironmentProbeSubsystem() { } /// /// Specifies whether the AR session should automatically place environment probes in the scene. /// /// /// true if automatic placement of environment probes is enabled. Otherwise, false. /// /// /// If both manual and automatic placement of environment probes are supported, manually placed environment /// probes can be specified via regardless of whether automatic placement is /// enabled or not. /// /// Thrown when setting this value to true for /// implementations that do not support automatic placement. public bool automaticPlacementRequested { get => provider.automaticPlacementRequested; set { if (value && !subsystemDescriptor.supportsAutomaticPlacement) throw new NotSupportedException("Subsystem does not support automatic placement of environment probes."); provider.automaticPlacementRequested = value; } } /// /// True if the AR session will automatically place environment probes in the scene, false otherwise. /// /// public bool automaticPlacementEnabled => provider.automaticPlacementEnabled; /// /// Specifies whether the environment textures should be returned as HDR textures. /// /// /// true if the environment textures should be returned as HDR textures. Otherwise, false. /// public bool environmentTextureHDRRequested { get => provider.environmentTextureHDRRequested; set => provider.environmentTextureHDRRequested = value; } /// /// True if HDR environment textures are enabled. /// public bool environmentTextureHDREnabled => provider.environmentTextureHDREnabled; /// /// Get the changes (added, updated, and removed) environment probes since the last call to . /// /// The [Allocator](xref:Unity.Collections.Allocator) to use when allocating the returned [NativeArrays](xref:Unity.Collections.NativeArray`1). /// /// describing the planes that have been added, updated, and removed /// since the last call to . The caller owns the memory allocated with [Allocator](xref:Unity.Collections.Allocator) and is responsible for disposing it. /// public override TrackableChanges GetChanges(Allocator allocator) { var changes = provider.GetChanges(XREnvironmentProbe.defaultValue, allocator); #if DEVELOPMENT_BUILD || UNITY_EDITOR m_ValidationUtility.ValidateAndDisposeIfThrown(changes); #endif return changes; } /// /// Tries to create an environment probe. /// /// The position and rotation at which to create the environment probe. /// The scale at which to create the environment probe. /// The size (dimensions) of the environment probe to create. /// If successful, populated with the newly created environment probe. Otherwise, it will contain default values. /// /// true if the environment probe was successfully added, otherwise false. /// /// Thrown when the environment probe subsystem is not running and /// this method is called to an add environment probe. /// Thrown for platforms that do not support manual placement of /// environment probes. public bool TryAddEnvironmentProbe(Pose pose, Vector3 scale, Vector3 size, out XREnvironmentProbe environmentProbe) { if (!running) { throw new InvalidOperationException("cannot add environment probes when environment probe system is not running"); } environmentProbe = XREnvironmentProbe.defaultValue; return provider.TryAddEnvironmentProbe(pose, scale, size, out environmentProbe); } /// /// Asynchronously removes the environment probe matching the trackable ID from the AR session. /// /// Trackable ID of the environment probe to be removed from the AR session. /// /// true if the environment probe is found in the current AR session and will be removed. Otherwise, /// false. /// /// /// RemoveEnvironmentProbe can be used to remove both manually placed and automatically placed /// environment probes if the implementation supports such removals, as indicated by the descriptor properties /// and /// . /// /// Thrown for platforms that do not support removal of the /// type of environment probe. public bool RemoveEnvironmentProbe(TrackableId trackableId) => running && provider.RemoveEnvironmentProbe(trackableId); /// /// The class for providers to implement to support the . /// public abstract class Provider : SubsystemProvider { /// /// Overridden by the provider implementation to set the automatic placement request state for the environment probe subsystem. /// /// Thrown in the default implementation if set to `true`. public virtual bool automaticPlacementRequested { get => false; set { if (value) { throw new NotSupportedException("Automatic placement of environment probes is not supported by this implementation"); } } } /// /// Overridden by the provider implementation to query whether automatic placement is enabled for the environment probe subsystem. /// public virtual bool automaticPlacementEnabled => false; /// /// Overridden by the provider implementation to request the state of HDR environment texture generation. /// /// Thrown if HDR textures are requested but the implementation /// does not support HDR environment textures. public virtual bool environmentTextureHDRRequested { get => false; set { if (value) { throw new NotSupportedException("HDR environment textures are not supported by this implementation"); } } } /// /// Overridden by the provider implementation to query the state of HDR environment texture generation. /// public virtual bool environmentTextureHDREnabled => false; /// /// Overridden by the provider implementation to manually add an environment probe to the AR session. /// /// The position and rotation at which to create the new environment probe. /// The scale of the new environment probe. /// The size (dimensions) of the new environment probe. /// If successful, should be populated with the newly created environment probe. /// /// true if a new environment probe was created, otherwise false. /// /// Thrown in the default implementation of this method. public virtual bool TryAddEnvironmentProbe(Pose pose, Vector3 scale, Vector3 size, out XREnvironmentProbe environmentProbe) { throw new NotSupportedException("Manual placement of environment probes is not supported by this implementation"); } /// /// Overridden by the provider to remove the environment probe matching the trackable ID from /// the AR session. /// /// Trackable ID of the environment probe to be removed from the AR session. /// /// true whether the environment probe is found in the current AR session and will be removed. /// Otherwise, false. /// /// /// You can use this method to remove both manually placed and automatically placed environment probes if the /// implementation supports such removals. Providers should implement this method to remove environment probes of /// the allowed types and to throw a System.NotSupportedException for removals of environment probes of /// disallowed types. /// /// Thrown in the default implementation. public virtual bool RemoveEnvironmentProbe(TrackableId trackableId) { throw new NotSupportedException("Removal of environment probes is not supported by this implementation"); } /// /// Get changes to environment probes (added, updated, and removed) since the last call to this method. /// /// A default value for environment probes. Implementations should first fill their output /// arrays with copies of this value, then copy in their own. See the . /// This allows additional fields to be added to the in the future. /// The allocator to use for the NativeArrays in the returned . /// The environment probes which have been added, updated, and removed since the last call to this method. /// public abstract TrackableChanges GetChanges(XREnvironmentProbe defaultEnvironmentProbe, Allocator allocator); } /// /// Registers a subsystem implementation based on the given subystem parameters. /// /// The parameters defining the environment probe functionality /// implemented by the subsystem provider. /// /// true if the subsystem implementation is registered. Otherwise, false. /// /// Thrown when the values specified in the /// parameter are invalid. Typically, this happens when /// required parameters are or empty or types that do not derive from the required base class. /// [Obsolete("XREnvironmentProbeSubsystem.Register(XREnvironmentProbeSubsystemCinfo) has been deprecated in AR Foundation version 6.0. Use XREnvironmentProbeSubsystemDescriptor.Register(XREnvironmentProbeSubsystemDescriptor.Cinfo) instead.")] public static bool Register(XREnvironmentProbeSubsystemCinfo environmentProbeSubsystemCinfo) { var environmentProbeSubsystemInfo = new XREnvironmentProbeSubsystemDescriptor.Cinfo() { id = environmentProbeSubsystemCinfo.id, providerType = environmentProbeSubsystemCinfo.providerType, subsystemTypeOverride = environmentProbeSubsystemCinfo.subsystemTypeOverride, supportsManualPlacement = environmentProbeSubsystemCinfo.supportsManualPlacement, supportsRemovalOfManual = environmentProbeSubsystemCinfo.supportsRemovalOfManual, supportsAutomaticPlacement = environmentProbeSubsystemCinfo.supportsAutomaticPlacement, supportsRemovalOfAutomatic = environmentProbeSubsystemCinfo.supportsRemovalOfAutomatic, supportsEnvironmentTexture = environmentProbeSubsystemCinfo.supportsEnvironmentTexture, supportsEnvironmentTextureHDR = environmentProbeSubsystemCinfo.supportsEnvironmentTextureHDR }; XREnvironmentProbeSubsystemDescriptor.Register(environmentProbeSubsystemInfo); return true; } } }