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.

492 lines
18 KiB

4 years ago
  1. using System;
  2. using System.ComponentModel;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using Unity.Collections;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. namespace UnityEngine.XR.ARSubsystems
  8. {
  9. /// <summary>
  10. /// Represents the properties included in the camera frame.
  11. /// </summary>
  12. [Flags]
  13. public enum XRCameraFrameProperties
  14. {
  15. /// <summary>
  16. /// The timestamp of the frame is included.
  17. /// </summary>
  18. [Description("Timestamp")]
  19. Timestamp = (1 << 0),
  20. /// <summary>
  21. /// The average brightness of the frame is included.
  22. /// </summary>
  23. [Description("AverageBrightness")]
  24. AverageBrightness = (1 << 1),
  25. /// <summary>
  26. /// The average color temperature of the frame is included.
  27. /// </summary>
  28. [Description("AverageColorTemperature")]
  29. AverageColorTemperature = (1 << 2),
  30. /// <summary>
  31. /// The color correction value of the frame is included.
  32. /// </summary>
  33. [Description("ColorCorrection")]
  34. ColorCorrection = (1 << 3),
  35. /// <summary>
  36. /// The project matrix for the frame is included.
  37. /// </summary>
  38. [Description("ProjectionMatrix")]
  39. ProjectionMatrix = (1 << 4),
  40. /// <summary>
  41. /// The display matrix for the frame is included.
  42. /// </summary>
  43. [Description("DisplayMatrix")]
  44. DisplayMatrix = (1 << 5),
  45. /// <summary>
  46. /// The average intensity in lumens is included.
  47. /// </summary>
  48. [Description("AverageIntensityInLumens")]
  49. AverageIntensityInLumens = (1 << 6),
  50. /// <summary>
  51. /// The camera exposure duration is included.
  52. /// </summary>
  53. [Description("ExposureDuration")]
  54. ExposureDuration = (1 << 7),
  55. /// <summary>
  56. /// The camera exposure offset is included.
  57. /// </summary>
  58. [Description("ExposureOffset")]
  59. ExposureOffset = (1 << 8),
  60. }
  61. /// <summary>
  62. /// Parameters of the Unity <c>Camera</c> that may be necessary/useful to the provider.
  63. /// </summary>
  64. [StructLayout(LayoutKind.Sequential)]
  65. public struct XRCameraFrame : IEquatable<XRCameraFrame>
  66. {
  67. /// <summary>
  68. /// The timestamp, in nanoseconds, associated with this frame.
  69. /// </summary>
  70. /// <value>
  71. /// The timestamp, in nanoseconds, associated with this frame.
  72. /// </value>
  73. public long timestampNs
  74. {
  75. get { return m_TimestampNs; }
  76. }
  77. long m_TimestampNs;
  78. /// <summary>
  79. /// The estimated brightness of the scene.
  80. /// </summary>
  81. /// <value>
  82. /// The estimated brightness of the scene.
  83. /// </value>
  84. public float averageBrightness
  85. {
  86. get { return m_AverageBrightness; }
  87. }
  88. float m_AverageBrightness;
  89. /// <summary>
  90. /// The estimated color temperature of the scene.
  91. /// </summary>
  92. /// <value>
  93. /// The estimated color temperature of the scene.
  94. /// </value>
  95. public float averageColorTemperature
  96. {
  97. get { return m_AverageColorTemperature; }
  98. }
  99. float m_AverageColorTemperature;
  100. /// <summary>
  101. /// The estimated color correction value of the scene.
  102. /// </summary>
  103. /// <value>
  104. /// The estimated color correction value of the scene.
  105. /// </value>
  106. public Color colorCorrection
  107. {
  108. get { return m_ColorCorrection; }
  109. }
  110. Color m_ColorCorrection;
  111. /// <summary>
  112. /// The 4x4 projection matrix for the camera frame.
  113. /// </summary>
  114. /// <value>
  115. /// The 4x4 projection matrix for the camera frame.
  116. /// </value>
  117. public Matrix4x4 projectionMatrix
  118. {
  119. get { return m_ProjectionMatrix; }
  120. }
  121. Matrix4x4 m_ProjectionMatrix;
  122. /// <summary>
  123. /// The 4x4 display matrix for the camera frame.
  124. /// </summary>
  125. /// <value>
  126. /// The 4x4 display matrix for the camera frame.
  127. /// </value>
  128. public Matrix4x4 displayMatrix
  129. {
  130. get { return m_DisplayMatrix; }
  131. }
  132. Matrix4x4 m_DisplayMatrix;
  133. /// <summary>
  134. /// The <see cref="TrackingState"/> associated with the camera.
  135. /// </summary>
  136. /// <value>
  137. /// The tracking state associated with the camera.
  138. /// </value>
  139. public TrackingState trackingState
  140. {
  141. get { return m_TrackingState; }
  142. }
  143. TrackingState m_TrackingState;
  144. /// <summary>
  145. /// A native pointer associated with this frame. The data
  146. /// pointed to by this pointer is specific to provider implementation.
  147. /// </summary>
  148. /// <value>
  149. /// The native pointer associated with this frame.
  150. /// </value>
  151. public IntPtr nativePtr
  152. {
  153. get { return m_NativePtr; }
  154. }
  155. IntPtr m_NativePtr;
  156. /// <summary>
  157. /// The set of all flags indicating which properties are included in the frame.
  158. /// </summary>
  159. /// <value>
  160. /// The set of all flags indicating which properties are included in the frame.
  161. /// </value>
  162. public XRCameraFrameProperties properties
  163. {
  164. get { return m_Properties; }
  165. }
  166. XRCameraFrameProperties m_Properties;
  167. /// <summary>
  168. /// The estimated intensity, in lumens, of the scene.
  169. /// </summary>
  170. /// <value>
  171. /// The estimated intensity, in lumens, of the scene.
  172. /// </value>
  173. public float averageIntensityInLumens
  174. {
  175. get { return m_AverageIntensityInLumens; }
  176. }
  177. float m_AverageIntensityInLumens;
  178. /// <summary>
  179. /// The camera exposure duration, in seconds with sub-millisecond precision, of the scene.
  180. /// </summary>
  181. /// <value>
  182. /// The camera exposure duration, in seconds with sub-millisecond precision, of the scene.
  183. /// </value>
  184. public double exposureDuration
  185. {
  186. get => m_ExposureDuration;
  187. }
  188. double m_ExposureDuration;
  189. /// <summary>
  190. /// The camera exposure offset of the scene for lighting scaling
  191. /// </summary>
  192. /// <value>
  193. /// The camera exposure offset of the scene for lighting scaling
  194. /// </value>
  195. public float exposureOffset
  196. {
  197. get => m_ExposureOffset;
  198. }
  199. float m_ExposureOffset;
  200. /// <summary>
  201. /// Whether the frame has a timestamp.
  202. /// </summary>
  203. /// <value>
  204. /// Whether the frame has a timestamp.
  205. /// </value>
  206. public bool hasTimestamp
  207. {
  208. get { return (m_Properties & XRCameraFrameProperties.Timestamp) != 0; }
  209. }
  210. /// <summary>
  211. /// Whether the frame has an average brightness.
  212. /// </summary>
  213. /// <value>
  214. /// Whether the frame has an average brightness.
  215. /// </value>
  216. public bool hasAverageBrightness
  217. {
  218. get { return (m_Properties & XRCameraFrameProperties.AverageBrightness) != 0; }
  219. }
  220. /// <summary>
  221. /// Whether the frame has an average color temperature.
  222. /// </summary>
  223. /// <value>
  224. /// Whether the frame has an average color temperature.
  225. /// </value>
  226. public bool hasAverageColorTemperature
  227. {
  228. get { return (m_Properties & XRCameraFrameProperties.AverageColorTemperature) != 0; }
  229. }
  230. /// <summary>
  231. /// Whether the frame has a color correction value.
  232. /// </summary>
  233. /// <value>
  234. /// Whether the frame has a color correction value.
  235. /// </value>
  236. public bool hasColorCorrection
  237. {
  238. get { return (m_Properties & XRCameraFrameProperties.ColorCorrection) != 0; }
  239. }
  240. /// <summary>
  241. /// Whether the frame has a projection matrix.
  242. /// </summary>
  243. /// <value>
  244. /// Whether the frame has a projection matrix.
  245. /// </value>
  246. public bool hasProjectionMatrix
  247. {
  248. get { return (m_Properties & XRCameraFrameProperties.ProjectionMatrix) != 0; }
  249. }
  250. /// <summary>
  251. /// Whether the frame has a display matrix.
  252. /// </summary>
  253. /// <value>
  254. /// Whether the frame has a display matrix.
  255. /// </value>
  256. public bool hasDisplayMatrix
  257. {
  258. get { return (m_Properties & XRCameraFrameProperties.DisplayMatrix) != 0; }
  259. }
  260. /// <summary>
  261. /// Whether the frame has an average intensity in lumens.
  262. /// </summary>
  263. /// <value>
  264. /// Whether the frame has an average intensity in lumens.
  265. /// </value>
  266. public bool hasAverageIntensityInLumens
  267. {
  268. get { return (m_Properties & XRCameraFrameProperties.AverageIntensityInLumens) != 0; }
  269. }
  270. /// <summary>
  271. /// Whether the frame has an exposure duration in seconds with sub-millisecond precision.
  272. /// </summary>
  273. /// <value>
  274. /// Whether the frame has an exposure duration in seconds with sub-millisecond precision.
  275. /// </value>
  276. public bool hasExposureDuration
  277. {
  278. get => (m_Properties & XRCameraFrameProperties.ExposureDuration) != 0;
  279. }
  280. /// <summary>
  281. /// Whether the frame has an exposure offset for scaling lighting.
  282. /// </summary>
  283. /// <value>
  284. /// Whether the frame has an exposure offset for scaling lighting.
  285. /// </value>
  286. public bool hasExposureOffset
  287. {
  288. get => (m_Properties & XRCameraFrameProperties.ExposureOffset) != 0;
  289. }
  290. /// <summary>
  291. /// Provides timestamp of the camera frame.
  292. /// </summary>
  293. /// <param name="timestampNs">The timestamp of the camera frame.</param>
  294. /// <returns>
  295. /// <c>true</c> if the timestamp was provided. Otherwise, <c>false</c>.
  296. /// </returns>
  297. public bool TryGetTimestamp(out long timestampNs)
  298. {
  299. timestampNs = this.timestampNs;
  300. return hasTimestamp;
  301. }
  302. /// <summary>
  303. /// Provides brightness for the whole image as an average of all pixels' brightness.
  304. /// </summary>
  305. /// <param name="averageBrightness">An estimated average brightness for the environment.</param>
  306. /// <returns>
  307. /// <c>true</c> if average brightness was provided. Otherwise, <c>false</c>.
  308. /// </returns>
  309. public bool TryGetAverageBrightness(out float averageBrightness)
  310. {
  311. averageBrightness = this.averageBrightness;
  312. return hasAverageBrightness;
  313. }
  314. /// <summary>
  315. /// Provides color temperature for the whole image as an average of all pixels' color temperature.
  316. /// </summary>
  317. /// <param name="averageColorTemperature">An estimated color temperature.</param>
  318. /// <returns>
  319. /// <c>true</c> if average color temperature was provided. Otherwise, <c>false</c>.
  320. /// </returns>
  321. public bool TryGetAverageColorTemperature(out float averageColorTemperature)
  322. {
  323. averageColorTemperature = this.averageColorTemperature;
  324. return hasAverageColorTemperature;
  325. }
  326. /// <summary>
  327. /// Provides projection matrix for the camera frame.
  328. /// </summary>
  329. /// <param name="projectionMatrix">The projection matrix used by the <c>XRCameraSubsystem</c>.</param>
  330. /// <returns>
  331. /// <c>true</c> if the projection matrix was provided. Otherwise, <c>false</c>.
  332. /// </returns>
  333. public bool TryGetProjectionMatrix(out Matrix4x4 projectionMatrix)
  334. {
  335. projectionMatrix = this.projectionMatrix;
  336. return this.hasProjectionMatrix;
  337. }
  338. /// <summary>
  339. /// Provides display matrix defining how texture is being rendered on the screen.
  340. /// </summary>
  341. /// <param name="displayMatrix">The display matrix for rendering.</param>
  342. /// <returns>
  343. /// <c>true</c> if the display matrix was provided. Otherwise, <c>false</c>.
  344. /// </returns>
  345. public bool TryGetDisplayMatrix(out Matrix4x4 displayMatrix)
  346. {
  347. displayMatrix = this.displayMatrix;
  348. return hasDisplayMatrix;
  349. }
  350. /// <summary>
  351. /// Provides intensity, in lumens, for the environment.
  352. /// </summary>
  353. /// <param name="averageBrightness">An estimated average intensity, in lumens, for the environment.</param>
  354. /// <returns>
  355. /// <c>true</c> if average intensity was provided. Otherwise, <c>false</c>.
  356. /// </returns>
  357. public bool TryGetAverageIntensityInLumens(out float averageIntensityInLumens)
  358. {
  359. averageIntensityInLumens = this.averageIntensityInLumens;
  360. return hasAverageIntensityInLumens;
  361. }
  362. /// <summary>
  363. /// Compares for equality.
  364. /// </summary>
  365. /// <param name="other">The other <see cref="XRCameraFrame"/> to compare against.</param>
  366. /// <returns><c>true</c> if the <see cref="XRCameraFrame"/> represents the same object.</returns>
  367. public bool Equals(XRCameraFrame other)
  368. {
  369. return (m_TimestampNs.Equals(other.m_TimestampNs) && m_AverageBrightness.Equals(other.m_AverageBrightness)
  370. && m_AverageColorTemperature.Equals(other.m_AverageColorTemperature)
  371. && m_ProjectionMatrix.Equals(other.m_ProjectionMatrix)
  372. && m_DisplayMatrix.Equals(other.m_DisplayMatrix)
  373. && m_AverageIntensityInLumens.Equals(other.m_AverageIntensityInLumens)
  374. && m_ExposureDuration.Equals(other.m_ExposureDuration)
  375. && m_ExposureOffset.Equals(other.m_ExposureOffset)
  376. && m_Properties.Equals(other.m_Properties));
  377. }
  378. /// <summary>
  379. /// Compares for equality.
  380. /// </summary>
  381. /// <param name="obj">An <c>object</c> to compare against.</param>
  382. /// <returns><c>true</c> if <paramref name="obj"/> is an <see cref="XRCameraFrame"/> and
  383. /// <see cref="Equals(XRCameraFrame)"/> is also <c>true</c>. Otherwise, <c>false</c>.</returns>
  384. public override bool Equals(System.Object obj)
  385. {
  386. return ((obj is XRCameraFrame) && Equals((XRCameraFrame)obj));
  387. }
  388. /// <summary>
  389. /// Compares <paramref name="lhs"/> and <paramref name="rhs"/> for equality using <see cref="Equals(XRCameraFrame)"/>.
  390. /// </summary>
  391. /// <param name="lhs">The left-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
  392. /// <param name="rhs">The right-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
  393. /// <returns><c>true</c> if <paramref name="lhs"/> compares equal to <paramref name="rhs"/>, <c>false</c> otherwise.</returns>
  394. public static bool operator ==(XRCameraFrame lhs, XRCameraFrame rhs)
  395. {
  396. return lhs.Equals(rhs);
  397. }
  398. /// <summary>
  399. /// Compares <paramref name="lhs"/> and <paramref name="rhs"/> for inequality using <see cref="Equals(XRCameraFrame)"/>.
  400. /// </summary>
  401. /// <param name="lhs">The left-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
  402. /// <param name="rhs">The right-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
  403. /// <returns><c>false</c> if <paramref name="lhs"/> compares equal to <paramref name="rhs"/>, <c>true</c> otherwise.</returns>
  404. public static bool operator !=(XRCameraFrame lhs, XRCameraFrame rhs)
  405. {
  406. return !lhs.Equals(rhs);
  407. }
  408. /// <summary>
  409. /// Generates a hash code suitable for use in <c>HashSet</c> and <c>Dictionary</c>.
  410. /// </summary>
  411. /// <returns>A hash of the <see cref="XRCameraFrame"/>.</returns>
  412. public override int GetHashCode()
  413. {
  414. int hashCode = 486187739;
  415. unchecked
  416. {
  417. hashCode = (hashCode * 486187739) + m_TimestampNs.GetHashCode();
  418. hashCode = (hashCode * 486187739) + m_AverageBrightness.GetHashCode();
  419. hashCode = (hashCode * 486187739) + m_AverageColorTemperature.GetHashCode();
  420. hashCode = (hashCode * 486187739) + m_ColorCorrection.GetHashCode();
  421. hashCode = (hashCode * 486187739) + m_ProjectionMatrix.GetHashCode();
  422. hashCode = (hashCode * 486187739) + m_DisplayMatrix.GetHashCode();
  423. hashCode = (hashCode * 486187739) + m_AverageIntensityInLumens.GetHashCode();
  424. hashCode = (hashCode * 486187739) + m_ExposureDuration.GetHashCode();
  425. hashCode = (hashCode * 486187739) + m_ExposureOffset.GetHashCode();
  426. hashCode = (hashCode * 486187739) + m_NativePtr.GetHashCode();
  427. hashCode = (hashCode * 486187739) + m_Properties.GetHashCode();
  428. }
  429. return hashCode;
  430. }
  431. public override string ToString()
  432. {
  433. StringBuilder stringBuilder = new StringBuilder();
  434. stringBuilder.AppendFormat("properties:{0}\n timestamp:{1}ns\n avgBrightness:{2}\n"
  435. + " avgColorTemp:{3}\n colorCorrection:{4}\n projection:\n{5}\n display:\n{6}\n"
  436. + " nativePtr{7}\n",
  437. m_Properties.ToString(), m_TimestampNs.ToString(),
  438. m_AverageBrightness.ToString("0.000"),
  439. m_AverageColorTemperature.ToString("0.000"),
  440. m_ColorCorrection.ToString(),
  441. m_ProjectionMatrix.ToString("0.000"), m_DisplayMatrix.ToString("0.000"),
  442. m_AverageIntensityInLumens.ToString("0.000"),
  443. m_NativePtr.ToString("X16"));
  444. return stringBuilder.ToString();
  445. }
  446. }
  447. }