// ENABLE_VR is not defined on Game Core but the assembly is available with limited features when the XR module is enabled. #if UNITY_INPUT_SYSTEM_ENABLE_XR && (ENABLE_VR || UNITY_GAMECORE) && !UNITY_FORCE_INPUTSYSTEM_XR_OFF || PACKAGE_DOCS_GENERATION using System.Runtime.InteropServices; using Unity.Collections.LowLevel.Unsafe; using UnityEngine.InputSystem.Controls; using UnityEngine.InputSystem.Layouts; using UnityEngine.InputSystem.LowLevel; using UnityEngine.InputSystem.Utilities; using UnityEngine.Scripting; using TrackingState = UnityEngine.XR.InputTrackingState; namespace UnityEngine.InputSystem.XR { /// /// State layout for a single pose. /// /// /// This is the low-level memory representation of a single pose, i.e the /// way poses are internally transmitted and stored in the system. PoseStates are used on devices containing s. /// /// [StructLayout(LayoutKind.Explicit, Size = kSizeInBytes)] public struct PoseState : IInputStateTypeInfo { internal const int kSizeInBytes = 60; internal static readonly FourCC s_Format = new FourCC('P', 'o', 's', 'e'); /// /// Memory format tag for PoseState. /// /// Returns "Pose". /// public FourCC format => s_Format; /// /// Constructor for PoseStates. /// /// Useful for creating PoseStates locally (not from ). /// /// Value to use for /// Value to use for /// Value to use for /// Value to use for /// Value to use for /// Value to use for public PoseState(bool isTracked, TrackingState trackingState, Vector3 position, Quaternion rotation, Vector3 velocity, Vector3 angularVelocity) { this.isTracked = isTracked; this.trackingState = trackingState; this.position = position; this.rotation = rotation; this.velocity = velocity; this.angularVelocity = angularVelocity; } /// /// Whether the pose is currently being fully tracked. Otherwise, the tracking is either unavailable, or simulated. /// /// /// Fully tracked means that the pose is accurate and not using any simulated or extrapolated positions, and the system tracking this pose is able to confidently track this object. /// [FieldOffset(0), InputControl(displayName = "Is Tracked", layout = "Button", sizeInBits = 8 /* needed to ensure optimization kicks-in */)] public bool isTracked; /// /// A Flags Enumeration specifying which other fields in the pose state are valid. /// [FieldOffset(4), InputControl(displayName = "Tracking State", layout = "Integer")] public TrackingState trackingState; /// /// The position in 3D space, relative to the tracking origin where this pose represents. /// /// /// Positions are represented in meters. /// This field is only valid if contains the value. /// See for information on tracking origins. /// [FieldOffset(8), InputControl(displayName = "Position", noisy = true)] public Vector3 position; /// /// The rotation in 3D space, relative to the tracking origin where this pose represents. /// /// /// This field is only valid if contains the value. /// See for information on tracking origins. /// [FieldOffset(20), InputControl(displayName = "Rotation", noisy = true)] public Quaternion rotation; /// /// The velocity in 3D space, relative to the tracking origin where this pose represents. /// /// /// Velocities are represented in meters per second. /// This field is only valid if contains the value. /// See for information on tracking origins. /// [FieldOffset(36), InputControl(displayName = "Velocity", noisy = true)] public Vector3 velocity; /// /// The angular velocity in 3D space, relative to the tracking origin where this pose represents. /// /// /// This field is only valid if contains the value. /// See for information on tracking origins. /// [FieldOffset(48), InputControl(displayName = "Angular Velocity", noisy = true)] public Vector3 angularVelocity; } /// /// A control representing a Pose in 3D space, relative to an XR tracking origin /// /// /// Note that unlike most other control types, PoseControls do not have /// a flexible memory layout. They are hardwired to and /// will not work correctly with a different memory layouts. Additional fields may /// be appended to the struct but what's there in the struct has to be located /// at exactly those memory addresses. /// /// For more information on tracking origins see . /// [Preserve, InputControlLayout(stateType = typeof(PoseState))] public class PoseControl : InputControl { /// /// Represents whether this pose is fully tracked or unavailable/simulated. /// /// Control representing whether the pose is being fully tracked. Maps to the value. /// public ButtonControl isTracked { get; set; } /// /// The other controls on this that are currently reporting data. /// /// /// This can be missing values when the device tracking this pose is restricted or not tracking properly. /// /// Control representing whether the pose is being fully tracked. Maps to the value of the pose retrieved from this control. /// public IntegerControl trackingState { get; set; } /// /// The position, in meters, of this tracked pose relative to the tracking origin. /// /// /// The data for this control is only valid if the value returned from contains value. /// /// Control representing whether the pose is being fully tracked. Maps to the value of the pose retrieved from this control. /// public Vector3Control position { get; set; } /// /// The rotation of this tracked pose relative to the tracking origin. /// /// /// The data for this control is only valid if the value returned from contains value. /// /// Control representing whether the pose is being fully tracked. Maps to the value of the pose retrieved from this control. /// public QuaternionControl rotation { get; set; } /// /// The velocity, in meters per second, of this tracked pose relative to the tracking origin. /// /// /// The data for this control is only valid if the value returned from contains value. /// /// Control representing whether the pose is being fully tracked. Maps to the value of the pose retrieved from this control. /// public Vector3Control velocity { get; set; } /// /// The angular velocity of this tracked pose relative to the tracking origin. /// /// /// The data for this control is only valid if the value returned from contains value. /// /// Control representing whether the pose is being fully tracked. Maps to the value of the pose retrieved from this control. /// public Vector3Control angularVelocity { get; set; } /// /// Default-initialize the pose control. /// /// /// Sets the to "Pose". /// public PoseControl() { m_StateBlock.format = PoseState.s_Format; } /// protected override void FinishSetup() { isTracked = GetChildControl("isTracked"); trackingState = GetChildControl("trackingState"); position = GetChildControl("position"); rotation = GetChildControl("rotation"); velocity = GetChildControl("velocity"); angularVelocity = GetChildControl("angularVelocity"); base.FinishSetup(); } /// public override unsafe PoseState ReadUnprocessedValueFromState(void* statePtr) { switch (m_OptimizedControlDataType) { case InputStateBlock.kFormatPose: return *(PoseState*)((byte*)statePtr + (int)m_StateBlock.byteOffset); default: return new PoseState() { isTracked = isTracked.ReadUnprocessedValueFromStateWithCaching(statePtr) > 0.5f, trackingState = (TrackingState)trackingState.ReadUnprocessedValueFromStateWithCaching(statePtr), position = position.ReadUnprocessedValueFromStateWithCaching(statePtr), rotation = rotation.ReadUnprocessedValueFromStateWithCaching(statePtr), velocity = velocity.ReadUnprocessedValueFromStateWithCaching(statePtr), angularVelocity = angularVelocity.ReadUnprocessedValueFromStateWithCaching(statePtr), }; } } /// public override unsafe void WriteValueIntoState(PoseState value, void* statePtr) { switch (m_OptimizedControlDataType) { case InputStateBlock.kFormatPose: *(PoseState*)((byte*)statePtr + (int)m_StateBlock.byteOffset) = value; break; default: isTracked.WriteValueIntoState(value.isTracked, statePtr); trackingState.WriteValueIntoState((uint)value.trackingState, statePtr); position.WriteValueIntoState(value.position, statePtr); rotation.WriteValueIntoState(value.rotation, statePtr); velocity.WriteValueIntoState(value.velocity, statePtr); angularVelocity.WriteValueIntoState(value.angularVelocity, statePtr); break; } } protected override FourCC CalculateOptimizedControlDataType() { if ( m_StateBlock.sizeInBits == PoseState.kSizeInBytes * 8 && m_StateBlock.bitOffset == 0 && isTracked.optimizedControlDataType == InputStateBlock.kFormatByte && trackingState.optimizedControlDataType == InputStateBlock.kFormatInt && position.optimizedControlDataType == InputStateBlock.kFormatVector3 && rotation.optimizedControlDataType == InputStateBlock.kFormatQuaternion && velocity.optimizedControlDataType == InputStateBlock.kFormatVector3 && angularVelocity.optimizedControlDataType == InputStateBlock.kFormatVector3 && trackingState.m_StateBlock.byteOffset == isTracked.m_StateBlock.byteOffset + 4 && position.m_StateBlock.byteOffset == isTracked.m_StateBlock.byteOffset + 8 && rotation.m_StateBlock.byteOffset == isTracked.m_StateBlock.byteOffset + 20 && velocity.m_StateBlock.byteOffset == isTracked.m_StateBlock.byteOffset + 36 && angularVelocity.m_StateBlock.byteOffset == isTracked.m_StateBlock.byteOffset + 48 ) return InputStateBlock.kFormatPose; return InputStateBlock.kFormatInvalid; } } } #endif