Maze / Library /PackageCache /com.unity.inputsystem@1.6.1 /InputSystem /Plugins /Android /AndroidSensors.cs
| using System; | |
| using System.ComponentModel; | |
| using System.Runtime.InteropServices; | |
| using UnityEngine.InputSystem.Android.LowLevel; | |
| using UnityEngine.InputSystem.LowLevel; | |
| using UnityEngine.InputSystem.Utilities; | |
| using UnityEngine.InputSystem.Layouts; | |
| using UnityEngine.InputSystem.Processors; | |
| ////TODO: make all the sensor class types internal | |
| namespace UnityEngine.InputSystem.Android.LowLevel | |
| { | |
| internal enum AndroidSensorType | |
| { | |
| None = 0, | |
| Accelerometer = 1, | |
| MagneticField = 2, | |
| Orientation = 3, // Was deprecated in API 8 https://developer.android.com/reference/android/hardware/Sensor#TYPE_ORIENTATION | |
| Gyroscope = 4, | |
| Light = 5, | |
| Pressure = 6, | |
| Temperature = 7, // Was deprecated in API 14 https://developer.android.com/reference/android/hardware/Sensor#TYPE_TEMPERATURE | |
| Proximity = 8, | |
| Gravity = 9, | |
| LinearAcceleration = 10, | |
| RotationVector = 11, | |
| RelativeHumidity = 12, | |
| AmbientTemperature = 13, | |
| MagneticFieldUncalibrated = 14, | |
| GameRotationVector = 15, | |
| GyroscopeUncalibrated = 16, | |
| SignificantMotion = 17, | |
| StepDetector = 18, | |
| StepCounter = 19, | |
| GeomagneticRotationVector = 20, | |
| HeartRate = 21, | |
| Pose6DOF = 28, | |
| StationaryDetect = 29, | |
| MotionDetect = 30, | |
| HeartBeat = 31, | |
| LowLatencyOffBodyDetect = 34, | |
| AccelerometerUncalibrated = 35, | |
| } | |
| [] | |
| internal struct AndroidSensorCapabilities | |
| { | |
| public AndroidSensorType sensorType; | |
| public string ToJson() | |
| { | |
| return JsonUtility.ToJson(this); | |
| } | |
| public static AndroidSensorCapabilities FromJson(string json) | |
| { | |
| if (json == null) | |
| throw new ArgumentNullException(nameof(json)); | |
| return JsonUtility.FromJson<AndroidSensorCapabilities>(json); | |
| } | |
| public override string ToString() | |
| { | |
| return $"type = {sensorType.ToString()}"; | |
| } | |
| } | |
| [] | |
| internal unsafe struct AndroidSensorState : IInputStateTypeInfo | |
| { | |
| public static FourCC kFormat = new FourCC('A', 'S', 'S', ' '); | |
| ////FIXME: Sensors to check if values matches old system | |
| // Accelerometer - OK | |
| // MagneticField - no alternative in old system | |
| // Gyroscope - OK | |
| // Light - no alternative in old system | |
| // Pressure - no alternative in old system | |
| // Proximity - no alternative in old system | |
| // Gravity - OK | |
| // LinearAcceleration - need to check | |
| // RotationVector - OK | |
| // RelativeHumidity - no alternative in old system | |
| // AmbientTemperature - no alternative in old system | |
| // StepCounter - no alternative in old system | |
| // GeomagneticRotationVector - no alternative in old system | |
| // HeartRate - no alternative in old system | |
| [] | |
| [] | |
| // Note: Using CompensateDirection instead of AndroidCompensateDirection, because we don't need to normalize velocity | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| [] | |
| public fixed float data[16]; | |
| public AndroidSensorState WithData(params float[] data) | |
| { | |
| if (data == null) | |
| throw new ArgumentNullException(nameof(data)); | |
| for (var i = 0; i < data.Length && i < 16; i++) | |
| this.data[i] = data[i]; | |
| // Fill the rest with zeroes | |
| for (var i = data.Length; i < 16; i++) | |
| this.data[i] = 0.0f; | |
| return this; | |
| } | |
| public FourCC format => kFormat; | |
| } | |
| [] | |
| internal class AndroidCompensateDirectionProcessor : CompensateDirectionProcessor | |
| { | |
| // Taken from platforms\android-<API>\arch-arm\usr\include\android\sensor.h | |
| private const float kSensorStandardGravity = 9.80665f; | |
| private const float kAccelerationMultiplier = -1.0f / kSensorStandardGravity; | |
| public override Vector3 Process(Vector3 vector, InputControl control) | |
| { | |
| return base.Process(vector * kAccelerationMultiplier, control); | |
| } | |
| } | |
| [] | |
| internal class AndroidCompensateRotationProcessor : CompensateRotationProcessor | |
| { | |
| public override Quaternion Process(Quaternion value, InputControl control) | |
| { | |
| // https://developer.android.com/reference/android/hardware/SensorEvent#values | |
| // "...The rotation vector represents the orientation of the device as a combination of an angle and an axis, in which the device has rotated through an angle theta around an axis <x, y, z>." | |
| // "...The three elements of the rotation vector are < x * sin(theta / 2), y* sin(theta / 2), z* sin(theta / 2)>, such that the magnitude of the rotation vector is equal to sin(theta / 2), and the direction of the rotation vector is equal to the direction of the axis of rotation." | |
| // "...The three elements of the rotation vector are equal to the last three components of a unit quaternion < cos(theta / 2), x* sin(theta/ 2), y* sin(theta / 2), z* sin(theta/ 2)>." | |
| // | |
| // In other words, axis + rotation is combined into Vector3, to recover the quaternion from it, we must compute 4th component as 1 - sqrt(x*x + y*y + z*z) | |
| var sinRho2 = value.x * value.x + value.y * value.y + value.z * value.z; | |
| value.w = (sinRho2 < 1.0f) ? Mathf.Sqrt(1.0f - sinRho2) : 0.0f; | |
| return base.Process(value, control); | |
| } | |
| } | |
| } | |
| namespace UnityEngine.InputSystem.Android | |
| { | |
| /// <summary> | |
| /// Accelerometer device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_ACCELEROMETER"/> | |
| [] | |
| public class AndroidAccelerometer : Accelerometer | |
| { | |
| } | |
| /// <summary> | |
| /// Magnetic field sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_MAGNETIC_FIELD"/> | |
| [] | |
| public class AndroidMagneticFieldSensor : MagneticFieldSensor | |
| { | |
| } | |
| /// <summary> | |
| /// Gyroscope device on android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_GYROSCOPE"/> | |
| [] | |
| public class AndroidGyroscope : Gyroscope | |
| { | |
| } | |
| /// <summary> | |
| /// Light sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_LIGHT"/> | |
| [] | |
| public class AndroidLightSensor : LightSensor | |
| { | |
| } | |
| /// <summary> | |
| /// Pressure sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_PRESSURE"/> | |
| [] | |
| public class AndroidPressureSensor : PressureSensor | |
| { | |
| } | |
| /// <summary> | |
| /// Proximity sensor type on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_PROXIMITY"/> | |
| [] | |
| public class AndroidProximity : ProximitySensor | |
| { | |
| } | |
| /// <summary> | |
| /// Gravity sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_GRAVITY"/> | |
| [] | |
| public class AndroidGravitySensor : GravitySensor | |
| { | |
| } | |
| /// <summary> | |
| /// Linear acceleration sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_LINEAR_ACCELERATION"/> | |
| [] | |
| public class AndroidLinearAccelerationSensor : LinearAccelerationSensor | |
| { | |
| } | |
| /// <summary> | |
| /// Rotation vector sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_ROTATION_VECTOR"/> | |
| [] | |
| public class AndroidRotationVector : AttitudeSensor | |
| { | |
| } | |
| /// <summary> | |
| /// Relative humidity sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_RELATIVE_HUMIDITY"/> | |
| [] | |
| public class AndroidRelativeHumidity : HumiditySensor | |
| { | |
| } | |
| /// <summary> | |
| /// Ambient temperature sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_AMBIENT_TEMPERATURE"/> | |
| [] | |
| public class AndroidAmbientTemperature : AmbientTemperatureSensor | |
| { | |
| } | |
| /// <summary> | |
| /// Step counter sensor device on Android. | |
| /// </summary> | |
| /// <seealso href="https://developer.android.com/reference/android/hardware/Sensor#TYPE_STEP_COUNTER"/> | |
| [] | |
| public class AndroidStepCounter : StepCounter | |
| { | |
| } | |
| } | |