// 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; /// /// Memory format tag for PoseState. /// /// Returns "Pose". /// public FourCC format => new FourCC('P', 'o', 's', 'e'); /// /// 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")] 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; private 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; private 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; private 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; private 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; private 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; private set; } /// /// Default-initialize the pose control. /// /// /// Sets the to "Pose". /// public PoseControl() { m_StateBlock.format = new FourCC('P', 'o', 's', 'e'); } /// 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) { return new PoseState() { isTracked = isTracked.ReadUnprocessedValueFromState(statePtr) > 0.5f, trackingState = (TrackingState)trackingState.ReadUnprocessedValueFromState(statePtr), position = position.ReadUnprocessedValueFromState(statePtr), rotation = rotation.ReadUnprocessedValueFromState(statePtr), velocity = velocity.ReadUnprocessedValueFromState(statePtr), angularVelocity = angularVelocity.ReadUnprocessedValueFromState(statePtr), }; } /// public override unsafe void WriteValueIntoState(PoseState value, void* statePtr) { 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); } } } #endif