using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Text; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using UnityEngine.Rendering; namespace UnityEngine.XR.ARSubsystems { /// /// Represents the properties included in the camera frame. /// [Flags] public enum XRCameraFrameProperties { /// /// The timestamp of the frame is included. /// [Description("Timestamp")] Timestamp = (1 << 0), /// /// The average brightness of the frame is included. /// [Description("AverageBrightness")] AverageBrightness = (1 << 1), /// /// The average color temperature of the frame is included. /// [Description("AverageColorTemperature")] AverageColorTemperature = (1 << 2), /// /// The color correction value of the frame is included. /// [Description("ColorCorrection")] ColorCorrection = (1 << 3), /// /// The project matrix for the frame is included. /// [Description("ProjectionMatrix")] ProjectionMatrix = (1 << 4), /// /// The display matrix for the frame is included. /// [Description("DisplayMatrix")] DisplayMatrix = (1 << 5), /// /// The average intensity in lumens is included. /// [Description("AverageIntensityInLumens")] AverageIntensityInLumens = (1 << 6), /// /// The camera exposure duration is included. /// [Description("ExposureDuration")] ExposureDuration = (1 << 7), /// /// The camera exposure offset is included. /// [Description("ExposureOffset")] ExposureOffset = (1 << 8), /// /// The estimated scene main light direction is included. /// [Description("MainLightDirection")] MainLightDirection = (1 << 9), /// /// The estimated scene main light color is included. /// [Description("MainLightColor")] MainLightColor = (1 << 10), /// /// The estimated scene main light intensity in lumens is included. /// [Description("MainLightIntensityLumens")] MainLightIntensityLumens = (1 << 11), /// /// Ambient spherical harmonics are included. /// [Description("AmbientSphericalHarmonics")] AmbientSphericalHarmonics = (1 << 12), /// /// The camera grain texture is included. /// [Description("CameraGrain")] CameraGrain = (1 << 13), /// /// The camera grain noise intensity is included. /// [Description("NoiseIntensity")] NoiseIntensity = (1 << 14), } /// /// Parameters of the Unity Camera that might be necessary or useful to the provider. /// [StructLayout(LayoutKind.Sequential)] public struct XRCameraFrame : IEquatable { /// /// The timestamp, in nanoseconds, associated with this frame. /// /// /// The timestamp, in nanoseconds, associated with this frame. /// public long timestampNs => m_TimestampNs; long m_TimestampNs; /// /// The estimated brightness of the scene. /// /// /// The estimated brightness of the scene. /// public float averageBrightness => m_AverageBrightness; float m_AverageBrightness; /// /// The estimated color temperature of the scene. /// /// /// The estimated color temperature of the scene. /// public float averageColorTemperature => m_AverageColorTemperature; float m_AverageColorTemperature; /// /// The estimated color correction value of the scene. /// /// /// The estimated color correction value of the scene. /// public Color colorCorrection => m_ColorCorrection; Color m_ColorCorrection; /// /// The 4x4 projection matrix for the camera frame. /// /// /// The 4x4 projection matrix for the camera frame. /// public Matrix4x4 projectionMatrix => m_ProjectionMatrix; Matrix4x4 m_ProjectionMatrix; /// /// The 4x4 display matrix for the camera frame. /// /// /// The 4x4 display matrix for the camera frame. /// public Matrix4x4 displayMatrix => m_DisplayMatrix; Matrix4x4 m_DisplayMatrix; /// /// The associated with the camera. /// /// /// The tracking state associated with the camera. /// public TrackingState trackingState => m_TrackingState; TrackingState m_TrackingState; /// /// A native pointer associated with this frame. The data /// pointed to by this pointer is specific to provider implementation. /// /// /// The native pointer associated with this frame. /// public IntPtr nativePtr => m_NativePtr; IntPtr m_NativePtr; /// /// The set of all flags indicating which properties are included in the frame. /// /// /// The set of all flags indicating which properties are included in the frame. /// public XRCameraFrameProperties properties => m_Properties; XRCameraFrameProperties m_Properties; /// /// The estimated intensity, in lumens, of the scene. /// /// /// The estimated intensity, in lumens, of the scene. /// public float averageIntensityInLumens => m_AverageIntensityInLumens; float m_AverageIntensityInLumens; /// /// The camera exposure duration, in seconds with sub-millisecond precision, of the scene. /// /// /// The camera exposure duration, in seconds with sub-millisecond precision, of the scene. /// public double exposureDuration => m_ExposureDuration; double m_ExposureDuration; /// /// The camera exposure offset of the scene for lighting scaling. /// /// /// The camera exposure offset of the scene for lighting scaling. /// public float exposureOffset => m_ExposureOffset; float m_ExposureOffset; /// /// The estimated intensity in lumens of the most influential real-world light in the scene. /// /// /// The estimated intensity in lumens of the most influential real-world light in the scene. /// public float mainLightIntensityLumens => m_MainLightIntensityLumens; float m_MainLightIntensityLumens; /// /// The estimated color of the most influential real-world light in the scene. /// /// /// The estimated color of the most influential real-world light in the scene. /// public Color mainLightColor => m_MainLightColor; Color m_MainLightColor; /// /// The estimated direction of the most influential real-world light in the scene. /// /// /// The estimated direction of the most influential real-world light in the scene. /// public Vector3 mainLightDirection => m_MainLightDirection; Vector3 m_MainLightDirection; /// /// The ambient spherical harmonic coefficients that represent lighting in the real-world. /// /// /// The ambient spherical harmonic coefficients that represent lighting in the real-world. /// /// /// See Rendering.SphericalHarmonicsL2 for further details. /// public SphericalHarmonicsL2 ambientSphericalHarmonics => m_AmbientSphericalHarmonics; SphericalHarmonicsL2 m_AmbientSphericalHarmonics; /// /// A texture that simulates the camera's noise. /// /// /// A texture that simulates the camera's noise. /// public XRTextureDescriptor cameraGrain => m_CameraGrain; XRTextureDescriptor m_CameraGrain; /// /// The level of intensity of camera grain noise in a scene. /// /// /// The level of intensity of camera grain noise in a scene. /// public float noiseIntensity => m_NoiseIntensity; float m_NoiseIntensity; /// /// True if the frame has a timestamp. /// /// /// True if the frame has a timestamp. /// public bool hasTimestamp => (m_Properties & XRCameraFrameProperties.Timestamp) != 0; /// /// True if the frame has an average brightness. /// /// /// True if the frame has an average brightness. /// public bool hasAverageBrightness => (m_Properties & XRCameraFrameProperties.AverageBrightness) != 0; /// /// True if the frame has an average color temperature. /// /// /// True if the frame has an average color temperature. /// public bool hasAverageColorTemperature => (m_Properties & XRCameraFrameProperties.AverageColorTemperature) != 0; /// /// True if the frame has a color correction value. /// /// /// True if the frame has a color correction value. /// public bool hasColorCorrection => (m_Properties & XRCameraFrameProperties.ColorCorrection) != 0; /// /// True if the frame has a projection matrix. /// /// /// True if the frame has a projection matrix. /// public bool hasProjectionMatrix => (m_Properties & XRCameraFrameProperties.ProjectionMatrix) != 0; /// /// True if the frame has a display matrix. /// /// /// True if the frame has a display matrix. /// public bool hasDisplayMatrix => (m_Properties & XRCameraFrameProperties.DisplayMatrix) != 0; /// /// True if the frame has an average intensity in lumens. /// /// /// True if the frame has an average intensity in lumens. /// public bool hasAverageIntensityInLumens => (m_Properties & XRCameraFrameProperties.AverageIntensityInLumens) != 0; /// /// True if the frame has an exposure duration in seconds with sub-millisecond precision. /// /// /// True if the frame has an exposure duration in seconds with sub-millisecond precision. /// public bool hasExposureDuration => (m_Properties & XRCameraFrameProperties.ExposureDuration) != 0; /// /// True if the frame has an exposure offset for scaling lighting. /// /// /// True if the frame has an exposure offset for scaling lighting. /// public bool hasExposureOffset => (m_Properties & XRCameraFrameProperties.ExposureOffset) != 0; /// /// True if the frame has the estimated main light channel-wise intensity of the scene. /// /// /// True if the frame has the estimated main light channel-wise intensity of the scene. /// public bool hasMainLightIntensityLumens => (m_Properties & XRCameraFrameProperties.MainLightIntensityLumens) != 0; /// /// True if the frame has the estimated main light color of the scene. /// /// /// True if the frame has the estimated main light color of the scene. /// public bool hasMainLightColor => (m_Properties & XRCameraFrameProperties.MainLightColor) != 0; /// /// True if the frame has the estimated main light direction of the scene. /// /// /// True if the frame has the estimated main light direction of the scene. /// public bool hasMainLightDirection => (m_Properties & XRCameraFrameProperties.MainLightDirection) != 0; /// /// True if the frame has the ambient spherical harmonics coefficients of the scene. /// /// /// True if the frame has the ambient spherical harmonics coefficients of the scene. /// public bool hasAmbientSphericalHarmonics => (m_Properties & XRCameraFrameProperties.AmbientSphericalHarmonics) != 0; /// /// True if the frame has a camera grain texture. /// /// /// True if the frame has a camera grain texture. /// public bool hasCameraGrain => (m_Properties & XRCameraFrameProperties.CameraGrain) != 0; /// /// True if the frame has a camera grain noise. /// /// /// True if the frame has a camera grain noise. /// public bool hasNoiseIntensity => (m_Properties & XRCameraFrameProperties.NoiseIntensity) != 0; /// /// Provides a timestamp of the camera frame. /// /// The timestamp of the camera frame. /// /// true if the timestamp was provided. Otherwise, false. /// public bool TryGetTimestamp(out long timestampNs) { timestampNs = this.timestampNs; return hasTimestamp; } /// /// Provides the brightness for the whole image as an average of all pixels' brightness. /// /// An estimated average brightness for the environment. /// /// true if average brightness was provided. Otherwise, false. /// public bool TryGetAverageBrightness(out float averageBrightness) { averageBrightness = this.averageBrightness; return hasAverageBrightness; } /// /// Provides the color temperature for the whole image as an average of all pixels' color temperature. /// /// An estimated color temperature. /// /// true if average color temperature was provided. Otherwise, false. /// public bool TryGetAverageColorTemperature(out float averageColorTemperature) { averageColorTemperature = this.averageColorTemperature; return hasAverageColorTemperature; } /// /// Provides the projection matrix for the camera frame. /// /// The projection matrix used by the XRCameraSubsystem. /// /// true if the projection matrix was provided. Otherwise, false. /// public bool TryGetProjectionMatrix(out Matrix4x4 projectionMatrix) { projectionMatrix = this.projectionMatrix; return this.hasProjectionMatrix; } /// /// Provides the display matrix defining how texture is being rendered on the screen. /// /// The display matrix for rendering. /// /// true if the display matrix was provided. Otherwise, false. /// public bool TryGetDisplayMatrix(out Matrix4x4 displayMatrix) { displayMatrix = this.displayMatrix; return hasDisplayMatrix; } /// /// Provides the intensity, in lumens, for the environment. /// /// An estimated average intensity, in lumens, for the environment. /// /// true if the average intensity was provided. Otherwise, false. /// public bool TryGetAverageIntensityInLumens(out float averageIntensityInLumens) { averageIntensityInLumens = this.averageIntensityInLumens; return hasAverageIntensityInLumens; } /// /// Compares for equality. /// /// The other to compare against. /// true if the represents the same object. 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_MainLightDirection.Equals(other.m_MainLightDirection) && m_MainLightIntensityLumens.Equals(other.m_MainLightIntensityLumens) && m_MainLightColor.Equals(other.m_MainLightColor) && m_AmbientSphericalHarmonics.Equals(other.m_AmbientSphericalHarmonics) && m_CameraGrain.Equals(other.m_CameraGrain) && m_NoiseIntensity.Equals(other.m_NoiseIntensity) && (m_Properties == other.m_Properties)); } /// /// Compares for equality. /// /// An object to compare against. /// true if is an and /// is also true. Otherwise, false. public override bool Equals(System.Object obj) { return ((obj is XRCameraFrame) && Equals((XRCameraFrame)obj)); } /// /// Compares and for equality using . /// /// The left-hand-side of the comparison. /// The right-hand-side of the comparison. /// true if compares equal to , false otherwise. public static bool operator ==(XRCameraFrame lhs, XRCameraFrame rhs) { return lhs.Equals(rhs); } /// /// Compares and for inequality using . /// /// The left-hand-side of the comparison. /// The right-hand-side of the comparison. /// false if compares equal to , true otherwise. public static bool operator !=(XRCameraFrame lhs, XRCameraFrame rhs) { return !lhs.Equals(rhs); } /// /// Generates a hash code suitable for use in HashSet and Dictionary. /// /// A hash of the . 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_MainLightDirection.GetHashCode(); hashCode = (hashCode * 486187739) + m_MainLightColor.GetHashCode(); hashCode = (hashCode * 486187739) + m_AmbientSphericalHarmonics.GetHashCode(); hashCode = (hashCode * 486187739) + m_MainLightIntensityLumens.GetHashCode(); hashCode = (hashCode * 486187739) + m_CameraGrain.GetHashCode(); hashCode = (hashCode * 486187739) + m_NoiseIntensity.GetHashCode(); hashCode = (hashCode * 486187739) + m_NativePtr.GetHashCode(); hashCode = (hashCode * 486187739) + ((int)m_Properties).GetHashCode(); } return hashCode; } /// /// Generates a string representation of this suitable for debugging purposes. /// /// A string representation of this . public override string ToString() { return $"properties:{m_Properties}\n timestamp:{m_TimestampNs}ns\n avgBrightness:{m_AverageBrightness.ToString("0.000")}\n" + $" avgColorTemp:{m_AverageColorTemperature.ToString("0.000")}\n colorCorrection:{m_ColorCorrection}\n" + $" projection:\n{m_ProjectionMatrix.ToString("0.000")}\n display:\n{m_DisplayMatrix.ToString("0.000")}\n" + $" exposureDuration: {m_ExposureDuration.ToString("0.000")}sec\n exposureOffset:{m_ExposureOffset}\n" + $" mainLightDirection: {m_MainLightDirection.ToString("0.000")}\n mainLightIntensityLumens: {m_MainLightIntensityLumens.ToString("0.000")}\n" + $" MainLightColor: {m_MainLightColor.ToString("0.000")}\n ambientSphericalHarmonics: \n{m_AmbientSphericalHarmonics}\n" + $" nativePtr: {m_NativePtr.ToString("X16")}\n"; } } }