XRCameraFrame.cs 18.5 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;

namespace UnityEngine.XR.ARSubsystems
{
    /// <summary>
    /// Represents the properties included in the camera frame.
    /// </summary>
    [Flags]
    public enum XRCameraFrameProperties
    {
        /// <summary>
        /// The timestamp of the frame is included.
        /// </summary>
        [Description("Timestamp")]
        Timestamp               = (1 << 0),

        /// <summary>
        /// The average brightness of the frame is included.
        /// </summary>
        [Description("AverageBrightness")]
        AverageBrightness       = (1 << 1),

        /// <summary>
        /// The average color temperature of the frame is included.
        /// </summary>
        [Description("AverageColorTemperature")]
        AverageColorTemperature = (1 << 2),

        /// <summary>
        /// The color correction value of the frame is included.
        /// </summary>
        [Description("ColorCorrection")]
        ColorCorrection = (1 << 3),

        /// <summary>
        /// The project matrix for the frame is included.
        /// </summary>
        [Description("ProjectionMatrix")]
        ProjectionMatrix        = (1 << 4),

        /// <summary>
        /// The display matrix for the frame is included.
        /// </summary>
        [Description("DisplayMatrix")]
        DisplayMatrix           = (1 << 5),

        /// <summary>
        /// The average intensity in lumens is included.
        /// </summary>
        [Description("AverageIntensityInLumens")]
        AverageIntensityInLumens      = (1 << 6),

        /// <summary>
        /// The camera exposure duration is included.
        /// </summary>
        [Description("ExposureDuration")]
        ExposureDuration = (1 << 7),
        
        /// <summary>
        /// The camera exposure offset is included.
        /// </summary>
        [Description("ExposureOffset")]
        ExposureOffset = (1 << 8),
    }

    /// <summary>
    /// Parameters of the Unity <c>Camera</c> that may be necessary/useful to the provider.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct XRCameraFrame : IEquatable<XRCameraFrame>
    {
        /// <summary>
        /// The timestamp, in nanoseconds, associated with this frame.
        /// </summary>
        /// <value>
        /// The timestamp, in nanoseconds, associated with this frame.
        /// </value>
        public long timestampNs
        {
            get { return m_TimestampNs; }
        }
        long m_TimestampNs;

        /// <summary>
        /// The estimated brightness of the scene.
        /// </summary>
        /// <value>
        /// The estimated brightness of the scene.
        /// </value>
        public float averageBrightness
        {
            get { return m_AverageBrightness; }
        }
        float m_AverageBrightness;

        /// <summary>
        /// The estimated color temperature of the scene.
        /// </summary>
        /// <value>
        /// The estimated color temperature of the scene.
        /// </value>
        public float averageColorTemperature
        {
            get { return m_AverageColorTemperature; }
        }
        float m_AverageColorTemperature;

        /// <summary>
        /// The estimated color correction value of the scene.
        /// </summary>
        /// <value>
        /// The estimated color correction value of the scene.
        /// </value>
        public Color colorCorrection
        {
            get { return m_ColorCorrection; }
        }
        Color m_ColorCorrection;

        /// <summary>
        /// The 4x4 projection matrix for the camera frame.
        /// </summary>
        /// <value>
        /// The 4x4 projection matrix for the camera frame.
        /// </value>
        public Matrix4x4 projectionMatrix
        {
            get { return m_ProjectionMatrix; }
        }
        Matrix4x4 m_ProjectionMatrix;

        /// <summary>
        /// The 4x4 display matrix for the camera frame.
        /// </summary>
        /// <value>
        /// The 4x4 display matrix for the camera frame.
        /// </value>
        public Matrix4x4 displayMatrix
        {
            get { return m_DisplayMatrix; }
        }
        Matrix4x4 m_DisplayMatrix;

        /// <summary>
        /// The <see cref="TrackingState"/> associated with the camera.
        /// </summary>
        /// <value>
        /// The tracking state associated with the camera.
        /// </value>
        public TrackingState trackingState
        {
            get { return m_TrackingState; }
        }
        TrackingState m_TrackingState;

        /// <summary>
        /// A native pointer associated with this frame. The data
        /// pointed to by this pointer is specific to provider implementation.
        /// </summary>
        /// <value>
        /// The native pointer associated with this frame.
        /// </value>
        public IntPtr nativePtr
        {
            get { return m_NativePtr; }
        }
        IntPtr m_NativePtr;

        /// <summary>
        /// The set of all flags indicating which properties are included in the frame.
        /// </summary>
        /// <value>
        /// The set of all flags indicating which properties are included in the frame.
        /// </value>
        public XRCameraFrameProperties properties
        {
            get { return m_Properties; }
        }
        XRCameraFrameProperties m_Properties;

        /// <summary>
        /// The estimated intensity, in lumens, of the scene.
        /// </summary>
        /// <value>
        /// The estimated intensity, in lumens, of the scene.
        /// </value>
        public float averageIntensityInLumens
        {
            get { return m_AverageIntensityInLumens; }
        }
        float m_AverageIntensityInLumens;

        /// <summary>
        /// The camera exposure duration, in seconds with sub-millisecond precision, of the scene.
        /// </summary>
        /// <value>
        /// The camera exposure duration, in seconds with sub-millisecond precision, of the scene.
        /// </value>
        public double exposureDuration
        {
            get => m_ExposureDuration;
        }
        double m_ExposureDuration;

        /// <summary>
        /// The camera exposure offset of the scene for lighting scaling
        /// </summary>
        /// <value>
        /// The camera exposure offset of the scene for lighting scaling
        /// </value>
        public float exposureOffset
        {
            get => m_ExposureOffset;
        }
        float m_ExposureOffset;

        /// <summary>
        /// Whether the frame has a timestamp.
        /// </summary>
        /// <value>
        /// Whether the frame has a timestamp.
        /// </value>
        public bool hasTimestamp
        {
            get { return (m_Properties & XRCameraFrameProperties.Timestamp) != 0; }
        }

        /// <summary>
        /// Whether the frame has an average brightness.
        /// </summary>
        /// <value>
        /// Whether the frame has an average brightness.
        /// </value>
        public bool hasAverageBrightness
        {
            get { return (m_Properties & XRCameraFrameProperties.AverageBrightness) != 0; }
        }

        /// <summary>
        /// Whether the frame has an average color temperature.
        /// </summary>
        /// <value>
        /// Whether the frame has an average color temperature.
        /// </value>
        public bool hasAverageColorTemperature
        {
            get { return (m_Properties & XRCameraFrameProperties.AverageColorTemperature) != 0; }
        }

        /// <summary>
        /// Whether the frame has a color correction value.
        /// </summary>
        /// <value>
        /// Whether the frame has a color correction value.
        /// </value>
        public bool hasColorCorrection
        {
            get { return (m_Properties & XRCameraFrameProperties.ColorCorrection) != 0; }
        }

        /// <summary>
        /// Whether the frame has a projection matrix.
        /// </summary>
        /// <value>
        /// Whether the frame has a projection matrix.
        /// </value>
        public bool hasProjectionMatrix
        {
            get { return (m_Properties & XRCameraFrameProperties.ProjectionMatrix) != 0; }
        }

        /// <summary>
        /// Whether the frame has a display matrix.
        /// </summary>
        /// <value>
        /// Whether the frame has a display matrix.
        /// </value>
        public bool hasDisplayMatrix
        {
            get { return (m_Properties & XRCameraFrameProperties.DisplayMatrix) != 0; }
        }

        /// <summary>
        /// Whether the frame has an average intensity in lumens.
        /// </summary>
        /// <value>
        /// Whether the frame has an average intensity in lumens.
        /// </value>
        public bool hasAverageIntensityInLumens
        {
            get { return (m_Properties & XRCameraFrameProperties.AverageIntensityInLumens) != 0; }
        }

        /// <summary>
        /// Whether the frame has an exposure duration in seconds with sub-millisecond precision.
        /// </summary>
        /// <value>
        /// Whether the frame has an exposure duration in seconds with sub-millisecond precision.
        /// </value>
        public bool hasExposureDuration
        {
            get => (m_Properties & XRCameraFrameProperties.ExposureDuration) != 0;
        }

        /// <summary>
        /// Whether the frame has an exposure offset for scaling lighting.
        /// </summary>
        /// <value>
        /// Whether the frame has an exposure offset for scaling lighting.
        /// </value>
        public bool hasExposureOffset
        {
            get => (m_Properties & XRCameraFrameProperties.ExposureOffset) != 0;
        }

        /// <summary>
        /// Provides timestamp of the camera frame.
        /// </summary>
        /// <param name="timestampNs">The timestamp of the camera frame.</param>
        /// <returns>
        /// <c>true</c> if the timestamp was provided. Otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetTimestamp(out long timestampNs)
        {
            timestampNs = this.timestampNs;
            return hasTimestamp;
        }

        /// <summary>
        /// Provides brightness for the whole image as an average of all pixels' brightness.
        /// </summary>
        /// <param name="averageBrightness">An estimated average brightness for the environment.</param>
        /// <returns>
        /// <c>true</c> if average brightness was provided. Otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetAverageBrightness(out float averageBrightness)
        {
            averageBrightness = this.averageBrightness;
            return hasAverageBrightness;
        }

        /// <summary>
        /// Provides color temperature for the whole image as an average of all pixels' color temperature.
        /// </summary>
        /// <param name="averageColorTemperature">An estimated color temperature.</param>
        /// <returns>
        /// <c>true</c> if average color temperature was provided. Otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetAverageColorTemperature(out float averageColorTemperature)
        {
            averageColorTemperature = this.averageColorTemperature;
            return hasAverageColorTemperature;
        }

        /// <summary>
        /// Provides projection matrix for the camera frame.
        /// </summary>
        /// <param name="projectionMatrix">The projection matrix used by the <c>XRCameraSubsystem</c>.</param>
        /// <returns>
        /// <c>true</c> if the projection matrix was provided. Otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetProjectionMatrix(out Matrix4x4 projectionMatrix)
        {
            projectionMatrix = this.projectionMatrix;
            return this.hasProjectionMatrix;
        }

        /// <summary>
        /// Provides display matrix defining how texture is being rendered on the screen.
        /// </summary>
        /// <param name="displayMatrix">The display matrix for rendering.</param>
        /// <returns>
        /// <c>true</c> if the display matrix was provided. Otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetDisplayMatrix(out Matrix4x4 displayMatrix)
        {
            displayMatrix = this.displayMatrix;
            return hasDisplayMatrix;
        }

        /// <summary>
        /// Provides intensity, in lumens, for the environment.
        /// </summary>
        /// <param name="averageBrightness">An estimated average intensity, in lumens, for the environment.</param>
        /// <returns>
        /// <c>true</c> if average intensity was provided. Otherwise, <c>false</c>.
        /// </returns>
        public bool TryGetAverageIntensityInLumens(out float averageIntensityInLumens)
        {
            averageIntensityInLumens = this.averageIntensityInLumens;
            return hasAverageIntensityInLumens;
        }

        /// <summary>
        /// Compares for equality.
        /// </summary>
        /// <param name="other">The other <see cref="XRCameraFrame"/> to compare against.</param>
        /// <returns><c>true</c> if the <see cref="XRCameraFrame"/> represents the same object.</returns>
        public bool Equals(XRCameraFrame other)
        {
            return (m_TimestampNs.Equals(other.m_TimestampNs) && m_AverageBrightness.Equals(other.m_AverageBrightness)
                    && m_AverageColorTemperature.Equals(other.m_AverageColorTemperature)
                    && m_ProjectionMatrix.Equals(other.m_ProjectionMatrix)
                    && m_DisplayMatrix.Equals(other.m_DisplayMatrix)
                    && m_AverageIntensityInLumens.Equals(other.m_AverageIntensityInLumens)
                    && m_ExposureDuration.Equals(other.m_ExposureDuration)
                    && m_ExposureOffset.Equals(other.m_ExposureOffset)
                    && m_Properties.Equals(other.m_Properties));
        }

        /// <summary>
        /// Compares for equality.
        /// </summary>
        /// <param name="obj">An <c>object</c> to compare against.</param>
        /// <returns><c>true</c> if <paramref name="obj"/> is an <see cref="XRCameraFrame"/> and
        /// <see cref="Equals(XRCameraFrame)"/> is also <c>true</c>. Otherwise, <c>false</c>.</returns>
        public override bool Equals(System.Object obj)
        {
            return ((obj is XRCameraFrame) && Equals((XRCameraFrame)obj));
        }


        /// <summary>
        /// Compares <paramref name="lhs"/> and <paramref name="rhs"/> for equality using <see cref="Equals(XRCameraFrame)"/>.
        /// </summary>
        /// <param name="lhs">The left-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
        /// <param name="rhs">The right-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
        /// <returns><c>true</c> if <paramref name="lhs"/> compares equal to <paramref name="rhs"/>, <c>false</c> otherwise.</returns>
        public static bool operator ==(XRCameraFrame lhs, XRCameraFrame rhs)
        {
            return lhs.Equals(rhs);
        }

        /// <summary>
        /// Compares <paramref name="lhs"/> and <paramref name="rhs"/> for inequality using <see cref="Equals(XRCameraFrame)"/>.
        /// </summary>
        /// <param name="lhs">The left-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
        /// <param name="rhs">The right-hand-side <see cref="XRCameraFrame"/> of the comparison.</param>
        /// <returns><c>false</c> if <paramref name="lhs"/> compares equal to <paramref name="rhs"/>, <c>true</c> otherwise.</returns>
        public static bool operator !=(XRCameraFrame lhs, XRCameraFrame rhs)
        {
            return !lhs.Equals(rhs);
        }

        /// <summary>
        /// Generates a hash code suitable for use in <c>HashSet</c> and <c>Dictionary</c>.
        /// </summary>
        /// <returns>A hash of the <see cref="XRCameraFrame"/>.</returns>
        public override int GetHashCode()
        {
            int hashCode = 486187739;
            unchecked
            {
                hashCode = (hashCode * 486187739) + m_TimestampNs.GetHashCode();
                hashCode = (hashCode * 486187739) + m_AverageBrightness.GetHashCode();
                hashCode = (hashCode * 486187739) + m_AverageColorTemperature.GetHashCode();
                hashCode = (hashCode * 486187739) + m_ColorCorrection.GetHashCode();
                hashCode = (hashCode * 486187739) + m_ProjectionMatrix.GetHashCode();
                hashCode = (hashCode * 486187739) + m_DisplayMatrix.GetHashCode();
                hashCode = (hashCode * 486187739) + m_AverageIntensityInLumens.GetHashCode();
                hashCode = (hashCode * 486187739) + m_ExposureDuration.GetHashCode();
                hashCode = (hashCode * 486187739) + m_ExposureOffset.GetHashCode();
                hashCode = (hashCode * 486187739) + m_NativePtr.GetHashCode();
                hashCode = (hashCode * 486187739) + m_Properties.GetHashCode();
            }
            return hashCode;
        }

        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat("properties:{0}\n   timestamp:{1}ns\n   avgBrightness:{2}\n"
                                       + "   avgColorTemp:{3}\n   colorCorrection:{4}\n   projection:\n{5}\n   display:\n{6}\n"
                                       + "   nativePtr{7}\n",
                                       m_Properties.ToString(), m_TimestampNs.ToString(),
                                       m_AverageBrightness.ToString("0.000"),
                                       m_AverageColorTemperature.ToString("0.000"),
                                       m_ColorCorrection.ToString(),
                                       m_ProjectionMatrix.ToString("0.000"), m_DisplayMatrix.ToString("0.000"),
                                       m_AverageIntensityInLumens.ToString("0.000"),
                                       m_NativePtr.ToString("X16"));

            return stringBuilder.ToString();
        }
    }
}