Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feature Request More performant Tracked Pose Driver (solution included)

Discussion in 'VR' started by DevDunk, Jul 19, 2023.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,879
    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:

    Code (CSharp):
    1. protected virtual void SetLocalTransform(Vector3 newPosition, Quaternion newRotation)
    2.         {
    3.             var positionValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Position) != 0;
    4.             var rotationValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Rotation) != 0;
    5.  
    6.             if (rotationValid &&
    7.                 (m_TrackingType == TrackingType.RotationAndPosition ||
    8.                  m_TrackingType == TrackingType.RotationOnly))
    9.             {
    10.                 transform.localRotation = newRotation;
    11.             }
    12.  
    13.             if (positionValid &&
    14.                 (m_TrackingType == TrackingType.RotationAndPosition ||
    15.                  m_TrackingType == TrackingType.PositionOnly))
    16.             {
    17.                 transform.localPosition = newPosition;
    18.             }
    19.         }

    New improved code:

    Code (CSharp):
    1. protected virtual void SetLocalTransform(Vector3 newPosition, Quaternion newRotation)
    2.         {
    3.             var positionValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Position) != 0;
    4.             var rotationValid = m_IgnoreTrackingState || (m_CurrentTrackingState & TrackingStates.Rotation) != 0;
    5.  
    6.             if(m_TrackingType == TrackingType.RotationAndPosition && rotationValid && positionValid)
    7.             {
    8.                 transform.SetLocalPositionAndRotation(newPosition, newRotation);
    9.             }
    10.             else
    11.             {
    12.                 if (rotationValid &&
    13.                 m_TrackingType == TrackingType.RotationOnly)
    14.                 {
    15.                     transform.localRotation = newRotation;
    16.                 }
    17.  
    18.                 if (positionValid &&
    19.                      m_TrackingType == TrackingType.PositionOnly)
    20.                 {
    21.                     transform.localPosition = newPosition;
    22.                 }
    23.             }
    24.         }
     
  2. DarkSoulsBoss2

    DarkSoulsBoss2

    Unity Technologies

    Joined:
    Mar 24, 2023
    Posts:
    22
    I've forwarded this onto our input folks and they're excited to take a look at potentially incorporate this!
     
    Fangh and DevDunk like this.
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,879
  4. chris-massie

    chris-massie

    Unity Technologies

    Joined:
    Jun 23, 2020
    Posts:
    225
    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.
     
    DevDunk likes this.
  5. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    975
  6. chris-massie

    chris-massie

    Unity Technologies

    Joined:
    Jun 23, 2020
    Posts:
    225
    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.
     
    DevDunk likes this.
  7. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,879
    Good to hear