SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
15 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. namespace UnityEngine.XR.ARSubsystems
  4. {
  5. /// <summary>
  6. /// Defines an interface for interacting with environment probe functionality for creating realistic lighting and
  7. /// environment texturing in AR scenes.
  8. /// </summary>
  9. public abstract class XREnvironmentProbeSubsystem : TrackingSubsystem<XREnvironmentProbe, XREnvironmentProbeSubsystemDescriptor>
  10. {
  11. /// <summary>
  12. /// Constructs an <see cref="XREnvironmentProbeSubsystem"/>.
  13. /// Do not create this directly.
  14. /// Call <c>Create</c> on an <see cref="XREnvironmentProbeSubsystemDescriptor"/> obtained from the <c>SubsystemManager</c>.
  15. /// </summary>
  16. public XREnvironmentProbeSubsystem() => m_Provider = CreateProvider();
  17. /// <summary>
  18. /// Specifies whether the AR session should automatically place environment probes in the scene.
  19. /// </summary>
  20. /// <value>
  21. /// <c>true</c> if automatic placement of environment probes is enabled. Otherwise, <c>false</c>.
  22. /// </value>
  23. /// <remarks>
  24. /// If both manual and automatic placement of environment probes are supported, manually placed environment
  25. /// probes may be specified via <see cref="AddEnvironmentProbe"/> regardless of whether automatic placement is
  26. /// enabled or not.
  27. /// </remarks>
  28. /// <exception cref="System.NotSupportedException">Thrown when setting this value to <c>true</c> for
  29. /// implementations that do not support automatic placement.</exception>
  30. public bool automaticPlacement
  31. {
  32. get { return m_AutomaticPlacement; }
  33. set
  34. {
  35. if (value && !SubsystemDescriptor.supportsAutomaticPlacement)
  36. throw new NotSupportedException("subsystem does not support automatic placement of environment probes.");
  37. if (m_AutomaticPlacement != value && running)
  38. {
  39. m_Provider.SetAutomaticPlacement(value);
  40. }
  41. m_AutomaticPlacement = value;
  42. }
  43. }
  44. bool m_AutomaticPlacement;
  45. /// <summary>
  46. /// Specifies whether the environment textures should be returned as HDR textures.
  47. /// </summary>
  48. /// <value>
  49. /// <c>true</c> if the environment textures should be returned as HDR textures. Otherwise, <c>false</c>.
  50. /// </value>
  51. public bool environmentTextureHDR
  52. {
  53. get { return m_EnvironmentTextureHDR; }
  54. set
  55. {
  56. if ((m_EnvironmentTextureHDR != value) && m_Provider.TrySetEnvironmentTextureHDREnabled(value))
  57. {
  58. m_EnvironmentTextureHDR = value;
  59. }
  60. }
  61. }
  62. bool m_EnvironmentTextureHDR = true;
  63. /// <summary>
  64. ///
  65. /// </summary>
  66. /// <param name="allocator"></param>
  67. /// <returns></returns>
  68. public override TrackableChanges<XREnvironmentProbe> GetChanges(Allocator allocator)
  69. {
  70. return m_Provider.GetChanges(XREnvironmentProbe.defaultValue, allocator);
  71. }
  72. /// <summary>
  73. /// Starts the subsystem. If <see cref="automaticPlacement"/> is <c>true</c>, environment probes will be created automatically.
  74. /// </summary>
  75. protected sealed override void OnStart()
  76. {
  77. m_Provider.SetAutomaticPlacement(automaticPlacement);
  78. m_Provider.Start();
  79. }
  80. /// <summary>
  81. /// Stops the subsystem. This does not remove existing environment probes, but it stops automatically placing them, and manually placed probes will not be updated
  82. /// until <see cref="OnStart"/> is called again.
  83. /// </summary>
  84. protected sealed override void OnStop() => m_Provider.Stop();
  85. /// <summary>
  86. /// Destroys the subsystem and any internal state.
  87. /// </summary>
  88. protected sealed override void OnDestroyed() => m_Provider.Destroy();
  89. /// <summary>
  90. /// Tries to create an environment probe.
  91. /// </summary>
  92. /// <param name="pose">The position and rotation at which to create the environment probe.</param>
  93. /// <param name="scale">The scale at which to create the environment probe.</param>
  94. /// <param name="size">The size (dimensions) of the environment probe to create.</param>
  95. /// <param name="environmentProbe">If successful, populated with the newly created environment probe. Otherwise, it will contain default values.</param>
  96. /// <returns>
  97. /// <c>true</c> if the environment probe was successfully added, otherwise <c>false</c>.
  98. /// </returns>
  99. /// <exception cref="InvalidOperationException">Thrown when the environment probe subsystem is not running and
  100. /// this method is called to an add environment probe.</exception>
  101. /// <exception cref="NotSupportedException">Thrown for platforms that do not support manual placement of
  102. /// environment probes.</exception>
  103. public bool TryAddEnvironmentProbe(Pose pose, Vector3 scale, Vector3 size, out XREnvironmentProbe environmentProbe)
  104. {
  105. if (!running)
  106. {
  107. throw new InvalidOperationException("cannot add environment probes when environment probe system is not running");
  108. }
  109. environmentProbe = XREnvironmentProbe.defaultValue;
  110. return m_Provider.TryAddEnvironmentProbe(pose, scale, size, out environmentProbe);
  111. }
  112. /// <summary>
  113. /// Asynchronously removes the environment probe matching the trackable ID from the AR session.
  114. /// </summary>
  115. /// <param name='trackableId'>Trackable ID of the environment probe to be removed from the AR session.</param>
  116. /// <returns>
  117. /// <c>true</c> if the environment probe is found in the current AR session and will be removed. Otherwise,
  118. /// <c>false</c>.
  119. /// </returns>
  120. /// <remarks>
  121. /// <c>RemoveEnvironmentProbe</c> may be used to remove both manually-placed and automatically-placed
  122. /// environment probes if the implementation supports such removals, as indicated by the descriptor properties
  123. /// <see cref="XREnvironmentProbeSubsystemDescriptor.supportsRemovalOfManual"/> and
  124. /// <see cref="XREnvironmentProbeSubsystemDescriptor.supportsRemovalOfAutomatic"/>.
  125. /// </remarks>
  126. /// <exception cref="InvalidOperationException">Thrown when the environment probe subsystem is not running and
  127. /// this method is called to an add environment probe.</exception>
  128. /// <exception cref="System.NotSupportedException">Thrown for platforms that do not support removal of the
  129. /// type of environment probe.</exception>
  130. public bool RemoveEnvironmentProbe(TrackableId trackableId)
  131. {
  132. if (!running)
  133. {
  134. throw new InvalidOperationException("cannot remove environment probes when environment probe system is not running");
  135. }
  136. return m_Provider.RemoveEnvironmentProbe(trackableId);
  137. }
  138. /// <summary>
  139. /// Must create an implementation of <see cref="Provider"/>, the provider-specific implementation of this subsystem.
  140. /// </summary>
  141. protected abstract Provider CreateProvider();
  142. /// <summary>
  143. /// The class for providers to implement to support the <see cref="XREnvironmentProbeSubsystem"/>.
  144. /// </summary>
  145. protected abstract class Provider
  146. {
  147. /// <summary>
  148. /// Starts the subsystem. Will only be invoked if <see cref="running"/> is <c>false</c>.
  149. /// </summary>
  150. public virtual void Start()
  151. { }
  152. /// <summary>
  153. /// Stops the subsystem. Will only be invoked if <see cref="running"/> is <c>true</c>.
  154. /// </summary>
  155. public virtual void Stop()
  156. { }
  157. /// <summary>
  158. /// Invoked when the <see cref="XREnvironmentProbeSubsystem"/> is about to be destroyed.
  159. /// </summary>
  160. public virtual void Destroy()
  161. { }
  162. /// <summary>
  163. /// Overridden by the provider implementation to set the automatic placement state for the environment probe
  164. /// subsystem.
  165. /// </summary>
  166. /// <param name='value'>Whether automatic placement of environment probes should be enabled (<c>true</c>) or
  167. /// disabled (<c>false</c>).</param>
  168. /// <exception cref="System.NotSupportedException">Thrown in the defualt implementation if
  169. /// <paramref name="value"/> is <c>true</c>.</exception>
  170. public virtual void SetAutomaticPlacement(bool value)
  171. {
  172. if (value)
  173. {
  174. throw new NotSupportedException("automatic placement of environment probes is not supported by this implementation");
  175. }
  176. }
  177. /// <summary>
  178. /// Overridden by the provider implementation to set the state of HDR environment texture generation.
  179. /// </summary>
  180. /// <param name="value">Whether HDR environment texture generation is enabled (<c>true</c>) or disabled
  181. /// (<c>false</c>).</param>
  182. /// <returns>
  183. /// Whether the HDR environment texture generation state was set.
  184. /// </returns>
  185. /// <exception cref="System.NotSupportedException">Thrown if the implementation does not support HDR
  186. /// environment textures if the state is being enabled.</exception>
  187. public virtual bool TrySetEnvironmentTextureHDREnabled(bool value)
  188. {
  189. if (value)
  190. {
  191. throw new NotSupportedException("HDR environment textures are not supported by this implementation");
  192. }
  193. return false;
  194. }
  195. /// <summary>
  196. /// Overridden by the provider implementation to manually add an environment probe to the AR session.
  197. /// </summary>
  198. /// <param name='pose'>The position and rotation at which to create the new environment probe.</param>
  199. /// <param name='scale'>The scale of the new environment probe.</param>
  200. /// <param name='size'>The size (dimensions) of the new environment probe.</param>
  201. /// <param name='environmentProbe'>If successful, should be populated with the newly created environment probe.</param>
  202. /// <returns>
  203. /// <c>true</c> if a new environment probe was created, otherwise <c>false</c>.
  204. /// </returns>
  205. /// <exception cref="System.NotSupportedException">Thrown in the default implementation of this method.</exception>
  206. public virtual bool TryAddEnvironmentProbe(Pose pose, Vector3 scale, Vector3 size, out XREnvironmentProbe environmentProbe)
  207. {
  208. throw new NotSupportedException("manual placement of environment probes is not supported by this implementation");
  209. }
  210. /// <summary>
  211. /// Overridden by the provider to remove the environment probe matching the trackable ID from
  212. /// the AR session.
  213. /// </summary>
  214. /// <param name='trackableId'>Trackable ID of the environment probe to be removed from the AR session.</param>
  215. /// <returns>
  216. /// <c>true</c> whether the environment probe is found in the current AR session and will be removed.
  217. /// Otherwise, <c>false</c>.
  218. /// </returns>
  219. /// <remarks>
  220. /// This method may be used to remove both manually-placed and automatically-placed environment probes if the
  221. /// implementation supports such removals. Providers should implement this method remove environment probes of
  222. /// the allowed types and to throw a <c>System.NotSupportedException</c> for removals of environment probes of
  223. /// disallowed types.
  224. /// </remarks>
  225. /// <exception cref="System.NotSupportedException">Thrown in the default implementation.</exception>
  226. public virtual bool RemoveEnvironmentProbe(TrackableId trackableId)
  227. {
  228. throw new NotSupportedException("removal of environment probes is not supported by this implementation");
  229. }
  230. /// <summary>
  231. /// Get changes (added, updated, and removed) in environment probes since the last call to this method.
  232. /// </summary>
  233. /// <param name="defaultEnvironmentProbe">A default value for environment probes. Implementations should first fill their output
  234. /// arrays with copies of this value, then copy in their own. See the <see cref="NativeCopyUtility"/>.
  235. /// This allows additional fields to be added to the <see cref="XREnvironmentProbe"/> in the future.</param>
  236. /// <param name="allocator">The allocator to use for the <c>NativeArray</c>s in the returned <see cref="TrackableChanges{T}"/>.</param>
  237. /// <returns>The environment probes which have been added, updated, and removed since the last call to this method.</returns>
  238. /// <seealso cref="NativeCopyUtility"/>
  239. public abstract TrackableChanges<XREnvironmentProbe> GetChanges(XREnvironmentProbe defaultEnvironmentProbe, Allocator allocator);
  240. }
  241. /// <summary>
  242. /// Registers a subsystem implementation based on the given subystem parameters.
  243. /// </summary>
  244. /// <param name='environmentProbeSubsystemCinfo'>The parameters defining the environment probe functionality
  245. /// implemented by the subsystem provider.</param>
  246. /// <returns>
  247. /// <c>true</c> if the subsystem implementation is registered. Otherwise, <c>false</c>.
  248. /// </returns>
  249. /// <exception cref="ArgumentException">Thrown when the values specified in the
  250. /// <paramref name="environmentProbeSubsystemCinfo"/> parameter are invalid. Typically, this will occur
  251. /// <list type="bullet">
  252. /// <item>
  253. /// <description>if <see cref="XREnvironmentProbeSubsystemCinfo.id"/> is <c>null</c> or empty</description>
  254. /// </item>
  255. /// <item>
  256. /// <description>if <see cref="XREnvironmentProbeSubsystemCinfo.implementationType"/> is <c>null</c>
  257. /// </description>
  258. /// </item>
  259. /// <item>
  260. /// <description>if <see cref="XREnvironmentProbeSubsystemCinfo.implementationType"/> does not derive from the
  261. /// <c>XREnvironmentProbeSubsystem</c> class
  262. /// </description>
  263. /// </item>
  264. /// </list>
  265. /// </exception>
  266. public static bool Register(XREnvironmentProbeSubsystemCinfo environmentProbeSubsystemCinfo)
  267. {
  268. XREnvironmentProbeSubsystemDescriptor environmentProbeSubsystemDescriptor = XREnvironmentProbeSubsystemDescriptor.Create(environmentProbeSubsystemCinfo);
  269. return SubsystemRegistration.CreateDescriptor(environmentProbeSubsystemDescriptor);
  270. }
  271. Provider m_Provider;
  272. }
  273. }