More performant Tracked Pose Driver (solution included)

The current TrackedPoseDriver sets the local position and rotation in 1 external call each, with both having to recalculate all children etc. This can definitely add overhead over time when adding animation to the hands etc.
Recent unity versions have SetLocalPositionAndRotation. Using this in SetLocalTransform will be beneficial for performance. No changes are made in behavior, with TrackingType.RotationAndPosition performance improved (1 less branch and combined call), and rotation/position only modes just have 1 extra branch when used.

Starts at line 584 for those who want to use the improved version.
Old code:

protected virtual void SetLocalTransform(Vector3 newPosition, Quaternion newRotation)
        {
            var positionValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Position) != 0;
            var rotationValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Rotation) != 0;

            if (rotationValid &&
                (m_TrackingType == TrackingType.RotationAndPosition ||
                 m_TrackingType == TrackingType.RotationOnly))
            {
                transform.localRotation = newRotation;
            }

            if (positionValid &&
                (m_TrackingType == TrackingType.RotationAndPosition ||
                 m_TrackingType == TrackingType.PositionOnly))
            {
                transform.localPosition = newPosition;
            }
        }

New improved code:

protected virtual void SetLocalTransform(Vector3 newPosition, Quaternion newRotation)
        {
            var positionValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Position) != 0;
            var rotationValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Rotation) != 0;

            if(m_TrackingType == TrackingType.RotationAndPosition && rotationValid && positionValid)
            {
                transform.SetLocalPositionAndRotation(newPosition, newRotation);
            }
            else
            {
                if (rotationValid &&
                m_TrackingType == TrackingType.RotationOnly)
                {
                    transform.localRotation = newRotation;
                }

                if (positionValid &&
                     m_TrackingType == TrackingType.PositionOnly)
                {
                    transform.localPosition = newPosition;
                }
            }
        }
3 Likes

I’ve forwarded this onto our input folks and they’re excited to take a look at potentially incorporate this!

3 Likes

This might help: https://github.com/Unity-Technologies/InputSystem/pull/1715

It has all the info on an implementation

Ah, I didn’t see that PR before I made a new one https://github.com/Unity-Technologies/InputSystem/pull/1716

We still want to apply the valid position or rotation if the other is invalid even when the tracking type is set to RotationAndPosition, so we still want to keep the old code after the optimized check and early return.

Thank you for the contribution and bringing this to our attention.

1 Like

Fixed in Input System 1.7

2 Likes

We also updated XR Core Utilities (2.2.3) to use those methods in some Transform extension methods for improved performance, especially when using XR Hands. We haven’t yet updated the rest of XR Interaction Toolkit to do the same, but that will come in a later version.

2 Likes

Good to hear

1 Like